|
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\User; |
|
34
|
|
|
use App\Security\Interfaces\HasPermissionsInterface; |
|
35
|
|
|
use Psr\Container\ContainerInterface; |
|
36
|
|
|
use Symfony\Component\Config\ConfigCache; |
|
37
|
|
|
use Symfony\Component\Config\Definition\Processor; |
|
38
|
|
|
use Symfony\Component\Config\Resource\FileResource; |
|
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->getPermissionStructure(); |
|
64
|
|
|
|
|
65
|
|
|
//dump($this->permission_structure); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
protected function getPermissionStructure() |
|
69
|
|
|
{ |
|
70
|
|
|
|
|
71
|
|
|
$cache = new ConfigCache($this->cache_file, $this->is_debug); |
|
72
|
|
|
|
|
73
|
|
|
//Check if the cache is fresh, else regenerate it. |
|
74
|
|
|
if (!$cache->isFresh()) { |
|
75
|
|
|
$permission_file = __DIR__.'/../../config/permissions.yaml'; |
|
76
|
|
|
|
|
77
|
|
|
//Read the permission config file... |
|
78
|
|
|
$config = Yaml::parse( |
|
79
|
|
|
file_get_contents($permission_file) |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
$configs = [$config]; |
|
83
|
|
|
|
|
84
|
|
|
//... And parse it |
|
85
|
|
|
$processor = new Processor(); |
|
86
|
|
|
$databaseConfiguration = new PermissionsConfiguration(); |
|
87
|
|
|
$processedConfiguration = $processor->processConfiguration( |
|
88
|
|
|
$databaseConfiguration, |
|
89
|
|
|
$configs |
|
90
|
|
|
); |
|
91
|
|
|
|
|
92
|
|
|
//Permission file is our file resource (it is used to invalidate cache) |
|
93
|
|
|
$resources = array(); |
|
94
|
|
|
$resources[] = new FileResource($permission_file); |
|
95
|
|
|
|
|
96
|
|
|
//Var export the structure and write it to cache file. |
|
97
|
|
|
$cache->write( |
|
98
|
|
|
sprintf('<?php return %s;', var_export($processedConfiguration, true)), |
|
99
|
|
|
$resources); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
//In the most cases we just need to dump the cached PHP file. |
|
103
|
|
|
return require $this->cache_file; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Check if a user/group is allowed to do the specified operation for the permission. |
|
109
|
|
|
* |
|
110
|
|
|
* See permissions.yaml for valid permission operation combinations. |
|
111
|
|
|
* |
|
112
|
|
|
* @param HasPermissionsInterface $user the user/group for which the operation should be checked |
|
113
|
|
|
* @param string $permission the name of the permission for which should be checked |
|
114
|
|
|
* @param string $operation the name of the operation for which should be checked |
|
115
|
|
|
* |
|
116
|
|
|
* @return bool|null true, if the user is allowed to do the operation (ALLOW), false if not (DISALLOW), and null, |
|
117
|
|
|
* if the value is set to inherit |
|
118
|
|
|
*/ |
|
119
|
|
|
public function dontInherit(HasPermissionsInterface $user, string $permission, string $operation): ?bool |
|
120
|
|
|
{ |
|
121
|
|
|
//Get the permissions from the user |
|
122
|
|
|
$perm_list = $user->getPermissions(); |
|
123
|
|
|
|
|
124
|
|
|
//Determine bit number using our configuration |
|
125
|
|
|
$bit = $this->permission_structure['perms'][$permission]['operations'][$operation]['bit']; |
|
126
|
|
|
|
|
127
|
|
|
return $perm_list->getPermissionValue($permission, $bit); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Checks if a user is allowed to do the specified operation for the permission. |
|
132
|
|
|
* In contrast to dontInherit() it tries to resolve the inherit values, of the user, by going upwards in the |
|
133
|
|
|
* hierachy (user -> group -> parent group -> so on). But even in this case it is possible, that the inherit value |
|
134
|
|
|
* could be resolved, and this function returns null. |
|
135
|
|
|
* |
|
136
|
|
|
* In that case the voter should set it manually to false by using ?? false. |
|
137
|
|
|
* |
|
138
|
|
|
* @param User $user the user for which the operation should be checked |
|
139
|
|
|
* @param string $permission the name of the permission for which should be checked |
|
140
|
|
|
* @param string $operation the name of the operation for which should be checked |
|
141
|
|
|
* |
|
142
|
|
|
* @return bool|null true, if the user is allowed to do the operation (ALLOW), false if not (DISALLOW), and null, |
|
143
|
|
|
* if the value is set to inherit |
|
144
|
|
|
*/ |
|
145
|
|
|
public function inherit(User $user, string $permission, string $operation): ?bool |
|
146
|
|
|
{ |
|
147
|
|
|
//Check if we need to inherit |
|
148
|
|
|
$allowed = $this->dontInherit($user, $permission, $operation); |
|
149
|
|
|
|
|
150
|
|
|
if (null !== $allowed) { |
|
151
|
|
|
//Just return the value of the user. |
|
152
|
|
|
return $allowed; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$parent = $user->getGroup(); |
|
156
|
|
|
while (null != $parent) { //The top group, has parent == null |
|
157
|
|
|
//Check if our current element gives a info about disallow/allow |
|
158
|
|
|
$allowed = $this->dontInherit($parent, $permission, $operation); |
|
159
|
|
|
if (null !== $allowed) { |
|
160
|
|
|
return $allowed; |
|
161
|
|
|
} |
|
162
|
|
|
//Else go up in the hierachy. |
|
163
|
|
|
$parent = $parent->getParent(); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
return null; //The inherited value is never resolved. Should be treat as false, in Voters. |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Lists the names of all operations that is supported for the given permission. |
|
171
|
|
|
* |
|
172
|
|
|
* If the Permission is not existing at all, a exception is thrown. |
|
173
|
|
|
* |
|
174
|
|
|
* This function is useful for the support() function of the voters. |
|
175
|
|
|
* |
|
176
|
|
|
* @param string $permission The permission for which the |
|
177
|
|
|
* |
|
178
|
|
|
* @return string[] A list of all operations that are supported by the given |
|
179
|
|
|
*/ |
|
180
|
|
|
public function listOperationsForPermission(string $permission): array |
|
181
|
|
|
{ |
|
182
|
|
|
$operations = $this->permission_structure['perms'][$permission]['operations']; |
|
183
|
|
|
|
|
184
|
|
|
return array_keys($operations); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Checks if the permission with the given name is existing. |
|
189
|
|
|
* |
|
190
|
|
|
* @param string $permission the name of the permission which we want to check |
|
191
|
|
|
* |
|
192
|
|
|
* @return bool True if a perm with that name is existing. False if not. |
|
193
|
|
|
*/ |
|
194
|
|
|
public function isValidPermission(string $permission): bool |
|
195
|
|
|
{ |
|
196
|
|
|
return isset($this->permission_structure['perms'][$permission]); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Checks if the permission operation combination with the given names is existing. |
|
201
|
|
|
* |
|
202
|
|
|
* @param string $permission the name of the permission which should be checked |
|
203
|
|
|
* @param string $operation the name of the operation which should be checked |
|
204
|
|
|
* |
|
205
|
|
|
* @return bool true if the given permission operation combination is existing |
|
206
|
|
|
*/ |
|
207
|
|
|
public function isValidOperation(string $permission, string $operation): bool |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->isValidPermission($permission) && |
|
210
|
|
|
isset($this->permission_structure['perms'][$permission]['operations'][$operation]); |
|
211
|
|
|
} |
|
212
|
|
|
} |
|
213
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.