Skip to content

Commit f6ff717

Browse files
authored
Merge pull request #34772 from nextcloud/fix/clean-ldap-access-factory-usage
Make sure to use AccessFactory to create Access instances and use DI
2 parents 1db0dde + 341dda1 commit f6ff717

File tree

12 files changed

+85
-109
lines changed

12 files changed

+85
-109
lines changed

apps/user_ldap/ajax/wizard.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
}
3939
$action = (string)$_POST['action'];
4040

41-
4241
if (!isset($_POST['ldap_serverconfig_chooser'])) {
4342
\OC_JSON::error(['message' => $l->t('No configuration specified')]);
4443
}
@@ -52,26 +51,8 @@
5251
$con->ldapConfigurationActive = true;
5352
$con->setIgnoreValidation(true);
5453

55-
$userManager = new \OCA\User_LDAP\User\Manager(
56-
\OC::$server->getConfig(),
57-
new \OCA\User_LDAP\FilesystemHelper(),
58-
\OC::$server->get(\Psr\Log\LoggerInterface::class),
59-
\OC::$server->getAvatarManager(),
60-
new \OCP\Image(),
61-
\OC::$server->getUserManager(),
62-
\OC::$server->getNotificationManager(),
63-
\OC::$server->get(\OCP\Share\IManager::class)
64-
);
65-
66-
$access = new \OCA\User_LDAP\Access(
67-
$con,
68-
$ldapWrapper,
69-
$userManager,
70-
new \OCA\User_LDAP\Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
71-
\OC::$server->getConfig(),
72-
\OC::$server->getUserManager(),
73-
\OC::$server->get(\Psr\Log\LoggerInterface::class)
74-
);
54+
$factory = \OC::$server->get(\OCA\User_LDAP\AccessFactory::class);
55+
$access = $factory->get($con);
7556

7657
$wizard = new \OCA\User_LDAP\Wizard($configuration, $ldapWrapper, $access);
7758

apps/user_ldap/lib/AccessFactory.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,12 @@
2929
use Psr\Log\LoggerInterface;
3030

