diff --git a/lib/Auth.php b/lib/Auth.php index 701c385..f91198a 100644 --- a/lib/Auth.php +++ b/lib/Auth.php @@ -244,6 +244,11 @@ class Auth { return $this->auth_instance()->domainadmin_get_configuration($domain, $variablename); } + public function domainadmin_get_user_quota($domainadmin, $excludeuser) + { + return $this->auth_instance()->domainadmin_get_user_quota($domainadmin, $excludeuser); + } + public function find_recipient($address) { return $this->auth_instance()->find_recipient($address); diff --git a/lib/Auth/LDAP.php b/lib/Auth/LDAP.php index 3ac1f43..f105e5f 100644 --- a/lib/Auth/LDAP.php +++ b/lib/Auth/LDAP.php @@ -638,6 +638,31 @@ class LDAP extends Net_LDAP3 { return array('domainadmin' => $maindomainadmin, $config_flag => $resultvalue); } + // get the quota that has already been shared among the users of the domains that this domainadmin manages. + // excluding the current quota for the specified user, that we want to change the quota for + public function domainadmin_get_user_quota($domainadmin, $excludeuser) + { + $quota = 0; + $domains = $this->domainadmin_get_domains($domainadmin); + foreach ($domains as $domain) { + // get all users that are part of this domain + $users_result = $this->_search( + $this->_standard_root_dn($domain), + "objectclass=kolabinetorgperson", + array($this->conf->get("quota_attribute"))); + if ($users_result != null && count($users_result) > 0) { + $users = $users_result->entries(true); + foreach ($users as $uid => $user) { + if (strtolower($uid) != strtolower($excludeuser)) { + $quota += $user[$this->conf->get("quota_attribute")]; + } + } + } + } + + return $quota; + } + public function domain_edit($domain, $attributes, $typeid = null) { $domain = $this->domain_info($domain, array_keys($attributes)); diff --git a/lib/api/kolab_api_service_form_value.php b/lib/api/kolab_api_service_form_value.php index 79a4720..f0d99ec 100644 --- a/lib/api/kolab_api_service_form_value.php +++ b/lib/api/kolab_api_service_form_value.php @@ -1237,7 +1237,66 @@ class kolab_api_service_form_value extends kolab_api_service private function validate_mailquota($value, $postdata = array(), $validation_type = null) { - return $this->validate_quota($value, $postdata, $validation_type); + $value = $this->validate_quota($value, $postdata, $validation_type); + + if (empty($value)) { + return $value; + } + + if (!is_numeric($value)) { + throw new Exception('Invalid value for mail quota. Please clear or enter a valid integer number!'); + } + + $value = intval($value); + + $conf = Conf::get_instance(); + $quota_attribute = $conf->get('domainadmin_quota_attribute'); + if ($quota_attribute == null || strlen($quota_attribute) == 0) { + $quota_attribute = $conf->get('quota_attribute'); + } + // check domain admin quota, only for users that are not part of the default domain + if (strtolower($postdata['ou']) != strtolower($conf->get('kolab_user_base_dn'))) { + $auth = Auth::get_instance(); + + // get the mailquota of the domain admin for the current domain + $result = $auth->domainadmin_get_configuration($_SESSION['user']->get_domain(), $quota_attribute); + + if (isset($result)) { + $domainadminquota = $result[$quota_attribute]; + $domainadmin = $result['domainadmin']; + // get all quotas from all users of that domain admin, excluding this user + $quota_used = $auth->domainadmin_get_user_quota($domainadmin, 'uid='.$postdata['uid'].','.$postdata['ou']); + + // check if existing quota plus this new quota would still fit the quota of the domain admin + if ($quota_used + $value > $domainadminquota) { + $available = $domainadminquota - $quota_used; + $domainadminquotaunit = "KB"; + if ($domainadminquota > 1024) { + $domainadminquota = $domainadminquota / 1024; + $domainadminquotaunit = "MB"; + } + if ($domainadminquota > 1024) { + $domainadminquota = $domainadminquota / 1024; + $domainadminquotaunit = "GB"; + } + $availableunit = "KB"; + if ($available > 1024) { + $available = $available / 1024; + $availableunit = "MB"; + } + if ($available > 1024) { + $available = $available / 1024; + $availableunit = "GB"; + } + + throw new Exception('error: mailquota of the domain admin has been exceeded.
'. + 'max available: '.$domainadminquota.' '.$domainadminquotaunit.';
'. + 'max available for this user: '.$available.' '.$availableunit); + } + } + } + + return (string) intval($value); } private function validate_tbitskolaboverallquota($value, $postdata = array(), $validation_type = null)