diff options
Diffstat (limited to 'apps/user_ldap/lib/Jobs/CleanUp.php')
-rw-r--r-- | apps/user_ldap/lib/Jobs/CleanUp.php | 163 |
1 files changed, 60 insertions, 103 deletions
diff --git a/apps/user_ldap/lib/Jobs/CleanUp.php b/apps/user_ldap/lib/Jobs/CleanUp.php index 849c30ecd65..76277b43c0b 100644 --- a/apps/user_ldap/lib/Jobs/CleanUp.php +++ b/apps/user_ldap/lib/Jobs/CleanUp.php @@ -1,39 +1,21 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Arthur Schiwon <blizzz@arthur-schiwon.de> - * @author Joas Schilling <coding@schilljs.com> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Roger Szabo <roger.szabo@web.de> - * @author Vinicius Cubas Brand <vinicius@eita.org.br> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ - namespace OCA\User_LDAP\Jobs; -use OC\BackgroundJob\TimedJob; use OCA\User_LDAP\Helper; -use OCA\User_LDAP\LDAP; use OCA\User_LDAP\Mapping\UserMapping; -use OCA\User_LDAP\User_LDAP; -use OCA\User_LDAP\User_Proxy; use OCA\User_LDAP\User\DeletedUsersIndex; +use OCA\User_LDAP\User_Proxy; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\TimedJob; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\Server; /** * Class CleanUp @@ -43,88 +25,75 @@ use OCA\User_LDAP\User\DeletedUsersIndex; * @package OCA\User_LDAP\Jobs; */ class CleanUp extends TimedJob { - /** @var int $limit amount of users that should be checked per run */ - protected $limit = 50; + /** @var ?int $limit amount of users that should be checked per run */ + protected $limit; /** @var int $defaultIntervalMin default interval in minutes */ - protected $defaultIntervalMin = 51; + protected $defaultIntervalMin = 60; - /** @var User_LDAP|User_Proxy $userBackend */ - protected $userBackend; - - /** @var \OCP\IConfig $ocConfig */ + /** @var IConfig $ocConfig */ protected $ocConfig; - /** @var \OCP\IDBConnection $db */ + /** @var IDBConnection $db */ protected $db; /** @var Helper $ldapHelper */ protected $ldapHelper; - /** @var \OCA\User_LDAP\Mapping\UserMapping */ + /** @var UserMapping */ protected $mapping; - /** @var \OCA\User_LDAP\User\DeletedUsersIndex */ - protected $dui; - - public function __construct() { - $minutes = \OC::$server->getConfig()->getSystemValue( - 'ldapUserCleanupInterval', strval($this->defaultIntervalMin)); - $this->setInterval(intval($minutes) * 60); + public function __construct( + ITimeFactory $timeFactory, + protected User_Proxy $userBackend, + protected DeletedUsersIndex $dui, + ) { + parent::__construct($timeFactory); + $minutes = Server::get(IConfig::class)->getSystemValue( + 'ldapUserCleanupInterval', (string)$this->defaultIntervalMin); + $this->setInterval((int)$minutes * 60); } /** * assigns the instances passed to run() to the class properties * @param array $arguments */ - public function setArguments($arguments) { + public function setArguments($arguments): void { //Dependency Injection is not possible, because the constructor will //only get values that are serialized to JSON. I.e. whatever we would //pass in app.php we do add here, except something else is passed e.g. //in tests. - if(isset($arguments['helper'])) { + if (isset($arguments['helper'])) { $this->ldapHelper = $arguments['helper']; } else { - $this->ldapHelper = new Helper(\OC::$server->getConfig()); + $this->ldapHelper = Server::get(Helper::class); } - if(isset($arguments['ocConfig'])) { + if (isset($arguments['ocConfig'])) { $this->ocConfig = $arguments['ocConfig']; } else { - $this->ocConfig = \OC::$server->getConfig(); + $this->ocConfig = Server::get(IConfig::class); } - if(isset($arguments['userBackend'])) { + if (isset($arguments['userBackend'])) { $this->userBackend = $arguments['userBackend']; - } else { - $this->userBackend = new User_Proxy( - $this->ldapHelper->getServerConfigurationPrefixes(true), - new LDAP(), - $this->ocConfig, - \OC::$server->getNotificationManager(), - \OC::$server->getUserSession(), - \OC::$server->query('LDAPUserPluginManager') - ); } - if(isset($arguments['db'])) { + if (isset($arguments['db'])) { $this->db = $arguments['db']; } else { - $this->db = \OC::$server->getDatabaseConnection(); + $this->db = Server::get(IDBConnection::class); } - if(isset($arguments['mapping'])) { + if (isset($arguments['mapping'])) { $this->mapping = $arguments['mapping']; } else { - $this->mapping = new UserMapping($this->db); + $this->mapping = Server::get(UserMapping::class); } - if(isset($arguments['deletedUsersIndex'])) { + if (isset($arguments['deletedUsersIndex'])) { $this->dui = $arguments['deletedUsersIndex']; - } else { - $this->dui = new DeletedUsersIndex( - $this->ocConfig, $this->db, $this->mapping); } } @@ -132,19 +101,13 @@ class CleanUp extends TimedJob { * makes the background job do its work * @param array $argument */ - public function run($argument) { + public function run($argument): void { $this->setArguments($argument); - if(!$this->isCleanUpAllowed()) { - return; - } - $users = $this->mapping->getList($this->getOffset(), $this->limit); - if(!is_array($users)) { - //something wrong? Let's start from the beginning next time and - //abort - $this->setOffset(true); + if (!$this->isCleanUpAllowed()) { return; } + $users = $this->mapping->getList($this->getOffset(), $this->getChunkSize()); $resetOffset = $this->isOffsetResetNecessary(count($users)); $this->checkUsers($users); $this->setOffset($resetOffset); @@ -152,46 +115,40 @@ class CleanUp extends TimedJob { /** * checks whether next run should start at 0 again - * @param int $resultCount - * @return bool */ - public function isOffsetResetNecessary($resultCount) { - return ($resultCount < $this->limit) ? true : false; + public function isOffsetResetNecessary(int $resultCount): bool { + return $resultCount < $this->getChunkSize(); } /** * checks whether cleaning up LDAP users is allowed - * @return bool */ - public function isCleanUpAllowed() { + public function isCleanUpAllowed(): bool { try { - if($this->ldapHelper->haveDisabledConfigurations()) { + if ($this->ldapHelper->haveDisabledConfigurations()) { return false; } } catch (\Exception $e) { return false; } - $enabled = $this->isCleanUpEnabled(); - - return $enabled; + return $this->isCleanUpEnabled(); } /** * checks whether clean up is enabled by configuration - * @return bool */ - private function isCleanUpEnabled() { + private function isCleanUpEnabled(): bool { return (bool)$this->ocConfig->getSystemValue( - 'ldapUserCleanupInterval', strval($this->defaultIntervalMin)); + 'ldapUserCleanupInterval', (string)$this->defaultIntervalMin); } /** * checks users whether they are still existing * @param array $users result from getMappedUsers() */ - private function checkUsers(array $users) { - foreach($users as $user) { + private function checkUsers(array $users): void { + foreach ($users as $user) { $this->checkUser($user); } } @@ -200,8 +157,8 @@ class CleanUp extends TimedJob { * checks whether a user is still existing in LDAP * @param string[] $user */ - private function checkUser(array $user) { - if($this->userBackend->userExistsOnLDAP($user['name'])) { + private function checkUser(array $user): void { + if ($this->userBackend->userExistsOnLDAP($user['name'])) { //still available, all good return; @@ -212,28 +169,28 @@ class CleanUp extends TimedJob { /** * gets the offset to fetch users from the mappings table - * @return int */ - private function getOffset() { - return intval($this->ocConfig->getAppValue('user_ldap', 'cleanUpJobOffset', 0)); + private function getOffset(): int { + return (int)$this->ocConfig->getAppValue('user_ldap', 'cleanUpJobOffset', '0'); } /** * sets the new offset for the next run * @param bool $reset whether the offset should be set to 0 */ - public function setOffset($reset = false) { - $newOffset = $reset ? 0 : - $this->getOffset() + $this->limit; - $this->ocConfig->setAppValue('user_ldap', 'cleanUpJobOffset', $newOffset); + public function setOffset(bool $reset = false): void { + $newOffset = $reset ? 0 + : $this->getOffset() + $this->getChunkSize(); + $this->ocConfig->setAppValue('user_ldap', 'cleanUpJobOffset', (string)$newOffset); } /** * returns the chunk size (limit in DB speak) - * @return int */ - public function getChunkSize() { + public function getChunkSize(): int { + if ($this->limit === null) { + $this->limit = (int)$this->ocConfig->getAppValue('user_ldap', 'cleanUpJobChunkSize', '50'); + } return $this->limit; } - } |