Passed
Push — master ( 0807e2...a2c262 )
by Blizzz
11:58 queued 12s
created

CredentialsCleanup::run()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2020 Robin Appelman <[email protected]>
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\Files_External\BackgroundJob;
25
26
use OCA\Files_External\Lib\Auth\Password\LoginCredentials;
27
use OCA\Files_External\Lib\StorageConfig;
28
use OCA\Files_External\Service\UserGlobalStoragesService;
29
use OCP\AppFramework\Utility\ITimeFactory;
30
use OCP\BackgroundJob\TimedJob;
31
use OCP\Security\ICredentialsManager;
32
use OCP\IUser;
33
use OCP\IUserManager;
34
35
class CredentialsCleanup extends TimedJob {
36
	private $credentialsManager;
37
	private $userGlobalStoragesService;
38
	private $userManager;
39
40
	public function __construct(
41
		ITimeFactory $time,
42
		ICredentialsManager $credentialsManager,
43
		UserGlobalStoragesService $userGlobalStoragesService,
44
		IUserManager $userManager
45
	) {
46
		parent::__construct($time);
47
48
		$this->credentialsManager = $credentialsManager;
49
		$this->userGlobalStoragesService = $userGlobalStoragesService;
50
		$this->userManager = $userManager;
51
52
		// run every day
53
		$this->setInterval(24 * 60 * 60);
54
	}
55
56
	protected function run($argument) {
57
		$this->userManager->callForSeenUsers(function (IUser $user) {
58
			$storages = $this->userGlobalStoragesService->getAllStoragesForUser($user);
59
60
			$usesLoginCredentials = array_reduce($storages, function (bool $uses, StorageConfig $storage) {
61
				return $uses || $storage->getAuthMechanism() instanceof LoginCredentials;
62
			}, false);
63
64
			if (!$usesLoginCredentials) {
65
				$this->credentialsManager->delete($user->getUID(), LoginCredentials::CREDENTIALS_IDENTIFIER);
66
			}
67
		});
68
	}
69
}
70