-
Notifications
You must be signed in to change notification settings - Fork 0
/
Acl.php
executable file
·386 lines (341 loc) · 12.5 KB
/
Acl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
<?php
/*
START LICENSE AND COPYRIGHT
This file is part of ZfExtended library
Copyright (c) 2013 - 2021 Marc Mittag; MittagQI - Quality Informatics; All rights reserved.
Contact: http://www.MittagQI.com/ / service (ATT) MittagQI.com
This file may be used under the terms of the GNU LESSER GENERAL PUBLIC LICENSE version 3
as published by the Free Software Foundation and appearing in the file lgpl3-license.txt
included in the packaging of this file. Please review the following information
to ensure the GNU LESSER GENERAL PUBLIC LICENSE version 3.0 requirements will be met:
https://www.gnu.org/licenses/lgpl-3.0.txt
@copyright Marc Mittag, MittagQI - Quality Informatics
@author MittagQI - Quality Informatics
@license GNU LESSER GENERAL PUBLIC LICENSE version 3
https://www.gnu.org/licenses/lgpl-3.0.txt
END LICENSE AND COPYRIGHT
*/
use MittagQI\Translate5\Acl\Rights;
use MittagQI\Translate5\Applet\Dispatcher;
use MittagQI\ZfExtended\Acl\AutoSetRoleResource;
use MittagQI\ZfExtended\Acl\ResourceManager;
use MittagQI\ZfExtended\Acl\SetAclRoleResource;
/**
* Singleton-Instanz
*
* - zu beziehen über $this->getInstance
* - bezieht seine Definitionen aus der modulName/configs/aclConfig.ini
*/
class ZfExtended_Acl extends Zend_Acl
{
/**
* Singleton Instanzen
*
* @var array _instances enthalten ACL Objekte
*/
protected static $_instance = null;
private array $_allRules;
/**
* Singleton Instanz - Hole Acl-Instanz
*
* - prüft, ob bereits eine Instanz erstellt wurde;
* falls ja, wird diese zurückgegeben
*
* @param bool $init causes getInstance, to create the singleton new
* @return ZfExtended_Acl
*/
public static function getInstance($init = false)
{
if (null === self::$_instance || $init) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Singleton Instanz auf NULL setzen, um sie neu initialiseren zu können
*/
public static function reset()
{
self::$_instance = null;
}
/**
* Creates a ACL with roles, resources and rights/privileges
* - must be used as singleton (getInstance())
* - only allow rules are set, default is deny
*/
protected function __construct()
{
$db = ZfExtended_Factory::get(ZfExtended_Models_Db_AclRules::class);
//currently we load the rules for all modules, if we ever need to differ,
// we have to do that in the isAllowed call
// the previous module filter was producing too many problems
$rules = $db->loadAll();
$this->addRoles(array_unique(array_column($rules, 'role')));
$this->addResources(array_unique(array_column($rules, 'resource')));
$this->addRules($rules);
}
/**
* Return all roles, not only the ones from the current module
* @return array
*/
public function getAllRoles()
{
$db = ZfExtended_Factory::get(ZfExtended_Models_Db_AclRules::class);
$s = $db->select()
->from($db->info($db::NAME), 'role')
->distinct();
return array_column($db->fetchAll($s)->toArray(), 'role');
}
/***
* Load acl entries for given resource and roles
* @param string $resource
* @param array $roles
* @return array
* @throws Zend_Db_Table_Exception
*/
public function getResourceByRoles(string $resource, array $roles)
{
$db = ZfExtended_Factory::get(ZfExtended_Models_Db_AclRules::class);
$s = $db->select()
->from($db->info($db::NAME))
->where('resource = ?', $resource)
->where('role IN (?)', $roles);
return $db->fetchAll($s)->toArray();
}
/***
* Get all initial page modules for given roles
* @param array $roles
* @return array
* @throws Zend_Db_Table_Exception
*/
public function getInitialPageModulesForRoles(array $roles): array
{
$db = ZfExtended_Factory::get(ZfExtended_Models_Db_AclRules::class);
$s = $db->select()
->from($db->info($db::NAME), 'module')
->where('resource = ?', Dispatcher::INITIAL_PAGE_RESOURCE)
->where('role IN (?)', $roles)
->distinct();
return array_column($db->fetchAll($s)->toArray(), 'module');
}
/**
* Returns all roles having a specifc resource and privilege
*/
public function getRolesWith(string $resource, string $privilege): array
{
$allRoles = $this->getAllRoles();
$result = [];
foreach ($allRoles as $role) {
if ($this->isAllowed($role, $resource, $privilege)) {
$result[] = $role;
}
}
return $result;
}
/**
* checks if one of the passed roles allows the resource / privelege
*
* @param Zend_Acl_Resource_Interface|string $resource
* @param string $privilege
* @return boolean
*/
public function isInAllowedRoles(array $roles, $resource = null, $privilege = null)
{
$allowed = false;
foreach ($roles as $role) {
try {
if ($this->isAllowed($role, $resource, $privilege)) {
$allowed = true;
break;
}
} catch (Zend_Acl_Role_Registry_Exception $exc) {
//if role is not registered because it is the role of a different module
} catch (Zend_Acl_Exception $e) {
//if the resource is not registered access is false
if (preg_match("/Resource '[^']*' not found/", $e->getMessage())) {
return false;
}
throw $e;
}
}
return $allowed;
}
/**
* Checks if the configured rights were valid
*/
public function checkRights()
{
if (! ZfExtended_Debug::hasLevel('core', 'acl')) {
return;
}
$this->getControllers();
$allowedResource = [Rights::ID, SetAclRoleResource::ID];
foreach ($this->_allRules as $rule) {
if (in_array($rule['resource'], $allowedResource)) {
continue;
}
$isAll = $rule['right'] == 'all';
$isClass = class_exists($rule['resource'], true);
$isController = class_exists($rule['resource'] . 'Controller', true);
//class_exists loads the class and isAll is set then its OK
if (($isClass || $isController) && $isAll) {
continue;
}
if (method_exists($rule['resource'] . 'Controller', $rule['right'] . 'Action')) {
continue;
}
if (method_exists($rule['resource'], $rule['right'])) {
continue;
}
//FIXME den code umbauen so dass er auf die neuen Klassen und den manager prüft
error_log('The following ACL resource and privilege does not exist in the Application: ' . $rule['resource'] . '; ' . $rule['right']);
}
}
/**
* Add the given rules to the internal ACL instance
* @throws Zend_Exception
*/
protected function addRules(array $rules)
{
if (empty($rules)) {
return;
}
$this->_allRules = $rules;
foreach ($rules as $rule) {
$role = $rule['role'];
$right = $rule['right'];
$resource = $rule['resource'];
if ($right == 'all') {
if ($resource == Rights::ID) {
throw new Zend_Exception('For the resource "frontend" the right "all" can not be used!');
}
$this->allow($role, $resource);
continue;
}
$this->allow($role, $resource, $right);
}
}
/**
* Retrieves all Roles that can be set in the user-management for the given roleset
* @param string[] $roles
* @return string[]
*/
public function getSetableRolesForRoles(array $roles): array
{
$setableRoles = [];
foreach ($this->_allRules as $rule) {
if ($rule['resource'] === SetAclRoleResource::ID
&& in_array($rule['role'], $roles)
&& ! in_array($rule['right'], $setableRoles)) {
$setableRoles[] = $rule['right'];
}
}
return $setableRoles;
}
/**
* Adds the resources to the internal ACL instance
*/
protected function addResources(array $resources)
{
foreach ($resources as $resource) {
$this->addResource(new Zend_Acl_Resource($resource));
}
}
/**
* Adds the roles to the internal ACL instance
*/
protected function addRoles(array $roles)
{
foreach ($roles as $role) {
$const = 'ACL_ROLE_' . strtoupper($role);
if (! defined($const)) {
define($const, $role);
}
$this->addRole(new Zend_Acl_Role($role));
}
}
/**
* Holt alle Controller aller Module
*
* - nochmals separat in Portal_AclTest analog implementiert, um sie hier private deklarieren zu können
*
* @return array array('moduleName'=>array('controllerName1','controllerName2',...),...)
*/
protected function getControllers()
{
$controllers = [];
$module = Zend_Registry::get('module');
$controllerDirs = Zend_Controller_Front::getInstance()->getControllerDirectory();
$this->includeController($controllerDirs[$module]);
foreach ($controllerDirs as $mod => $dir) {
if (substr($mod, 0, 8) === '_plugins') {
$this->includeController($dir);
}
}
foreach (get_declared_classes() as $class) {
if (is_subclass_of($class, 'Zend_Controller_Action') && preg_match("'Controller$'", $class)) {
$controllers[] = strtolower(substr($class, 0, strpos($class, "Controller")));
}
}
return $controllers;
}
protected function includeController($path)
{
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::CURRENT_AS_FILEINFO | FilesystemIterator::SKIP_DOTS));
foreach ($objects as $file => $object) {
if (strstr($file . '#', "Controller.php#") !== false) {
include_once $file;
}
}
}
/**
* Returns a list of frontend privileges / rights to the given roles
* @throws Zend_Acl_Exception
* @throws \MittagQI\Translate5\Test\Api\Exception
*/
public function getFrontendRights(array $roles): array
{
$resources = ResourceManager::getBusinessLogicResources();
$rights = [];
foreach ($resources as $resource) {
$rights = array_merge($rights, $this->getRightsToRolesAndResource($roles, $resource->getId()));
}
return $rights;
}
/**
* returns the configured rights to a resource roles combination
* @throws Zend_Acl_Exception
*/
public function getRightsToRolesAndResource(array $roles, string $resource): array
{
$result = [];
foreach ($roles as $role) {
try {
$roleObject = $this->getRole($role);
} catch (Zend_Acl_Role_Registry_Exception $exc) {
//if role is not registered because it is the role of a different module
continue;
}
$res = $this->_getRules($this->get($resource), $roleObject);
if (! empty($res)) {
$result = array_merge($result, array_keys($res['byPrivilegeId']));
}
}
return array_values(array_unique($result));
}
/***
* This will get all auto-set roles for $newUserRoles roles array, and merge them together with $newUserRoles and $oldUserRoles.
* Info: Some user roles are required to be set for user if the user has a specific role.
* ex: if a user has, admin and pm roles, the user must also have the editor role.
* @param array $newUserRoles
* @param array $oldUserRoles
* @return array
*/
public function mergeAutoSetRoles(array $newUserRoles, array $oldUserRoles): array
{
// get all auto set roles for the newUserRoles array
$setAdditionally = $this->getRightsToRolesAndResource($newUserRoles, AutoSetRoleResource::ID);
//merge the old roles and the allowed roles from the request
return array_unique(array_merge($newUserRoles, $oldUserRoles, $setAdditionally));
}
}