Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
AdminService
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 5
110
0.00% covered (danger)
0.00%
0 / 1
 setOwner
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 getUsersPermissions
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addPermissionToClass
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 getServiceManager
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRolePrivilegeRetriever
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
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) 2009-2012 (original work) Public Research Centre Henri Tudor (under the project TAO-SUSTAIN & TAO-DEV);
19 *
20 *
21 */
22
23namespace oat\taoDacSimple\model;
24
25use oat\oatbox\service\ServiceManager;
26
27/**
28 * Service to administer the privileges
29 *
30 * @author Joel Bout <joel@taotesting.com>
31 */
32class AdminService
33{
34    /**
35     * Set a new Owner, removing the old owner(s)
36     *
37     * @param string $resourceUri
38     * @param string $userUri
39     * @return bool
40     */
41    public static function setOwner($resourceUri, $userUri)
42    {
43        /** @var DataBaseAccess $db */
44        $db = self::getServiceManager()->get(DataBaseAccess::SERVICE_ID);
45
46        // Needs better abstraction
47        $dbRow = $db->getUsersWithPermissions([$resourceUri]);
48        foreach ($dbRow as $row) {
49            if ($row['resource_id'] == $resourceUri && $row['privilege'] == 'OWNER') {
50                $db->removePermissions($row['user_id'], $resourceUri, ['OWNER']);
51            }
52        }
53
54        $db->addPermissions($userUri, $resourceUri, ['OWNER']);
55
56        return true;
57    }
58
59    /**
60     * Get a list of users with permissions for a given resource
61     *
62     * Returns an associative array  with userid as key and an array of rights as value
63     *
64     * @param $resourceId
65     * @return array
66     */
67    public static function getUsersPermissions($resourceId)
68    {
69        return self::getRolePrivilegeRetriever()->retrieveByResourceIds([$resourceId]);
70    }
71
72    /**
73     * recursivly add permissions to a class and all instances
74     * @param \core_kernel_classes_Class $class
75     * @param $userUri
76     * @param $rights
77     */
78    public static function addPermissionToClass(\core_kernel_classes_Class $class, $userUri, $rights)
79    {
80
81        /** @var DataBaseAccess $dbAccess */
82        $dbAccess = self::getServiceManager()->get(DataBaseAccess::SERVICE_ID);
83        $dbAccess->addPermissions($userUri, $class->getUri(), $rights);
84        foreach ($class->getInstances(false) as $instance) {
85            $dbAccess->addPermissions($userUri, $instance->getUri(), $rights);
86        }
87        foreach ($class->getSubClasses(false) as $subclass) {
88            self::addPermissionToClass($subclass, $userUri, $rights);
89        }
90    }
91
92    public static function getServiceManager()
93    {
94        return ServiceManager::getServiceManager();
95    }
96
97    private static function getRolePrivilegeRetriever(): RolePrivilegeRetriever
98    {
99        return self::getServiceManager()->get(RolePrivilegeRetriever::class);
100    }
101}