Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
RolePrivilegeRetriever
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 retrieveByResourceIds
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 removeDuplicatedEntries
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getDataBaseAccess
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-2022 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoDacSimple\model;
24
25use oat\oatbox\service\ConfigurableService;
26
27class RolePrivilegeRetriever extends ConfigurableService
28{
29    /**
30     * Sample data to be returned:
31     * [
32     *    'http://www.tao.lu/Ontologies/TAO.rdf#BackOfficeRole' => [
33     *        'GRANT',
34     *        'READ',
35     *        'WRITE'
36     *    ],
37     *    'http://www.tao.lu/Ontologies/TAO.rdf#ItemAuthor' => [
38     *        'GRANT',
39     *        'READ'
40     *    ],
41     * ]
42     */
43    public function retrieveByResourceIds(array $resourceIds): array
44    {
45        $results = $this->getDataBaseAccess()
46            ->getUsersWithPermissions($resourceIds);
47
48        $permissions = [];
49
50        foreach ($results as $result) {
51            $user = $result['user_id'];
52
53            if (!isset($permissions[$user])) {
54                $permissions[$user] = [];
55            }
56
57            $permissions[$user][] = $result['privilege'];
58        }
59
60        return $this->removeDuplicatedEntries($permissions);
61    }
62
63    private function removeDuplicatedEntries(array $permissions)
64    {
65        foreach ($permissions as $roleUri => &$entries) {
66            $entries = array_unique($entries);
67        }
68
69        return $permissions;
70    }
71
72    private function getDataBaseAccess(): DataBaseAccess
73    {
74        return $this->getServiceLocator()->get(DataBaseAccess::SERVICE_ID);
75    }
76}