4747use OCA \User_LDAP \User \User ;
4848use OCP \IConfig ;
4949use OCP \ILogger ;
50- use OCP \IUser ;
5150use OCP \IUserSession ;
5251use OCP \Notification \IManager as INotificationManager ;
5352use OCP \Util ;
@@ -59,9 +58,6 @@ class User_LDAP extends BackendUtility implements \OCP\IUserBackend, \OCP\UserIn
5958 /** @var INotificationManager */
6059 protected $ notificationManager ;
6160
62- /** @var string */
63- protected $ currentUserInDeletionProcess ;
64-
6561 /** @var UserPluginManager */
6662 protected $ userPluginManager ;
6763
@@ -76,20 +72,6 @@ public function __construct(Access $access, IConfig $ocConfig, INotificationMana
7672 $ this ->ocConfig = $ ocConfig ;
7773 $ this ->notificationManager = $ notificationManager ;
7874 $ this ->userPluginManager = $ userPluginManager ;
79- $ this ->registerHooks ($ userSession );
80- }
81-
82- protected function registerHooks (IUserSession $ userSession ) {
83- $ userSession ->listen ('\OC\User ' , 'preDelete ' , [$ this , 'preDeleteUser ' ]);
84- $ userSession ->listen ('\OC\User ' , 'postDelete ' , [$ this , 'postDeleteUser ' ]);
85- }
86-
87- public function preDeleteUser (IUser $ user ) {
88- $ this ->currentUserInDeletionProcess = $ user ->getUID ();
89- }
90-
91- public function postDeleteUser () {
92- $ this ->currentUserInDeletionProcess = null ;
9375 }
9476
9577 /**
@@ -315,25 +297,35 @@ public function userExistsOnLDAP($user) {
315297 if (is_null ($ user )) {
316298 return false ;
317299 }
300+ $ uid = $ user instanceof User ? $ user ->getUsername () : $ user ->getOCName ();
301+ $ cacheKey = 'userExistsOnLDAP ' . $ uid ;
302+ $ userExists = $ this ->access ->connection ->getFromCache ($ cacheKey );
303+ if (!is_null ($ userExists )) {
304+ return (bool )$ userExists ;
305+ }
318306
319307 $ dn = $ user ->getDN ();
320308 //check if user really still exists by reading its entry
321309 if (!is_array ($ this ->access ->readAttribute ($ dn , '' , $ this ->access ->connection ->ldapUserFilter ))) {
322310 try {
323311 $ uuid = $ this ->access ->getUserMapper ()->getUUIDByDN ($ dn );
324312 if (!$ uuid ) {
313+ $ this ->access ->connection ->writeToCache ($ cacheKey , false );
325314 return false ;
326315 }
327316 $ newDn = $ this ->access ->getUserDnByUuid ($ uuid );
328317 //check if renamed user is still valid by reapplying the ldap filter
329318 if ($ newDn === $ dn || !is_array ($ this ->access ->readAttribute ($ newDn , '' , $ this ->access ->connection ->ldapUserFilter ))) {
319+ $ this ->access ->connection ->writeToCache ($ cacheKey , false );
330320 return false ;
331321 }
332322 $ this ->access ->getUserMapper ()->setDNbyUUID ($ newDn , $ uuid );
323+ $ this ->access ->connection ->writeToCache ($ cacheKey , true );
333324 return true ;
334325 } catch (ServerNotAvailableException $ e ) {
335326 throw $ e ;
336327 } catch (\Exception $ e ) {
328+ $ this ->access ->connection ->writeToCache ($ cacheKey , false );
337329 return false ;
338330 }
339331 }
@@ -342,6 +334,7 @@ public function userExistsOnLDAP($user) {
342334 $ user ->unmark ();
343335 }
344336
337+ $ this ->access ->connection ->writeToCache ($ cacheKey , true );
345338 return true ;
346339 }
347340
@@ -364,15 +357,10 @@ public function userExists($uid) {
364357 $ this ->access ->connection ->ldapHost , ILogger::DEBUG );
365358 $ this ->access ->connection ->writeToCache ('userExists ' .$ uid , false );
366359 return false ;
367- } else if ($ user instanceof OfflineUser) {
368- //express check for users marked as deleted. Returning true is
369- //necessary for cleanup
370- return true ;
371360 }
372361
373- $ result = $ this ->userExistsOnLDAP ($ user );
374- $ this ->access ->connection ->writeToCache ('userExists ' .$ uid , $ result );
375- return $ result ;
362+ $ this ->access ->connection ->writeToCache ('userExists ' .$ uid , true );
363+ return true ;
376364 }
377365
378366 /**
@@ -430,21 +418,13 @@ public function getHome($uid) {
430418
431419 // early return path if it is a deleted user
432420 $ user = $ this ->access ->userManager ->get ($ uid );
433- if ($ user instanceof OfflineUser) {
434- if ($ this ->currentUserInDeletionProcess !== null
435- && $ this ->currentUserInDeletionProcess === $ user ->getOCName ()
436- ) {
437- return $ user ->getHomePath ();
438- } else {
439- throw new NoUserException ($ uid . ' is not a valid user anymore ' );
440- }
441- } else if ($ user === null ) {
421+ if ($ user instanceof User || $ user instanceof OfflineUser) {
422+ $ path = $ user ->getHomePath () ?: false ;
423+ } else {
442424 throw new NoUserException ($ uid . ' is not a valid user anymore ' );
443425 }
444426
445- $ path = $ user ->getHomePath ();
446427 $ this ->access ->cacheUserHome ($ uid , $ path );
447-
448428 return $ path ;
449429 }
450430
0 commit comments