Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 118 |
|
0.00% |
0 / 19 |
CRAP | |
0.00% |
0 / 1 |
| ActionEnforcer | |
0.00% |
0 / 118 |
|
0.00% |
0 / 19 |
1806 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getExtensionId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getControllerClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAction | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getParameters | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getController | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
20 | |||
| getRequest | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getResponse | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| verifyAuthorization | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
30 | |||
| __invoke | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| resolve | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
20 | |||
| resolveParameters | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
72 | |||
| getClassInstance | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| getControllerInstance | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getServiceId | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getActionFinder | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getContainer | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getMiddlewareRequestHandler | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 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) 2014-2021 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\tao\model\routing; |
| 24 | |
| 25 | use Context; |
| 26 | use GuzzleHttp\Psr7\ServerRequest; |
| 27 | use oat\generis\model\Middleware\MiddlewareRequestHandler; |
| 28 | use oat\tao\model\routing\Contract\ActionFinderInterface; |
| 29 | use oat\tao\model\routing\Service\ActionFinder; |
| 30 | use Psr\Container\ContainerInterface; |
| 31 | use ReflectionException; |
| 32 | use IExecutable; |
| 33 | use ActionEnforcingException; |
| 34 | use oat\tao\model\http\ResponseEmitter; |
| 35 | use oat\oatbox\service\ServiceManagerAwareInterface; |
| 36 | use oat\oatbox\service\ServiceManagerAwareTrait; |
| 37 | use oat\tao\model\http\Controller; |
| 38 | use Psr\Http\Message\ResponseInterface; |
| 39 | use Psr\Http\Message\ServerRequestInterface; |
| 40 | use ReflectionMethod; |
| 41 | use common_session_SessionManager; |
| 42 | use tao_models_classes_AccessDeniedException; |
| 43 | use oat\tao\model\accessControl\AclProxy; |
| 44 | use oat\tao\model\accessControl\data\DataAccessControl; |
| 45 | use oat\tao\model\accessControl\data\PermissionException; |
| 46 | use oat\tao\model\accessControl\func\AclProxy as FuncProxy; |
| 47 | use oat\oatbox\event\EventManager; |
| 48 | use oat\tao\model\event\BeforeAction; |
| 49 | use oat\oatbox\log\LoggerAwareTrait; |
| 50 | use oat\oatbox\log\TaoLoggerAwareInterface; |
| 51 | use oat\tao\model\action\CommonModuleInterface; |
| 52 | |
| 53 | /** |
| 54 | * @TODO ActionEnforcer class documentation. |
| 55 | * |
| 56 | * @author Jerome Bogaerts <jerome@taotesting.com> |
| 57 | * @author Joel Bout <joel@taotesting.com> |
| 58 | */ |
| 59 | class ActionEnforcer implements IExecutable, ServiceManagerAwareInterface, TaoLoggerAwareInterface |
| 60 | { |
| 61 | use ServiceManagerAwareTrait; |
| 62 | use LoggerAwareTrait; |
| 63 | |
| 64 | private $extension; |
| 65 | |
| 66 | private $controllerClass; |
| 67 | private $action; |
| 68 | private $parameters; |
| 69 | |
| 70 | private $request; |
| 71 | private $response; |
| 72 | |
| 73 | /** @var ContainerInterface */ |
| 74 | private $container; |
| 75 | |
| 76 | public function __construct($extensionId, $controller, $action, array $parameters) |
| 77 | { |
| 78 | $this->extension = $extensionId; |
| 79 | $this->controllerClass = $controller; |
| 80 | $this->action = $action; |
| 81 | $this->parameters = $parameters; |
| 82 | } |
| 83 | |
| 84 | protected function getExtensionId() |
| 85 | { |
| 86 | return $this->extension; |
| 87 | } |
| 88 | |
| 89 | protected function getControllerClass() |
| 90 | { |
| 91 | return $this->controllerClass; |
| 92 | } |
| 93 | |
| 94 | protected function getAction() |
| 95 | { |
| 96 | return $this->action; |
| 97 | } |
| 98 | |
| 99 | protected function getParameters() |
| 100 | { |
| 101 | return $this->parameters; |
| 102 | } |
| 103 | |
| 104 | protected function getController() |
| 105 | { |
| 106 | $controllerClass = $this->getControllerClass(); |
| 107 | if (!class_exists($controllerClass)) { |
| 108 | throw new ActionEnforcingException( |
| 109 | 'Controller "' . $controllerClass . '" could not be loaded.', |
| 110 | $controllerClass, |
| 111 | $this->getAction() |
| 112 | ); |
| 113 | } |
| 114 | |
| 115 | $controller = $this->getControllerInstance($controllerClass); |
| 116 | |
| 117 | $this->propagate($controller); |
| 118 | if ($controller instanceof Controller) { |
| 119 | $controller->setRequest($this->getRequest()); |
| 120 | $controller->setResponse($this->getResponse()); |
| 121 | } |
| 122 | if ($controller instanceof CommonModuleInterface) { |
| 123 | $controller->initialize(); |
| 124 | } |
| 125 | return $controller; |
| 126 | } |
| 127 | |
| 128 | protected function getRequest() |
| 129 | { |
| 130 | if (!$this->request) { |
| 131 | $this->request = ServerRequest::fromGlobals(); |
| 132 | } |
| 133 | |
| 134 | return $this->request; |
| 135 | } |
| 136 | |
| 137 | protected function getResponse() |
| 138 | { |
| 139 | if (!$this->response) { |
| 140 | $this->response = $this->getContainer()->get(ResponseInterface::class); |
| 141 | } |
| 142 | return $this->response; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @throws PermissionException |
| 147 | * @throws \common_exception_Error |
| 148 | * @throws \common_exception_MissingParameter |
| 149 | * @throws tao_models_classes_AccessDeniedException |
| 150 | */ |
| 151 | protected function verifyAuthorization() |
| 152 | { |
| 153 | $user = common_session_SessionManager::getSession()->getUser(); |
| 154 | if (!AclProxy::hasAccess($user, $this->getControllerClass(), $this->getAction(), $this->getParameters())) { |
| 155 | $func = new FuncProxy(); |
| 156 | $data = new DataAccessControl(); |
| 157 | //now go into details to see which kind of permissions are not correct |
| 158 | if ( |
| 159 | $func->hasAccess($user, $this->getControllerClass(), $this->getAction(), $this->getParameters()) && |
| 160 | !$data->hasAccess($user, $this->getControllerClass(), $this->getAction(), $this->getParameters()) |
| 161 | ) { |
| 162 | if ($user->getIdentifier()) { |
| 163 | throw new PermissionException( |
| 164 | $user->getIdentifier(), |
| 165 | $this->getAction(), |
| 166 | $this->getControllerClass(), |
| 167 | $this->getExtensionId() |
| 168 | ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | throw new tao_models_classes_AccessDeniedException( |
| 173 | $user->getIdentifier(), |
| 174 | $this->getAction(), |
| 175 | $this->getControllerClass(), |
| 176 | $this->getExtensionId() |
| 177 | ); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | public function __invoke(ServerRequestInterface $request, ResponseInterface $response = null) |
| 182 | { |
| 183 | $this->request = $request; |
| 184 | $this->response = $response; |
| 185 | $this->execute(); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * @throws ActionEnforcingException |
| 190 | * @throws ReflectionException |
| 191 | * @throws \common_exception_Error |
| 192 | * @throws \common_exception_MissingParameter |
| 193 | * @throws tao_models_classes_AccessDeniedException |
| 194 | */ |
| 195 | public function execute() |
| 196 | { |
| 197 | // Are we authorized to execute this action? |
| 198 | try { |
| 199 | $this->verifyAuthorization(); |
| 200 | } catch (PermissionException $pe) { |
| 201 | // forward the action (yes it's an awful hack, but far better than adding a step in Bootstrap's dispatch |
| 202 | // error). |
| 203 | |
| 204 | Context::getInstance()->setExtensionName('tao'); |
| 205 | $this->action = 'denied'; |
| 206 | $this->controllerClass = 'tao_actions_Permission'; |
| 207 | $this->extension = 'tao'; |
| 208 | } |
| 209 | |
| 210 | $response = $this->resolve($this->getRequest()); |
| 211 | |
| 212 | $emitter = new ResponseEmitter(); |
| 213 | $emitter($response); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * @throws ActionEnforcingException |
| 218 | * @throws ReflectionException |
| 219 | * @throws \common_exception_Error |
| 220 | */ |
| 221 | public function resolve(ServerRequestInterface $request): ResponseInterface |
| 222 | { |
| 223 | $this->request = $request; |
| 224 | |
| 225 | /** @var ControllerService $controllerService */ |
| 226 | $controllerService = $this->getServiceLocator()->get(ControllerService::SERVICE_ID); |
| 227 | try { |
| 228 | $controllerService->checkController($this->getControllerClass()); |
| 229 | $action = $controllerService->getAction($this->getControllerClass(), $this->getAction()); |
| 230 | } catch (RouterException $e) { |
| 231 | throw new ActionEnforcingException($e->getMessage(), $this->getControllerClass(), $this->getAction()); |
| 232 | } |
| 233 | |
| 234 | $this->response = $this->getMiddlewareRequestHandler() |
| 235 | ->withOriginalResponse($this->getResponse()) |
| 236 | ->handle($request); |
| 237 | |
| 238 | $controller = $this->getController(); |
| 239 | |
| 240 | if (!method_exists($controller, $action)) { |
| 241 | throw new ActionEnforcingException( |
| 242 | "Unable to find the action '" . $action . "' in '" . get_class($controller) . "'.", |
| 243 | $this->getControllerClass(), |
| 244 | $this->getAction() |
| 245 | ); |
| 246 | } |
| 247 | |
| 248 | $actionParameters = $this->resolveParameters($request, $controller, $action); |
| 249 | |
| 250 | // Action method is invoked, passing request parameters as method parameters. |
| 251 | $user = common_session_SessionManager::getSession()->getUser(); |
| 252 | $this->logDebug( |
| 253 | 'Invoking ' . get_class($controller) . '::' . $action . ' by ' . $user->getIdentifier(), |
| 254 | ['GENERIS', 'CLEARRFW'] |
| 255 | ); |
| 256 | |
| 257 | $eventManager = $this->getServiceLocator()->get(EventManager::SERVICE_ID); |
| 258 | $eventManager->trigger(new BeforeAction()); |
| 259 | |
| 260 | $response = call_user_func_array([$controller, $action], $actionParameters); |
| 261 | |
| 262 | return $response instanceof ResponseInterface ? $response : $controller->getPsrResponse(); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * @throws ReflectionException |
| 267 | */ |
| 268 | private function resolveParameters(ServerRequestInterface $request, $controller, string $action): array |
| 269 | { |
| 270 | // search parameters method |
| 271 | $reflect = new ReflectionMethod($controller, $action); |
| 272 | $parameters = $this->getParameters(); |
| 273 | |
| 274 | $actionParameters = []; |
| 275 | foreach ($reflect->getParameters() as $param) { |
| 276 | $paramName = $param->getName(); |
| 277 | $paramType = $param->getType(); |
| 278 | $paramTypeName = $paramType !== null ? $paramType->getName() : null; |
| 279 | |
| 280 | if (isset($parameters[$paramName])) { |
| 281 | $actionParameters[$paramName] = $parameters[$paramName]; |
| 282 | } elseif ($paramTypeName === ServerRequest::class) { |
| 283 | $actionParameters[$paramName] = $request; |
| 284 | } elseif (class_exists($paramTypeName) || interface_exists($paramTypeName)) { |
| 285 | $actionParameters[$paramName] = $this->getClassInstance($paramTypeName); |
| 286 | } elseif (!$param->isDefaultValueAvailable()) { |
| 287 | $this->logWarning( |
| 288 | 'Missing parameter ' . $paramName . ' for ' . $this->getControllerClass() . '@' . $action |
| 289 | ); |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | return $actionParameters; |
| 294 | } |
| 295 | |
| 296 | private function getClassInstance(string $className): object |
| 297 | { |
| 298 | $serviceId = $this->getServiceId($className); |
| 299 | $container = $this->getContainer(); |
| 300 | |
| 301 | if ($container->has($serviceId)) { |
| 302 | return $container->get($serviceId); |
| 303 | } |
| 304 | |
| 305 | return $this->propagate(new $className()); |
| 306 | } |
| 307 | |
| 308 | private function getControllerInstance(string $className): object |
| 309 | { |
| 310 | return $this->getActionFinder()->find($className) ?? $this->propagate(new $className()); |
| 311 | } |
| 312 | |
| 313 | private function getServiceId(string $className): string |
| 314 | { |
| 315 | return defined("$className::SERVICE_ID") |
| 316 | ? $className::SERVICE_ID |
| 317 | : $className; |
| 318 | } |
| 319 | |
| 320 | private function getActionFinder(): ActionFinderInterface |
| 321 | { |
| 322 | return $this->getContainer()->get(ActionFinder::class); |
| 323 | } |
| 324 | |
| 325 | private function getContainer(): ContainerInterface |
| 326 | { |
| 327 | if (!$this->container) { |
| 328 | $this->container = $this->getServiceManager()->getContainer(); |
| 329 | } |
| 330 | |
| 331 | return $this->container; |
| 332 | } |
| 333 | |
| 334 | private function getMiddlewareRequestHandler(): MiddlewareRequestHandler |
| 335 | { |
| 336 | return $this->getContainer()->get(MiddlewareRequestHandler::class); |
| 337 | } |
| 338 | } |