AclManagerInit::getModelClassesSwap()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 6
c 1
b 1
f 0
nc 2
nop 0
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 12
rs 10
1
<?php
2
3
namespace Ubiquity\security\acl\traits;
4
5
use Ubiquity\cache\CacheManager;
6
use Ubiquity\cache\ClassUtils;
7
use Ubiquity\controllers\Startup;
8
use Ubiquity\exceptions\AclException;
9
use Ubiquity\security\acl\cache\AclControllerParser;
10
use Ubiquity\security\acl\models\AclList;
11
use Ubiquity\security\acl\persistence\AclCacheProvider;
12
use Ubiquity\security\acl\persistence\AclDAOProvider;
13
use Ubiquity\security\acl\persistence\AclProviderInterface;
14
15
/**
16
 * @property ?AclList $aclList
17
 * @property array $providersPersistence
18
 */
19
trait AclManagerInit {
20
	/**
21
	 * Create AclList with default roles and resources.
22
	 */
23 25
	public static function start(): void {
24 25
		self::$aclList = new AclList();
25 25
		self::$aclList->init();
26
	}
27
28
	/**
29
	 * Start the Acls with AclCacheProvider (for attributes or annotations).
30
	 */
31 6
	public static function startWithCacheProvider(): void {
32 6
		self::start();
33 6
		self::initFromProviders([new AclCacheProvider()]);
34
	}
35
36
	/**
37
	 * Check whether the Acl service is started.
38
	 *
39
	 * @return bool
40
	 */
41 2
	public static function isStarted(): bool {
42 2
		return isset(self::$aclList) && (self::$aclList instanceof AclList);
43
	}
44
45
	/**
46
	 * Load acls, roles, resources and permissions from providers.
47
	 *
48
	 * @param AclProviderInterface[] $providers
49
	 */
50 11
	public static function initFromProviders(?array $providers = []): void {
51 11
		self::$aclList->setProviders($providers);
52 11
		if (\count($providers) > 0) {
53 10
			self::$aclList->loadAcls();
54 10
			self::$aclList->loadRoles();
55 10
			self::$aclList->loadResources();
56 10
			self::$aclList->loadPermissions();
57
		}
58
	}
59
60
	/**
61
	 *
62
	 * @param array|string $selectedProviders
63
	 */
64 3
	public static function reloadFromSelectedProviders($selectedProviders = '*'): void {
65 3
		$sProviders = self::$aclList->getProviders();
66 3
		self::$aclList->clear();
67 3
		$providers = [];
68 3
		foreach ($sProviders as $prov) {
69 3
			if ($selectedProviders === '*' || (\is_array($selectedProviders) && \array_search(\get_class($prov), $selectedProviders) !== false)) {
70 3
				$providers[] = $prov;
71
			}
72
		}
73 3
		self::initFromProviders($providers);
74 3
		self::$aclList->setProviders($sProviders);
75
	}
76
77
	/**
78
	 *
79
	 * @param string $providerClass
80
	 * @return AclProviderInterface|NULL
81
	 */
82 2
	public static function getProvider(string $providerClass):?AclProviderInterface {
83 2
		return self::$aclList->getProvider($providerClass);
84
	}
85
86
	public static function getModelClassesSwap(): array {
87
		$result = [];
88
		$aclList = self::getAclList();
89
		if (isset($aclList)) {
90
			foreach ($aclList->getProviders() as $prov) {
91
				$result += $prov->getModelClassesSwap();
92
			}
93
		}
94
		return $result;
95
	}
96
97 2
	public static function filterProviders(string $providerClass):void {
98 2
		$providers = self::$aclList->getProviders();
99 2
		$filter = [];
100 2
		foreach ($providers as $prov) {
101 2
			if ($prov instanceof $providerClass) {
102 2
				$filter[] = $prov;
103
			}
104
		}
105 2
		self::$aclList->setProviders($filter);
106 2
		self::$providersPersistence = $providers;
107
	}
108
109 2
	public static function removefilterProviders():void {
110 2
		self::$aclList->setProviders(self::$providersPersistence);
111
	}
112
113
	/**
114
	 * Initializes AclDAOProvider and creates ACL tables in the specified dbOffset.
115
	 * Do not use in production
116
	 * @param array $config
117
	 * @param string $dbOffset
118
	 * @param array $classes
119
	 *        	associative array['acl'=>'','role'=>'','resource'=>'','permission'=>'']
120
	 * @return AclDAOProvider
121
	 * @throws AclException
122
	 */
123
	public static function initializeDAOProvider(array &$config, string $dbOffset='default', array $classes=[]): AclDAOProvider {
124
		self::start();
125
		return AclDAOProvider::initializeProvider($config,$dbOffset,$classes);
126
	}
127
128
	/**
129
	 * Initialize acls cache with controllers annotations.
130
	 * Do not execute at runtime
131
	 *
132
	 * @param array $config
133
	 * @param bool $silent
134
	 * @throws \Ubiquity\exceptions\AclException
135
	 */
136 2
	public static function initCache(array &$config, bool $silent=false): void {
137 2
		self::cacheOperation($config, $silent, function ($parser) {
138 2
			$parser->save();
139 2
		}, "ACLs cache reset\n");
140
	}
141
142
	/**
143
	 * @param array $config
144
	 * @return bool
145
	 */
146
	public static function checkCache(array &$config): bool {
147
		return self::cacheOperation($config, true, function ($parser) {
148
			return $parser->cacheUpdated();
149
		});
150
	}
151
152 2
	private static function cacheOperation(array &$config, bool $silent, $operation, $message=null) {
153 2
		if (!self::isStarted()) {
154
			self::start();
155
			self::initFromProviders([
156
				new AclCacheProvider()
157
			]);
158
		}
159 2
		self::filterProviders(AclCacheProvider::class);
160 2
		self::reloadFromSelectedProviders([]);
161 2
		self::registerAnnotations();
162 2
		$files = \Ubiquity\cache\CacheManager::getControllersFiles($config, $silent);
163 2
		$parser = new AclControllerParser();
164
165 2
		foreach ($files as $file) {
166 2
			if (\is_file($file)) {
167 2
				$controller = ClassUtils::getClassFullNameFromFile($file);
168
				try {
169 2
					$parser->parse($controller);
170
				} catch (\Exception $e) {
171
					if ($e instanceof AclException) {
172
						throw $e;
173
					}
174
				}
175
			}
176
		}
177 2
		$result = $operation($parser);
178 2
		self::removefilterProviders();
179 2
		self::reloadFromSelectedProviders();
180 2
		if (!$silent) {
181 2
			echo $message;
182
		}
183 2
		return $result;
184
	}
185
186 2
	protected static function registerAnnotations(): void {
187 2
		CacheManager::getAnnotationsEngineInstance()->registerAcls();
188
	}
189
}