@@ -262,7 +262,8 @@ public function getUsers($search = '', $limit = 10, $offset = 0) {
262262 /**
263263 * checks whether a user is still available on LDAP
264264 *
265- * @param string|User $user either the Nextcloud user id or an instance of that user
265+ * @param string|User $user either the Nextcloud user id or an instance of
266+ * that user
266267 * @throws \Exception
267268 * @throws ServerNotAvailableException
268269 */
@@ -421,26 +422,21 @@ public function getHome($uid) {
421422 return $ path ;
422423 }
423424
424- /**
425- * get display name of the user
426- * @param string $uid user ID of the user
427- * @return string|false display name
428- */
429- public function getDisplayName ($ uid ) {
430- if ($ this ->userPluginManager ->implementsActions (Backend::GET_DISPLAYNAME )) {
431- return $ this ->userPluginManager ->getDisplayName ($ uid );
432- }
433-
434- if (!$ this ->userExists ($ uid )) {
435- return false ;
425+ private function getDisplayNameFromDatabase (string $ uid ): ?string {
426+ $ user = $ this ->access ->userManager ->get ($ uid );
427+ if ($ user instanceof User) {
428+ $ displayName = $ user ->fetchStoredDisplayName ();
429+ if ($ displayName !== '' ) {
430+ return $ displayName ;
431+ }
436432 }
437-
438- $ cacheKey = 'getDisplayName ' . $ uid ;
439- if (!is_null ($ displayName = $ this ->access ->connection ->getFromCache ($ cacheKey ))) {
440- return $ displayName ;
433+ if ($ user instanceof OfflineUser) {
434+ return $ user ->getDisplayName ();
441435 }
436+ return null ;
437+ }
442438
443- //Check whether the display name is configured to have a 2nd feature
439+ private function getDisplayNameFromLdap ( string $ uid ): string {
444440 $ additionalAttribute = $ this ->access ->connection ->ldapUserDisplayName2 ;
445441 $ displayName2 = '' ;
446442 if ($ additionalAttribute !== '' ) {
@@ -462,16 +458,40 @@ public function getDisplayName($uid) {
462458
463459 $ user = $ this ->access ->userManager ->get ($ uid );
464460 if ($ user instanceof User) {
465- $ displayName = $ user ->composeAndStoreDisplayName ($ displayName , (string )$ displayName2 );
466- $ this ->access ->connection ->writeToCache ($ cacheKey , $ displayName );
461+ return $ user ->composeAndStoreDisplayName ($ displayName , (string )$ displayName2 );
467462 }
468463 if ($ user instanceof OfflineUser) {
469- $ displayName = $ user ->getDisplayName ();
464+ return $ user ->getDisplayName ();
470465 }
466+ }
467+
468+ return '' ;
469+ }
470+
471+ public function getDisplayName ($ uid ): string {
472+ if ($ this ->userPluginManager ->implementsActions (Backend::GET_DISPLAYNAME )) {
473+ return $ this ->userPluginManager ->getDisplayName ($ uid );
474+ }
475+
476+ if (!$ this ->userExists ($ uid )) {
477+ return '' ;
478+ }
479+
480+ $ cacheKey = 'getDisplayName ' . $ uid ;
481+ if (!is_null ($ displayName = $ this ->access ->connection ->getFromCache ($ cacheKey ))) {
471482 return $ displayName ;
472483 }
473484
474- return null ;
485+ if ($ displayName = $ this ->getDisplayNameFromDatabase ($ uid )) {
486+ $ this ->access ->connection ->writeToCache ($ cacheKey , $ displayName );
487+ return $ displayName ;
488+ }
489+
490+ if ($ displayName = $ this ->getDisplayNameFromLdap ($ uid )) {
491+ $ this ->access ->connection ->writeToCache ($ cacheKey , $ displayName );
492+ }
493+
494+ return $ displayName ;
475495 }
476496
477497 /**
@@ -495,7 +515,8 @@ public function setDisplayName($uid, $displayName) {
495515 * @param string $search
496516 * @param int|null $limit
497517 * @param int|null $offset
498- * @return array an array of all displayNames (value) and the corresponding uids (key)
518+ * @return array an array of all displayNames (value) and the corresponding
519+ * uids (key)
499520 */
500521 public function getDisplayNames ($ search = '' , $ limit = null , $ offset = null ) {
501522 $ cacheKey = 'getDisplayNames- ' . $ search . '- ' . $ limit . '- ' . $ offset ;
0 commit comments