Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.00% |
19 / 20 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
PermissionManager | |
95.00% |
19 / 20 |
|
75.00% |
3 / 4 |
12 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
isAuthorized | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
getAllConfiguredPermissions | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
permissionRuleExists | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
6 |
1 | <?php |
2 | |
3 | /** |
4 | * Permission Manager |
5 | * |
6 | * PHP version 8 |
7 | * |
8 | * Copyright (C) Villanova University 2010. |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License version 2, |
12 | * as published by the Free Software Foundation. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 | * GNU General Public License for more details. |
18 | * |
19 | * You should have received a copy of the GNU General Public License |
20 | * along with this program; if not, write to the Free Software |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
22 | * |
23 | * @category VuFind |
24 | * @package Authorization |
25 | * @author Demian Katz <demian.katz@villanova.edu> |
26 | * @author Oliver Goldschmidt <o.goldschmidt@tuhh.de> |
27 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
28 | * @link http://vufind.org/wiki/ Wiki |
29 | */ |
30 | |
31 | namespace VuFind\Role; |
32 | |
33 | use LmcRbacMvc\Service\AuthorizationServiceAwareTrait; |
34 | |
35 | use function in_array; |
36 | use function is_array; |
37 | |
38 | /** |
39 | * Permission Manager |
40 | * |
41 | * @category VuFind |
42 | * @package Authorization |
43 | * @author Demian Katz <demian.katz@villanova.edu> |
44 | * @author Oliver Goldschmidt <o.goldschmidt@tuhh.de> |
45 | * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License |
46 | * @link http://vufind.org/wiki/ Wiki |
47 | */ |
48 | class PermissionManager |
49 | { |
50 | use AuthorizationServiceAwareTrait; |
51 | |
52 | /** |
53 | * List config |
54 | * |
55 | * @var array |
56 | */ |
57 | protected $config; |
58 | |
59 | /** |
60 | * Constructor |
61 | * |
62 | * @param array $config configuration |
63 | */ |
64 | public function __construct(array $config) |
65 | { |
66 | $this->config = $config; |
67 | } |
68 | |
69 | /** |
70 | * Determine if the user is authorized in a certain context or not |
71 | * |
72 | * @param string $permission Permission |
73 | * @param mixed $context Context for the permission behavior (optional) |
74 | * |
75 | * @return bool |
76 | */ |
77 | public function isAuthorized($permission, $context = null) |
78 | { |
79 | $authService = $this->getAuthorizationService(); |
80 | |
81 | // if no authorization service is available return false |
82 | if (!$authService) { |
83 | return false; |
84 | } |
85 | |
86 | if ($authService->isGranted($permission, $context)) { |
87 | return true; |
88 | } |
89 | |
90 | return false; |
91 | } |
92 | |
93 | /** |
94 | * Get a list of all configured permissions. |
95 | * |
96 | * @return string[] |
97 | */ |
98 | public function getAllConfiguredPermissions(): array |
99 | { |
100 | $permissions = []; |
101 | foreach ($this->config as $value) { |
102 | $permissions = array_merge($permissions, (array)($value['permission'] ?? [])); |
103 | } |
104 | return array_values(array_unique($permissions)); |
105 | } |
106 | |
107 | /** |
108 | * Check if a permission rule exists |
109 | * |
110 | * @param string $permission Permission |
111 | * |
112 | * @return bool |
113 | */ |
114 | public function permissionRuleExists($permission) |
115 | { |
116 | foreach ($this->config as $value) { |
117 | if (!isset($value['permission'])) { |
118 | continue; |
119 | } |
120 | if ($value['permission'] == $permission) { |
121 | return true; |
122 | } |
123 | if ( |
124 | is_array($value['permission']) |
125 | && in_array($permission, $value['permission']) |
126 | ) { |
127 | return true; |
128 | } |
129 | } |
130 | return false; |
131 | } |
132 | } |