From 3af1ab2b2a5e925ac2fd65b0182c6553fbf7417f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Wed, 5 Oct 2022 17:13:33 +0200 Subject: [PATCH 1/9] Add user enabled state backend feature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/private/User/User.php | 39 +++++++++++---- .../Backend/IProvideEnabledStateBackend.php | 47 +++++++++++++++++++ 2 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 lib/public/User/Backend/IProvideEnabledStateBackend.php diff --git a/lib/private/User/User.php b/lib/private/User/User.php index c68d4ee290ae5..8830b4aae71c4 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -55,6 +55,7 @@ use OCP\User\Backend\ISetDisplayNameBackend; use OCP\User\Backend\ISetPasswordBackend; use OCP\User\Backend\IProvideAvatarBackend; +use OCP\User\Backend\IProvideEnabledStateBackend; use OCP\User\Backend\IGetHomeBackend; use OCP\UserInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -432,25 +433,45 @@ public function canChangeDisplayName() { * @return bool */ public function isEnabled() { - if ($this->enabled === null) { - $enabled = $this->config->getUserValue($this->uid, 'core', 'enabled', 'true'); - $this->enabled = $enabled === 'true'; + $queryDatabaseValue = function (): bool { + if ($this->enabled === null) { + $enabled = $this->config->getUserValue($this->uid, 'core', 'enabled', 'true'); + $this->enabled = $enabled === 'true'; + } + return (bool) $this->enabled; + }; + if ($this->backend instanceof IProvideEnabledStateBackend) { + return $this->backend->isUserEnabled($this->uid, $queryDatabaseValue); + } else { + return $queryDatabaseValue(); } - return (bool) $this->enabled; } /** * set the enabled status for the user * - * @param bool $enabled + * @return void */ public function setEnabled(bool $enabled = true) { $oldStatus = $this->isEnabled(); - $this->enabled = $enabled; - if ($oldStatus !== $this->enabled) { - // TODO: First change the value, then trigger the event as done for all other properties. - $this->triggerChange('enabled', $enabled, $oldStatus); + $setDatabaseValue = function (bool $enabled): void { $this->config->setUserValue($this->uid, 'core', 'enabled', $enabled ? 'true' : 'false'); + $this->enabled = $enabled; + }; + if ($this->backend instanceof IProvideEnabledStateBackend) { + $queryDatabaseValue = function (): bool { + if ($this->enabled === null) { + $enabled = $this->config->getUserValue($this->uid, 'core', 'enabled', 'true'); + $this->enabled = $enabled === 'true'; + } + return (bool) $this->enabled; + }; + $this->backend->setUserEnabled($this->uid, $enabled, $queryDatabaseValue, $setDatabaseValue); + } else { + $setDatabaseValue($enabled); + } + if ($oldStatus !== $enabled) { + $this->triggerChange('enabled', $enabled, $oldStatus); } } diff --git a/lib/public/User/Backend/IProvideEnabledStateBackend.php b/lib/public/User/Backend/IProvideEnabledStateBackend.php new file mode 100644 index 0000000000000..143eca548ab92 --- /dev/null +++ b/lib/public/User/Backend/IProvideEnabledStateBackend.php @@ -0,0 +1,47 @@ + + * + * @author Côme Chilliet + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCP\User\Backend; + +/** + * @since 26.0.0 + */ +interface IProvideEnabledStateBackend { + /** + * @since 26.0.0 + * + * @param callable():bool $queryDatabaseValue A callable to query the enabled state from database + */ + public function isUserEnabled(string $uid, callable $queryDatabaseValue): bool; + + /** + * @since 26.0.0 + * + * @param callable():bool $queryDatabaseValue A callable to query the enabled state from database + * @param callable(bool):void $setDatabaseValue A callable to set the enabled state in the database. + */ + public function setUserEnabled(string $uid, bool $enabled, callable $queryDatabaseValue, callable $setDatabaseValue): void; +} From 684a81e4bd72b96e13828a423c4765456bc4ae4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Wed, 5 Oct 2022 17:58:56 +0200 Subject: [PATCH 2/9] Use the new IProvideEnabledStateBackend interface in user_ldap MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/user_ldap/lib/User/DeletedUsersIndex.php | 51 ++++++++----------- apps/user_ldap/lib/User_LDAP.php | 27 ++++++++-- apps/user_ldap/lib/User_Proxy.php | 20 +++++--- 3 files changed, 58 insertions(+), 40 deletions(-) diff --git a/apps/user_ldap/lib/User/DeletedUsersIndex.php b/apps/user_ldap/lib/User/DeletedUsersIndex.php index 1e057987eef01..d679ca86d93ac 100644 --- a/apps/user_ldap/lib/User/DeletedUsersIndex.php +++ b/apps/user_ldap/lib/User/DeletedUsersIndex.php @@ -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 @@ * @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 @@ public function __construct(\OCP\IConfig $config, UserMapping $mapping, IManager /** * 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 @@ private function fetchDeletedUsers() { /** * 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 @@ public function getUsers() { /** * 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 @@ public function hasUsers() { /** * 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 @@ public function markUser($ocName) { $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'); + } } diff --git a/apps/user_ldap/lib/User_LDAP.php b/apps/user_ldap/lib/User_LDAP.php index 772b2f4609517..bf317f1b95b86 100644 --- a/apps/user_ldap/lib/User_LDAP.php +++ b/apps/user_ldap/lib/User_LDAP.php @@ -42,6 +42,7 @@ 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\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 @@ public function __construct(Access $access, IConfig $ocConfig, INotificationMana $this->notificationManager = $notificationManager; $this->userPluginManager = $userPluginManager; $this->logger = \OC::$server->get(LoggerInterface::class); + $this->deletedUsersIndex = \OC::$server->get(DeletedUsersIndex::class); } /** @@ -392,13 +397,13 @@ public function deleteUser($uid) { } } - $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 @@ public function deleteUser($uid) { ['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 @@ public function createUser($username, $password) { } 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); + } } diff --git a/apps/user_ldap/lib/User_Proxy.php b/apps/user_ldap/lib/User_Proxy.php index b07c632eeeb92..59f72c2c2f3b9 100644 --- a/apps/user_ldap/lib/User_Proxy.php +++ b/apps/user_ldap/lib/User_Proxy.php @@ -36,15 +36,15 @@ 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 */ - 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 @@ public function getNewLDAPConnection($uid) { 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]); + } } From 50ccfb4f5bd200c2b12b737c3c7ba8c0e4eb1302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 23 May 2023 16:03:17 +0200 Subject: [PATCH 3/9] [user_ldap] Add per-connection setting for marking remnants as disabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/user_ldap/js/wizard/wizardTabAdvanced.js | 13 ++++++++++ apps/user_ldap/lib/Configuration.php | 3 +++ apps/user_ldap/lib/Connection.php | 1 + apps/user_ldap/lib/User_LDAP.php | 4 +-- apps/user_ldap/templates/settings.php | 25 ++++++++++--------- 5 files changed, 32 insertions(+), 14 deletions(-) diff --git a/apps/user_ldap/js/wizard/wizardTabAdvanced.js b/apps/user_ldap/js/wizard/wizardTabAdvanced.js index a438b847401a0..3b25189796869 100644 --- a/apps/user_ldap/js/wizard/wizardTabAdvanced.js +++ b/apps/user_ldap/js/wizard/wizardTabAdvanced.js @@ -67,6 +67,10 @@ OCA = OCA || {}; $element: $('#ldap_attributes_for_user_search'), setMethod: 'setSearchAttributesUsers' }, + ldap_mark_remnants_as_disabled: { + $element: $('#ldap_mark_remnants_as_disabled'), + setMethod: 'setMarkRemnantsAsDisabled' + }, ldap_group_display_name: { $element: $('#ldap_group_display_name'), setMethod: 'setGroupDisplayName' @@ -275,6 +279,15 @@ OCA = OCA || {}; this.setElementValue(this.managedItems.ldap_attributes_for_user_search.$element, attributes); }, + /** + * enables or disables marking remnants as disabled + * + * @param {string} markRemnantsAsDisabled contains an int + */ + setMarkRemnantsAsDisabled: function(markRemnantsAsDisabled) { + this.setElementValue(this.managedItems.ldap_mark_remnants_as_disabled.$element, markRemnantsAsDisabled); + }, + /** * sets the display name attribute for groups * diff --git a/apps/user_ldap/lib/Configuration.php b/apps/user_ldap/lib/Configuration.php index ef64f75a9ef60..abdb174c88236 100644 --- a/apps/user_ldap/lib/Configuration.php +++ b/apps/user_ldap/lib/Configuration.php @@ -115,6 +115,7 @@ class Configuration { 'ldapExpertUsernameAttr' => null, 'ldapExpertUUIDUserAttr' => null, 'ldapExpertUUIDGroupAttr' => null, + 'markRemnantsAsDisabled' => false, 'lastJpegPhotoLookup' => null, 'ldapNestedGroups' => false, 'ldapPagingSize' => null, @@ -468,6 +469,7 @@ public function getDefaults(): array { 'ldap_expert_uuid_group_attr' => '', 'has_memberof_filter_support' => 0, 'use_memberof_to_detect_membership' => 1, + 'ldap_mark_remnants_as_disabled' => 0, 'last_jpegPhoto_lookup' => 0, 'ldap_nested_groups' => 0, 'ldap_paging_size' => 500, @@ -543,6 +545,7 @@ public function getConfigTranslationArray(): array { 'ldap_expert_uuid_group_attr' => 'ldapExpertUUIDGroupAttr', 'has_memberof_filter_support' => 'hasMemberOfFilterSupport', 'use_memberof_to_detect_membership' => 'useMemberOfToDetectMembership', + 'ldap_mark_remnants_as_disabled' => 'markRemnantsAsDisabled', 'last_jpegPhoto_lookup' => 'lastJpegPhotoLookup', 'ldap_nested_groups' => 'ldapNestedGroups', 'ldap_paging_size' => 'ldapPagingSize', diff --git a/apps/user_ldap/lib/Connection.php b/apps/user_ldap/lib/Connection.php index d8d00dd4d2732..76a80583029a4 100644 --- a/apps/user_ldap/lib/Connection.php +++ b/apps/user_ldap/lib/Connection.php @@ -63,6 +63,7 @@ * @property string ldapEmailAttribute * @property string ldapExtStorageHomeAttribute * @property string homeFolderNamingRule + * @property bool|string markRemnantsAsDisabled * @property bool|string ldapNestedGroups * @property string[] ldapBaseGroups * @property string ldapGroupFilter diff --git a/apps/user_ldap/lib/User_LDAP.php b/apps/user_ldap/lib/User_LDAP.php index bf317f1b95b86..61abb1627f95c 100644 --- a/apps/user_ldap/lib/User_LDAP.php +++ b/apps/user_ldap/lib/User_LDAP.php @@ -676,8 +676,8 @@ public function createUser($username, $password) { } public function isUserEnabled(string $uid, callable $queryDatabaseValue): bool { - if ($this->deletedUsersIndex->isUserMarked($uid) && ($this->ocConfig->getAppValue('user_ldap', 'markRemnantsAsDisabled', '0') === '1')) { - return true; + if ($this->deletedUsersIndex->isUserMarked($uid) && ((int)$this->access->connection->markRemnantsAsDisabled === 1)) { + return false; } else { return $queryDatabaseValue(); } diff --git a/apps/user_ldap/templates/settings.php b/apps/user_ldap/templates/settings.php index 916ff84b82ae5..ae4091288b500 100644 --- a/apps/user_ldap/templates/settings.php +++ b/apps/user_ldap/templates/settings.php @@ -1,6 +1,6 @@ '.$l->t('Warning: The PHP LDAP module is not installed, the backend will not work. Please ask your system administrator to install it.').'

'); } - ?> +?> @@ -91,20 +91,21 @@

t('Optional. An LDAP attribute to be added to the display name in brackets. Results in e.g. »John Doe (john.doe@example.org)«.'));?>

t('Base User Tree'));?>

t('User Search Attributes'));?>

+

t('When switched on, users imported from LDAP which are then missing will be disabled'));?>

