]> source.dussan.org Git - nextcloud-server.git/commitdiff
refactor(LDAP): pass IConfig via constructor to Group_LDAP
authorArthur Schiwon <blizzz@arthur-schiwon.de>
Mon, 9 Oct 2023 20:18:39 +0000 (22:18 +0200)
committerArthur Schiwon <blizzz@arthur-schiwon.de>
Tue, 7 Nov 2023 18:22:28 +0000 (19:22 +0100)
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
apps/user_ldap/lib/Group_LDAP.php
apps/user_ldap/lib/Group_Proxy.php
apps/user_ldap/tests/Group_LDAPTest.php
apps/user_ldap/tests/Integration/Lib/IntegrationTestAttributeDetection.php

index 8d27eb1815d92d6e2420b255959477a33977c046..429818503f3f9778d4243111f7239a8ecd5b9251 100644 (file)
@@ -72,8 +72,9 @@ class Group_LDAP extends BackendUtility implements GroupInterface, IGroupLDAP, I
         * @var string $ldapGroupMemberAssocAttr contains the LDAP setting (in lower case) with the same name
         */
        protected string $ldapGroupMemberAssocAttr;
+       private IConfig $config;
 
-       public function __construct(Access $access, GroupPluginManager $groupPluginManager) {
+       public function __construct(Access $access, GroupPluginManager $groupPluginManager, IConfig $config) {
                parent::__construct($access);
                $filter = $this->access->connection->ldapGroupFilter;
                $gAssoc = $this->access->connection->ldapGroupMemberAssocAttr;
@@ -87,6 +88,7 @@ class Group_LDAP extends BackendUtility implements GroupInterface, IGroupLDAP, I
                $this->groupPluginManager = $groupPluginManager;
                $this->logger = Server::get(LoggerInterface::class);
                $this->ldapGroupMemberAssocAttr = strtolower((string)$gAssoc);
+               $this->config = $config;
        }
 
        /**
@@ -681,9 +683,7 @@ class Group_LDAP extends BackendUtility implements GroupInterface, IGroupLDAP, I
                if ($user instanceof OfflineUser) {
                        // We load known group memberships from configuration for remnants,
                        // because LDAP server does not contain them anymore
-                       /** @var IConfig $config */
-                       $config = Server::get(IConfig::class);
-                       $groupStr = $config->getUserValue($uid, 'user_ldap', 'cached-group-memberships-' . $this->access->connection->getConfigPrefix(), '[]');
+                       $groupStr = $this->config->getUserValue($uid, 'user_ldap', 'cached-group-memberships-' . $this->access->connection->getConfigPrefix(), '[]');
                        return json_decode($groupStr) ?? [];
                }
 
@@ -800,10 +800,8 @@ class Group_LDAP extends BackendUtility implements GroupInterface, IGroupLDAP, I
 
                $groups = array_unique($groups, SORT_LOCALE_STRING);
                $this->access->connection->writeToCache($cacheKey, $groups);
-               /** @var IConfig $config */
-               $config = Server::get(IConfig::class);
                $groupStr = \json_encode($groups);
-               $config->setUserValue($ncUid, 'user_ldap', 'cached-group-memberships-' . $this->access->connection->getConfigPrefix(), $groupStr);
+               $this->config->setUserValue($ncUid, 'user_ldap', 'cached-group-memberships-' . $this->access->connection->getConfigPrefix(), $groupStr);
 
                return $groups;
        }
index c8c986318ec7e2624d272befbcc3281479001b10..ae355f8e53ed20f41a0a89f8ffb945ea469ff1ee 100644 (file)
@@ -31,6 +31,8 @@ namespace OCA\User_LDAP;
 use OCP\Group\Backend\IDeleteGroupBackend;
 use OCP\Group\Backend\IGetDisplayNameBackend;
 use OCP\Group\Backend\INamedBackend;
