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