]> source.dussan.org Git - nextcloud-server.git/commitdiff
Use the new IProvideEnabledStateBackend interface in user_ldap
authorCôme Chilliet <come.chilliet@nextcloud.com>
Wed, 5 Oct 2022 15:58:56 +0000 (17:58 +0200)
committerCôme Chilliet <come.chilliet@nextcloud.com>
Tue, 23 May 2023 09:25:22 +0000 (11:25 +0200)
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
apps/user_ldap/lib/User/DeletedUsersIndex.php
apps/user_ldap/lib/User_LDAP.php
apps/user_ldap/lib/User_Proxy.php

index 1e057987eef017e66e35bbc91b7b7acdc50d662f..d679ca86d93ac70c0af677ad2ad7a4fadeea338f 100644 (file)
@@ -24,6 +24,7 @@
 namespace OCA\User_LDAP\User;
 
 use OCA\User_LDAP\Mapping\UserMapping;
+use OCP\IConfig;
 use OCP\Share\IManager;
 
 /**
@@ -31,24 +32,16 @@ use OCP\Share\IManager;
  * @package OCA\User_LDAP
  */
 class DeletedUsersIndex {
-       /**
-        * @var \OCP\IConfig $config
-        */
-       protected $config;
-
-       /**
-        * @var \OCA\User_LDAP\Mapping\UserMapping $mapping
-        */
-       protected $mapping;
+       protected IConfig $config;
+       protected UserMapping $mapping;
+       protected ?array $deletedUsers = null;
+       private IManager $shareManager;
 
-       /**
-        * @var array $deletedUsers
-        */
-       protected $deletedUsers;
-       /** @var IManager */
-       private $shareManager;
-
-       public function __construct(\OCP\IConfig $config, UserMapping $mapping, IManager $shareManager) {
+       public function __construct(
+               IConfig $config,
+               UserMapping $mapping,
+               IManager $shareManager
+       ) {
                $this->config = $config;
                $this->mapping = $mapping;
                $this->shareManager = $shareManager;
@@ -56,11 +49,10 @@ class DeletedUsersIndex {
 
        /**
         * reads LDAP users marked as deleted from the database
-        * @return \OCA\User_LDAP\User\OfflineUser[]
+        * @return OfflineUser[]
         */
-       private function fetchDeletedUsers() {
-               $deletedUsers = $this->config->getUsersForUserValue(
-                       'user_ldap', 'isDeleted', '1');
+       private function fetchDeletedUsers(): array {
+               $deletedUsers = $this->config->getUsersForUserValue('user_ldap', 'isDeleted', '1');
 
                $userObjects = [];
                foreach ($deletedUsers as $user) {
@@ -73,9 +65,9 @@ class DeletedUsersIndex {
 
        /**
         * returns all LDAP users that are marked as deleted
-        * @return \OCA\User_LDAP\User\OfflineUser[]
+        * @return OfflineUser[]
         */
-       public function getUsers() {
+       public function getUsers(): array {
                if (is_array($this->deletedUsers)) {
                        return $this->deletedUsers;
                }
@@ -84,9 +76,8 @@ class DeletedUsersIndex {
 
        /**
         * whether at least one user was detected as deleted
-        * @return bool
         */
-       public function hasUsers() {
+       public function hasUsers(): bool {
                if (!is_array($this->deletedUsers)) {
                        $this->fetchDeletedUsers();
                }
@@ -96,12 +87,10 @@ class DeletedUsersIndex {
        /**
         * marks a user as deleted
         *
-        * @param string $ocName
         * @throws \OCP\PreConditionNotMetException
         */
-       public function markUser($ocName) {
-               $curValue = $this->config->getUserValue($ocName, 'user_ldap', 'isDeleted', '0');
-               if ($curValue === '1') {
+       public function markUser(string $ocName): void {
+               if ($this->isUserMarked($ocName)) {
                        // the user is already marked, do not write to DB again
                        return;
                }
@@ -109,4 +98,8 @@ class DeletedUsersIndex {
                $this->config->setUserValue($ocName, 'user_ldap', 'foundDeleted', (string)time());
                $this->deletedUsers = null;
        }
+
+       public function isUserMarked(string $ocName): bool {
+               return ($this->config->getUserValue($ocName, 'user_ldap', 'isDeleted', '0') === '1');
+       }
 }
index 772b2f460951771587697f90d33845addd20526b..bf317f1b95b866e902e839238064f454504d919e 100644 (file)
@@ -42,6 +42,7 @@ use OC\ServerNotAvailableException;
 use OC\User\Backend;
 use OC\User\NoUserException;
 use OCA\User_LDAP\Exceptions\NotOnLDAP;
+use OCA\User_LDAP\User\DeletedUsersIndex;
 use OCA\User_LDAP\User\OfflineUser;
 use OCA\User_LDAP\User\User;
 use OCP\IConfig;
@@ -50,10 +51,11 @@ use OCP\IUserSession;
 use OCP\Notification\IManager as INotificationManager;
 use OCP\User\Backend\ICountMappedUsersBackend;
 use OCP\User\Backend\ICountUsersBackend;
+use OCP\User\Backend\IProvideEnabledStateBackend;
 use OCP\UserInterface;
 use Psr\Log\LoggerInterface;
 
-class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend {
+class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend, IProvideEnabledStateBackend {
        /** @var \OCP\IConfig */
        protected $ocConfig;
 
@@ -66,6 +68,8 @@ class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, I
        /** @var LoggerInterface */
        protected $logger;
 
+       protected DeletedUsersIndex $deletedUsersIndex;
+
        /**
         * @param Access $access
         * @param \OCP\IConfig $ocConfig
@@ -78,6 +82,7 @@ class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, I
                $this->notificationManager = $notificationManager;
                $this->userPluginManager = $userPluginManager;
                $this->logger = \OC::$server->get(LoggerInterface::class);
+               $this->deletedUsersIndex = \OC::$server->get(DeletedUsersIndex::class);
        }
 
        /**
@@ -392,13 +397,13 @@ class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, I
                        }
                }
 
-               $marked = (int)$this->ocConfig->getUserValue($uid, 'user_ldap', 'isDeleted', 0);
-               if ($marked === 0) {
+               $marked = $this->deletedUsersIndex->isUserMarked($uid);
+               if (!$marked) {
                        try {
                                $user = $this->access->userManager->get($uid);
                                if (($user instanceof User) && !$this->userExistsOnLDAP($uid, true)) {
                                        $user->markUser();
-                                       $marked = 1;
+                                       $marked = true;
                                }
                        } catch (\Exception $e) {
                                $this->logger->debug(
@@ -406,7 +411,7 @@ class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, I
                                        ['app' => 'user_ldap', 'exception' => $e]
                                );
                        }
-                       if ($marked === 0) {
+                       if (!$marked) {
                                $this->logger->notice(
                                        'User '.$uid . ' is not marked as deleted, not cleaning up.',
                                        ['app' => 'user_ldap']
@@ -669,4 +674,16 @@ class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, I
                }
                return false;
        }
+
+       public function isUserEnabled(string $uid, callable $queryDatabaseValue): bool {
+               if ($this->deletedUsersIndex->isUserMarked($uid) && ($this->ocConfig->getAppValue('user_ldap', 'markRemnantsAsDisabled', '0') === '1')) {
+                       return true;
+               } else {
+                       return $queryDatabaseValue();
+               }
+       }
+
+       public function setUserEnabled(string $uid, bool $enabled, callable $queryDatabaseValue, callable $setDatabaseValue): void {
+               $setDatabaseValue($enabled);
+       }
 }
index b07c632eeeb921bd1638be7d79af614a4af0af9b..59f72c2c2f3b9371540775d6d12237e39752653f 100644 (file)
@@ -36,15 +36,15 @@ use OCP\IConfig;
 use OCP\IUserBackend;
 use OCP\IUserSession;
 use OCP\Notification\IManager as INotificationManager;
+use OCP\UserInterface;
 use OCP\User\Backend\ICountMappedUsersBackend;
 use OCP\User\Backend\ICountUsersBackend;
-use OCP\UserInterface;
+use OCP\User\Backend\IProvideEnabledStateBackend;
 
-class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend {
-  /** @var array<string,User_LDAP> */
-       private $backends = [];
-       /** @var ?User_LDAP */
-       private $refBackend = null;
+class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend, IProvideEnabledStateBackend {
+       /** @var User_LDAP[] */
+       private array $backends = [];
+       private ?User_LDAP $refBackend = null;
 
        private bool $isSetUp = false;
        private Helper $helper;
@@ -438,4 +438,12 @@ class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP
        public function createUser($username, $password) {
                return $this->handleRequest($username, 'createUser', [$username, $password]);
        }
+
+       public function isUserEnabled(string $uid, callable $queryDatabaseValue): bool {
+               return $this->handleRequest($uid, 'isUserEnabled', [$uid, $queryDatabaseValue]);
+       }
+
+       public function setUserEnabled(string $uid, bool $enabled, callable $queryDatabaseValue, callable $setDatabaseValue): void {
+               $this->handleRequest($uid, 'setUserEnabled', [$uid, $enabled, $queryDatabaseValue, $setDatabaseValue]);
+       }
 }