Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
38 / 38 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| MediaSourcePermissionsMapper | |
100.00% |
38 / 38 |
|
100.00% |
6 / 6 |
18 | |
100.00% |
1 / 1 |
| map | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
9 | |||
| hasReadAccess | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| hasWriteAccess | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
| getActionAccessControl | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| hasReadAccessByContext | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| hasWriteAccessByContext | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 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) 2021 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoMediaManager\model\mapper; |
| 24 | |
| 25 | use taoItems_actions_ItemContent; |
| 26 | use oat\tao\model\accessControl\Context; |
| 27 | use oat\tao\model\accessControl\ActionAccessControl; |
| 28 | use oat\tao\model\media\mapper\MediaBrowserPermissionsMapper; |
| 29 | |
| 30 | class MediaSourcePermissionsMapper extends MediaBrowserPermissionsMapper |
| 31 | { |
| 32 | private const PERMISSION_PREVIEW = 'PREVIEW'; |
| 33 | private const PERMISSION_DOWNLOAD = 'DOWNLOAD'; |
| 34 | private const PERMISSION_UPLOAD = 'UPLOAD'; |
| 35 | private const PERMISSION_DELETE = 'DELETE'; |
| 36 | |
| 37 | /** @var ActionAccessControl */ |
| 38 | private $actionAccessControl; |
| 39 | |
| 40 | public function map(array $data, string $resourceUri): array |
| 41 | { |
| 42 | $data = parent::map($data, $resourceUri); |
| 43 | $hasReadAccess = $this->hasReadAccess($resourceUri); |
| 44 | |
| 45 | if ( |
| 46 | $this->hasReadAccessByContext(taoItems_actions_ItemContent::class, 'previewAsset') |
| 47 | && $hasReadAccess |
| 48 | ) { |
| 49 | $data[self::DATA_PERMISSIONS][] = self::PERMISSION_PREVIEW; |
| 50 | } |
| 51 | |
| 52 | if ( |
| 53 | $this->hasReadAccessByContext(taoItems_actions_ItemContent::class, 'downloadAsset') |
| 54 | && $hasReadAccess |
| 55 | ) { |
| 56 | $data[self::DATA_PERMISSIONS][] = self::PERMISSION_DOWNLOAD; |
| 57 | } |
| 58 | |
| 59 | $hasWriteAccess = $this->hasWriteAccess($resourceUri); |
| 60 | |
| 61 | if ( |
| 62 | $this->hasWriteAccessByContext(taoItems_actions_ItemContent::class, 'deleteAsset') |
| 63 | && $hasWriteAccess |
| 64 | ) { |
| 65 | $data[self::DATA_PERMISSIONS][] = self::PERMISSION_DELETE; |
| 66 | } |
| 67 | |
| 68 | if ( |
| 69 | $this->hasWriteAccessByContext(taoItems_actions_ItemContent::class, 'uploadAsset') |
| 70 | && $hasWriteAccess |
| 71 | ) { |
| 72 | $data[self::DATA_PERMISSIONS][] = self::PERMISSION_UPLOAD; |
| 73 | } |
| 74 | |
| 75 | return $data; |
| 76 | } |
| 77 | |
| 78 | protected function hasReadAccess(string $uri): bool |
| 79 | { |
| 80 | return parent::hasReadAccess($uri) |
| 81 | && $this->hasReadAccessByContext(taoItems_actions_ItemContent::class, 'viewAsset'); |
| 82 | } |
| 83 | |
| 84 | protected function hasWriteAccess(string $uri): bool |
| 85 | { |
| 86 | $canDelete = $this->hasWriteAccessByContext( |
| 87 | taoItems_actions_ItemContent::class, |
| 88 | 'deleteAsset' |
| 89 | ); |
| 90 | $canUpload = $this->hasWriteAccessByContext( |
| 91 | taoItems_actions_ItemContent::class, |
| 92 | 'uploadAsset' |
| 93 | ); |
| 94 | |
| 95 | return parent::hasWriteAccess($uri) && ($canDelete || $canUpload); |
| 96 | } |
| 97 | |
| 98 | private function getActionAccessControl(): ActionAccessControl |
| 99 | { |
| 100 | if (!isset($this->actionAccessControl)) { |
| 101 | $this->actionAccessControl = $this->getServiceLocator()->get(ActionAccessControl::SERVICE_ID); |
| 102 | } |
| 103 | |
| 104 | return $this->actionAccessControl; |
| 105 | } |
| 106 | |
| 107 | private function hasReadAccessByContext(string $controller, string $action): bool |
| 108 | { |
| 109 | return $this->getActionAccessControl()->contextHasReadAccess( |
| 110 | new Context([ |
| 111 | Context::PARAM_CONTROLLER => $controller, |
| 112 | Context::PARAM_ACTION => $action, |
| 113 | ]) |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | private function hasWriteAccessByContext(string $controller, string $action): bool |
| 118 | { |
| 119 | return $this->getActionAccessControl()->contextHasWriteAccess( |
| 120 | new Context([ |
| 121 | Context::PARAM_CONTROLLER => $controller, |
| 122 | Context::PARAM_ACTION => $action, |
| 123 | ]) |
| 124 | ); |
| 125 | } |
| 126 | } |