Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
MediaBrowserPermissionsMapper
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
6 / 6
11
100.00% covered (success)
100.00%
1 / 1
 enableAccessControl
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 map
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 hasReadAccess
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 hasWriteAccess
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getPermissionChecker
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 isAccessControlEnabled
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
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
21declare(strict_types=1);
22
23namespace oat\tao\model\media\mapper;
24
25use oat\oatbox\service\ConfigurableService;
26use oat\tao\model\accessControl\AccessControlEnablerInterface;
27use oat\tao\model\accessControl\PermissionChecker;
28use oat\tao\model\accessControl\PermissionCheckerInterface;
29
30class MediaBrowserPermissionsMapper extends ConfigurableService implements
31    AccessControlEnablerInterface,
32    MediaBrowserMapperInterface
33{
34    protected const DATA_PERMISSIONS = 'permissions';
35
36    /** @var PermissionCheckerInterface */
37    private $permissionChecker;
38
39    /** @var bool */
40    private $enableAccessControl;
41
42    public function enableAccessControl(): AccessControlEnablerInterface
43    {
44        $this->enableAccessControl = true;
45
46        return $this;
47    }
48
49    public function map(array $data, string $resourceUri): array
50    {
51        $data[self::DATA_PERMISSIONS] = [];
52
53        if ($this->hasReadAccess($resourceUri)) {
54            $data[self::DATA_PERMISSIONS][] = PermissionCheckerInterface::PERMISSION_READ;
55        }
56
57        if ($this->hasWriteAccess($resourceUri)) {
58            $data[self::DATA_PERMISSIONS][] = PermissionCheckerInterface::PERMISSION_WRITE;
59        }
60
61        return $data;
62    }
63
64    protected function hasReadAccess(string $uri): bool
65    {
66        return $this->isAccessControlEnabled()
67            ? $this->getPermissionChecker()->hasReadAccess($uri)
68            : true;
69    }
70
71    protected function hasWriteAccess(string $uri): bool
72    {
73        return $this->isAccessControlEnabled()
74            ? $this->getPermissionChecker()->hasWriteAccess($uri)
75            : true;
76    }
77
78    private function getPermissionChecker(): PermissionCheckerInterface
79    {
80        if (!$this->permissionChecker) {
81            $this->permissionChecker = $this->getServiceLocator()->get(PermissionChecker::class);
82        }
83
84        return $this->permissionChecker;
85    }
86
87    private function isAccessControlEnabled(): bool
88    {
89        return $this->enableAccessControl === true;
90    }
91}