Completed
Pull Request — master (#28003)
by Piotr
11:34
created

Manager::check()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Jörn Friedrich Dreyer <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2018, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Authentication\AccountModule;
23
24
use OC\Authentication\Exceptions\AccountCheckException;
25
use OCP\App\IServiceLoader;
26
use OCP\Authentication\IAccountModule;
27
use OCP\IConfig;
28
use OCP\ILogger;
29
use OCP\IUser;
30
31
class Manager {
32
33
	/** @var IConfig */
34
	private $config;
35
36
	/** @var ILogger */
37
	private $logger;
38
39
	/** @var IServiceLoader */
40
	private $loader;
41
42
	/**
43
	 * @param IConfig $config
44
	 * @param ILogger $logger
45
	 * @param IServiceLoader $loader
46
	 */
47
	public function __construct(IConfig $config, ILogger $logger, IServiceLoader $loader) {
48
		$this->config = $config;
49
		$this->logger = $logger;
50
		$this->loader = $loader;
51
	}
52
53
	/**
54
	 * Get the list of account modules for the given user
55
	 * Limited to auth-modules that are enabled for this user
56
	 *
57
	 * @param IUser $user
58
	 * @return IAccountModule[]
59
	 */
60
	public function getAccountModules(IUser $user) {
61
		$loaded = $this->loader->load(['account-modules'], $user);
62
63
		// load order from appconfig
64
		$rawOrder = $this->config->getAppValue('core', 'account-module-order', '[]');
65
		$order = \json_decode($rawOrder);
66
		if (!\is_array($order)) {
67
			$order = [];
68
		}
69
70
		// get map of loaded module classes
71
		$modules = [];
72
		foreach ($loaded as $module) {
73
			$modules[\get_class($module)] = $module;
74
		}
75
76
		// replace class name with instance
77
		foreach ($order as $i => $className) {
78
			if (isset($modules[$className])) {
79
				unset($modules[$className]);
80
				$order[$i] = $modules[$className];
81
			} else {
82
				unset($order[$i]);
83
			}
84
		}
85
		// add unordered modules
86
		foreach ($modules as $className => $instance) {
87
			$order[] = $instance;
88
		}
89
90
		return $order;
91
	}
92
93
	/**
94
	 * @param IUser $user
95
	 * @throws AccountCheckException
96
	 */
97
	public function check(IUser $user) {
98
		foreach ($this->getAccountModules($user) as $accountModule) {
99
			try {
100
				$accountModule->check($user);
101
			} catch (AccountCheckException $ex) {
102
				$this->logger->debug('IAccountModule check failed: {message}, {code}', [
103
					'app'=>__METHOD__,
104
					'message' => $ex->getMessage(),
105
					'code' => $ex->getCode()
106
				]);
107
				throw $ex;
108
			}
109
		}
110
	}
111
}
112