+use OCP\GroupInterface;
+use OCP\IConfig;
 
 class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend, INamedBackend, IDeleteGroupBackend {
        private $backends = [];
@@ -38,16 +40,19 @@ class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGet
        private Helper $helper;
        private GroupPluginManager $groupPluginManager;
        private bool $isSetUp = false;
+       private IConfig $config;
 
        public function __construct(
                Helper $helper,
                ILDAPWrapper $ldap,
                AccessFactory $accessFactory,
-               GroupPluginManager $groupPluginManager
+               GroupPluginManager $groupPluginManager,
+               IConfig $config,
        ) {
                parent::__construct($ldap, $accessFactory);
                $this->helper = $helper;
                $this->groupPluginManager = $groupPluginManager;
+               $this->config = $config;
        }
 
        protected function setup(): void {
@@ -58,7 +63,7 @@ class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGet
                $serverConfigPrefixes = $this->helper->getServerConfigurationPrefixes(true);
                foreach ($serverConfigPrefixes as $configPrefix) {
                        $this->backends[$configPrefix] =
-                               new Group_LDAP($this->getAccess($configPrefix), $this->groupPluginManager);
+                               new Group_LDAP($this->getAccess($configPrefix), $this->groupPluginManager, $this->config);
                        if (is_null($this->refBackend)) {
                                $this->refBackend = &$this->backends[$configPrefix];
                        }
index 01c99c837829ba4b78e4d3fb05e6d56f3cfbe2fd..764ce8a284449a8343242adaf38aa16d9879728a 100644 (file)
@@ -40,7 +40,6 @@ use OCA\User_LDAP\User\Manager;
 use OCA\User_LDAP\User\OfflineUser;
 use OCP\GroupInterface;
 use OCP\IConfig;
-use OCP\Server;
 use PHPUnit\Framework\MockObject\MockObject;
 use Test\TestCase;
 
@@ -52,24 +51,21 @@ use Test\TestCase;
  * @package OCA\User_LDAP\Tests
  */
 class Group_LDAPTest extends TestCase {
+       private MockObject|Access $access;
+       private MockObject|GroupPluginManager $pluginManager;
+       private MockObject|IConfig $config;
+       private GroupLDAP $groupBackend;
 
        public function setUp(): void {
                parent::setUp();
 
                $this->access = $this->getAccessMock();
                $this->pluginManager = $this->createMock(GroupPluginManager::class);
+               $this->config = $this->createMock(IConfig::class);
        }
 
        public function initBackend(): void {
-               $this->groupBackend = new GroupLDAP($this->access, $this->pluginManager);
-       }
-
-
-       public function tearDown(): void {
-               parent::tearDown();
-
-               $realConfig = Server::get(IConfig::class);
-               $realConfig->deleteUserValue('userX', 'user_ldap', 'cached-group-memberships-');
+               $this->groupBackend = new GroupLDAP($this->access, $this->pluginManager, $this->config);
        }
 
        public function testCountEmptySearchString() {
@@ -848,9 +844,10 @@ class Group_LDAPTest extends TestCase {
 
                $offlineUser = $this->createMock(OfflineUser::class);
 
-               // FIXME: should be available via CI
-               $realConfig = Server::get(IConfig::class);
-               $realConfig->setUserValue('userX', 'user_ldap', 'cached-group-memberships-', \json_encode(['groupB', 'groupF']));
+               $this->config->expects($this->any())
+                       ->method('getUserValue')
+                       ->with('userX', 'user_ldap', 'cached-group-memberships-', $this->anything())
+                       ->willReturn(\json_encode(['groupB', 'groupF']));
 
                $this->access->userManager->expects($this->any())
                        ->method('get')
index eb70c774e2573a5ba395d3ceffd2d5f1e31a25d3..15676c4cdc4ee9ff81beab06dd464c059ab30280 100644 (file)
@@ -30,6 +30,8 @@ use OCA\User_LDAP\Mapping\UserMapping;
 use OCA\User_LDAP\Tests\Integration\AbstractIntegrationTest;
 use OCA\User_LDAP\User_LDAP;
 use OCA\User_LDAP\UserPluginManager;
+use OCP\IConfig;
+use Psr\Log\LoggerInterface;
 
 require_once __DIR__ . '/../Bootstrap.php';
 
@@ -56,7 +58,7 @@ class IntegrationTestAttributeDetection extends AbstractIntegrationTest {
                $userManager->clearBackends();
                $userManager->registerBackend($userBackend);
 
-               $groupBackend = new Group_LDAP($this->access, \OC::$server->query(GroupPluginManager::class));
+               $groupBackend = new Group_LDAP($this->access, \OC::$server->query(GroupPluginManager::class), \OC::$server->get(IConfig::class));
                $groupManger = \OC::$server->getGroupManager();
                $groupManger->clearBackends();
                $groupManger->addBackend($groupBackend);