Skip to content

Commit ad4fe1c

Browse files
committed
Fix ldap testing and move stuff to dependency injection
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
1 parent 4c76400 commit ad4fe1c

File tree

7 files changed

+132
-213
lines changed

7 files changed

+132
-213
lines changed

apps/user_ldap/lib/AccessFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function __construct(
5656
$this->dispatcher = $dispatcher;
5757
}
5858

59-
public function get(Connection $connection) {
59+
public function get(Connection $connection): Access {
6060
return new Access(
6161
$connection,
6262
$this->ldap,

apps/user_ldap/lib/Jobs/Sync.php

Lines changed: 87 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
66
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
77
* @author Joas Schilling <coding@schilljs.com>
8+
* @author Carl Schwan <carl@carlschwan.eu>
89
*
910
* @license GNU AGPL version 3 or any later version
1011
*
@@ -24,7 +25,9 @@
2425
*/
2526
namespace OCA\User_LDAP\Jobs;
2627

27-
use OC\BackgroundJob\TimedJob;
28+
use OCA\User_LDAP\User\User;
29+
use OCP\AppFramework\Utility\ITimeFactory;
30+
use OCP\BackgroundJob\TimedJob;
2831
use OC\ServerNotAvailableException;
2932
use OCA\User_LDAP\AccessFactory;
3033
use OCA\User_LDAP\Configuration;
@@ -44,51 +47,77 @@
4447
class Sync extends TimedJob {
4548
public const MAX_INTERVAL = 12 * 60 * 60; // 12h
4649
public const MIN_INTERVAL = 30 * 60; // 30min
47-
/** @var Helper */
48-
protected $ldapHelper;
49-
/** @var LDAP */
50-
protected $ldap;
50+
51+
protected Helper $ldapHelper;
52+
protected LDAP $ldap;
5153
protected Manager $userManager;
52-
/** @var UserMapping */
53-
protected $mapper;
54-
/** @var IConfig */
55-
protected $config;
56-
/** @var IAvatarManager */
57-
protected $avatarManager;
58-
/** @var IDBConnection */
59-
protected $dbc;
60-
/** @var IUserManager */
61-
protected $ncUserManager;
62-
/** @var LoggerInterface */
63-
protected $logger;
64-
/** @var IManager */
65-
protected $notificationManager;
66-
/** @var ConnectionFactory */
67-
protected $connectionFactory;
68-
/** @var AccessFactory */
69-
protected $accessFactory;
54+
protected UserMapping $mapper;
55+
protected IConfig $config;
56+
protected IAvatarManager $avatarManager;
57+
protected IDBConnection $dbc;
58+
protected IUserManager $ncUserManager;
59+
protected LoggerInterface $logger;
60+
protected IManager $notificationManager;
61+
protected ConnectionFactory $connectionFactory;
62+
protected AccessFactory $accessFactory;
7063
protected IEventDispatcher $dispatcher;
7164

72-
public function __construct(Manager $userManager) {
65+
public function __construct(
66+
Manager $userManager,
67+
IEventDispatcher $dispatcher,
68+
IConfig $config,
69+
ITimeFactory $timeFactory,
70+
IDBConnection $dbConnection,
71+
IAvatarManager $avatarManager,
72+
IUserManager $ncUserManager,
73+
LoggerInterface $logger,
74+
IManager $notificationManager,
75+
UserMapping $userMapping,
76+
Helper $ldapHelper,
77+
ConnectionFactory $connectionFactory
78+
) {
79+
parent::__construct($timeFactory);
7380
$this->userManager = $userManager;
81+
$this->dispatcher = $dispatcher;
82+
$this->config = $config;
7483
$this->setInterval(
75-
\OC::$server->getConfig()->getAppValue(
84+
(int)$config->getAppValue(
7685
'user_ldap',
7786
'background_sync_interval',
7887
self::MIN_INTERVAL
7988
)
8089
);
81-
$this->dispatcher = \OC::$server->get(IEventDispatcher::class);
90+
91+
$this->ldapHelper = $ldapHelper;
92+
$this->ldap = new LDAP($this->config->getSystemValueString('ldap_log_file'));
93+
$this->avatarManager = $avatarManager;
94+
$this->dbc = $dbConnection;
95+
$this->ncUserManager = $ncUserManager;
96+
$this->logger = $logger;
97+
$this->userManager = $userManager;
98+
$this->notificationManager = $notificationManager;
99+
$this->mapper = $userMapping;
100+
$this->connectionFactory = $connectionFactory;
101+
102+
$this->accessFactory = new AccessFactory(
103+
$this->ldap,
104+
$this->userManager,
105+
$this->ldapHelper,
106+
$this->config,
107+
$this->ncUserManager,
108+
$this->logger,
109+
$this->dispatcher
110+
);
82111
}
83112

84113
/**
85-
* updates the interval
114+
* Updates the interval
86115
*
87-
* the idea is to adjust the interval depending on the amount of known users
116+
* The idea is to adjust the interval depending on the amount of known users
88117
* and the attempt to update each user one day. At most it would run every
89118
* 30 minutes, and at least every 12 hours.
90119
*/
91-
public function updateInterval() {
120+
public function updateInterval(): void {
92121
$minPagingSize = $this->getMinPagingSize();
93122
$mappedUsers = $this->mapper->count();
94123

@@ -101,10 +130,9 @@ public function updateInterval() {
101130
}
102131

103132
/**
104-
* returns the smallest configured paging size
105-
* @return int
133+
* Returns the smallest configured paging size
106134
*/
107-
protected function getMinPagingSize() {
135+
protected function getMinPagingSize(): int {
108136
$configKeys = $this->config->getAppKeys('user_ldap');
109137
$configKeys = array_filter($configKeys, function ($key) {
110138
return strpos($key, 'ldap_paging_size') !== false;
@@ -121,8 +149,6 @@ protected function getMinPagingSize() {
121149
* @param array $argument
122150
*/
123151
public function run($argument) {
124-
$this->setArgument($argument);
125-
126152
$isBackgroundJobModeAjax = $this->config
127153
->getAppValue('core', 'backgroundjobs_mode', 'ajax') === 'ajax';
128154
if ($isBackgroundJobModeAjax) {
@@ -157,10 +183,10 @@ public function run($argument) {
157183
}
158184

159185
/**
160-
* @param array $cycleData
186+
* @param array{offset: int, prefix: string} $cycleData
161187
* @return bool whether more results are expected from the same configuration
162188
*/
163-
public function runCycle($cycleData) {
189+
public function runCycle(array $cycleData): bool {
164190
$connection = $this->connectionFactory->get($cycleData['prefix']);
165191
$access = $this->accessFactory->get($connection);
166192
$access->setUserMapper($this->mapper);
@@ -185,24 +211,23 @@ public function runCycle($cycleData) {
185211
}
186212

187213
/**
188-
* returns the info about the current cycle that should be run, if any,
214+
* Returns the info about the current cycle that should be run, if any,
189215
* otherwise null
190-
*
191-
* @return array|null
216+
* @return ?array{offset: int, prefix: string}
192217
*/
193-
public function getCycle() {
218+
public function getCycle(): ?array {
194219
$prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true);
195220
if (count($prefixes) === 0) {
196221
return null;
197222
}
198223

199224
$cycleData = [
200-
'prefix' => $this->config->getAppValue('user_ldap', 'background_sync_prefix', null),
201-
'offset' => (int)$this->config->getAppValue('user_ldap', 'background_sync_offset', 0),
225+
'prefix' => $this->config->getAppValue('user_ldap', 'background_sync_prefix', 'none'),
226+
'offset' => (int)$this->config->getAppValue('user_ldap', 'background_sync_offset', '0'),
202227
];
203228

204229
if (
205-
$cycleData['prefix'] !== null
230+
$cycleData['prefix'] !== 'none'
206231
&& in_array($cycleData['prefix'], $prefixes)
207232
) {
208233
return $cycleData;
@@ -214,21 +239,21 @@ public function getCycle() {
214239
/**
215240
* Save the provided cycle information in the DB
216241
*
217-
* @param array $cycleData
242+
* @param array{prefix: ?string, offset: int} $cycleData
218243
*/
219-
public function setCycle(array $cycleData) {
244+
public function setCycle(array $cycleData): void {
220245
$this->config->setAppValue('user_ldap', 'background_sync_prefix', $cycleData['prefix']);
221-
$this->config->setAppValue('user_ldap', 'background_sync_offset', $cycleData['offset']);
246+
$this->config->setAppValue('user_ldap', 'background_sync_offset', (string)$cycleData['offset']);
222247
}
223248

224249
/**
225250
* returns data about the next cycle that should run, if any, otherwise
226251
* null. It also always goes for the next LDAP configuration!
227252
*
228253
* @param array|null $cycleData the old cycle
229-
* @return array|null
254+
* @return ?array{prefix: string, offset: int}
230255
*/
231-
public function determineNextCycle(array $cycleData = null) {
256+
public function determineNextCycle(?array $cycleData = null): ?array {
232257
$prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true);
233258
if (count($prefixes) === 0) {
234259
return null;
@@ -242,19 +267,18 @@ public function determineNextCycle(array $cycleData = null) {
242267
}
243268
$cycleData['prefix'] = $prefix;
244269
$cycleData['offset'] = 0;
245-
$this->setCycle(['prefix' => $prefix, 'offset' => 0]);
270+
$this->setCycle($cycleData);
246271

247272
return $cycleData;
248273
}
249274

250275
/**
251-
* Checks whether the provided cycle should be run. Currently only the
276+
* Checks whether the provided cycle should be run. Currently, only the
252277
* last configuration change goes into account (at least one hour).
253278
*
254-
* @param $cycleData
255-
* @return bool
279+
* @param $cycleData{prefix: string}
256280
*/
257-
public function qualifiesToRun($cycleData) {
281+
public function qualifiesToRun(array $cycleData): bool {
258282
$lastChange = $this->config->getAppValue('user_ldap', $cycleData['prefix'] . '_lastChange', 0);
259283
if ((time() - $lastChange) > 60 * 30) {
260284
return true;
@@ -263,23 +287,20 @@ public function qualifiesToRun($cycleData) {
263287
}
264288

265289
/**
266-
* increases the offset of the current cycle for the next run
290+
* Increases the offset of the current cycle for the next run
267291
*
268-
* @param $cycleData
292+
* @param array{prefix: string, offset: int} $cycleData
269293
*/
270-
protected function increaseOffset($cycleData) {
294+
protected function increaseOffset(array $cycleData): void {
271295
$ldapConfig = new Configuration($cycleData['prefix']);
272-
$cycleData['offset'] += (int)$ldapConfig->ldapPagingSize;
296+
$cycleData['offset'] += $ldapConfig->ldapPagingSize;
273297
$this->setCycle($cycleData);
274298
}
275299

276300
/**
277-
* determines the next configuration prefix based on the last one (if any)
278-
*
279-
* @param string|null $lastPrefix
280-
* @return string|null
301+
* Determines the next configuration prefix based on the last one (if any)
281302
*/
282-
protected function getNextPrefix($lastPrefix) {
303+
protected function getNextPrefix(?string $lastPrefix): ?string {
283304
$prefixes = $this->ldapHelper->getServerConfigurationPrefixes(true);
284305
$noOfPrefixes = count($prefixes);
285306
if ($noOfPrefixes === 0) {
@@ -299,87 +320,10 @@ protected function getNextPrefix($lastPrefix) {
299320
}
300321

301322
/**
302-
* "fixes" DI
303-
*
304-
* @param array $argument
323+
* Only used in tests
305324
*/
306-
public function setArgument($argument) {
307-
if (isset($argument['config'])) {
308-
$this->config = $argument['config'];
309-
} else {
310-
$this->config = \OC::$server->getConfig();
311-
}
312-
313-
if (isset($argument['helper'])) {
314-
$this->ldapHelper = $argument['helper'];
315-
} else {
316-
$this->ldapHelper = new Helper($this->config, \OC::$server->getDatabaseConnection());
317-
}
318-
319-
if (isset($argument['ldapWrapper'])) {
320-
$this->ldap = $argument['ldapWrapper'];
321-
} else {
322-
$this->ldap = new LDAP($this->config->getSystemValueString('ldap_log_file'));
323-
}
324-
325-
if (isset($argument['avatarManager'])) {
326-
$this->avatarManager = $argument['avatarManager'];
327-
} else {
328-
$this->avatarManager = \OC::$server->getAvatarManager();
329-
}
330-
331-
if (isset($argument['dbc'])) {
332-
$this->dbc = $argument['dbc'];
333-
} else {
334-
$this->dbc = \OC::$server->getDatabaseConnection();
335-
}
336-
337-
if (isset($argument['ncUserManager'])) {
338-
$this->ncUserManager = $argument['ncUserManager'];
339-
} else {
340-
$this->ncUserManager = \OC::$server->getUserManager();
341-
}
342-
343-
if (isset($argument['logger'])) {
344-
$this->logger = $argument['logger'];
345-
} else {
346-
$this->logger = \OC::$server->get(LoggerInterface::class);
347-
}
348-
349-
if (isset($argument['notificationManager'])) {
350-
$this->notificationManager = $argument['notificationManager'];
351-
} else {
352-
$this->notificationManager = \OC::$server->getNotificationManager();
353-
}
354-
355-
if (isset($argument['userManager'])) {
356-
$this->userManager = $argument['userManager'];
357-
}
358-
359-
if (isset($argument['mapper'])) {
360-
$this->mapper = $argument['mapper'];
361-
} else {
362-
$this->mapper = new UserMapping($this->dbc);
363-
}
364-
365-
if (isset($argument['connectionFactory'])) {
366-
$this->connectionFactory = $argument['connectionFactory'];
367-
} else {
368-
$this->connectionFactory = new ConnectionFactory($this->ldap);
369-
}
370-
371-
if (isset($argument['accessFactory'])) {
372-
$this->accessFactory = $argument['accessFactory'];
373-
} else {
374-
$this->accessFactory = new AccessFactory(
375-
$this->ldap,
376-
$this->userManager,
377-
$this->ldapHelper,
378-
$this->config,
379-
$this->ncUserManager,
380-
$this->logger,
381-
$this->dispatcher,
382-
);
383-
}
325+
public function overwritePropertiesForTest(LDAP $ldapWrapper, AccessFactory $accessFactory): void {
326+
$this->ldap = $ldapWrapper;
327+
$this->accessFactory = $accessFactory;
384328
}
385329
}

0 commit comments

Comments
 (0)