Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.43% covered (success)
96.43%
27 / 28
90.00% covered (success)
90.00%
9 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
ChangeAccessCommand
96.43% covered (success)
96.43%
27 / 28
90.00% covered (success)
90.00%
9 / 10
15
0.00% covered (danger)
0.00%
0 / 1
 grantResourceForUser
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 getResourceIdsToGrant
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUserIdsToGrant
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 revokeResourceForUser
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 removeRevokeResourceForUser
88.89% covered (warning)
88.89%
8 / 9
0.00% covered (danger)
0.00%
0 / 1
3.01
 getResourceIdsToRevoke
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUserIdsToRevoke
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUserIdsToRevokePermissions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getUserPermissionsToRevoke
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getResourceIdsByUserAndPermissionToRevoke
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) 2023 (original work) Open Assessment Technologies SA.
19 */
20
21declare(strict_types=1);
22
23namespace oat\taoDacSimple\model\Command;
24
25class ChangeAccessCommand
26{
27    /**
28     * An array in the form ['userId' [ 'READ' => ['resource1', 'resource2']]]
29     *
30     * @var string[][][]
31     */
32    private array $userRevokedPermissions = [];
33
34    /**
35     * An array in the form ['resourceId' [ 'READ' => ['userId1', 'userId2']]]
36     *
37     * @var string[][][]
38     */
39    private array $giveAccessMap = [];
40
41    /**
42     * An array in the form ['resourceId' [ 'READ' => ['userId1', 'userId2']]]
43     *
44     * @var string[][][]
45     */
46    private array $revokeAccessMap = [];
47
48    public function grantResourceForUser(string $resourceId, string $permission, string $userId): void
49    {
50        $this->giveAccessMap[$resourceId] ??= [];
51        $this->giveAccessMap[$resourceId][$permission] ??= [];
52
53        if (!in_array($userId, $this->giveAccessMap[$resourceId][$permission], true)) {
54            $this->giveAccessMap[$resourceId][$permission][] = $userId;
55        }
56    }
57
58    public function getResourceIdsToGrant(): array
59    {
60        return array_keys($this->giveAccessMap);
61    }
62
63    public function getUserIdsToGrant(string $resourceId, string $permission): array
64    {
65        return $this->giveAccessMap[$resourceId][$permission] ?? [];
66    }
67
68    public function revokeResourceForUser(string $resourceId, string $permission, string $userId): void
69    {
70        $this->revokeAccessMap[$resourceId] ??= [];
71        $this->revokeAccessMap[$resourceId][$permission] ??= [];
72
73        if (!in_array($userId, $this->revokeAccessMap[$resourceId][$permission], true)) {
74            $this->revokeAccessMap[$resourceId][$permission][] = $userId;
75        }
76
77        $this->userRevokedPermissions[$userId] ??= [];
78        $this->userRevokedPermissions[$userId][$permission] ??= [];
79
80        if (!in_array($resourceId, $this->userRevokedPermissions[$userId][$permission], true)) {
81            $this->userRevokedPermissions[$userId][$permission][] = $resourceId;
82        }
83    }
84
85    public function removeRevokeResourceForUser(string $resourceId, string $permission, string $userId): void
86    {
87        $this->userRevokedPermissions[$userId][$permission] ??= [];
88
89        $key = array_search($resourceId, $this->userRevokedPermissions[$userId][$permission]);
90
91        if ($key !== false) {
92            unset($this->userRevokedPermissions[$userId][$permission][$key]);
93        }
94
95        $this->revokeAccessMap[$resourceId][$permission] ??= [];
96
97        $key = array_search($userId, $this->revokeAccessMap[$resourceId][$permission]);
98
99        if ($key === false) {
100            return;
101        }
102
103        unset($this->revokeAccessMap[$resourceId][$permission][$key]);
104    }
105
106    public function getResourceIdsToRevoke(): array
107    {
108        return array_keys($this->revokeAccessMap);
109    }
110
111    public function getUserIdsToRevoke(string $resourceId, string $permission): array
112    {
113        return $this->revokeAccessMap[$resourceId][$permission] ?? [];
114    }
115
116    public function getUserIdsToRevokePermissions(): array
117    {
118        return array_keys($this->userRevokedPermissions);
119    }
120
121    public function getUserPermissionsToRevoke(string $userId): array
122    {
123        return array_keys($this->userRevokedPermissions[$userId]);
124    }
125
126    public function getResourceIdsByUserAndPermissionToRevoke(string $userId, string $permission): array
127    {
128        return $this->userRevokedPermissions[$userId][$permission] ?? [];
129    }
130}