Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 64 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| CacheHelper | |
0.00% |
0 / 64 |
|
0.00% |
0 / 8 |
342 | |
0.00% |
0 / 1 |
| getCacheImplementation | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| cacheModule | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getControllerAccess | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
72 | |||
| getExtensionAccess | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
| flushExtensionAccess | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| flushControllerAccess | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| buildModuleSerial | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| removeModule | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace oat\funcAcl\helpers; |
| 4 | |
| 5 | use common_cache_Cache; |
| 6 | use common_cache_Exception; |
| 7 | use core_kernel_classes_Class; |
| 8 | use core_kernel_classes_Property; |
| 9 | use core_kernel_classes_Resource; |
| 10 | use oat\funcAcl\models\AccessService; |
| 11 | use oat\generis\model\GenerisRdf; |
| 12 | use oat\oatbox\service\ServiceManager; |
| 13 | use oat\tao\helpers\ControllerHelper; |
| 14 | |
| 15 | /** |
| 16 | * This program is free software; you can redistribute it and/or |
| 17 | * modify it under the terms of the GNU General Public License |
| 18 | * as published by the Free Software Foundation; under version 2 |
| 19 | * of the License (non-upgradable). |
| 20 | * |
| 21 | * This program is distributed in the hope that it will be useful, |
| 22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 24 | * GNU General Public License for more details. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 29 | * |
| 30 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 31 | * (under the project TAO-TRANSFER); |
| 32 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 33 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 34 | */ |
| 35 | |
| 36 | /** |
| 37 | * Short description of class CacheHelper |
| 38 | * |
| 39 | * @access public |
| 40 | * |
| 41 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 42 | * |
| 43 | * @package tao |
| 44 | */ |
| 45 | class CacheHelper |
| 46 | { |
| 47 | // --- ASSOCIATIONS --- |
| 48 | |
| 49 | // --- ATTRIBUTES --- |
| 50 | |
| 51 | /** |
| 52 | * prefix for the extension cache |
| 53 | * |
| 54 | * @var string |
| 55 | */ |
| 56 | public const CACHE_PREFIX_EXTENSION = 'acl_e_'; |
| 57 | |
| 58 | public const SERIAL_PREFIX_MODULE = 'acl'; |
| 59 | |
| 60 | /** |
| 61 | * Serial to store extensions access to |
| 62 | * |
| 63 | * @var string |
| 64 | */ |
| 65 | public const SERIAL_EXTENSIONS = 'acl_extensions'; |
| 66 | // --- OPERATIONS --- |
| 67 | |
| 68 | /** |
| 69 | * Returns the funcACL Cache implementation |
| 70 | * |
| 71 | * @return common_cache_Cache |
| 72 | */ |
| 73 | private static function getCacheImplementation() |
| 74 | { |
| 75 | return ServiceManager::getServiceManager()->get('generis/cache'); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * force recache of a controller |
| 80 | * |
| 81 | * @access public |
| 82 | * |
| 83 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 84 | * |
| 85 | * @param Resource $module |
| 86 | * |
| 87 | * @return void |
| 88 | */ |
| 89 | public static function cacheModule(core_kernel_classes_Resource $module) |
| 90 | { |
| 91 | $controllerClassName = MapHelper::getControllerFromUri($module->getUri()); |
| 92 | self::flushControllerAccess($controllerClassName); |
| 93 | self::getControllerAccess($controllerClassName); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Return the cached description of the roles |
| 98 | * that have access to this controller |
| 99 | * |
| 100 | * @param string $controllerClassName |
| 101 | * |
| 102 | * @return array |
| 103 | */ |
| 104 | public static function getControllerAccess($controllerClassName) |
| 105 | { |
| 106 | try { |
| 107 | $returnValue = self::getCacheImplementation()->get(self::SERIAL_PREFIX_MODULE . $controllerClassName); |
| 108 | } catch (common_cache_Exception $e) { |
| 109 | $extId = MapHelper::getExtensionFromController($controllerClassName); |
| 110 | $extension = MapHelper::getUriForExtension($extId); |
| 111 | $module = MapHelper::getUriForController($controllerClassName); |
| 112 | |
| 113 | $roleClass = new core_kernel_classes_Class(GenerisRdf::CLASS_ROLE); |
| 114 | $accessProperty = new core_kernel_classes_Property(AccessService::PROPERTY_ACL_GRANTACCESS); |
| 115 | |
| 116 | $returnValue = ['module' => [], 'actions' => []]; |
| 117 | |
| 118 | // roles by extensions |
| 119 | $roles = $roleClass->searchInstances([ |
| 120 | $accessProperty->getUri() => $extension, |
| 121 | ], [ |
| 122 | 'recursive' => true, 'like' => false, |
| 123 | ]); |
| 124 | |
| 125 | foreach ($roles as $grantedRole) { |
| 126 | $returnValue['module'][] = $grantedRole->getUri(); |
| 127 | } |
| 128 | |
| 129 | // roles by controller |
| 130 | $filters = [ |
| 131 | $accessProperty->getUri() => $module, |
| 132 | ]; |
| 133 | $options = ['recursive' => true, 'like' => false]; |
| 134 | |
| 135 | foreach ($roleClass->searchInstances($filters, $options) as $grantedRole) { |
| 136 | $returnValue['module'][] = $grantedRole->getUri(); |
| 137 | } |
| 138 | |
| 139 | // roles by action |
| 140 | $actions = ControllerHelper::getActions($controllerClassName); |
| 141 | if (is_iterable($actions)) { |
| 142 | foreach ($actions as $actionName) { |
| 143 | $actionUri = MapHelper::getUriForAction($controllerClassName, $actionName); |
| 144 | $rolesForAction = $roleClass->searchInstances([ |
| 145 | $accessProperty->getUri() => $actionUri, |
| 146 | ], ['recursive' => true, 'like' => false]); |
| 147 | |
| 148 | if (!empty($rolesForAction)) { |
| 149 | $actionName = MapHelper::getActionFromUri($actionUri); |
| 150 | $returnValue['actions'][$actionName] = []; |
| 151 | |
| 152 | foreach ($rolesForAction as $roleResource) { |
| 153 | $returnValue['actions'][$actionName][] = $roleResource->getUri(); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | self::getCacheImplementation()->put($returnValue, self::SERIAL_PREFIX_MODULE . $controllerClassName); |
| 159 | } |
| 160 | |
| 161 | return $returnValue; |
| 162 | } |
| 163 | |
| 164 | public static function getExtensionAccess($extId) |
| 165 | { |
| 166 | try { |
| 167 | $returnValue = self::getCacheImplementation()->get(self::CACHE_PREFIX_EXTENSION . $extId); |
| 168 | } catch (common_cache_Exception $e) { |
| 169 | $returnValue = []; |
| 170 | $aclExtUri = AccessService::singleton()->makeEMAUri($extId); |
| 171 | $roleClass = new core_kernel_classes_Class(GenerisRdf::CLASS_ROLE); |
| 172 | $roles = $roleClass->searchInstances([ |
| 173 | AccessService::PROPERTY_ACL_GRANTACCESS => $aclExtUri, |
| 174 | ], [ |
| 175 | 'recursive' => true, |
| 176 | 'like' => false, |
| 177 | ]); |
| 178 | |
| 179 | foreach ($roles as $grantedRole) { |
| 180 | $returnValue[] = $grantedRole->getUri(); |
| 181 | } |
| 182 | self::getCacheImplementation()->put($returnValue, self::CACHE_PREFIX_EXTENSION . $extId); |
| 183 | } |
| 184 | |
| 185 | return $returnValue; |
| 186 | } |
| 187 | |
| 188 | public static function flushExtensionAccess($extensionId) |
| 189 | { |
| 190 | self::getCacheImplementation()->remove(self::CACHE_PREFIX_EXTENSION . $extensionId); |
| 191 | |
| 192 | foreach (ControllerHelper::getControllers($extensionId) as $controllerClassName) { |
| 193 | self::flushControllerAccess($controllerClassName); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | public static function flushControllerAccess($controllerClassName) |
| 198 | { |
| 199 | self::getCacheImplementation()->remove(self::SERIAL_PREFIX_MODULE . $controllerClassName); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * Short description of method buildModuleSerial |
| 204 | * |
| 205 | * @access private |
| 206 | * |
| 207 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 208 | * |
| 209 | * @param Resource $module |
| 210 | * |
| 211 | * @return string |
| 212 | */ |
| 213 | private static function buildModuleSerial(core_kernel_classes_Resource $module) |
| 214 | { |
| 215 | $returnValue = (string) ''; |
| 216 | |
| 217 | $uri = explode('#', $module->getUri()); |
| 218 | list($type, $extId) = explode('_', $uri[1]); |
| 219 | $returnValue = self::SERIAL_PREFIX_MODULE . $extId . urlencode($module->getUri()); |
| 220 | |
| 221 | return (string) $returnValue; |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Short description of method removeModule |
| 226 | * |
| 227 | * @access public |
| 228 | * |
| 229 | * @author Jerome Bogaerts, <jerome@taotesting.com> |
| 230 | * |
| 231 | * @param Resource $module |
| 232 | * |
| 233 | * @return void |
| 234 | */ |
| 235 | public static function removeModule(core_kernel_classes_Resource $module) |
| 236 | { |
| 237 | self::getCacheImplementation()->remove(self::buildModuleSerial($module)); |
| 238 | } |
| 239 | } |