delboy1978uk /
passport
| 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 | |||
| 26 | 1 | if ($role->getRoleName() === 'superuser' || ($role->getRoleName() === $roleName |
|
| 27 | 1 | && $role->getClass() === $resource->getResourceType() |
|
| 28 | 1 | && $passportRole->getEntityId() === $resource->getResourceId() |
|
| 29 | )) { |
||
| 30 | 1 | return true; |
|
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | |||
| 35 | 1 | return false; |
|
| 36 | } |
||
| 37 | |||
| 38 | 1 | public function grantEntitlement( |
|
| 39 | PassportInterface $passport, |
||
| 40 | RoleInterface $role, |
||
| 41 | ResourceInterface $resource = null, |
||
| 42 | int $approvedBy = null, |
||
| 43 | ): PassportRole { |
||
| 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; |
|
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 50 | 1 | $approvedBy ? $entitlement->setApprovedById($approvedBy) : null; |
|
| 51 | 1 | $this->entityManager->persist($entitlement); |
|
| 52 | 1 | $this->entityManager->flush(); |
|
| 53 | 1 | $passport->getEntitlements()->add($entitlement); |
|
| 54 | |||
| 55 | 1 | return $entitlement; |
|
| 56 | } |
||
| 57 | |||
| 58 | 1 | public function revokeEntitlement(PassportRole $passportRole): bool |
|
| 59 | { |
||
| 60 | 1 | $this->entityManager->remove($passportRole); |
|
| 61 | 1 | $this->entityManager->flush(); |
|
| 62 | |||
| 63 | 1 | return true; |
|
| 64 | } |
||
| 65 | |||
| 66 | 2 | public function createNewRole(RoleInterface $role): RoleInterface |
|
| 67 | { |
||
| 68 | 2 | $this->entityManager->persist($role); |
|
| 69 | 2 | $this->entityManager->flush(); |
|
| 70 | |||
| 71 | 2 | return $role; |
|
| 72 | } |
||
| 73 | |||
| 74 | 2 | public function removeRole(RoleInterface $role): void |
|
| 75 | { |
||
| 76 | 2 | $this->entityManager->remove($role); |
|
| 77 | 2 | $this->entityManager->flush(); |
|
| 78 | } |
||
| 79 | |||
| 80 | 1 | public function findRole(string $name): ?Role |
|
| 81 | { |
||
| 82 | 1 | return $this->entityManager->getRepository(Role::class)->findOneBy([ |
|
| 83 | 1 | 'roleName' => $name, |
|
| 84 | 1 | ]); |
|
| 85 | } |
||
| 86 | |||
| 87 | public function findPassportRoles(Role $role, int $entityId): array |
||
| 88 | { |
||
| 89 | return $this->entityManager->getRepository(PassportRole::class)->findBy([ |
||
| 90 | 'role' => $role, |
||
| 91 | 'entityId' => $entityId, |
||
| 92 | ]); |
||
| 93 | } |
||
| 94 | |||
| 95 | 1 | public function findUserPassport(int $userId): Passport |
|
| 96 | { |
||
| 97 | 1 | $roles = $this->entityManager->getRepository(PassportRole::class)->findBy([ |
|
| 98 | 1 | 'userId' => $userId, |
|
| 99 | 1 | ]); |
|
| 100 | |||
| 101 | 1 | return new Passport($userId, new ArrayCollection($roles)); |
|
| 102 | } |
||
| 103 | |||
| 104 | 1 | public function hasPassportRole(PassportInterface $passport, string $roleName, ?int $entityId = null): bool |
|
| 105 | { |
||
| 106 | 1 | foreach ($passport->getEntitlements() as $entitlement) { |
|
| 107 | 1 | if ($entitlement->getRole()->getRoleName() === $roleName && $entitlement->getEntityId() === $entityId) { |
|
| 108 | 1 | return true; |
|
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | return false; |
||
| 113 | } |
||
| 114 | } |
||
| 115 |