t('The LDAP attribute to use to generate the groups\'s display name.'));?>

t('Base Group Tree'));?>

t('Group Search Attributes'));?>

+ p(' selected'); + } ?>>uniqueMember

t('The LDAP attribute that on group objects contains an LDAP search URL that determines what objects belong to the group. (An empty setting disables dynamic group membership functionality.)'));?>

t('When switched on, groups that contain groups are supported. (Only works if the group member attribute contains DNs.)'));?>

t('Chunksize used for paged LDAP searches that may return bulky results like user or group enumeration. (Setting it 0 disables paged LDAP searches in those situations.)'));?>

From 285c42ab14199ab1c6a5f32cf1590e7a0aa3be49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 23 May 2023 17:18:19 +0200 Subject: [PATCH 4/9] Fix user tests, avoid setting enabled state to the same value MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/private/User/User.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/private/User/User.php b/lib/private/User/User.php index 8830b4aae71c4..3b7123bd5751d 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -467,10 +467,11 @@ public function setEnabled(bool $enabled = true) { return (bool) $this->enabled; }; $this->backend->setUserEnabled($this->uid, $enabled, $queryDatabaseValue, $setDatabaseValue); - } else { + if ($oldStatus !== $enabled) { + $this->triggerChange('enabled', $enabled, $oldStatus); + } + } elseif ($oldStatus !== $enabled) { $setDatabaseValue($enabled); - } - if ($oldStatus !== $enabled) { $this->triggerChange('enabled', $enabled, $oldStatus); } } From 47bb12b226065f18d7b8b166c45903180ffb654a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 25 May 2023 11:40:40 +0200 Subject: [PATCH 5/9] Fix autoloaders MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index f35b9c94fbc45..7c1a4d29efd76 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -641,6 +641,7 @@ 'OCP\\User\\Backend\\IGetRealUIDBackend' => $baseDir . '/lib/public/User/Backend/IGetRealUIDBackend.php', 'OCP\\User\\Backend\\IPasswordConfirmationBackend' => $baseDir . '/lib/public/User/Backend/IPasswordConfirmationBackend.php', 'OCP\\User\\Backend\\IProvideAvatarBackend' => $baseDir . '/lib/public/User/Backend/IProvideAvatarBackend.php', + 'OCP\\User\\Backend\\IProvideEnabledStateBackend' => $baseDir . '/lib/public/User/Backend/IProvideEnabledStateBackend.php', 'OCP\\User\\Backend\\ISearchKnownUsersBackend' => $baseDir . '/lib/public/User/Backend/ISearchKnownUsersBackend.php', 'OCP\\User\\Backend\\ISetDisplayNameBackend' => $baseDir . '/lib/public/User/Backend/ISetDisplayNameBackend.php', 'OCP\\User\\Backend\\ISetPasswordBackend' => $baseDir . '/lib/public/User/Backend/ISetPasswordBackend.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 8de7ef99d0216..f263d156a9c62 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -674,6 +674,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\User\\Backend\\IGetRealUIDBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IGetRealUIDBackend.php', 'OCP\\User\\Backend\\IPasswordConfirmationBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IPasswordConfirmationBackend.php', 'OCP\\User\\Backend\\IProvideAvatarBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IProvideAvatarBackend.php', + 'OCP\\User\\Backend\\IProvideEnabledStateBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/IProvideEnabledStateBackend.php', 'OCP\\User\\Backend\\ISearchKnownUsersBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ISearchKnownUsersBackend.php', 'OCP\\User\\Backend\\ISetDisplayNameBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ISetDisplayNameBackend.php', 'OCP\\User\\Backend\\ISetPasswordBackend' => __DIR__ . '/../../..' . '/lib/public/User/Backend/ISetPasswordBackend.php', From add59d2309cc902923aab60dd4348164eed033fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 25 May 2023 12:18:28 +0200 Subject: [PATCH 6/9] Use DI for DeletedUsersIndex and fix tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/user_ldap/lib/User_LDAP.php | 36 ++-- apps/user_ldap/lib/User_Proxy.php | 21 +- .../Lib/IntegrationTestAttributeDetection.php | 4 +- .../IntegrationTestFetchUsersByLoginName.php | 4 +- .../Integration/Lib/IntegrationTestPaging.php | 4 +- .../Lib/User/IntegrationTestUserAvatar.php | 3 +- .../Lib/User/IntegrationTestUserCleanUp.php | 4 +- .../User/IntegrationTestUserDisplayName.php | 4 +- apps/user_ldap/tests/User_LDAPTest.php | 189 ++++++++++-------- 9 files changed, 151 insertions(+), 118 deletions(-) diff --git a/apps/user_ldap/lib/User_LDAP.php b/apps/user_ldap/lib/User_LDAP.php index 61abb1627f95c..fcd5a009e413e 100644 --- a/apps/user_ldap/lib/User_LDAP.php +++ b/apps/user_ldap/lib/User_LDAP.php @@ -56,33 +56,27 @@ use Psr\Log\LoggerInterface; class User_LDAP extends BackendUtility implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend, IProvideEnabledStateBackend { - /** @var \OCP\IConfig */ - protected $ocConfig; - - /** @var INotificationManager */ - protected $notificationManager; - - /** @var UserPluginManager */ - protected $userPluginManager; - - /** @var LoggerInterface */ - protected $logger; - + protected IConfig $ocConfig; + protected INotificationManager $notificationManager; + protected UserPluginManager $userPluginManager; + protected LoggerInterface $logger; protected DeletedUsersIndex $deletedUsersIndex; - /** - * @param Access $access - * @param \OCP\IConfig $ocConfig - * @param \OCP\Notification\IManager $notificationManager - * @param IUserSession $userSession - */ - public function __construct(Access $access, IConfig $ocConfig, INotificationManager $notificationManager, IUserSession $userSession, UserPluginManager $userPluginManager) { + public function __construct( + Access $access, + IConfig $ocConfig, + INotificationManager $notificationManager, + IUserSession $userSession, + UserPluginManager $userPluginManager, + LoggerInterface $logger, + DeletedUsersIndex $deletedUsersIndex, + ) { parent::__construct($access); $this->ocConfig = $ocConfig; $this->notificationManager = $notificationManager; $this->userPluginManager = $userPluginManager; - $this->logger = \OC::$server->get(LoggerInterface::class); - $this->deletedUsersIndex = \OC::$server->get(DeletedUsersIndex::class); + $this->logger = $logger; + $this->deletedUsersIndex = $deletedUsersIndex; } /** diff --git a/apps/user_ldap/lib/User_Proxy.php b/apps/user_ldap/lib/User_Proxy.php index 59f72c2c2f3b9..c95329cebed60 100644 --- a/apps/user_ldap/lib/User_Proxy.php +++ b/apps/user_ldap/lib/User_Proxy.php @@ -31,6 +31,7 @@ */ namespace OCA\User_LDAP; +use OCA\User_LDAP\User\DeletedUsersIndex; use OCA\User_LDAP\User\User; use OCP\IConfig; use OCP\IUserBackend; @@ -40,6 +41,7 @@ use OCP\User\Backend\ICountMappedUsersBackend; use OCP\User\Backend\ICountUsersBackend; use OCP\User\Backend\IProvideEnabledStateBackend; +use Psr\Log\LoggerInterface; class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend, IProvideEnabledStateBackend { /** @var User_LDAP[] */ @@ -52,6 +54,8 @@ class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP private INotificationManager $notificationManager; private IUserSession $userSession; private UserPluginManager $userPluginManager; + private LoggerInterface $logger; + private DeletedUsersIndex $deletedUsersIndex; public function __construct( Helper $helper, @@ -60,7 +64,9 @@ public function __construct( IConfig $ocConfig, INotificationManager $notificationManager, IUserSession $userSession, - UserPluginManager $userPluginManager + UserPluginManager $userPluginManager, + LoggerInterface $logger, + DeletedUsersIndex $deletedUsersIndex, ) { parent::__construct($ldap, $accessFactory); $this->helper = $helper; @@ -68,6 +74,8 @@ public function __construct( $this->notificationManager = $notificationManager; $this->userSession = $userSession; $this->userPluginManager = $userPluginManager; + $this->logger = $logger; + $this->deletedUsersIndex = $deletedUsersIndex; } protected function setup(): void { @@ -77,8 +85,15 @@ protected function setup(): void { $serverConfigPrefixes = $this->helper->getServerConfigurationPrefixes(true); foreach ($serverConfigPrefixes as $configPrefix) { - $this->backends[$configPrefix] = - new User_LDAP($this->getAccess($configPrefix), $this->ocConfig, $this->notificationManager, $this->userSession, $this->userPluginManager); + $this->backends[$configPrefix] = new User_LDAP( + $this->getAccess($configPrefix), + $this->ocConfig, + $this->notificationManager, + $this->userSession, + $this->userPluginManager, + $this->logger, + $this->deletedUsersIndex, + ); if (is_null($this->refBackend)) { $this->refBackend = &$this->backends[$configPrefix]; diff --git a/apps/user_ldap/tests/Integration/Lib/IntegrationTestAttributeDetection.php b/apps/user_ldap/tests/Integration/Lib/IntegrationTestAttributeDetection.php index eb70c774e2573..a742c0b80764b 100644 --- a/apps/user_ldap/tests/Integration/Lib/IntegrationTestAttributeDetection.php +++ b/apps/user_ldap/tests/Integration/Lib/IntegrationTestAttributeDetection.php @@ -28,8 +28,10 @@ use OCA\User_LDAP\Mapping\GroupMapping; use OCA\User_LDAP\Mapping\UserMapping; use OCA\User_LDAP\Tests\Integration\AbstractIntegrationTest; +use OCA\User_LDAP\User\DeletedUsersIndex; use OCA\User_LDAP\User_LDAP; use OCA\User_LDAP\UserPluginManager; +use Psr\Log\LoggerInterface; require_once __DIR__ . '/../Bootstrap.php'; @@ -51,7 +53,7 @@ public function init() { $groupMapper->clear(); $this->access->setGroupMapper($groupMapper); - $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->query(UserPluginManager::class)); + $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->get(UserPluginManager::class), \OC::$server->get(LoggerInterface::class), \OC::$server->get(DeletedUsersIndex::class)); $userManager = \OC::$server->getUserManager(); $userManager->clearBackends(); $userManager->registerBackend($userBackend); diff --git a/apps/user_ldap/tests/Integration/Lib/IntegrationTestFetchUsersByLoginName.php b/apps/user_ldap/tests/Integration/Lib/IntegrationTestFetchUsersByLoginName.php index 36c8ab4c0d307..7b8f9fda754d0 100644 --- a/apps/user_ldap/tests/Integration/Lib/IntegrationTestFetchUsersByLoginName.php +++ b/apps/user_ldap/tests/Integration/Lib/IntegrationTestFetchUsersByLoginName.php @@ -26,8 +26,10 @@ use OCA\User_LDAP\Mapping\UserMapping; use OCA\User_LDAP\Tests\Integration\AbstractIntegrationTest; +use OCA\User_LDAP\User\DeletedUsersIndex; use OCA\User_LDAP\User_LDAP; use OCA\User_LDAP\UserPluginManager; +use Psr\Log\LoggerInterface; require_once __DIR__ . '/../Bootstrap.php'; @@ -49,7 +51,7 @@ public function init() { $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping->clear(); $this->access->setUserMapper($this->mapping); - $this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->query(UserPluginManager::class)); + $this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->get(UserPluginManager::class), \OC::$server->get(LoggerInterface::class), \OC::$server->get(DeletedUsersIndex::class)); } /** diff --git a/apps/user_ldap/tests/Integration/Lib/IntegrationTestPaging.php b/apps/user_ldap/tests/Integration/Lib/IntegrationTestPaging.php index b941fa6fc669b..6b272d3ad3ce5 100644 --- a/apps/user_ldap/tests/Integration/Lib/IntegrationTestPaging.php +++ b/apps/user_ldap/tests/Integration/Lib/IntegrationTestPaging.php @@ -27,8 +27,10 @@ use OCA\User_LDAP\Mapping\UserMapping; use OCA\User_LDAP\Tests\Integration\AbstractIntegrationTest; +use OCA\User_LDAP\User\DeletedUsersIndex; use OCA\User_LDAP\User_LDAP; use OCA\User_LDAP\UserPluginManager; +use Psr\Log\LoggerInterface; require_once __DIR__ . '/../Bootstrap.php'; @@ -50,7 +52,7 @@ public function init() { require(__DIR__ . '/../setup-scripts/createExplicitUsers.php'); parent::init(); - $this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->query(UserPluginManager::class)); + $this->backend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->get(UserPluginManager::class), \OC::$server->get(LoggerInterface::class), \OC::$server->get(DeletedUsersIndex::class)); } public function initConnection() { diff --git a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserAvatar.php b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserAvatar.php index ec1cebbe08721..c5b7f73bbcc0d 100644 --- a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserAvatar.php +++ b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserAvatar.php @@ -30,6 +30,7 @@ use OCA\User_LDAP\FilesystemHelper; use OCA\User_LDAP\Mapping\UserMapping; use OCA\User_LDAP\Tests\Integration\AbstractIntegrationTest; +use OCA\User_LDAP\User\DeletedUsersIndex; use OCA\User_LDAP\User\Manager; use OCA\User_LDAP\User\User; use OCA\User_LDAP\User_LDAP; @@ -53,7 +54,7 @@ public function init() { $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping->clear(); $this->access->setUserMapper($this->mapping); - $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->query(UserPluginManager::class)); + $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->get(UserPluginManager::class), \OC::$server->get(LoggerInterface::class), \OC::$server->get(DeletedUsersIndex::class)); \OC_User::useBackend($userBackend); } diff --git a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserCleanUp.php b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserCleanUp.php index 5da672d8a553c..623d08d565da1 100644 --- a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserCleanUp.php +++ b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserCleanUp.php @@ -26,8 +26,10 @@ use OCA\User_LDAP\Jobs\CleanUp; use OCA\User_LDAP\Mapping\UserMapping; use OCA\User_LDAP\Tests\Integration\AbstractIntegrationTest; +use OCA\User_LDAP\User\DeletedUsersIndex; use OCA\User_LDAP\User_LDAP; use OCA\User_LDAP\UserPluginManager; +use Psr\Log\LoggerInterface; require_once __DIR__ . '/../../Bootstrap.php'; @@ -46,7 +48,7 @@ public function init() { $this->mapping->clear(); $this->access->setUserMapper($this->mapping); - $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->query(UserPluginManager::class)); + $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->get(UserPluginManager::class), \OC::$server->get(LoggerInterface::class), \OC::$server->get(DeletedUsersIndex::class)); \OC_User::useBackend($userBackend); } diff --git a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserDisplayName.php b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserDisplayName.php index 7353c5bef30b0..6c12c32744c6e 100644 --- a/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserDisplayName.php +++ b/apps/user_ldap/tests/Integration/Lib/User/IntegrationTestUserDisplayName.php @@ -26,8 +26,10 @@ use OCA\User_LDAP\Mapping\UserMapping; use OCA\User_LDAP\Tests\Integration\AbstractIntegrationTest; +use OCA\User_LDAP\User\DeletedUsersIndex; use OCA\User_LDAP\User_LDAP; use OCA\User_LDAP\UserPluginManager; +use Psr\Log\LoggerInterface; require_once __DIR__ . '/../../Bootstrap.php'; @@ -45,7 +47,7 @@ public function init() { $this->mapping = new UserMapping(\OC::$server->getDatabaseConnection()); $this->mapping->clear(); $this->access->setUserMapper($this->mapping); - $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->query(UserPluginManager::class)); + $userBackend = new User_LDAP($this->access, \OC::$server->getConfig(), \OC::$server->getNotificationManager(), \OC::$server->getUserSession(), \OC::$server->get(UserPluginManager::class), \OC::$server->get(LoggerInterface::class), \OC::$server->get(DeletedUsersIndex::class)); \OC_User::useBackend($userBackend); } diff --git a/apps/user_ldap/tests/User_LDAPTest.php b/apps/user_ldap/tests/User_LDAPTest.php index b00c93e79f0c8..d04282e1dc577 100644 --- a/apps/user_ldap/tests/User_LDAPTest.php +++ b/apps/user_ldap/tests/User_LDAPTest.php @@ -37,6 +37,7 @@ use OCA\User_LDAP\Connection; use OCA\User_LDAP\Mapping\AbstractMapping; use OCA\User_LDAP\Mapping\UserMapping; +use OCA\User_LDAP\User\DeletedUsersIndex; use OCA\User_LDAP\User\Manager; use OCA\User_LDAP\User\OfflineUser; use OCA\User_LDAP\User\User; @@ -48,6 +49,8 @@ use OCP\IUser; use OCP\Notification\IManager as INotificationManager; use Test\TestCase; +use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; /** * Class Test_User_Ldap_Direct @@ -59,22 +62,26 @@ class User_LDAPTest extends TestCase { /** @var User_LDAP */ protected $backend; - /** @var Access|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Access|MockObject */ protected $access; - /** @var OfflineUser|\PHPUnit\Framework\MockObject\MockObject */ + /** @var OfflineUser|MockObject */ protected $offlineUser; - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IConfig|MockObject */ protected $config; - /** @var INotificationManager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var INotificationManager|MockObject */ protected $notificationManager; - /** @var Session|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Session|MockObject */ protected $session; - /** @var UserPluginManager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var UserPluginManager|MockObject */ protected $pluginManager; - /** @var Connection|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Connection|MockObject */ protected $connection; - /** @var Manager|\PHPUnit\Framework\MockObject\MockObject */ + /** @var Manager|MockObject */ protected $userManager; + /** @var LoggerInterface|MockObject */ + protected $logger; + /** @var DeletedUsersIndex|MockObject */ + protected $deletedUsersIndex; protected function setUp(): void { parent::setUp(); @@ -95,12 +102,18 @@ protected function setUp(): void { $this->session = $this->createMock(Session::class); $this->pluginManager = $this->createMock(UserPluginManager::class); + $this->logger = $this->createMock(LoggerInterface::class); + + $this->deletedUsersIndex = $this->createMock(DeletedUsersIndex::class); + $this->backend = new User_LDAP( $this->access, $this->config, $this->notificationManager, $this->session, - $this->pluginManager + $this->pluginManager, + $this->logger, + $this->deletedUsersIndex, ); } @@ -109,21 +122,21 @@ private function prepareMockForUserExists() { ->method('username2dn') ->willReturnCallback(function ($uid) { switch ($uid) { - case 'gunslinger': - return 'dnOfRoland,dc=test'; - break; - case 'formerUser': - return 'dnOfFormerUser,dc=test'; - break; - case 'newyorker': - return 'dnOfNewYorker,dc=test'; - break; - case 'ladyofshadows': - return 'dnOfLadyOfShadows,dc=test'; - break; - default: - return false; - } + case 'gunslinger': + return 'dnOfRoland,dc=test'; + break; + case 'formerUser': + return 'dnOfFormerUser,dc=test'; + break; + case 'newyorker': + return 'dnOfNewYorker,dc=test'; + break; + case 'ladyofshadows': + return 'dnOfLadyOfShadows,dc=test'; + break; + default: + return false; + } }); $this->access->method('fetchUsersByLoginName') @@ -199,7 +212,7 @@ public function testCheckPasswordUidReturn() { ->method('get') ->willReturn($user); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); \OC_User::useBackend($backend); @@ -209,7 +222,7 @@ public function testCheckPasswordUidReturn() { public function testCheckPasswordWrongPassword() { $this->prepareAccessForCheckPassword(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); \OC_User::useBackend($backend); $result = $backend->checkPassword('roland', 'wrong'); @@ -218,7 +231,7 @@ public function testCheckPasswordWrongPassword() { public function testCheckPasswordWrongUser() { $this->prepareAccessForCheckPassword(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); \OC_User::useBackend($backend); $result = $backend->checkPassword('mallory', 'evil'); @@ -233,7 +246,7 @@ public function testCheckPasswordNoDisplayName() { ->method('get') ->willReturn(null); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); \OC_User::useBackend($backend); $result = $backend->checkPassword('roland', 'dt19'); @@ -251,7 +264,7 @@ public function testCheckPasswordPublicAPI() { ->method('get') ->willReturn($user); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); \OC_User::useBackend($backend); $user = \OC::$server->getUserManager()->checkPassword('roland', 'dt19'); @@ -264,7 +277,7 @@ public function testCheckPasswordPublicAPI() { public function testCheckPasswordPublicAPIWrongPassword() { $this->prepareAccessForCheckPassword(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); \OC_User::useBackend($backend); $user = \OC::$server->getUserManager()->checkPassword('roland', 'wrong'); @@ -277,7 +290,7 @@ public function testCheckPasswordPublicAPIWrongPassword() { public function testCheckPasswordPublicAPIWrongUser() { $this->prepareAccessForCheckPassword(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); \OC_User::useBackend($backend); $user = \OC::$server->getUserManager()->checkPassword('mallory', 'evil'); @@ -289,7 +302,7 @@ public function testCheckPasswordPublicAPIWrongUser() { } public function testDeleteUserCancel() { - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $result = $backend->deleteUser('notme'); $this->assertFalse($result); } @@ -309,10 +322,10 @@ public function testDeleteUserSuccess() { ->method('getConnectionResource') ->willReturn('this is an ldap link'); - $this->config->expects($this->any()) - ->method('getUserValue') - ->with($uid, 'user_ldap', 'isDeleted') - ->willReturn('1'); + $this->deletedUsersIndex->expects($this->once()) + ->method('isUserMarked') + ->with($uid) + ->willReturn(true); $offlineUser = $this->createMock(OfflineUser::class); $offlineUser->expects($this->once()) @@ -322,7 +335,7 @@ public function testDeleteUserSuccess() { ->method('get') ->willReturn($offlineUser); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $result = $backend->deleteUser($uid); $this->assertTrue($result); @@ -339,10 +352,10 @@ public function testDeleteUserWithPlugin() { ->with('uid') ->willReturn(true); - $this->config->expects($this->once()) - ->method('getUserValue') - ->with('uid', 'user_ldap', 'isDeleted', 0) - ->willReturn(1); + $this->deletedUsersIndex->expects($this->once()) + ->method('isUserMarked') + ->with('uid') + ->willReturn(true); $mapper = $this->createMock(UserMapping::class); $mapper->expects($this->once()) @@ -388,7 +401,7 @@ private function prepareAccessForGetUsers() { } else { $result = []; foreach ($users as $user) { - if (stripos($user, $search) !== false) { + if (stripos($user, $search) !== false) { $result[] = $user; } } @@ -411,7 +424,7 @@ private function prepareAccessForGetUsers() { public function testGetUsersNoParam() { $this->prepareAccessForGetUsers(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $result = $backend->getUsers(); $this->assertEquals(3, count($result)); @@ -419,7 +432,7 @@ public function testGetUsersNoParam() { public function testGetUsersLimitOffset() { $this->prepareAccessForGetUsers(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $result = $backend->getUsers('', 1, 2); $this->assertEquals(1, count($result)); @@ -427,7 +440,7 @@ public function testGetUsersLimitOffset() { public function testGetUsersLimitOffset2() { $this->prepareAccessForGetUsers(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $result = $backend->getUsers('', 2, 1); $this->assertEquals(2, count($result)); @@ -435,7 +448,7 @@ public function testGetUsersLimitOffset2() { public function testGetUsersSearchWithResult() { $this->prepareAccessForGetUsers(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $result = $backend->getUsers('yo'); $this->assertEquals(2, count($result)); @@ -443,7 +456,7 @@ public function testGetUsersSearchWithResult() { public function testGetUsersSearchEmptyResult() { $this->prepareAccessForGetUsers(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $result = $backend->getUsers('nix'); $this->assertEquals(0, count($result)); @@ -459,7 +472,7 @@ private function getUsers($search = '', $limit = null, $offset = null) { public function testGetUsersViaAPINoParam() { $this->prepareAccessForGetUsers(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); \OC_User::useBackend($backend); $result = $this->getUsers(); @@ -468,7 +481,7 @@ public function testGetUsersViaAPINoParam() { public function testGetUsersViaAPILimitOffset() { $this->prepareAccessForGetUsers(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); \OC_User::useBackend($backend); $result = $this->getUsers('', 1, 2); @@ -477,7 +490,7 @@ public function testGetUsersViaAPILimitOffset() { public function testGetUsersViaAPILimitOffset2() { $this->prepareAccessForGetUsers(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); \OC_User::useBackend($backend); $result = $this->getUsers('', 2, 1); @@ -486,7 +499,7 @@ public function testGetUsersViaAPILimitOffset2() { public function testGetUsersViaAPISearchWithResult() { $this->prepareAccessForGetUsers(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); \OC_User::useBackend($backend); $result = $this->getUsers('yo'); @@ -495,7 +508,7 @@ public function testGetUsersViaAPISearchWithResult() { public function testGetUsersViaAPISearchEmptyResult() { $this->prepareAccessForGetUsers(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); \OC_User::useBackend($backend); $result = $this->getUsers('nix'); @@ -503,7 +516,7 @@ public function testGetUsersViaAPISearchEmptyResult() { } public function testUserExists() { - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $this->prepareMockForUserExists(); $user = $this->createMock(User::class); @@ -522,7 +535,7 @@ public function testUserExists() { } public function testUserExistsForDeleted() { - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $this->prepareMockForUserExists(); $mapper = $this->createMock(UserMapping::class); @@ -546,7 +559,7 @@ public function testUserExistsForDeleted() { } public function testUserExistsForNeverExisting() { - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $this->prepareMockForUserExists(); $this->access->expects($this->any()) @@ -565,7 +578,7 @@ public function testUserExistsForNeverExisting() { } public function testUserExistsPublicAPI() { - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $this->prepareMockForUserExists(); \OC_User::useBackend($backend); @@ -595,7 +608,7 @@ public function testUserExistsPublicAPI() { } public function testDeleteUserExisting() { - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); //we do not support deleting existing users at all $result = $backend->deleteUser('gunslinger'); @@ -603,7 +616,7 @@ public function testDeleteUserExisting() { } public function testGetHomeAbsolutePath() { - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $this->prepareMockForUserExists(); $this->connection->expects($this->any()) @@ -652,7 +665,7 @@ public function testGetHomeAbsolutePath() { } public function testGetHomeRelative() { - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $this->prepareMockForUserExists(); $dataDir = \OC::$server->getConfig()->getSystemValue( @@ -706,7 +719,7 @@ public function testGetHomeRelative() { public function testGetHomeNoPath() { $this->expectException(\Exception::class); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $this->prepareMockForUserExists(); $this->connection->expects($this->any()) @@ -754,7 +767,7 @@ public function testGetHomeNoPath() { public function testGetHomeDeletedUser() { $uid = 'newyorker'; - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $this->prepareMockForUserExists(); $this->connection->expects($this->any()) @@ -810,7 +823,7 @@ public function testGetHomeWithPlugin() { }); /** @noinspection PhpUnhandledExceptionInspection */ - $this->assertEquals($this->backend->getHome('uid'),'result'); + $this->assertEquals($this->backend->getHome('uid'), 'result'); } private function prepareAccessForGetDisplayName() { @@ -829,16 +842,16 @@ private function prepareAccessForGetDisplayName() { ->method('readAttribute') ->willReturnCallback(function ($dn, $attr) { switch ($dn) { - case 'dnOfRoland,dc=test': - if ($attr === 'displayname') { - return ['Roland Deschain']; - } - return []; - break; - - default: - return false; - } + case 'dnOfRoland,dc=test': + if ($attr === 'displayname') { + return ['Roland Deschain']; + } + return []; + break; + + default: + return false; + } }); $this->access->method('fetchUsersByLoginName') ->willReturn([]); @@ -846,7 +859,7 @@ private function prepareAccessForGetDisplayName() { public function testGetDisplayName() { $this->prepareAccessForGetDisplayName(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $this->prepareMockForUserExists(); $this->connection->expects($this->any()) @@ -927,7 +940,7 @@ public function testGetDisplayNamePublicAPI() { } }); $this->prepareAccessForGetDisplayName(); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $this->prepareMockForUserExists(); $this->connection->expects($this->any()) @@ -998,7 +1011,7 @@ public function testGetDisplayNameWithPlugin() { ->with('uid') ->willReturn('result'); - $this->assertEquals($this->backend->getDisplayName('uid'),'result'); + $this->assertEquals($this->backend->getDisplayName('uid'), 'result'); } //no test for getDisplayNames, because it just invokes getUsers and @@ -1009,7 +1022,7 @@ public function testCountUsers() { ->method('countUsers') ->willReturn(5); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $result = $backend->countUsers(); $this->assertEquals(5, $result); @@ -1020,7 +1033,7 @@ public function testCountUsersFailing() { ->method('countUsers') ->willReturn(false); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $result = $backend->countUsers(); $this->assertFalse($result); @@ -1035,7 +1048,7 @@ public function testCountUsersWithPlugin() { ->method('countUsers') ->willReturn(42); - $this->assertEquals($this->backend->countUsers(),42); + $this->assertEquals($this->backend->countUsers(), 42); } public function testLoginName2UserNameSuccess() { @@ -1064,7 +1077,7 @@ public function testLoginName2UserNameSuccess() { ->method('writeToCache') ->with($this->equalTo('loginName2UserName-'.$loginName), $this->equalTo($username)); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $user = $this->createMock(User::class); $user->expects($this->any()) ->method('getUsername') @@ -1109,7 +1122,7 @@ public function testLoginName2UserNameNoUsersOnLDAP() { ->method('getAttributes') ->willReturn(['dn', 'uid', 'mail', 'displayname']); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $name = $backend->loginName2UserName($loginName); $this->assertSame(false, $name); @@ -1146,7 +1159,7 @@ public function testLoginName2UserNameOfflineUser() { ->method('getAttributes') ->willReturn(['dn', 'uid', 'mail', 'displayname']); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $name = $backend->loginName2UserName($loginName); $this->assertSame(false, $name); @@ -1223,7 +1236,7 @@ public function testSetPasswordInvalid() { $this->userManager->expects($this->atLeastOnce()) ->method('get') ->willReturn($this->createMock(User::class)); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); \OC_User::useBackend($backend); $this->assertTrue(\OC_User::setPassword('roland', 'dt')); @@ -1236,7 +1249,7 @@ public function testSetPasswordValid() { ->method('get') ->willReturn($this->createMock(User::class)); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); $this->userManager->expects($this->any()) ->method('get') ->willReturn($this->createMock(User::class)); @@ -1252,7 +1265,7 @@ public function testSetPasswordValidDisabled() { ->willReturn($this->createMock(User::class)); $this->prepareAccessForSetPassword(false); - $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager); + $backend = new UserLDAP($this->access, $this->config, $this->notificationManager, $this->session, $this->pluginManager, $this->logger, $this->deletedUsersIndex); \OC_User::useBackend($backend); $this->assertFalse(\OC_User::setPassword('roland', 'dt12234$')); @@ -1295,11 +1308,11 @@ public function testSetPasswordWithPlugin() { ->willReturn(true); $this->pluginManager->expects($this->once()) ->method('setPassword') - ->with('uid','password') + ->with('uid', 'password') ->willReturn('result'); /** @noinspection PhpUnhandledExceptionInspection */ - $this->assertEquals($this->backend->setPassword('uid', 'password'),'result'); + $this->assertEquals($this->backend->setPassword('uid', 'password'), 'result'); } public function avatarDataProvider() { @@ -1340,7 +1353,7 @@ public function testCanChangeAvatarWithPlugin() { ->with('uid') ->willReturn('result'); - $this->assertEquals($this->backend->canChangeAvatar('uid'),'result'); + $this->assertEquals($this->backend->canChangeAvatar('uid'), 'result'); } public function testSetDisplayNameWithPlugin() { @@ -1413,7 +1426,7 @@ public function testCreateUserWithPlugin() { ->method('getUserMapper') ->willReturn($this->createMock(UserMapping::class)); - $this->assertEquals($this->backend->createUser($uid, $pwd),true); + $this->assertEquals($this->backend->createUser($uid, $pwd), true); } public function testCreateUserFailing() { From 367b4fe9f2361e9982dcd6e1e836f0bd3d37ce71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 25 May 2023 15:55:04 +0200 Subject: [PATCH 7/9] Fix User_Proxy tests as well MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/user_ldap/tests/User_ProxyTest.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/apps/user_ldap/tests/User_ProxyTest.php b/apps/user_ldap/tests/User_ProxyTest.php index edeefeb4b0e3d..a557eb4bc6f31 100644 --- a/apps/user_ldap/tests/User_ProxyTest.php +++ b/apps/user_ldap/tests/User_ProxyTest.php @@ -31,12 +31,14 @@ use OCA\User_LDAP\AccessFactory; use OCA\User_LDAP\Helper; use OCA\User_LDAP\ILDAPWrapper; +use OCA\User_LDAP\User\DeletedUsersIndex; use OCA\User_LDAP\User_Proxy; use OCA\User_LDAP\UserPluginManager; use OCP\IConfig; use OCP\IUserSession; use OCP\Notification\IManager as INotificationManager; use PHPUnit\Framework\MockObject\MockObject; +use Psr\Log\LoggerInterface; use Test\TestCase; class User_ProxyTest extends TestCase { @@ -56,6 +58,10 @@ class User_ProxyTest extends TestCase { private $proxy; /** @var UserPluginManager|MockObject */ private $userPluginManager; + /** @var LoggerInterface|MockObject */ + protected $logger; + /** @var DeletedUsersIndex|MockObject */ + protected $deletedUsersIndex; protected function setUp(): void { parent::setUp(); @@ -67,6 +73,8 @@ protected function setUp(): void { $this->notificationManager = $this->createMock(INotificationManager::class); $this->userSession = $this->createMock(IUserSession::class); $this->userPluginManager = $this->createMock(UserPluginManager::class); + $this->logger = $this->createMock(LoggerInterface::class); + $this->deletedUsersIndex = $this->createMock(DeletedUsersIndex::class); $this->proxy = $this->getMockBuilder(User_Proxy::class) ->setConstructorArgs([ $this->helper, @@ -75,7 +83,9 @@ protected function setUp(): void { $this->config, $this->notificationManager, $this->userSession, - $this->userPluginManager + $this->userPluginManager, + $this->logger, + $this->deletedUsersIndex, ]) ->setMethods(['handleRequest']) ->getMock(); From 1603cdc8d2798ddc2799a75deaf42b58515f84cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Tue, 20 Jun 2023 15:10:39 +0200 Subject: [PATCH 8/9] Fix since annotations and add boolean return type for setUserEnabled MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/user_ldap/lib/User_LDAP.php | 3 ++- apps/user_ldap/lib/User_Proxy.php | 4 ++-- lib/private/User/User.php | 6 +++--- lib/public/User/Backend/IProvideEnabledStateBackend.php | 8 ++++---- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/apps/user_ldap/lib/User_LDAP.php b/apps/user_ldap/lib/User_LDAP.php index fcd5a009e413e..d4a851bd7aa7e 100644 --- a/apps/user_ldap/lib/User_LDAP.php +++ b/apps/user_ldap/lib/User_LDAP.php @@ -677,7 +677,8 @@ public function isUserEnabled(string $uid, callable $queryDatabaseValue): bool { } } - public function setUserEnabled(string $uid, bool $enabled, callable $queryDatabaseValue, callable $setDatabaseValue): void { + public function setUserEnabled(string $uid, bool $enabled, callable $queryDatabaseValue, callable $setDatabaseValue): bool { $setDatabaseValue($enabled); + return $enabled; } } diff --git a/apps/user_ldap/lib/User_Proxy.php b/apps/user_ldap/lib/User_Proxy.php index c95329cebed60..e0f1bb2d522af 100644 --- a/apps/user_ldap/lib/User_Proxy.php +++ b/apps/user_ldap/lib/User_Proxy.php @@ -458,7 +458,7 @@ 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]); + public function setUserEnabled(string $uid, bool $enabled, callable $queryDatabaseValue, callable $setDatabaseValue): bool { + return $this->handleRequest($uid, 'setUserEnabled', [$uid, $enabled, $queryDatabaseValue, $setDatabaseValue]); } } diff --git a/lib/private/User/User.php b/lib/private/User/User.php index 3b7123bd5751d..edaab879ef7be 100644 --- a/lib/private/User/User.php +++ b/lib/private/User/User.php @@ -438,7 +438,7 @@ public function isEnabled() { $enabled = $this->config->getUserValue($this->uid, 'core', 'enabled', 'true'); $this->enabled = $enabled === 'true'; } - return (bool) $this->enabled; + return $this->enabled; }; if ($this->backend instanceof IProvideEnabledStateBackend) { return $this->backend->isUserEnabled($this->uid, $queryDatabaseValue); @@ -464,9 +464,9 @@ public function setEnabled(bool $enabled = true) { $enabled = $this->config->getUserValue($this->uid, 'core', 'enabled', 'true'); $this->enabled = $enabled === 'true'; } - return (bool) $this->enabled; + return $this->enabled; }; - $this->backend->setUserEnabled($this->uid, $enabled, $queryDatabaseValue, $setDatabaseValue); + $enabled = $this->backend->setUserEnabled($this->uid, $enabled, $queryDatabaseValue, $setDatabaseValue); if ($oldStatus !== $enabled) { $this->triggerChange('enabled', $enabled, $oldStatus); } diff --git a/lib/public/User/Backend/IProvideEnabledStateBackend.php b/lib/public/User/Backend/IProvideEnabledStateBackend.php index 143eca548ab92..8a4755ffbef0b 100644 --- a/lib/public/User/Backend/IProvideEnabledStateBackend.php +++ b/lib/public/User/Backend/IProvideEnabledStateBackend.php @@ -27,21 +27,21 @@ namespace OCP\User\Backend; /** - * @since 26.0.0 + * @since 28.0.0 */ interface IProvideEnabledStateBackend { /** - * @since 26.0.0 + * @since 28.0.0 * * @param callable():bool $queryDatabaseValue A callable to query the enabled state from database */ public function isUserEnabled(string $uid, callable $queryDatabaseValue): bool; /** - * @since 26.0.0 + * @since 28.0.0 * * @param callable():bool $queryDatabaseValue A callable to query the enabled state from database * @param callable(bool):void $setDatabaseValue A callable to set the enabled state in the database. */ - public function setUserEnabled(string $uid, bool $enabled, callable $queryDatabaseValue, callable $setDatabaseValue): void; + public function setUserEnabled(string $uid, bool $enabled, callable $queryDatabaseValue, callable $setDatabaseValue): bool; } From 189ccc2d722bdfb16a96432f709d18438c80c29e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=B4me=20Chilliet?= Date: Thu, 29 Jun 2023 16:15:12 +0200 Subject: [PATCH 9/9] Add method to list disabled users to IProvideEnabledStateBackend MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- apps/user_ldap/lib/User_LDAP.php | 4 ++++ apps/user_ldap/lib/User_Proxy.php | 12 ++++++++++++ .../User/Backend/IProvideEnabledStateBackend.php | 9 +++++++++ 3 files changed, 25 insertions(+) diff --git a/apps/user_ldap/lib/User_LDAP.php b/apps/user_ldap/lib/User_LDAP.php index d4a851bd7aa7e..f9ae6bbee66d0 100644 --- a/apps/user_ldap/lib/User_LDAP.php +++ b/apps/user_ldap/lib/User_LDAP.php @@ -681,4 +681,8 @@ public function setUserEnabled(string $uid, bool $enabled, callable $queryDataba $setDatabaseValue($enabled); return $enabled; } + + public function getDisabledUserList(int $offset = 0, ?int $limit = null): array { + throw new \Exception('This is implemented directly in User_Proxy'); + } } diff --git a/apps/user_ldap/lib/User_Proxy.php b/apps/user_ldap/lib/User_Proxy.php index e0f1bb2d522af..0449c89bd2479 100644 --- a/apps/user_ldap/lib/User_Proxy.php +++ b/apps/user_ldap/lib/User_Proxy.php @@ -32,6 +32,7 @@ namespace OCA\User_LDAP; use OCA\User_LDAP\User\DeletedUsersIndex; +use OCA\User_LDAP\User\OfflineUser; use OCA\User_LDAP\User\User; use OCP\IConfig; use OCP\IUserBackend; @@ -461,4 +462,15 @@ public function isUserEnabled(string $uid, callable $queryDatabaseValue): bool { public function setUserEnabled(string $uid, bool $enabled, callable $queryDatabaseValue, callable $setDatabaseValue): bool { return $this->handleRequest($uid, 'setUserEnabled', [$uid, $enabled, $queryDatabaseValue, $setDatabaseValue]); } + + public function getDisabledUserList(int $offset = 0, ?int $limit = null): array { + return array_map( + fn (OfflineUser $user) => $user->getOCName(), + array_slice( + $this->deletedUsersIndex->getUsers(), + $offset, + $limit + ) + ); + } } diff --git a/lib/public/User/Backend/IProvideEnabledStateBackend.php b/lib/public/User/Backend/IProvideEnabledStateBackend.php index 8a4755ffbef0b..d03beacd7b858 100644 --- a/lib/public/User/Backend/IProvideEnabledStateBackend.php +++ b/lib/public/User/Backend/IProvideEnabledStateBackend.php @@ -44,4 +44,13 @@ public function isUserEnabled(string $uid, callable $queryDatabaseValue): bool; * @param callable(bool):void $setDatabaseValue A callable to set the enabled state in the database. */ public function setUserEnabled(string $uid, bool $enabled, callable $queryDatabaseValue, callable $setDatabaseValue): bool; + + /** + * Get the list of disabled users, to merge with the ones disabled in database + * + * @since 28.0.0 + * + * @return string[] + */ + public function getDisabledUserList(int $offset = 0, ?int $limit = null): array; }