Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 24 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ChangePermissionsTask | |
0.00% |
0 / 24 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
| __invoke | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
| validateParams | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getChangePermissionsService | |
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) 2020-2023 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoDacSimple\model\tasks; |
| 24 | |
| 25 | use common_exception_MissingParameter; |
| 26 | use oat\generis\model\OntologyAwareTrait; |
| 27 | use oat\oatbox\extension\AbstractAction; |
| 28 | use oat\oatbox\reporting\Report; |
| 29 | use oat\tao\model\taskQueue\Task\TaskAwareInterface; |
| 30 | use oat\tao\model\taskQueue\Task\TaskAwareTrait; |
| 31 | use oat\taoDacSimple\model\ChangePermissionsService; |
| 32 | use Exception; |
| 33 | use JsonSerializable; |
| 34 | use oat\taoDacSimple\model\Command\ChangePermissionsCommand; |
| 35 | |
| 36 | /** |
| 37 | * Class ChangePermissionsTask |
| 38 | * |
| 39 | * Handling permission changes in background |
| 40 | */ |
| 41 | class ChangePermissionsTask extends AbstractAction implements TaskAwareInterface, JsonSerializable |
| 42 | { |
| 43 | use TaskAwareTrait; |
| 44 | use OntologyAwareTrait; |
| 45 | |
| 46 | public const PARAM_RESOURCE = 'resource'; |
| 47 | public const PARAM_PRIVILEGES = 'privileges'; |
| 48 | public const PARAM_RECURSIVE = 'recursive'; |
| 49 | |
| 50 | private const MANDATORY_PARAMS = [ |
| 51 | self::PARAM_RESOURCE, |
| 52 | self::PARAM_PRIVILEGES |
| 53 | ]; |
| 54 | |
| 55 | public function __invoke($params = []): Report |
| 56 | { |
| 57 | $this->validateParams($params); |
| 58 | |
| 59 | try { |
| 60 | $command = new ChangePermissionsCommand( |
| 61 | $this->getResource($params[self::PARAM_RESOURCE]), |
| 62 | (array) $params[self::PARAM_PRIVILEGES], |
| 63 | filter_var($params[self::PARAM_RECURSIVE] ?? false, FILTER_VALIDATE_BOOL) |
| 64 | ); |
| 65 | |
| 66 | $this->getChangePermissionsService()->change($command); |
| 67 | |
| 68 | return Report::createSuccess('Permissions saved'); |
| 69 | } catch (Exception $e) { |
| 70 | $errMessage = sprintf('Saving permissions failed: %s', $e->getMessage()); |
| 71 | $this->getLogger()->error($errMessage); |
| 72 | |
| 73 | return Report::createError($errMessage); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | private function validateParams(array $params): void |
| 78 | { |
| 79 | foreach (self::MANDATORY_PARAMS as $param) { |
| 80 | if (!isset($params[$param])) { |
| 81 | throw new common_exception_MissingParameter( |
| 82 | sprintf( |
| 83 | 'Missing parameter `%s` in %s', |
| 84 | $param, |
| 85 | self::class |
| 86 | ) |
| 87 | ); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public function jsonSerialize(): string |
| 93 | { |
| 94 | return __CLASS__; |
| 95 | } |
| 96 | |
| 97 | private function getChangePermissionsService(): ChangePermissionsService |
| 98 | { |
| 99 | return $this->getServiceManager()->getContainer()->get(ChangePermissionsService::class); |
| 100 | } |
| 101 | } |