-
Notifications
You must be signed in to change notification settings - Fork 189
/
PermissionAdmin.php
369 lines (325 loc) · 11.3 KB
/
PermissionAdmin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
namespace Kunstmaan\AdminBundle\Helper\Security\Acl\Permission;
use Doctrine\ORM\EntityManager;
use Kunstmaan\AdminBundle\Entity\AbstractEntity;
use Kunstmaan\AdminBundle\Entity\AclChangeset;
use Kunstmaan\AdminBundle\Entity\BaseUser;
use Kunstmaan\AdminBundle\Entity\Role;
use Kunstmaan\UtilitiesBundle\Helper\Shell\Shell;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Security\Acl\Domain\RoleSecurityIdentity;
use Symfony\Component\Security\Acl\Exception\AclNotFoundException;
use Symfony\Component\Security\Acl\Model\AclInterface;
use Symfony\Component\Security\Acl\Model\AclProviderInterface;
use Symfony\Component\Security\Acl\Model\AuditableEntryInterface;
use Symfony\Component\Security\Acl\Model\MutableAclInterface;
use Symfony\Component\Security\Acl\Model\MutableAclProviderInterface;
use Symfony\Component\Security\Acl\Model\ObjectIdentityRetrievalStrategyInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Role\RoleInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Helper to manage the permissions on a certain entity
*/
class PermissionAdmin
{
const ADD = 'ADD';
const DELETE = 'DEL';
/**
* @var AbstractEntity
*/
protected $resource = null;
/**
* @var EntityManager
*/
protected $em = null;
/**
* @var TokenStorageInterface
*/
protected $tokenStorage = null;
/**
* @var MutableAclProviderInterface
*/
protected $aclProvider = null;
/**
* @var ObjectIdentityRetrievalStrategyInterface
*/
protected $oidRetrievalStrategy = null;
/**
* @var PermissionMap
*/
protected $permissionMap = null;
/**
* @var array
*/
protected $permissions = null;
/**
* @var EventDispatcherInterface
*/
protected $eventDispatcher = null;
/**
* @var KernelInterface
*/
protected $kernel;
/**
* @var Shell
*/
protected $shellHelper;
/**
* @param EntityManager $em The EntityManager
* @param TokenStorageInterface $tokenStorage The token storage
* @param AclProviderInterface $aclProvider The ACL provider
* @param ObjectIdentityRetrievalStrategyInterface $oidRetrievalStrategy The object retrieval strategy
* @param EventDispatcherInterface $eventDispatcher The event dispatcher
* @param Shell $shellHelper The shell helper
* @param KernelInterface $kernel The kernel
*/
public function __construct(
EntityManager $em,
TokenStorageInterface $tokenStorage,
AclProviderInterface $aclProvider,
ObjectIdentityRetrievalStrategyInterface $oidRetrievalStrategy,
EventDispatcherInterface $eventDispatcher,
Shell $shellHelper,
KernelInterface $kernel
) {
$this->em = $em;
$this->tokenStorage = $tokenStorage;
$this->aclProvider = $aclProvider;
$this->oidRetrievalStrategy = $oidRetrievalStrategy;
$this->eventDispatcher = $eventDispatcher;
$this->shellHelper = $shellHelper;
$this->kernel = $kernel;
}
/**
* Initialize permission admin with specified entity.
*
* @param AbstractEntity $resource The object which has the permissions
* @param PermissionMapInterface $permissionMap The permission map to use
*/
public function initialize(AbstractEntity $resource, PermissionMapInterface $permissionMap)
{
$this->resource = $resource;
$this->permissionMap = $permissionMap;
$this->permissions = [];
// Init permissions
try {
$objectIdentity = $this->oidRetrievalStrategy->getObjectIdentity($this->resource);
/* @var $acl AclInterface */
$acl = $this->aclProvider->findAcl($objectIdentity);
$objectAces = $acl->getObjectAces();
/* @var $ace AuditableEntryInterface */
foreach ($objectAces as $ace) {
$securityIdentity = $ace->getSecurityIdentity();
if ($securityIdentity instanceof RoleSecurityIdentity) {
$this->permissions[$securityIdentity->getRole()] = new MaskBuilder($ace->getMask());
}
}
} catch (AclNotFoundException $e) {
// No Acl found - do nothing (or should we initialize with default values here?)
}
}
/**
* Get permissions.
*
* @return MaskBuilder[]
*/
public function getPermissions()
{
return $this->permissions;
}
/**
* Get permission for specified role.
*
* @param RoleInterface|string $role
*
* @return MaskBuilder|null
*/
public function getPermission($role)
{
if ($role instanceof RoleInterface || $role instanceof \Symfony\Component\Security\Core\Role\Role) {
$role = $role->getRole();
}
if (isset($this->permissions[$role])) {
return $this->permissions[$role];
}
return null;
}
/**
* Get all roles.
*
* @return Role[]
*/
public function getAllRoles()
{
return $this->em->getRepository(Role::class)->findAll();
}
/**
* Get all manageable roles for pages
*
* @return Role[]
*/
public function getManageableRolesForPages()
{
$roles = $this->em->getRepository(Role::class)->findAll();
if (($token = $this->tokenStorage->getToken()) && ($user = $token->getUser())) {
if ($user && !$user->isSuperAdmin() && ($superAdminRole = array_keys($roles, 'ROLE_SUPER_ADMIN'))) {
$superAdminRole = current($superAdminRole);
unset($roles[$superAdminRole]);
}
}
return $roles;
}
/**
* Get possible permissions.
*
* @return array
*/
public function getPossiblePermissions()
{
return $this->permissionMap->getPossiblePermissions();
}
/**
* Handle form entry of permission changes.
*
* @return bool
*/
public function bindRequest(Request $request)
{
$changes = $request->request->get('permission-hidden-fields');
if (empty($changes)) {
return true;
}
// Just apply the changes to the current node (non recursively)
$this->applyAclChangeset($this->resource, $changes, false);
// Apply recursively (on request)
$applyRecursive = $request->request->get('applyRecursive');
if ($applyRecursive) {
// Serialize changes & store them in DB
$user = $this->tokenStorage->getToken()->getUser();
$this->createAclChangeSet($this->resource, $changes, $user);
$cmd = 'php ' . $this->kernel->getProjectDir() . '/bin/console kuma:acl:apply';
$cmd .= ' --env=' . $this->kernel->getEnvironment();
$this->shellHelper->runInBackground($cmd);
}
return true;
}
/**
* Create a new ACL changeset.
*
* @param AbstractEntity $entity The entity
* @param array $changes The changes
* @param UserInterface $user The user
*
* @return AclChangeset
*/
public function createAclChangeSet(AbstractEntity $entity, $changes, UserInterface $user)
{
$aclChangeset = new AclChangeset();
$aclChangeset->setRef($entity);
$aclChangeset->setChangeset($changes);
/* @var $user BaseUser */
$aclChangeset->setUser($user);
$this->em->persist($aclChangeset);
$this->em->flush();
return $aclChangeset;
}
/**
* Apply the specified ACL changeset.
*
* @param AbstractEntity $entity The entity
* @param array $changeset The changeset
* @param bool $recursive The recursive
*/
public function applyAclChangeset(AbstractEntity $entity, $changeset, $recursive = true)
{
if ($recursive) {
if (!method_exists($entity, 'getChildren')) {
return;
}
// Iterate over children and apply recursively
foreach ($entity->getChildren() as $child) {
$this->applyAclChangeset($child, $changeset);
}
}
// Apply ACL modifications to node
$objectIdentity = $this->oidRetrievalStrategy->getObjectIdentity($entity);
try {
/* @var $acl MutableAclInterface */
$acl = $this->aclProvider->findAcl($objectIdentity);
} catch (AclNotFoundException $e) {
/* @var $acl MutableAclInterface */
$acl = $this->aclProvider->createAcl($objectIdentity);
}
// Process permissions in changeset
foreach ($changeset as $role => $roleChanges) {
$index = $this->getObjectAceIndex($acl, $role);
$mask = 0;
if (false !== $index) {
$mask = $this->getMaskAtIndex($acl, $index);
}
foreach ($roleChanges as $type => $permissions) {
$maskChange = new MaskBuilder();
foreach ($permissions as $permission) {
$maskChange->add($permission);
}
switch ($type) {
case self::ADD:
$mask |= $maskChange->get();
break;
case self::DELETE:
$mask &= ~$maskChange->get();
break;
}
}
if (false !== $index) {
$acl->updateObjectAce($index, $mask);
} else {
$securityIdentity = new RoleSecurityIdentity($role);
$acl->insertObjectAce($securityIdentity, $mask);
}
}
$this->aclProvider->updateAcl($acl);
}
/**
* Get current object ACE index for specified role.
*
* @param AclInterface $acl The AclInterface
* @param string $role The role
*
* @return bool|int
*/
private function getObjectAceIndex(AclInterface $acl, $role)
{
$objectAces = $acl->getObjectAces();
/* @var $ace AuditableEntryInterface */
foreach ($objectAces as $index => $ace) {
$securityIdentity = $ace->getSecurityIdentity();
if (($securityIdentity instanceof RoleSecurityIdentity) && $securityIdentity->getRole() == $role) {
return $index;
}
}
return false;
}
/**
* Get object ACE mask at specified index.
*
* @param AclInterface $acl The acl interface
* @param int $index The index
*
* @return bool|int
*/
private function getMaskAtIndex(AclInterface $acl, $index)
{
$objectAces = $acl->getObjectAces();
/* @var $ace AuditableEntryInterface */
$ace = $objectAces[$index];
$securityIdentity = $ace->getSecurityIdentity();
if ($securityIdentity instanceof RoleSecurityIdentity) {
return $ace->getMask();
}
return false;
}
}