Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
21 / 21 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
MediaPermissionService | |
100.00% |
21 / 21 |
|
100.00% |
4 / 4 |
5 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
isAllowedToEditResource | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
isAllowedToEditMedia | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
isAllowedToPreview | |
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\accessControl; |
24 | |
25 | use oat\oatbox\user\User; |
26 | use oat\tao\model\accessControl\ActionAccessControl; |
27 | use oat\tao\model\accessControl\Context; |
28 | use oat\tao\model\accessControl\PermissionChecker; |
29 | use oat\taoMediaManager\controller\MediaImport; |
30 | use oat\taoMediaManager\controller\MediaManager; |
31 | use core_kernel_classes_Resource as Resource; |
32 | |
33 | class MediaPermissionService |
34 | { |
35 | /** @var ActionAccessControl */ |
36 | private $actionAccessControl; |
37 | |
38 | /** @var PermissionChecker */ |
39 | private $permissionChecker; |
40 | |
41 | public function __construct(ActionAccessControl $actionAcl, PermissionChecker $permissionChecker) |
42 | { |
43 | $this->actionAccessControl = $actionAcl; |
44 | $this->permissionChecker = $permissionChecker; |
45 | } |
46 | |
47 | public function isAllowedToEditResource(Resource $resource, User $user = null): bool |
48 | { |
49 | $editContext = new Context([ |
50 | Context::PARAM_CONTROLLER => MediaManager::class, |
51 | Context::PARAM_ACTION => 'editInstance', |
52 | Context::PARAM_USER => $user |
53 | ]); |
54 | |
55 | return $this->permissionChecker->hasWriteAccess($resource->getUri(), $user) |
56 | && $this->actionAccessControl->contextHasWriteAccess($editContext); |
57 | } |
58 | |
59 | public function isAllowedToEditMedia(User $user = null): bool |
60 | { |
61 | $editContext = new Context([ |
62 | Context::PARAM_CONTROLLER => MediaImport::class, |
63 | Context::PARAM_ACTION => 'editMedia', |
64 | Context::PARAM_USER => $user |
65 | ]); |
66 | |
67 | return $this->actionAccessControl->contextHasWriteAccess($editContext); |
68 | } |
69 | |
70 | public function isAllowedToPreview(User $user = null): bool |
71 | { |
72 | $previewContext = new Context([ |
73 | Context::PARAM_CONTROLLER => MediaManager::class, |
74 | Context::PARAM_ACTION => 'isPreviewEnabled', |
75 | Context::PARAM_USER => $user |
76 | ]); |
77 | |
78 | return $this->actionAccessControl->contextHasReadAccess($previewContext); |
79 | } |
80 | } |