1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Del\Passport; |
4
|
|
|
|
5
|
|
|
use Del\Passport\Passport; |
6
|
|
|
use Del\Passport\Entity\PassportRole; |
7
|
|
|
use Del\Passport\Entity\Role; |
8
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
9
|
|
|
use Doctrine\Common\Collections\Collection; |
10
|
|
|
use Doctrine\ORM\EntityManager; |
11
|
|
|
|
12
|
|
|
class PassportControl |
13
|
|
|
{ |
14
|
3 |
|
public function __construct( |
15
|
|
|
private EntityManager $entityManager |
16
|
|
|
) |
17
|
3 |
|
{} |
18
|
|
|
|
19
|
1 |
|
public function isAuthorized(PassportInterface $passport, ResourceInterface $resource, string $roleName): bool |
20
|
|
|
{ |
21
|
1 |
|
$entitlements = $passport->getEntitlements(); |
22
|
|
|
|
23
|
1 |
|
foreach ($entitlements as $passportRole) { |
24
|
1 |
|
$role = $passportRole->getRole(); |
25
|
1 |
|
codecept_debug($role->getRoleName()); |
26
|
1 |
|
codecept_debug($passportRole->getEntityId()); |
27
|
1 |
|
codecept_debug('resource ' . $resource->getResourceId()); |
28
|
1 |
|
codecept_debug($resource->getResourceType()); |
29
|
|
|
|
30
|
1 |
|
if ($role->getRoleName() === $roleName |
31
|
1 |
|
&& $role->getClass() === $resource->getResourceType() |
32
|
1 |
|
&& $passportRole->getEntityId() === $resource->getResourceId() |
33
|
|
|
) { |
34
|
1 |
|
return true; |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
1 |
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function grantEntitlement(PassportInterface $passport, RoleInterface $role, ResourceInterface $resource = null): bool |
43
|
|
|
{ |
44
|
1 |
|
$userId = $passport->getUserId(); |
45
|
1 |
|
$resource ? $resourceId = $resource->getResourceId() : null; |
46
|
1 |
|
$entitlement = new PassportRole(); |
47
|
1 |
|
$entitlement->setUserId($userId); |
48
|
1 |
|
$entitlement->setRole($role); |
49
|
1 |
|
$resourceId ? $entitlement->setEntityId($resourceId) : null; |
|
|
|
|
50
|
1 |
|
$this->entityManager->persist($entitlement); |
51
|
1 |
|
$this->entityManager->flush(); |
52
|
1 |
|
$passport->getEntitlements()->add($entitlement); |
53
|
|
|
|
54
|
1 |
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
public function revokeEntitlement(PassportRole $passportRole): bool |
58
|
|
|
{ |
59
|
1 |
|
$this->entityManager->remove($passportRole); |
60
|
1 |
|
$this->entityManager->flush(); |
61
|
|
|
|
62
|
1 |
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
2 |
|
public function createNewRole(RoleInterface $role): RoleInterface |
66
|
|
|
{ |
67
|
2 |
|
$this->entityManager->persist($role); |
68
|
2 |
|
$this->entityManager->flush(); |
69
|
|
|
|
70
|
2 |
|
return $role; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function removeRole(RoleInterface $role): void |
74
|
|
|
{ |
75
|
2 |
|
$this->entityManager->remove($role); |
76
|
2 |
|
$this->entityManager->flush(); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
public function findRole(string $name): ?Role |
80
|
|
|
{ |
81
|
1 |
|
return $this->entityManager->getRepository(Role::class)->findOneBy([ |
82
|
1 |
|
'roleName' => $name, |
83
|
1 |
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
public function findUserPassport(int $userId): Passport |
87
|
|
|
{ |
88
|
1 |
|
$roles = $this->entityManager->getRepository(PassportRole::class)->findBy([ |
89
|
1 |
|
'userId' => $userId, |
90
|
1 |
|
]); |
91
|
|
|
|
92
|
1 |
|
return new Passport($userId, new ArrayCollection($roles)); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function hasPassportRole(PassportInterface $passport, string $roleName, ?int $entityId = null): bool |
96
|
|
|
{ |
97
|
1 |
|
foreach ($passport->getEntitlements() as $entitlement) { |
98
|
1 |
|
if ($entitlement->getRole()->getRoleName() === $roleName && $entitlement->getEntityId() === $entityId) { |
99
|
1 |
|
return true; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|