From bdae1bab9d0d8ab0ed17bfcdb4624e007d4213f5 Mon Sep 17 00:00:00 2001 From: SJ Date: Sat, 5 Jul 2014 17:09:38 +0200 Subject: [PATCH] introduced a new group management feature --- util/db-mysql.sql | 4 +- util/db-upgrade-0.1.24-vs-0.1.25.sql | 10 ++++ webui/controller/group/add.php | 4 ++ webui/controller/group/edit.php | 5 ++ webui/controller/user/edit.php | 2 +- webui/controller/user/settings.php | 4 +- webui/model/group/group.php | 51 +++++++++++++++++-- webui/model/user/auth.php | 18 ++++++- webui/model/user/user.php | 17 ++++--- .../theme/default/templates/group/add.tpl | 7 +++ .../theme/default/templates/group/edit.tpl | 15 ++++-- 11 files changed, 113 insertions(+), 24 deletions(-) diff --git a/util/db-mysql.sql b/util/db-mysql.sql index 68a86b83..b7059b7d 100644 --- a/util/db-mysql.sql +++ b/util/db-mysql.sql @@ -224,9 +224,9 @@ create table if not exists `group` ( create table if not exists `group_user` ( `id` bigint unsigned not null, - `uid` int unsigned not null, + `email` char(128) not null, key `group_user_idx` (`id`), - key `group_user_idx2` (`uid`) + key `group_user_idx2` (`email`) ) ENGINE=InnoDB; diff --git a/util/db-upgrade-0.1.24-vs-0.1.25.sql b/util/db-upgrade-0.1.24-vs-0.1.25.sql index 630c86c6..ef7eb11f 100644 --- a/util/db-upgrade-0.1.24-vs-0.1.25.sql +++ b/util/db-upgrade-0.1.24-vs-0.1.25.sql @@ -36,3 +36,13 @@ create unique index `entry` on archiving_rule (`domain`,`from`,`to`,`subject`,`_ create unique index `entry` on retention_rule (`domain`,`from`,`to`,`subject`,`_size`,`size`,`attachment_name`,`attachment_type`,`_attachment_size`,`attachment_size`,`spam`); +-- 2014.07.05 +drop table if exists `group_user`; + +create table if not exists `group_user` ( + `id` bigint unsigned not null, + `email` char(128) not null, + key `group_user_idx` (`id`), + key `group_user_idx2` (`email`) +) ENGINE=InnoDB; + diff --git a/webui/controller/group/add.php b/webui/controller/group/add.php index ab61d7eb..6c57c2b3 100644 --- a/webui/controller/group/add.php +++ b/webui/controller/group/add.php @@ -70,6 +70,10 @@ class ControllerGroupAdd extends Controller { $this->error['email'] = $this->data['text_missing_data']; } + if(!isset($this->request->post['assigned_email']) || $this->request->post['assigned_email'] == '') { + $this->error['assigned_email'] = $this->data['text_missing_data']; + } + if (!$this->error) { return true; } else { diff --git a/webui/controller/group/edit.php b/webui/controller/group/edit.php index dbcbb4f8..c22ecb3f 100644 --- a/webui/controller/group/edit.php +++ b/webui/controller/group/edit.php @@ -59,6 +59,7 @@ class ControllerGroupEdit extends Controller { else { $this->data['group'] = $this->model_group_group->get_domain_by_id($this->data['id']); $this->data['email'] = $this->model_group_group->get_emails_by_group_id($this->data['id']); + $this->data['assigned_email'] = $this->model_group_group->get_assigned_emails_by_group_id($this->data['id']); } } else { @@ -83,6 +84,10 @@ class ControllerGroupEdit extends Controller { $this->error['email'] = $this->data['text_missing_data']; } + if(!isset($this->request->post['assigned_email']) || $this->request->post['assigned_email'] == '') { + $this->error['assigned_email'] = $this->data['text_missing_data']; + } + if(!isset($this->request->post['id']) || !is_numeric($this->request->post['id']) || (int)$this->request->post['id'] < 0) { $this->error['id'] = $this->data['text_invalid_data']; } diff --git a/webui/controller/user/edit.php b/webui/controller/user/edit.php index 31e5a768..1544a913 100644 --- a/webui/controller/user/edit.php +++ b/webui/controller/user/edit.php @@ -75,11 +75,11 @@ class ControllerUserEdit extends Controller { $this->data['user']['domains'] = $this->model_user_user->get_domains_by_uid($this->data['uid']); $this->data['user']['group_membership'] = $this->model_user_user->get_additional_uids($this->data['uid']); - $this->data['user']['group'] = $this->model_group_group->get_groups_by_uid($this->data['uid']); $this->data['user']['folder'] = $this->model_folder_folder->get_folders_by_uid($this->data['uid']); $this->data['emails'] = $this->model_user_user->get_emails($this->data['user']['username']); + $this->data['user']['group'] = $this->model_group_group->get_groups_by_email(array($this->data['emails'])); //} } else { diff --git a/webui/controller/user/settings.php b/webui/controller/user/settings.php index 904a514a..ae9a6b2e 100644 --- a/webui/controller/user/settings.php +++ b/webui/controller/user/settings.php @@ -38,8 +38,8 @@ class ControllerUserSettings extends Controller { $auditdomains .= ', ' . $d; } $auditdomains = preg_replace("/^,\s/", "", $auditdomains); - - $auditgroups = preg_replace("/\s/", ", ", $this->model_group_group->get_groups_by_uid($session->get("uid"))); + + $auditgroups = preg_replace("/\s/", ", ", $this->model_group_group->get_groups_by_email($session->get("emails"))); $folders = $session->get("folders"); diff --git a/webui/model/group/group.php b/webui/model/group/group.php index 94df45b8..d0e2567c 100644 --- a/webui/model/group/group.php +++ b/webui/model/group/group.php @@ -45,6 +45,19 @@ class ModelGroupGroup extends Model { public function get_emails_by_group_id($id = 0) { $emails = ''; + $query = $this->db->query("SELECT `email` FROM `" . TABLE_GROUP_USER . "` WHERE id=?", array($id)); + + foreach ($query->rows as $q) { + $emails .= $q['email'] . "\n"; + } + + return preg_replace("/\n$/", "", $emails); + } + + + public function get_assigned_emails_by_group_id($id = 0) { + $emails = ''; + $query = $this->db->query("SELECT `email` FROM `" . TABLE_GROUP_EMAIL . "` WHERE id=?", array($id)); foreach ($query->rows as $q) { @@ -86,11 +99,19 @@ class ModelGroupGroup extends Model { foreach ($emails as $email) { $email = rtrim($email); if(validemail($email)) { - $query = $this->db->query("INSERT INTO `" . TABLE_GROUP_EMAIL . "` (id, email) VALUES(?,?)", array($gid, $email)); + $query = $this->db->query("INSERT INTO `" . TABLE_GROUP_USER . "` (id, email) VALUES(?,?)", array($gid, $email)); } } + $emails = explode("\n", $group['assigned_email']); + foreach ($emails as $email) { + $email = rtrim($email); + if(validemail($email)) { + $query = $this->db->query("INSERT INTO `" . TABLE_GROUP_EMAIL . "` (id, email) VALUES(?,?)", array($gid, $email)); + } + } + LOGGER("add group: " . $group['groupname'] . ", id=" . (int)$gid); return 1; @@ -102,17 +123,30 @@ class ModelGroupGroup extends Model { $query = $this->db->query("UPDATE `" . TABLE_GROUP . "` SET `groupname`=? WHERE id=?", array($group['groupname'], (int)$group['id'])); - $query = $this->db->query("DELETE FROM `" . TABLE_GROUP_EMAIL . "` WHERE id=?", array($group['id'])); + $query = $this->db->query("DELETE FROM `" . TABLE_GROUP_USER . "` WHERE id=?", array($group['id'])); $emails = explode("\n", $group['email']); foreach ($emails as $email) { $email = rtrim($email); + if(validemail($email)) { + $query = $this->db->query("INSERT INTO `" . TABLE_GROUP_USER . "` (id, email) VALUES(?,?)", array($group['id'], $email)); + } + } + + + $query = $this->db->query("DELETE FROM `" . TABLE_GROUP_EMAIL . "` WHERE id=?", array($group['id'])); + + $emails = explode("\n", $group['assigned_email']); + foreach ($emails as $email) { + $email = rtrim($email); + if(validemail($email)) { $query = $this->db->query("INSERT INTO `" . TABLE_GROUP_EMAIL . "` (id, email) VALUES(?,?)", array($group['id'], $email)); } } + return $this->db->countAffected(); } @@ -132,11 +166,13 @@ class ModelGroupGroup extends Model { $query = $this->db->query("DELETE FROM `" . TABLE_GROUP_EMAIL . "` WHERE id=?", array($id)); + $query = $this->db->query("DELETE FROM `" . TABLE_GROUP_USER . "` WHERE id=?", array($id)); + $query = $this->db->query("DELETE FROM `" . TABLE_GROUP . "` WHERE id=?", array((int)$id)); LOGGER("remove group: id=$id"); - return $this->db->countAffected(); + return 1; } @@ -214,10 +250,15 @@ class ModelGroupGroup extends Model { } - public function get_groups_by_uid($uid = 0) { + public function get_groups_by_email($email = array()) { $groups = ''; + $q = '?'; - $query = $this->db->query("SELECT `" . TABLE_GROUP_USER . "`.id, groupname FROM `" . TABLE_GROUP_USER . "`, `" . TABLE_GROUP . "` WHERE `" . TABLE_GROUP_USER . "`.id=`" . TABLE_GROUP . "`.id AND uid=?", array($uid) ); + for($i=1; $idb->query("SELECT `" . TABLE_GROUP_USER . "`.id, groupname FROM `" . TABLE_GROUP_USER . "`, `" . TABLE_GROUP . "` WHERE `" . TABLE_GROUP_USER . "`.id=`" . TABLE_GROUP . "`.id AND email IN ($q)", $email); if(isset($query->rows)) { foreach ($query->rows as $q) { $groups .= "\n" . $q['groupname']; } diff --git a/webui/model/user/auth.php b/webui/model/user/auth.php index 93b79d56..ee4ff898 100644 --- a/webui/model/user/auth.php +++ b/webui/model/user/auth.php @@ -129,6 +129,9 @@ class ModelUserAuth extends Model { $emails = $this->get_email_array_from_ldap_attr($query->rows); + $extra_emails = $this->model_user_user->get_email_addresses_from_groups($emails)); + $emails = array_merge($emails, $extra_emails); + $this->add_session_vars($a['cn'], $username, $emails, $role); AUDIT(ACTION_LOGIN, $username, '', '', 'successful auth against LDAP'); @@ -270,6 +273,7 @@ class ModelUserAuth extends Model { private function checkLoginAgainstIMAP($username = '', $password = '') { $session = Registry::get('session'); + $emails = array($username); if(!strchr($username, '@')) { return 0; } @@ -277,7 +281,10 @@ class ModelUserAuth extends Model { if($imap->login($username, $password)) { $imap->logout(); - $this->add_session_vars($username, $username, array($username), 0); + $extra_emails = $this->model_user_user->get_email_addresses_from_groups($emails)); + $emails = array_merge($emails, $extra_emails); + + $this->add_session_vars($username, $username, $emails, 0); $session->set("password", $password); @@ -290,6 +297,7 @@ class ModelUserAuth extends Model { private function checkLoginAgainstPOP3($username = '', $password = '') { $rc = 0; + $emails = array($username); try { $conn = new Zend_Mail_Protocol_Pop3(POP3_HOST, POP3_PORT, POP3_SSL); @@ -302,7 +310,10 @@ class ModelUserAuth extends Model { try { $conn->login($username, $password); - $this->add_session_vars($username, $username, array($username), 0); + $extra_emails = $this->model_user_user->get_email_addresses_from_groups($emails)); + $emails = array_merge($emails, $extra_emails); + + $this->add_session_vars($username, $username, $emails, 0); $rc = 1; } catch (Zend_Mail_Protocol_Exception $e) {} @@ -351,6 +362,9 @@ class ModelUserAuth extends Model { $emails = $this->get_email_array_from_ldap_attr($query->rows); + $extra_emails = $this->model_user_user->get_email_addresses_from_groups($emails)); + $emails = array_merge($emails, $extra_emails); + if($this->check_ldap_membership($ldap_auditor_member_dn, $query->rows) == 1) { $role = 2; } if($this->check_ldap_membership($ldap_admin_member_dn, $query->rows) == 1) { $role = 1; } diff --git a/webui/model/user/user.php b/webui/model/user/user.php index 400c213d..753eed4e 100644 --- a/webui/model/user/user.php +++ b/webui/model/user/user.php @@ -69,8 +69,7 @@ class ModelUserUser extends Model { } - $query = $this->db->query("SELECT `" . TABLE_GROUP_EMAIL . "`.email FROM `" . TABLE_GROUP_EMAIL . "`, `" . TABLE_GROUP_USER . "` WHERE `" . TABLE_GROUP_EMAIL . "`.id=`" . TABLE_GROUP_USER . "`.id and `" . TABLE_GROUP_USER . "`.uid=?", array($uid) ); - + $query = $this->db->query("SELECT g.email FROM `" . TABLE_GROUP_EMAIL . "` g WHERE g.id IN (SELECT u.id FROM `" . TABLE_GROUP_USER . "` u WHERE u.email IN (?))", $data); if(isset($query->rows)) { foreach ($query->rows as $q) { @@ -359,7 +358,7 @@ class ModelUserUser extends Model { } $this->update_domains_settings((int)$user['uid'], $user['domains']); - $this->update_group_settings((int)$user['uid'], $user['group']); + $this->update_group_settings($emails[0], $user['group']); $this->update_folder_settings((int)$user['uid'], $user['folder']); return 1; @@ -442,7 +441,7 @@ class ModelUserUser extends Model { } $this->update_domains_settings((int)$user['uid'], $user['domains']); - $this->update_group_settings((int)$user['uid'], $user['group']); + $this->update_group_settings($emails[0], $user['group']); $this->update_folder_settings((int)$user['uid'], $user['folder']); return 1; @@ -471,12 +470,14 @@ class ModelUserUser extends Model { } - private function update_group_settings($uid = -1, $group = '') { + private function update_group_settings($email = '', $group = '') { $__g = array(); - if($uid <= 0) { return 0; } + $email = rtrim($email); - $query = $this->db->query("DELETE FROM `" . TABLE_GROUP_USER . "` WHERE uid=?", array($uid)); + if($email == '') { return 0; } + + $query = $this->db->query("DELETE FROM `" . TABLE_GROUP_USER . "` WHERE email=?", array($email)); $query = $this->db->query("SELECT id, groupname FROM `" . TABLE_GROUP . "`"); @@ -492,7 +493,7 @@ class ModelUserUser extends Model { $g = rtrim($g); if($g && !isset($__g[$groups[$g]])) { - $query = $this->db->query("INSERT INTO `" . TABLE_GROUP_USER . "` (id, uid) VALUES(?,?)", array($groups[$g], (int)$uid)); + $query = $this->db->query("INSERT INTO `" . TABLE_GROUP_USER . "` (id, email) VALUES(?,?)", array($groups[$g], $email)); $__g[$groups[$g]] = 1; } } diff --git a/webui/view/theme/default/templates/group/add.tpl b/webui/view/theme/default/templates/group/add.tpl index b19df9d4..092af17f 100644 --- a/webui/view/theme/default/templates/group/add.tpl +++ b/webui/view/theme/default/templates/group/add.tpl @@ -42,6 +42,13 @@ +
+ +
+ +
+
+
*:
**:
diff --git a/webui/view/theme/default/templates/group/edit.tpl b/webui/view/theme/default/templates/group/edit.tpl index 254ef556..8741caef 100644 --- a/webui/view/theme/default/templates/group/edit.tpl +++ b/webui/view/theme/default/templates/group/edit.tpl @@ -32,7 +32,7 @@
- +
@@ -48,11 +48,18 @@
- +
- + +
+ +
+
+ +
+
- +