Skip to content

Commit c1480aa

Browse files
committed
refactor(LDAP): pass IConfig via constructor to Group_LDAP
Signed-off-by: Arthur Schiwon <blizzz@arthur-schiwon.de>
1 parent 039da6b commit c1480aa

4 files changed

Lines changed: 23 additions & 23 deletions

File tree

apps/user_ldap/lib/Group_LDAP.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ class Group_LDAP extends ABackend implements GroupInterface, IGroupLDAP, IGetDis
7474
* @var string $ldapGroupMemberAssocAttr contains the LDAP setting (in lower case) with the same name
7575
*/
7676
protected string $ldapGroupMemberAssocAttr;
77+
private IConfig $config;
7778

78-
public function __construct(Access $access, GroupPluginManager $groupPluginManager) {
79+
public function __construct(Access $access, GroupPluginManager $groupPluginManager, IConfig $config) {
7980
$this->access = $access;
8081
$filter = $this->access->connection->ldapGroupFilter;
8182
$gAssoc = $this->access->connection->ldapGroupMemberAssocAttr;
@@ -89,6 +90,7 @@ public function __construct(Access $access, GroupPluginManager $groupPluginManag
8990
$this->groupPluginManager = $groupPluginManager;
9091
$this->logger = Server::get(LoggerInterface::class);
9192
$this->ldapGroupMemberAssocAttr = strtolower((string)$gAssoc);
93+
$this->config = $config;
9294
}
9395

9496
/**
@@ -684,9 +686,7 @@ public function getUserGroups($uid): array {
684686
if ($user instanceof OfflineUser) {
685687
// We load known group memberships from configuration for remnants,
686688
// because LDAP server does not contain them anymore
687-
/** @var IConfig $config */
688-
$config = Server::get(IConfig::class);
689-
$groupStr = $config->getUserValue($uid, 'user_ldap', 'cached-group-memberships-' . $this->access->connection->getConfigPrefix(), '[]');
689+
$groupStr = $this->config->getUserValue($uid, 'user_ldap', 'cached-group-memberships-' . $this->access->connection->getConfigPrefix(), '[]');
690690
return json_decode($groupStr) ?? [];
691691
}
692692

@@ -803,10 +803,8 @@ public function getUserGroups($uid): array {
803803

804804
$groups = array_unique($groups, SORT_LOCALE_STRING);
805805
$this->access->connection->writeToCache($cacheKey, $groups);
806-
/** @var IConfig $config */
807-
$config = Server::get(IConfig::class);
808806
$groupStr = \json_encode($groups);
809-
$config->setUserValue($ncUid, 'user_ldap', 'cached-group-memberships-' . $this->access->connection->getConfigPrefix(), $groupStr);
807+
$this->config->setUserValue($ncUid, 'user_ldap', 'cached-group-memberships-' . $this->access->connection->getConfigPrefix(), $groupStr);
810808

811809
return $groups;
812810
}

apps/user_ldap/lib/Group_Proxy.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,27 @@
3535
use OCP\Group\Backend\IGroupDetailsBackend;
3636
use OCP\Group\Backend\INamedBackend;
3737
use OCP\GroupInterface;
38+
use OCP\IConfig;
3839

3940
class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGetDisplayNameBackend, INamedBackend, IDeleteGroupBackend, IBatchMethodsBackend {
4041
private $backends = [];
4142
private ?Group_LDAP $refBackend = null;
4243
private Helper $helper;
4344
private GroupPluginManager $groupPluginManager;
4445
private bool $isSetUp = false;
46+
private IConfig $config;
4547

4648
public function __construct(
4749
Helper $helper,
4850
ILDAPWrapper $ldap,
4951
AccessFactory $accessFactory,
50-
GroupPluginManager $groupPluginManager
52+
GroupPluginManager $groupPluginManager,
53+
IConfig $config,
5154
) {
5255
parent::__construct($ldap, $accessFactory);
5356
$this->helper = $helper;
5457
$this->groupPluginManager = $groupPluginManager;
58+
$this->config = $config;
5559
}
5660

5761
protected function setup(): void {
@@ -62,7 +66,7 @@ protected function setup(): void {
6266
$serverConfigPrefixes = $this->helper->getServerConfigurationPrefixes(true);
6367
foreach ($serverConfigPrefixes as $configPrefix) {
6468
$this->backends[$configPrefix] =
65-
new Group_LDAP($this->getAccess($configPrefix), $this->groupPluginManager);
69+
new Group_LDAP($this->getAccess($configPrefix), $this->groupPluginManager, $this->config);
6670
if (is_null($this->refBackend)) {
6771
$this->refBackend = &$this->backends[$configPrefix];
6872
}

apps/user_ldap/tests/Group_LDAPTest.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
use OCA\User_LDAP\User\OfflineUser;
4141
use OCP\GroupInterface;
4242
use OCP\IConfig;
43-
use OCP\Server;
4443
use PHPUnit\Framework\MockObject\MockObject;
4544
use Test\TestCase;
4645

@@ -52,24 +51,21 @@
5251
* @package OCA\User_LDAP\Tests
5352
*/
5453
class Group_LDAPTest extends TestCase {
54+
private MockObject|Access $access;
55+
private MockObject|GroupPluginManager $pluginManager;
56+
private MockObject|IConfig $config;
57+
private GroupLDAP $groupBackend;
5558

5659
public function setUp(): void {
5760
parent::setUp();
5861

5962
$this->access = $this->getAccessMock();
6063
$this->pluginManager = $this->createMock(GroupPluginManager::class);
64+
$this->config = $this->createMock(IConfig::class);
6165
}
6266

6367
public function initBackend(): void {
64-
$this->groupBackend = new GroupLDAP($this->access, $this->pluginManager);
65-
}
66-
67-
68-
public function tearDown(): void {
69-
parent::tearDown();
70-
71-
$realConfig = Server::get(IConfig::class);
72-
$realConfig->deleteUserValue('userX', 'user_ldap', 'cached-group-memberships-');
68+
$this->groupBackend = new GroupLDAP($this->access, $this->pluginManager, $this->config);
7369
}
7470

7571
public function testCountEmptySearchString() {
@@ -848,9 +844,10 @@ public function testGetUserGroupsOfflineUser() {
848844

849845
$offlineUser = $this->createMock(OfflineUser::class);
850846

851-
// FIXME: should be available via CI
852-
$realConfig = Server::get(IConfig::class);
853-
$realConfig->setUserValue('userX', 'user_ldap', 'cached-group-memberships-', \json_encode(['groupB', 'groupF']));
847+
$this->config->expects($this->any())
848+
->method('getUserValue')
849+
->with('userX', 'user_ldap', 'cached-group-memberships-', $this->anything())
850+
->willReturn(\json_encode(['groupB', 'groupF']));
854851

855852
$this->access->userManager->expects($this->any())
856853
->method('get')

apps/user_ldap/tests/Integration/Lib/IntegrationTestAttributeDetection.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use OCA\User_LDAP\User\DeletedUsersIndex;
3232
use OCA\User_LDAP\User_LDAP;
3333
use OCA\User_LDAP\UserPluginManager;
34+
use OCP\IConfig;
3435
use Psr\Log\LoggerInterface;
3536

3637
require_once __DIR__ . '/../Bootstrap.php';
@@ -58,7 +59,7 @@ public function init() {
5859
$userManager->clearBackends();
5960
$userManager->registerBackend($userBackend);
6061

61-
$groupBackend = new Group_LDAP($this->access, \OC::$server->query(GroupPluginManager::class));
62+
$groupBackend = new Group_LDAP($this->access, \OC::$server->query(GroupPluginManager::class), \OC::$server->get(IConfig::class));
6263
$groupManger = \OC::$server->getGroupManager();
6364
$groupManger->clearBackends();
6465
$groupManger->addBackend($groupBackend);

0 commit comments

Comments
 (0)