diff --git a/apps/user_ldap/lib/Access.php b/apps/user_ldap/lib/Access.php index 426cd05e52aa0..96661d0ad046e 100644 --- a/apps/user_ldap/lib/Access.php +++ b/apps/user_ldap/lib/Access.php @@ -688,7 +688,7 @@ private function ldap2NextcloudNames($ldapObjects, $isUsers) { } $sndName = isset($ldapObject[$sndAttribute][0]) ? $ldapObject[$sndAttribute][0] : ''; - $this->cacheUserDisplayName($ncName, $nameByLDAP, $sndName); + $this->storeUserDisplayName($ncName, $nameByLDAP, $sndName); } elseif ($nameByLDAP !== null) { $this->cacheGroupDisplayName($ncName, $nameByLDAP); } @@ -745,14 +745,12 @@ public function cacheGroupExists(string $gid): void { * @param string $displayName2 the second display name * @throws \Exception */ - public function cacheUserDisplayName($ocName, $displayName, $displayName2 = '') { + public function storeUserDisplayName($ocName, $displayName, $displayName2 = '') { $user = $this->userManager->get($ocName); if ($user === null) { return; } - $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); - $cacheKeyTrunk = 'getDisplayName'; - $this->connection->writeToCache($cacheKeyTrunk . $ocName, $displayName); + $user->composeAndStoreDisplayName($displayName, $displayName2); } public function cacheGroupDisplayName(string $ncName, string $displayName): void { diff --git a/apps/user_ldap/lib/User/User.php b/apps/user_ldap/lib/User/User.php index 8ddec8033703a..d61bc53eb49b3 100644 --- a/apps/user_ldap/lib/User/User.php +++ b/apps/user_ldap/lib/User/User.php @@ -192,11 +192,6 @@ public function processAttributes($ldapEntry) { } if ($displayName !== '') { $this->composeAndStoreDisplayName($displayName, $displayName2); - $this->access->cacheUserDisplayName( - $this->getUsername(), - $displayName, - $displayName2 - ); } unset($attr); @@ -404,6 +399,10 @@ public function composeAndStoreDisplayName($displayName, $displayName2 = '') { return $displayName; } + public function getDisplayName(): ?string { + return $this->config->getUserValue($this->uid, 'user_ldap', 'displayName', null); + } + /** * Stores the LDAP Username in the Database * @param string $userName @@ -432,20 +431,11 @@ private function wasRefreshed($feature) { * @param string $valueFromLDAP if known, to save an LDAP read request * @return null */ - public function updateEmail($valueFromLDAP = null) { + public function updateEmail(string $valueFromLDAP) { if ($this->wasRefreshed('email')) { return; } $email = (string)$valueFromLDAP; - if (is_null($valueFromLDAP)) { - $emailAttribute = $this->connection->ldapEmailAttribute; - if ($emailAttribute !== '') { - $aEmail = $this->access->readAttribute($this->dn, $emailAttribute); - if (is_array($aEmail) && (count($aEmail) > 0)) { - $email = (string)$aEmail[0]; - } - } - } if ($email !== '') { $user = $this->userManager->get($this->uid); if (!is_null($user)) { @@ -489,17 +479,8 @@ public function updateQuota($valueFromLDAP = null) { } $quota = false; - if (is_null($valueFromLDAP) && $quotaAttribute !== '') { - $aQuota = $this->access->readAttribute($this->dn, $quotaAttribute); - if ($aQuota && (count($aQuota) > 0) && $this->verifyQuotaValue($aQuota[0])) { - $quota = $aQuota[0]; - } elseif (is_array($aQuota) && isset($aQuota[0])) { - $this->log->log('no suitable LDAP quota found for user ' . $this->uid . ': [' . $aQuota[0] . ']', ILogger::DEBUG); - } - } elseif ($this->verifyQuotaValue($valueFromLDAP)) { + if ($valueFromLDAP !== null && $this->verifyQuotaValue($valueFromLDAP)) { $quota = $valueFromLDAP; - } else { - $this->log->log('no suitable LDAP quota found for user ' . $this->uid . ': [' . $valueFromLDAP . ']', ILogger::DEBUG); } if ($quota === false && $this->verifyQuotaValue($defaultQuota)) { diff --git a/apps/user_ldap/lib/User_LDAP.php b/apps/user_ldap/lib/User_LDAP.php index 79f230ae00b69..ad6bc8aa2081f 100644 --- a/apps/user_ldap/lib/User_LDAP.php +++ b/apps/user_ldap/lib/User_LDAP.php @@ -39,6 +39,7 @@ namespace OCA\User_LDAP; +use Exception; use OC\ServerNotAvailableException; use OC\User\Backend; use OC\User\NoUserException; @@ -79,7 +80,7 @@ public function __construct(Access $access, IConfig $ocConfig, INotificationMana * * @param string $uid the Nextcloud user name * @return boolean either the user can or cannot - * @throws \Exception + * @throws Exception */ public function canChangeAvatar($uid) { if ($this->userPluginManager->implementsActions(Backend::PROVIDE_AVATAR)) { @@ -106,7 +107,7 @@ public function canChangeAvatar($uid) { * * @param string $loginName * @return string|false - * @throws \Exception + * @throws Exception */ public function loginName2UserName($loginName) { $cacheKey = 'loginName2UserName-' . $loginName; @@ -133,7 +134,7 @@ public function loginName2UserName($loginName) { return false; } } - + /** * returns the username for the given LDAP DN, if available * @@ -216,7 +217,7 @@ public function setPassword($uid, $password) { $user = $this->access->userManager->get($uid); if (!$user instanceof User) { - throw new \Exception('LDAP setPassword: Could not get user object for uid ' . $uid . + throw new Exception('LDAP setPassword: Could not get user object for uid ' . $uid . '. Maybe the LDAP entry has no set display name attribute?'); } if ($user->getUsername() !== false && $this->access->setPassword($user->getDN(), $password)) { @@ -287,7 +288,7 @@ public function getUsers($search = '', $limit = 10, $offset = 0) { * @param string|\OCA\User_LDAP\User\User $user either the Nextcloud user * name or an instance of that user * @return bool - * @throws \Exception + * @throws Exception * @throws \OC\ServerNotAvailableException */ public function userExistsOnLDAP($user) { @@ -324,7 +325,7 @@ public function userExistsOnLDAP($user) { return true; } catch (ServerNotAvailableException $e) { throw $e; - } catch (\Exception $e) { + } catch (Exception $e) { $this->access->connection->writeToCache($cacheKey, false); return false; } @@ -342,7 +343,7 @@ public function userExistsOnLDAP($user) { * check if a user exists * @param string $uid the username * @return boolean - * @throws \Exception when connection could not be established + * @throws Exception when connection could not be established */ public function userExists($uid) { $userExists = $this->access->connection->getFromCache('userExists'.$uid); @@ -399,7 +400,7 @@ public function deleteUser($uid) { * @param string $uid the username * @return bool|string * @throws NoUserException - * @throws \Exception + * @throws Exception */ public function getHome($uid) { // user Exists check required as it is not done in user proxy! @@ -431,8 +432,10 @@ public function getHome($uid) { /** * get display name of the user + * * @param string $uid user ID of the user * @return string|false display name + * @throws Exception */ public function getDisplayName($uid) { if ($this->userPluginManager->implementsActions(Backend::GET_DISPLAYNAME)) { @@ -443,44 +446,11 @@ public function getDisplayName($uid) { return false; } - $cacheKey = 'getDisplayName'.$uid; - if (!is_null($displayName = $this->access->connection->getFromCache($cacheKey))) { - return $displayName; - } - - //Check whether the display name is configured to have a 2nd feature - $additionalAttribute = $this->access->connection->ldapUserDisplayName2; - $displayName2 = ''; - if ($additionalAttribute !== '') { - $displayName2 = $this->access->readAttribute( - $this->access->username2dn($uid), - $additionalAttribute); - } - - $displayName = $this->access->readAttribute( - $this->access->username2dn($uid), - $this->access->connection->ldapUserDisplayName); - - if ($displayName && (count($displayName) > 0)) { - $displayName = $displayName[0]; - - if (is_array($displayName2)) { - $displayName2 = count($displayName2) > 0 ? $displayName2[0] : ''; - } - - $user = $this->access->userManager->get($uid); - if ($user instanceof User) { - $displayName = $user->composeAndStoreDisplayName($displayName, $displayName2); - $this->access->connection->writeToCache($cacheKey, $displayName); - } - if ($user instanceof OfflineUser) { - /** @var OfflineUser $user*/ - $displayName = $user->getDisplayName(); - } - return $displayName; + $user = $this->access->userManager->get($uid); + if (null === $user) { + return false; } - - return null; + return $user->getDisplayName(); } /** @@ -492,7 +462,7 @@ public function getDisplayName($uid) { public function setDisplayName($uid, $displayName) { if ($this->userPluginManager->implementsActions(Backend::SET_DISPLAYNAME)) { $this->userPluginManager->setDisplayName($uid, $displayName); - $this->access->cacheUserDisplayName($uid, $displayName); + $this->access->storeUserDisplayName($uid, $displayName); return $displayName; } return false; @@ -574,7 +544,7 @@ public function countUsers() { public function getBackendName() { return 'LDAP'; } - + /** * Return access for LDAP interaction. * @param string $uid @@ -583,7 +553,7 @@ public function getBackendName() { public function getLDAPAccess($uid) { return $this->access; } - + /** * Return LDAP connection resource from a cloned connection. * The cloned connection needs to be closed manually.