3131
class AccessFactory {
32-
/** @var ILDAPWrapper */
33-
protected $ldap;
34-
/** @var Manager */
35-
protected $userManager;
36-
/** @var Helper */
37-
protected $helper;
38-
/** @var IConfig */
39-
protected $config;
40-
/** @var IUserManager */
41-
private $ncUserManager;
42-
/** @var LoggerInterface */
43-
private $logger;
32+
private ILDAPWrapper $ldap;
33+
private Manager $userManager;
34+
private Helper $helper;
35+
private IConfig $config;
36+
private IUserManager $ncUserManager;
37+
private LoggerInterface $logger;
4438

4539
public function __construct(
4640
ILDAPWrapper $ldap,
@@ -57,7 +51,7 @@ public function __construct(
5751
$this->logger = $logger;
5852
}
5953

60-
public function get(Connection $connection) {
54+
public function get(Connection $connection): Access {
6155
return new Access(
6256
$connection,
6357
$this->ldap,

apps/user_ldap/lib/Command/TestConfig.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use OCA\User_LDAP\AccessFactory;
3030
use OCA\User_LDAP\Connection;
3131
use OCA\User_LDAP\Helper;
32+
use OCA\User_LDAP\ILDAPWrapper;
3233
use Symfony\Component\Console\Command\Command;
3334
use Symfony\Component\Console\Input\InputArgument;
3435
use Symfony\Component\Console\Input\InputInterface;
@@ -40,29 +41,35 @@ class TestConfig extends Command {
4041
protected const BINDFAILURE = 2;
4142
protected const SEARCHFAILURE = 3;
4243

43-
/** @var AccessFactory */
44-
protected $accessFactory;
44+
protected AccessFactory $accessFactory;
45+
protected Helper $helper;
46+
protected ILDAPWrapper $ldap;
4547

46-
public function __construct(AccessFactory $accessFactory) {
48+
public function __construct(
49+
AccessFactory $accessFactory,
50+
Helper $helper,
51+
ILDAPWrapper $ldap
52+
) {
4753
$this->accessFactory = $accessFactory;
54+
$this->helper = $helper;
55+
$this->ldap = $ldap;
4856
parent::__construct();
4957
}
5058

51-
protected function configure() {
59+
protected function configure(): void {
5260
$this
5361
->setName('ldap:test-config')
5462
->setDescription('tests an LDAP configuration')
5563
->addArgument(
56-
'configID',
57-
InputArgument::REQUIRED,
58-
'the configuration ID'
59-
)
64+
'configID',
65+
InputArgument::REQUIRED,
66+
'the configuration ID'
67+
)
6068
;
6169
}
6270

6371
protected function execute(InputInterface $input, OutputInterface $output): int {
64-
$helper = new Helper(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection());
65-
$availableConfigs = $helper->getServerConfigurationPrefixes();
72+
$availableConfigs = $this->helper->getServerConfigurationPrefixes();
6673
$configID = $input->getArgument('configID');
6774
if (!in_array($configID, $availableConfigs)) {
6875
$output->writeln('Invalid configID');
@@ -94,8 +101,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
94101
* Tests the specified connection
95102
*/
96103
protected function testConfig(string $configID): int {
97-
$lw = new \OCA\User_LDAP\LDAP();
98-
$connection = new Connection($lw, $configID);
104+
$connection = new Connection($this->ldap, $configID);
99105

100106
// Ensure validation is run before we attempt the bind
101107
$connection->getConfiguration();

apps/user_ldap/lib/Group_Proxy.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,13 @@ class Group_Proxy extends Proxy implements \OCP\GroupInterface, IGroupLDAP, IGet
3939
private GroupPluginManager $groupPluginManager;
4040
private bool $isSetUp = false;
4141

42-
public function __construct(Helper $helper, ILDAPWrapper $ldap, GroupPluginManager $groupPluginManager) {
43-
parent::__construct($ldap);
42+
public function __construct(
43+
Helper $helper,
44+
ILDAPWrapper $ldap,
45+
AccessFactory $accessFactory,
46+
GroupPluginManager $groupPluginManager
47+
) {
48+
parent::__construct($ldap, $accessFactory);
4449
$this->helper = $helper;
4550
$this->groupPluginManager = $groupPluginManager;
4651
}

apps/user_ldap/lib/ILDAPUserPlugin.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
namespace OCA\User_LDAP;
2525

2626
interface ILDAPUserPlugin {
27-
2827
/**
2928
* Check if plugin implements actions
3029
* @return int
@@ -85,7 +84,7 @@ public function canChangeAvatar($uid);
8584

8685
/**
8786
* Count the number of users
88-
* @return int|bool
87+
* @return int|false
8988
*/
9089
public function countUsers();
9190
}

apps/user_ldap/lib/Proxy.php

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -34,57 +34,41 @@
3434

3535
use OCA\User_LDAP\Mapping\GroupMapping;
3636
use OCA\User_LDAP\Mapping\UserMapping;
37-
use OCA\User_LDAP\User\Manager;
38-
use OCP\IConfig;
39-
use OCP\IUserManager;
37+
use OCP\ICache;
4038
use OCP\Server;
41-
use Psr\Log\LoggerInterface;
4239

4340
abstract class Proxy {
44-
private static $accesses = [];
45-
private $ldap = null;
46-
/** @var bool */
47-
private $isSingleBackend;
48-
49-
/** @var \OCP\ICache|null */
50-
private $cache;
51-
52-
/**
53-
* @param ILDAPWrapper $ldap
54-
*/
55-
public function __construct(ILDAPWrapper $ldap) {
41+
/** @var array<string,Access> */
42+
private static array $accesses = [];
43+
private ILDAPWrapper $ldap;
44+
private ?bool $isSingleBackend = null;
45+
private ?ICache $cache = null;
46+
private AccessFactory $accessFactory;
47+
48+
public function __construct(
49+
ILDAPWrapper $ldap,
50+
AccessFactory $accessFactory
51+
) {
5652
$this->ldap = $ldap;
53+
$this->accessFactory = $accessFactory;
5754
$memcache = \OC::$server->getMemCacheFactory();
5855
if ($memcache->isAvailable()) {
5956
$this->cache = $memcache->createDistributed();
6057
}
6158
}
6259

63-
/**
64-
* @param string $configPrefix
65-
*/
6660
private function addAccess(string $configPrefix): void {
67-
$ocConfig = Server::get(IConfig::class);
6861
$userMap = Server::get(UserMapping::class);
6962
$groupMap = Server::get(GroupMapping::class);
70-
$coreUserManager = Server::get(IUserManager::class);
71-
$logger = Server::get(LoggerInterface::class);
72-
$helper = Server::get(Helper::class);
73-
74-
$userManager = Server::get(Manager::class);
7563

7664
$connector = new Connection($this->ldap, $configPrefix);
77-
$access = new Access($connector, $this->ldap, $userManager, $helper, $ocConfig, $coreUserManager, $logger);
65+
$access = $this->accessFactory->get($connector);
7866
$access->setUserMapper($userMap);
7967
$access->setGroupMapper($groupMap);
8068
self::$accesses[$configPrefix] = $access;
8169
}
8270

83-
/**
84-
* @param string $configPrefix
85-
* @return mixed
86-
*/
87-
protected function getAccess($configPrefix) {
71+
protected function getAccess(string $configPrefix): Access {
8872
if (!isset(self::$accesses[$configPrefix])) {
8973
$this->addAccess($configPrefix);
9074
}

apps/user_ldap/lib/UserPluginManager.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function register(ILDAPUserPlugin $plugin) {
6565
\OC::$server->getLogger()->debug("Registered action ".$action." to plugin ".get_class($plugin), ['app' => 'user_ldap']);
6666
}
6767
}
68-
if (method_exists($plugin,'deleteUser')) {
68+
if (method_exists($plugin, 'deleteUser')) {
6969
$this->which['deleteUser'] = $plugin;
7070
\OC::$server->getLogger()->debug("Registered action deleteUser to plugin ".get_class($plugin), ['app' => 'user_ldap']);
7171
}
@@ -92,7 +92,7 @@ public function createUser($username, $password) {
9292
$plugin = $this->which[Backend::CREATE_USER];
9393

9494
if ($plugin) {
95-
return $plugin->createUser($username,$password);
95+
return $plugin->createUser($username, $password);
9696
}
9797
throw new \Exception('No plugin implements createUser in this LDAP Backend.');
9898
}
@@ -108,7 +108,7 @@ public function setPassword($uid, $password) {
108108
$plugin = $this->which[Backend::SET_PASSWORD];
109109

110110
if ($plugin) {
111-
return $plugin->setPassword($uid,$password);
111+
return $plugin->setPassword($uid, $password);
112112
}
113113
throw new \Exception('No plugin implements setPassword in this LDAP Backend.');
114114
}
@@ -176,7 +176,7 @@ public function setDisplayName($uid, $displayName) {
176176

177177
/**
178178
* Count the number of users
179-
* @return int|bool
179+
* @return int|false
180180
* @throws \Exception
181181
*/
182182
public function countUsers() {

apps/user_ldap/lib/User_LDAP.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,7 @@ public function hasUserListings() {
582582
/**
583583
* counts the users in LDAP
584584
*
585-
* @return int|bool
585+
* @return int|false
586586
*/
587587
public function countUsers() {
588588
if ($this->userPluginManager->implementsActions(Backend::COUNT_USERS)) {

apps/user_ldap/lib/User_Proxy.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
use OCP\UserInterface;
4242

4343
class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP, ICountUsersBackend, ICountMappedUsersBackend {
44+
/** @var array<string,User_LDAP> */
4445
private $backends = [];
45-
/** @var User_LDAP */
46+
/** @var ?User_LDAP */
4647
private $refBackend = null;
4748

4849
private bool $isSetUp = false;
@@ -55,12 +56,13 @@ class User_Proxy extends Proxy implements IUserBackend, UserInterface, IUserLDAP
5556
public function __construct(
5657
Helper $helper,
5758
ILDAPWrapper $ldap,
59+
AccessFactory $accessFactory,
5860
IConfig $ocConfig,
5961
INotificationManager $notificationManager,
6062
IUserSession $userSession,
6163
UserPluginManager $userPluginManager
6264
) {
63-
parent::__construct($ldap);
65+
parent::__construct($ldap, $accessFactory);
6466
$this->helper = $helper;
6567
$this->ocConfig = $ocConfig;
6668
$this->notificationManager = $notificationManager;
@@ -377,7 +379,7 @@ public function hasUserListings() {
377379
/**
378380
* Count the number of users
379381
*
380-
* @return int|bool
382+
* @return int|false
381383
*/
382384
public function countUsers() {
383385
$this->setup();
@@ -386,7 +388,7 @@ public function countUsers() {
386388
foreach ($this->backends as $backend) {
387389
$backendUsers = $backend->countUsers();
388390
if ($backendUsers !== false) {
389-
$users += $backendUsers;
391+
$users = (int)$users + $backendUsers;
390392
}
391393
}
392394
return $users;

apps/user_ldap/tests/User_ProxyTest.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,36 +28,41 @@
2828
*/
2929
namespace OCA\User_LDAP\Tests;
3030

31+
use OCA\User_LDAP\AccessFactory;
3132
use OCA\User_LDAP\Helper;
3233
use OCA\User_LDAP\ILDAPWrapper;
3334
use OCA\User_LDAP\User_Proxy;
3435
use OCA\User_LDAP\UserPluginManager;
3536
use OCP\IConfig;
3637
use OCP\IUserSession;
3738
use OCP\Notification\IManager as INotificationManager;
39+
use PHPUnit\Framework\MockObject\MockObject;
3840
use Test\TestCase;
3941

4042
class User_ProxyTest extends TestCase {
41-
/** @var Helper|\PHPUnit\Framework\MockObject\MockObject */
43+
/** @var Helper|MockObject */
4244
protected $helper;
43-
/** @var ILDAPWrapper|\PHPUnit\Framework\MockObject\MockObject */
45+
/** @var ILDAPWrapper|MockObject */
4446
private $ldapWrapper;
45-
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
47+
/** @var AccessFactory|MockObject */
48+
private $accessFactory;
49+
/** @var IConfig|MockObject */
4650
private $config;
47-
/** @var INotificationManager|\PHPUnit\Framework\MockObject\MockObject */
51+
/** @var INotificationManager|MockObject */
4852
private $notificationManager;
49-
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
53+
/** @var IUserSession|MockObject */
5054
private $userSession;
51-
/** @var User_Proxy|\PHPUnit\Framework\MockObject\MockObject */
55+
/** @var User_Proxy|MockObject */
5256
private $proxy;
53-
/** @var UserPluginManager|\PHPUnit\Framework\MockObject\MockObject */
57+
/** @var UserPluginManager|MockObject */
5458
private $userPluginManager;
5559

5660
protected function setUp(): void {
5761
parent::setUp();
5862

5963
$this->helper = $this->createMock(Helper::class);
6064
$this->ldapWrapper = $this->createMock(ILDAPWrapper::class);
65+
$this->accessFactory = $this->createMock(AccessFactory::class);
6166
$this->config = $this->createMock(IConfig::class);
6267
$this->notificationManager = $this->createMock(INotificationManager::class);
6368
$this->userSession = $this->createMock(IUserSession::class);
@@ -66,6 +71,7 @@ protected function setUp(): void {
6671
->setConstructorArgs([
6772
$this->helper,
6873
$this->ldapWrapper,
74+
$this->accessFactory,
6975
$this->config,
7076
$this->notificationManager,
7177
$this->userSession,

0 commit comments

Comments
 (0)