Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| ActionAccessService | |
0.00% |
0 / 30 |
|
0.00% |
0 / 2 |
72 | |
0.00% |
0 / 1 |
| add | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| remove | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
42 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2009-2012 (original work) Public Research Centre Henri Tudor (under the project TAO-SUSTAIN & TAO-DEV); |
| 19 | */ |
| 20 | |
| 21 | namespace oat\funcAcl\models; |
| 22 | |
| 23 | use common_Logger; |
| 24 | use core_kernel_classes_Class; |
| 25 | use core_kernel_classes_Property; |
| 26 | use core_kernel_classes_Resource; |
| 27 | use oat\funcAcl\helpers\CacheHelper; |
| 28 | use oat\funcAcl\helpers\MapHelper; |
| 29 | use oat\funcAcl\helpers\ModelHelper; |
| 30 | use oat\funcAcl\models\event\AccessRightAddedEvent; |
| 31 | use oat\funcAcl\models\event\AccessRightRemovedEvent; |
| 32 | |
| 33 | /** |
| 34 | * access operation for actions |
| 35 | * |
| 36 | * @access public |
| 37 | * |
| 38 | * @author Jehan Bihin |
| 39 | * |
| 40 | * @package tao |
| 41 | * |
| 42 | * @since 2.2 |
| 43 | */ |
| 44 | class ActionAccessService extends AccessService |
| 45 | { |
| 46 | /** |
| 47 | * Short description of method add |
| 48 | * |
| 49 | * @access public |
| 50 | * |
| 51 | * @author Jehan Bihin, <jehan.bihin@tudor.lu> |
| 52 | * |
| 53 | * @param string $roleUri |
| 54 | * @param string $accessUri |
| 55 | * |
| 56 | * @return mixed |
| 57 | */ |
| 58 | public function add($roleUri, $accessUri) |
| 59 | { |
| 60 | $uri = explode('#', $accessUri); |
| 61 | list($type, $ext, $mod, $act) = explode('_', $uri[1]); |
| 62 | |
| 63 | $role = new core_kernel_classes_Resource($roleUri); |
| 64 | $module = new core_kernel_classes_Resource($this->makeEMAUri($ext, $mod)); |
| 65 | $actionAccessProperty = new core_kernel_classes_Property(static::PROPERTY_ACL_GRANTACCESS); |
| 66 | $moduleAccessProperty = new core_kernel_classes_Property(static::PROPERTY_ACL_GRANTACCESS); |
| 67 | |
| 68 | $values = $role->getPropertyValues($actionAccessProperty); |
| 69 | |
| 70 | if (!in_array($accessUri, $values)) { |
| 71 | $role->setPropertyValue($actionAccessProperty, $accessUri); |
| 72 | $this->getEventManager()->trigger(new AccessRightAddedEvent($roleUri, $accessUri)); |
| 73 | $controllerClassName = MapHelper::getControllerFromUri($module->getUri()); |
| 74 | CacheHelper::flushControllerAccess($controllerClassName); |
| 75 | } else { |
| 76 | common_Logger::w('Tried to regrant access for role ' . $role->getUri() . ' to action ' . $accessUri); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Short description of method remove |
| 82 | * |
| 83 | * @access public |
| 84 | * |
| 85 | * @author Jehan Bihin, <jehan.bihin@tudor.lu> |
| 86 | * |
| 87 | * @param string $roleUri |
| 88 | * @param string $accessUri |
| 89 | * |
| 90 | * @return mixed |
| 91 | */ |
| 92 | public function remove($roleUri, $accessUri) |
| 93 | { |
| 94 | $uri = explode('#', $accessUri); |
| 95 | list($type, $ext, $mod, $act) = explode('_', $uri[1]); |
| 96 | |
| 97 | $role = new core_kernel_classes_Class($roleUri); |
| 98 | $actionAccessProperty = new core_kernel_classes_Property(static::PROPERTY_ACL_GRANTACCESS); |
| 99 | |
| 100 | $module = new core_kernel_classes_Resource($this->makeEMAUri($ext, $mod)); |
| 101 | $controllerClassName = MapHelper::getControllerFromUri($module->getUri()); |
| 102 | |
| 103 | // access via controller? |
| 104 | $controllerAccess = CacheHelper::getControllerAccess($controllerClassName); |
| 105 | |
| 106 | if (in_array($roleUri, $controllerAccess['module'])) { |
| 107 | // remove access to controller |
| 108 | ModuleAccessService::singleton()->remove($roleUri, $module->getUri()); |
| 109 | |
| 110 | // add access to all other actions |
| 111 | foreach (ModelHelper::getActions($module) as $action) { |
| 112 | if ($action->getUri() != $accessUri) { |
| 113 | $this->add($roleUri, $action->getUri()); |
| 114 | $this->getEventManager()->trigger(new AccessRightAddedEvent($roleUri, $action->getUri())); |
| 115 | } |
| 116 | } |
| 117 | } elseif (isset($controllerAccess['actions'][$act]) && in_array($roleUri, $controllerAccess['actions'][$act])) { |
| 118 | // remove action only |
| 119 | $role->removePropertyValues($actionAccessProperty, ['pattern' => $accessUri]); |
| 120 | $this->getEventManager()->trigger(new AccessRightRemovedEvent($roleUri, $accessUri)); |
| 121 | |
| 122 | CacheHelper::flushControllerAccess($controllerClassName); |
| 123 | } |
| 124 | } |
| 125 | } |