Skip to content

Commit

Permalink
Merge pull request #27420 from owncloud/quotaFixes
Browse files Browse the repository at this point in the history
Quota fixes
  • Loading branch information
Vincent Petry committed Mar 22, 2017
2 parents 04cfdda + ba992a1 commit b1af6d1
Show file tree
Hide file tree
Showing 2 changed files with 365 additions and 31 deletions.
62 changes: 54 additions & 8 deletions apps/user_ldap/lib/User/User.php
Expand Up @@ -165,6 +165,10 @@ public function processAttributes($ldapEntry) {
$attr = strtolower($this->connection->ldapQuotaAttribute);
if(isset($ldapEntry[$attr])) {
$this->updateQuota($ldapEntry[$attr][0]);
} else {
if ($this->connection->ldapQuotaDefault !== '') {
$this->updateQuota();
}
}
unset($attr);

Expand Down Expand Up @@ -450,6 +454,20 @@ public function updateEmail($valueFromLDAP = null) {
}

/**
* Overall process goes as follow:
* 1. fetch the quota from LDAP and check if it's parseable with the "verifyQuotaValue" function
* 2. if the value can't be fetched, is empty or not parseable, use the default LDAP quota
* 3. if the default LDAP quota can't be parsed, use the ownCloud's default quota (use 'default')
* 4. check if the target user exists and set the quota for the user.
*
* In order to improve performance and prevent an unwanted extra LDAP call, the $valueFromLDAP
* parameter can be passed with the value of the attribute. This value will be considered as the
* quota for the user coming from the LDAP server (step 1 of the process) It can be useful to
* fetch all the user's attributes in one call and use the fetched values in this function.
* The expected value for that parameter is a string describing the quota for the user. Valid
* values are 'none' (unlimited), 'default' (the ownCloud's default quota), '1234' (quota in
* bytes), '1234 MB' (quota in MB - check the \OC_Helper::computerFileSize method for more info)
*
* fetches the quota from LDAP and stores it as ownCloud user value
* @param string $valueFromLDAP the quota attribute's value can be passed,
* to save the readAttribute request
Expand All @@ -459,23 +477,51 @@ public function updateQuota($valueFromLDAP = null) {
if($this->wasRefreshed('quota')) {
return;
}
//can be null
$quotaDefault = $this->connection->ldapQuotaDefault;
$quota = $quotaDefault !== '' ? $quotaDefault : null;
$quota = !is_null($valueFromLDAP) ? $valueFromLDAP : $quota;

$quota = false;
if(is_null($valueFromLDAP)) {
$quotaAttribute = $this->connection->ldapQuotaAttribute;
if(!empty($quotaAttribute)) {
if ($quotaAttribute !== '') {
$aQuota = $this->access->readAttribute($this->dn, $quotaAttribute);
if($aQuota && (count($aQuota) > 0)) {
$quota = $aQuota[0];
if ($this->verifyQuotaValue($aQuota[0])) {
$quota = $aQuota[0];
} else {
$this->log->log('not suitable LDAP quota found for user ' . $this->uid . ': [' . $aQuota[0] . ']', \OCP\Util::WARN);
}
}
}
} else {
if ($this->verifyQuotaValue($valueFromLDAP)) {
$quota = $valueFromLDAP;
} else {
$this->log->log('not suitable LDAP quota found for user ' . $this->uid . ': [' . $valueFromLDAP . ']', \OCP\Util::WARN);
}
}
if(!is_null($quota)) {
$this->userManager->get($this->uid)->setQuota($quota);

if ($quota === false) {
// quota not found using the LDAP attribute (or not parseable). Try the default quota
$defaultQuota = $this->connection->ldapQuotaDefault;
if ($this->verifyQuotaValue($defaultQuota)) {
$quota = $defaultQuota;
}
}

$targetUser = $this->userManager->get($this->uid);
if ($targetUser) {
if($quota !== false) {
$targetUser->setQuota($quota);
} else {
$this->log->log('not suitable default quota found for user ' . $this->uid . ': [' . $defaultQuota . ']', \OCP\Util::WARN);
$targetUser->setQuota('default');
}
} else {
$this->log->log('trying to set a quota for user ' . $this->uid . ' but the user is missing', \OCP\Util::ERROR);
}
}

private function verifyQuotaValue($quotaValue) {
return $quotaValue === 'none' || $quotaValue === 'default' || \OC_Helper::computerFileSize($quotaValue) !== false;
}

/**
Expand Down

0 comments on commit b1af6d1

Please sign in to comment.