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\Collection; |
9
|
|
|
use Doctrine\ORM\EntityManager; |
10
|
|
|
|
11
|
|
|
class PassportControl |
12
|
|
|
{ |
13
|
|
|
/** @var EntityManager */ |
14
|
|
|
private $entityManager; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* PassportControl constructor. |
18
|
|
|
* @param EntityManager $entityManager |
19
|
|
|
*/ |
20
|
|
|
public function __construct(EntityManager $entityManager) |
21
|
|
|
{ |
22
|
|
|
$this->entityManager = $entityManager; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param PassportInterface $passport |
27
|
|
|
* @param ResourceInterface $resource |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
|
|
public function isAuthorized(PassportInterface $passport, ResourceInterface $resource): bool |
|
|
|
|
31
|
|
|
{ |
32
|
|
|
return true; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param PassportInterface $passport |
37
|
|
|
* @param RoleInterface $role |
38
|
|
|
* @param ResourceInterface $resource |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
public function grantEntitlement(PassportInterface $passport, RoleInterface $role, ResourceInterface $resource = null): bool |
42
|
|
|
{ |
43
|
|
|
$userId = $passport->getUserId(); |
44
|
|
|
$resource ? $resourceId = $resource->getResourceId() : null; |
45
|
|
|
$entitlement = new PassportRole(); |
46
|
|
|
$entitlement->setUserId($userId); |
47
|
|
|
$entitlement->setRole($role); |
|
|
|
|
48
|
|
|
$resourceId ? $entitlement->setEntityId($resourceId) : null; |
|
|
|
|
49
|
|
|
$this->entityManager->persist($entitlement); |
50
|
|
|
$this->entityManager->flush($entitlement); |
51
|
|
|
|
52
|
|
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param PassportInterface $passport |
57
|
|
|
* @param RoleInterface $role |
58
|
|
|
* @param ResourceInterface $resource |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function revokeEntitlement(PassportInterface $passport, RoleInterface $role, ResourceInterface $resource): bool |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param int $userId |
|
|
|
|
68
|
|
|
* @return PassportInterface |
69
|
|
|
*/ |
70
|
|
|
public function createNewRole(RoleInterface $role): RoleInterface |
71
|
|
|
{ |
72
|
|
|
$this->entityManager->persist($role); |
73
|
|
|
$this->entityManager->flush($role); |
74
|
|
|
|
75
|
|
|
return $role; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param int $userId |
|
|
|
|
80
|
|
|
* @return PassportInterface |
81
|
|
|
*/ |
82
|
|
|
public function removeRole(RoleInterface $role): void |
83
|
|
|
{ |
84
|
|
|
$this->entityManager->remove($role); |
85
|
|
|
$this->entityManager->flush($role); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param string $name |
90
|
|
|
* @return Role|null |
91
|
|
|
*/ |
92
|
|
|
public function findRole(string $name): ?Role |
93
|
|
|
{ |
94
|
|
|
return $this->entityManager->getRepository(Role::class)->findOneBy([ |
95
|
|
|
'roleName' => $name, |
96
|
|
|
]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $name |
101
|
|
|
* @param int $id |
102
|
|
|
* @return PassportRole|null |
103
|
|
|
*/ |
104
|
|
|
public function findPassportRole(string $name, int $id, int $entityId = null): ?PassportRole |
105
|
|
|
{ |
106
|
|
|
$criteria = [ |
107
|
|
|
'role' => $name, |
108
|
|
|
'userId' => $id, |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
if ($entityId) { |
|
|
|
|
112
|
|
|
$criteria['entityId'] = $entityId; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $this->entityManager->getRepository(PassportRole::class)->findOneBy($criteria); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param int $id |
120
|
|
|
* @return Passport |
121
|
|
|
*/ |
122
|
|
|
public function findUserPassport(int $id): Passport |
123
|
|
|
{ |
124
|
|
|
$roles = $this->entityManager->getRepository(PassportRole::class)->findBy([ |
125
|
|
|
'userId' => $id, |
126
|
|
|
]); |
127
|
|
|
|
128
|
|
|
return new Passport($id, $roles); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.