1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* part-db version 0.1 |
4
|
|
|
* Copyright (C) 2005 Christoph Lechner |
5
|
|
|
* http://www.cl-projects.de/. |
6
|
|
|
* |
7
|
|
|
* part-db version 0.2+ |
8
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
9
|
|
|
* http://code.google.com/p/part-db/ |
10
|
|
|
* |
11
|
|
|
* Part-DB Version 0.4+ |
12
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
13
|
|
|
* https://github.com/jbtronics |
14
|
|
|
* |
15
|
|
|
* This program is free software; you can redistribute it and/or |
16
|
|
|
* modify it under the terms of the GNU General Public License |
17
|
|
|
* as published by the Free Software Foundation; either version 2 |
18
|
|
|
* of the License, or (at your option) any later version. |
19
|
|
|
* |
20
|
|
|
* This program is distributed in the hope that it will be useful, |
21
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
22
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23
|
|
|
* GNU General Public License for more details. |
24
|
|
|
* |
25
|
|
|
* You should have received a copy of the GNU General Public License |
26
|
|
|
* along with this program; if not, write to the Free Software |
27
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
namespace App\Services; |
31
|
|
|
|
32
|
|
|
use App\Configuration\PermissionsConfiguration; |
33
|
|
|
use App\Entity\UserSystem\User; |
34
|
|
|
use App\Security\Interfaces\HasPermissionsInterface; |
35
|
|
|
use Symfony\Component\Config\ConfigCache; |
36
|
|
|
use Symfony\Component\Config\Definition\Processor; |
37
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
38
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
39
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; |
40
|
|
|
use Symfony\Component\Yaml\Yaml; |
41
|
|
|
|
42
|
|
|
class PermissionResolver |
43
|
|
|
{ |
44
|
|
|
protected $permission_structure; |
45
|
|
|
|
46
|
|
|
protected $is_debug; |
47
|
|
|
protected $cache_file; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* PermissionResolver constructor. |
51
|
|
|
* |
52
|
|
|
* @param ParameterBagInterface $params |
53
|
|
|
*/ |
54
|
|
|
public function __construct(ParameterBagInterface $params, ContainerInterface $container) |
|
|
|
|
55
|
|
|
{ |
56
|
|
|
$cache_dir = $container->getParameter('kernel.cache_dir'); |
57
|
|
|
//Here the cached structure will be saved. |
58
|
|
|
$this->cache_file = $cache_dir.'/permissions.php.cache'; |
59
|
|
|
$this->is_debug = $container->getParameter('kernel.debug'); |
60
|
|
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
$this->permission_structure = $this->generatePermissionStructure(); |
64
|
|
|
|
65
|
|
|
//dump($this->permission_structure); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getPermissionStructure() : array |
69
|
|
|
{ |
70
|
|
|
return $this->permission_structure; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
protected function generatePermissionStructure() |
74
|
|
|
{ |
75
|
|
|
|
76
|
|
|
$cache = new ConfigCache($this->cache_file, $this->is_debug); |
77
|
|
|
|
78
|
|
|
//Check if the cache is fresh, else regenerate it. |
79
|
|
|
if (!$cache->isFresh()) { |
80
|
|
|
$permission_file = __DIR__.'/../../config/permissions.yaml'; |
81
|
|
|
|
82
|
|
|
//Read the permission config file... |
83
|
|
|
$config = Yaml::parse( |
84
|
|
|
file_get_contents($permission_file) |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$configs = [$config]; |
88
|
|
|
|
89
|
|
|
//... And parse it |
90
|
|
|
$processor = new Processor(); |
91
|
|
|
$databaseConfiguration = new PermissionsConfiguration(); |
92
|
|
|
$processedConfiguration = $processor->processConfiguration( |
93
|
|
|
$databaseConfiguration, |
94
|
|
|
$configs |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
//Permission file is our file resource (it is used to invalidate cache) |
98
|
|
|
$resources = array(); |
99
|
|
|
$resources[] = new FileResource($permission_file); |
100
|
|
|
|
101
|
|
|
//Var export the structure and write it to cache file. |
102
|
|
|
$cache->write( |
103
|
|
|
sprintf('<?php return %s;', var_export($processedConfiguration, true)), |
104
|
|
|
$resources); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
//In the most cases we just need to dump the cached PHP file. |
108
|
|
|
return require $this->cache_file; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Check if a user/group is allowed to do the specified operation for the permission. |
114
|
|
|
* |
115
|
|
|
* See permissions.yaml for valid permission operation combinations. |
116
|
|
|
* |
117
|
|
|
* @param HasPermissionsInterface $user the user/group for which the operation should be checked |
118
|
|
|
* @param string $permission the name of the permission for which should be checked |
119
|
|
|
* @param string $operation the name of the operation for which should be checked |
120
|
|
|
* |
121
|
|
|
* @return bool|null true, if the user is allowed to do the operation (ALLOW), false if not (DISALLOW), and null, |
122
|
|
|
* if the value is set to inherit |
123
|
|
|
*/ |
124
|
|
|
public function dontInherit(HasPermissionsInterface $user, string $permission, string $operation): ?bool |
125
|
|
|
{ |
126
|
|
|
//Get the permissions from the user |
127
|
|
|
$perm_list = $user->getPermissions(); |
128
|
|
|
|
129
|
|
|
//Determine bit number using our configuration |
130
|
|
|
$bit = $this->permission_structure['perms'][$permission]['operations'][$operation]['bit']; |
131
|
|
|
|
132
|
|
|
return $perm_list->getPermissionValue($permission, $bit); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Checks if a user is allowed to do the specified operation for the permission. |
137
|
|
|
* In contrast to dontInherit() it tries to resolve the inherit values, of the user, by going upwards in the |
138
|
|
|
* hierachy (user -> group -> parent group -> so on). But even in this case it is possible, that the inherit value |
139
|
|
|
* could be resolved, and this function returns null. |
140
|
|
|
* |
141
|
|
|
* In that case the voter should set it manually to false by using ?? false. |
142
|
|
|
* |
143
|
|
|
* @param User $user the user for which the operation should be checked |
144
|
|
|
* @param string $permission the name of the permission for which should be checked |
145
|
|
|
* @param string $operation the name of the operation for which should be checked |
146
|
|
|
* |
147
|
|
|
* @return bool|null true, if the user is allowed to do the operation (ALLOW), false if not (DISALLOW), and null, |
148
|
|
|
* if the value is set to inherit |
149
|
|
|
*/ |
150
|
|
|
public function inherit(User $user, string $permission, string $operation): ?bool |
151
|
|
|
{ |
152
|
|
|
//Check if we need to inherit |
153
|
|
|
$allowed = $this->dontInherit($user, $permission, $operation); |
154
|
|
|
|
155
|
|
|
if (null !== $allowed) { |
156
|
|
|
//Just return the value of the user. |
157
|
|
|
return $allowed; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
$parent = $user->getGroup(); |
161
|
|
|
while (null != $parent) { //The top group, has parent == null |
162
|
|
|
//Check if our current element gives a info about disallow/allow |
163
|
|
|
$allowed = $this->dontInherit($parent, $permission, $operation); |
164
|
|
|
if (null !== $allowed) { |
165
|
|
|
return $allowed; |
166
|
|
|
} |
167
|
|
|
//Else go up in the hierachy. |
168
|
|
|
$parent = $parent->getParent(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return null; //The inherited value is never resolved. Should be treat as false, in Voters. |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Sets the new value for the operation |
176
|
|
|
* @param HasPermissionsInterface $user The user or group for which the value should be changed. |
177
|
|
|
* @param string $permission The name of the permission that should be changed. |
178
|
|
|
* @param string $operation The name of the operation that should be changed. |
179
|
|
|
* @param bool|null $new_val The new value for the permission. true = ALLOW, false = DISALLOW, null = INHERIT |
180
|
|
|
*/ |
181
|
|
|
public function setPermission(HasPermissionsInterface $user, string $permission, string $operation, ?bool $new_val) : void |
182
|
|
|
{ |
183
|
|
|
//Get the permissions from the user |
184
|
|
|
$perm_list = $user->getPermissions(); |
185
|
|
|
|
186
|
|
|
//Determine bit number using our configuration |
187
|
|
|
$bit = $this->permission_structure['perms'][$permission]['operations'][$operation]['bit']; |
188
|
|
|
|
189
|
|
|
$perm_list->setPermissionValue($permission, $bit, $new_val); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Lists the names of all operations that is supported for the given permission. |
194
|
|
|
* |
195
|
|
|
* If the Permission is not existing at all, a exception is thrown. |
196
|
|
|
* |
197
|
|
|
* This function is useful for the support() function of the voters. |
198
|
|
|
* |
199
|
|
|
* @param string $permission The permission for which the |
200
|
|
|
* |
201
|
|
|
* @return string[] A list of all operations that are supported by the given |
202
|
|
|
*/ |
203
|
|
|
public function listOperationsForPermission(string $permission): array |
204
|
|
|
{ |
205
|
|
|
if(!$this->isValidPermission($permission)) { |
206
|
|
|
throw new \InvalidArgumentException(sprintf('A permission with that name is not existing! Got %s.', $permission)); |
207
|
|
|
} |
208
|
|
|
$operations = $this->permission_structure['perms'][$permission]['operations']; |
209
|
|
|
|
210
|
|
|
|
211
|
|
|
return array_keys($operations); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Checks if the permission with the given name is existing. |
216
|
|
|
* |
217
|
|
|
* @param string $permission the name of the permission which we want to check |
218
|
|
|
* |
219
|
|
|
* @return bool True if a perm with that name is existing. False if not. |
220
|
|
|
*/ |
221
|
|
|
public function isValidPermission(string $permission): bool |
222
|
|
|
{ |
223
|
|
|
return isset($this->permission_structure['perms'][$permission]); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* Checks if the permission operation combination with the given names is existing. |
228
|
|
|
* |
229
|
|
|
* @param string $permission the name of the permission which should be checked |
230
|
|
|
* @param string $operation the name of the operation which should be checked |
231
|
|
|
* |
232
|
|
|
* @return bool true if the given permission operation combination is existing |
233
|
|
|
*/ |
234
|
|
|
public function isValidOperation(string $permission, string $operation): bool |
235
|
|
|
{ |
236
|
|
|
return $this->isValidPermission($permission) && |
237
|
|
|
isset($this->permission_structure['perms'][$permission]['operations'][$operation]); |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.