Skip to content

Commit 167f462

Browse files
authored
Merge pull request #7713 from nextcloud/fix-sync-offset
Fix LDAP Background Sync does not reset offset
2 parents 8c9e584 + cf915b0 commit 167f462

6 files changed

Lines changed: 358 additions & 7 deletions

File tree

apps/user_ldap/composer/composer/autoload_classmap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
return array(
99
'OCA\\User_LDAP\\Access' => $baseDir . '/../lib/Access.php',
10+
'OCA\\User_LDAP\\AccessFactory' => $baseDir . '/../lib/AccessFactory.php',
1011
'OCA\\User_LDAP\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php',
1112
'OCA\\User_LDAP\\BackendUtility' => $baseDir . '/../lib/BackendUtility.php',
1213
'OCA\\User_LDAP\\Command\\CheckUser' => $baseDir . '/../lib/Command/CheckUser.php',
@@ -19,6 +20,7 @@
1920
'OCA\\User_LDAP\\Command\\TestConfig' => $baseDir . '/../lib/Command/TestConfig.php',
2021
'OCA\\User_LDAP\\Configuration' => $baseDir . '/../lib/Configuration.php',
2122
'OCA\\User_LDAP\\Connection' => $baseDir . '/../lib/Connection.php',
23+
'OCA\\User_LDAP\\ConnectionFactory' => $baseDir . '/../lib/ConnectionFactory.php',
2224
'OCA\\User_LDAP\\Controller\\ConfigAPIController' => $baseDir . '/../lib/Controller/ConfigAPIController.php',
2325
'OCA\\User_LDAP\\Controller\\RenewPasswordController' => $baseDir . '/../lib/Controller/RenewPasswordController.php',
2426
'OCA\\User_LDAP\\Exceptions\\ConstraintViolationException' => $baseDir . '/../lib/Exceptions/ConstraintViolationException.php',

apps/user_ldap/composer/composer/autoload_static.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class ComposerStaticInitUser_LDAP
1919

2020
public static $classMap = array (
2121
'OCA\\User_LDAP\\Access' => __DIR__ . '/..' . '/../lib/Access.php',
22+
'OCA\\User_LDAP\\AccessFactory' => __DIR__ . '/..' . '/../lib/AccessFactory.php',
2223
'OCA\\User_LDAP\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php',
2324
'OCA\\User_LDAP\\BackendUtility' => __DIR__ . '/..' . '/../lib/BackendUtility.php',
2425
'OCA\\User_LDAP\\Command\\CheckUser' => __DIR__ . '/..' . '/../lib/Command/CheckUser.php',
@@ -31,6 +32,7 @@ class ComposerStaticInitUser_LDAP
3132
'OCA\\User_LDAP\\Command\\TestConfig' => __DIR__ . '/..' . '/../lib/Command/TestConfig.php',
3233
'OCA\\User_LDAP\\Configuration' => __DIR__ . '/..' . '/../lib/Configuration.php',
3334
'OCA\\User_LDAP\\Connection' => __DIR__ . '/..' . '/../lib/Connection.php',
35+
'OCA\\User_LDAP\\ConnectionFactory' => __DIR__ . '/..' . '/../lib/ConnectionFactory.php',
3436
'OCA\\User_LDAP\\Controller\\ConfigAPIController' => __DIR__ . '/..' . '/../lib/Controller/ConfigAPIController.php',
3537
'OCA\\User_LDAP\\Controller\\RenewPasswordController' => __DIR__ . '/..' . '/../lib/Controller/RenewPasswordController.php',
3638
'OCA\\User_LDAP\\Exceptions\\ConstraintViolationException' => __DIR__ . '/..' . '/../lib/Exceptions/ConstraintViolationException.php',
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
4+
*
5+
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\User_LDAP;
25+
26+
27+
use OCA\User_LDAP\User\Manager;
28+
use OCP\IConfig;
29+
30+
class AccessFactory {
31+
/** @var ILDAPWrapper */
32+
protected $ldap;
33+
/** @var Manager */
34+
protected $userManager;
35+
/** @var Helper */
36+
protected $helper;
37+
/** @var IConfig */
38+
protected $config;
39+
40+
public function __construct(
41+
ILDAPWrapper $ldap,
42+
Manager $userManager,
43+
Helper $helper,
44+
IConfig $config)
45+
{
46+
$this->ldap = $ldap;
47+
$this->userManager = $userManager;
48+
$this->helper = $helper;
49+
$this->config = $config;
50+
}
51+
52+
public function get(Connection $connection) {
53+
return new Access(
54+
$connection,
55+
$this->ldap,
56+
$this->userManager,
57+
$this->helper,
58+
$this->config
59+
);
60+
}
61+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* @copyright Copyright (c) 2018 Arthur Schiwon <blizzz@arthur-schiwon.de>
4+
*
5+
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
6+
*
7+
* @license GNU AGPL version 3 or any later version
8+
*
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU Affero General Public License as
11+
* published by the Free Software Foundation, either version 3 of the
12+
* License, or (at your option) any later version.
13+
*
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU Affero General Public License for more details.
18+
*
19+
* You should have received a copy of the GNU Affero General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
*
22+
*/
23+
24+
namespace OCA\User_LDAP;
25+
26+
27+
class ConnectionFactory {
28+
/** @var ILDAPWrapper */
29+
private $ldap;
30+
31+
public function __construct(ILDAPWrapper $ldap) {
32+
$this->ldap = $ldap;
33+
}
34+
35+
public function get($prefix) {
36+
return new Connection($this->ldap, $prefix, 'user_ldap');
37+
}
38+
}

apps/user_ldap/lib/Jobs/Sync.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
use OC\BackgroundJob\TimedJob;
2727
use OC\ServerNotAvailableException;
2828
use OCA\User_LDAP\Access;
29+
use OCA\User_LDAP\AccessFactory;
2930
use OCA\User_LDAP\Configuration;
3031
use OCA\User_LDAP\Connection;
32+
use OCA\User_LDAP\ConnectionFactory;
3133
use OCA\User_LDAP\FilesystemHelper;
3234
use OCA\User_LDAP\Helper;
3335
use OCA\User_LDAP\LDAP;
@@ -62,6 +64,10 @@ class Sync extends TimedJob {
6264
protected $ncUserManager;
6365
/** @var IManager */
6466
protected $notificationManager;
67+
/** @var ConnectionFactory */
68+
protected $connectionFactory;
69+
/** @var AccessFactory */
70+
protected $accessFactory;
6571

6672
public function __construct() {
6773
$this->setInterval(
@@ -112,7 +118,7 @@ protected function getMinPagingSize() {
112118
/**
113119
* @param array $argument
114120
*/
115-
protected function run($argument) {
121+
public function run($argument) {
116122
$this->setArgument($argument);
117123

118124
$isBackgroundJobModeAjax = $this->config
@@ -140,11 +146,11 @@ protected function run($argument) {
140146
if ($expectMoreResults) {
141147
$this->increaseOffset($cycleData);
142148
} else {
143-
$this->determineNextCycle();
149+
$this->determineNextCycle($cycleData);
144150
}
145151
$this->updateInterval();
146152
} catch (ServerNotAvailableException $e) {
147-
$this->determineNextCycle();
153+
$this->determineNextCycle($cycleData);
148154
}
149155
}
150156

@@ -153,8 +159,8 @@ protected function run($argument) {
153159
* @return bool whether more results are expected from the same configuration
154160
*/
155161
public function runCycle($cycleData) {
156-
$connection = new Connection($this->ldap, $cycleData['prefix']);
157-
$access = new Access($connection, $this->ldap, $this->userManager, $this->ldapHelper, $this->config);
162+
$connection = $this->connectionFactory->get($cycleData['prefix']);
163+
$access = $this->accessFactory->get($connection);
158164
$access->setUserMapper($this->mapper);
159165

160166
$filter = $access->combineFilterWithAnd(array(
@@ -173,7 +179,7 @@ public function runCycle($cycleData) {
173179
if($connection->ldapPagingSize === 0) {
174180
return true;
175181
}
176-
return count($results) !== $connection->ldapPagingSize;
182+
return count($results) >= $connection->ldapPagingSize;
177183
}
178184

179185
/**
@@ -246,7 +252,7 @@ public function determineNextCycle(array $cycleData = null) {
246252
* @param $cycleData
247253
* @return bool
248254
*/
249-
protected function qualifiesToRun($cycleData) {
255+
public function qualifiesToRun($cycleData) {
250256
$lastChange = $this->config->getAppValue('user_ldap', $cycleData['prefix'] . '_lastChange', 0);
251257
if((time() - $lastChange) > 60 * 30) {
252258
return true;
@@ -358,5 +364,22 @@ public function setArgument($argument) {
358364
} else {
359365
$this->mapper = new UserMapping($this->dbc);
360366
}
367+
368+
if(isset($argument['connectionFactory'])) {
369+
$this->connectionFactory = $argument['connectionFactory'];
370+
} else {
371+
$this->connectionFactory = new ConnectionFactory($this->ldap);
372+
}
373+
374+
if(isset($argument['accessFactory'])) {
375+
$this->accessFactory = $argument['accessFactory'];
376+
} else {
377+
$this->accessFactory = new AccessFactory(
378+
$this->ldap,
379+
$this->userManager,
380+
$this->ldapHelper,
381+
$this->config
382+
);
383+
}
361384
}
362385
}

0 commit comments

Comments
 (0)