1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Potievdev\SlimRbac\Component; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Exception\UniqueConstraintViolationException; |
6
|
|
|
use Doctrine\ORM\EntityManager; |
7
|
|
|
use Doctrine\ORM\OptimisticLockException; |
8
|
|
|
use Potievdev\SlimRbac\Exception\DatabaseException; |
9
|
|
|
use Potievdev\SlimRbac\Exception\InvalidArgumentException; |
10
|
|
|
use Potievdev\SlimRbac\Helper\ValidatorHelper; |
11
|
|
|
use Potievdev\SlimRbac\Models\RepositoryRegistry; |
12
|
|
|
use Potievdev\SlimRbac\Structure\AuthOptions; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class BaseComponent |
16
|
|
|
* @package Potievdev\SlimRbac\Component |
17
|
|
|
*/ |
18
|
|
|
class BaseComponent |
19
|
|
|
{ |
20
|
|
|
/** @var AuthOptions $authOptions */ |
21
|
|
|
protected $authOptions; |
22
|
|
|
|
23
|
|
|
/** @var EntityManager $entityManager */ |
24
|
|
|
protected $entityManager; |
25
|
|
|
|
26
|
|
|
/** @var RepositoryRegistry $repositoryRegistry */ |
27
|
|
|
protected $repositoryRegistry; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* AuthManager constructor. |
31
|
|
|
* @param AuthOptions $authOptions |
32
|
|
|
*/ |
33
|
|
|
public function __construct(AuthOptions $authOptions) |
34
|
|
|
{ |
35
|
|
|
$this->authOptions = $authOptions; |
36
|
|
|
$this->entityManager = $authOptions->getEntityManager(); |
37
|
|
|
$this->repositoryRegistry = new RepositoryRegistry($this->entityManager); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Insert or update entity |
42
|
|
|
* @param object $entity |
43
|
|
|
* @return object |
44
|
|
|
* @throws DatabaseException |
45
|
|
|
* @throws UniqueConstraintViolationException |
46
|
|
|
*/ |
47
|
|
|
protected function saveEntity($entity) |
48
|
|
|
{ |
49
|
|
|
try { |
50
|
|
|
$this->entityManager->persist($entity); |
51
|
|
|
$this->entityManager->flush($entity); |
52
|
|
|
return $entity; |
53
|
|
|
} catch (OptimisticLockException $e) { |
54
|
|
|
throw new DatabaseException($e->getMessage()); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Checks access status |
60
|
|
|
* @param integer $userId |
61
|
|
|
* @param string $permissionName |
62
|
|
|
* @return bool |
63
|
|
|
* @throws \Exception |
64
|
|
|
*/ |
65
|
|
|
public function checkAccess($userId, $permissionName) |
66
|
|
|
{ |
67
|
|
|
if (ValidatorHelper::isInteger($userId) == false) { |
|
|
|
|
68
|
|
|
throw new InvalidArgumentException('User identifier must be number.'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @var integer $permissionId */ |
72
|
|
|
$permissionId = $this->repositoryRegistry |
73
|
|
|
->getPermissionRepository() |
74
|
|
|
->getPermissionIdByName($permissionName); |
75
|
|
|
|
76
|
|
|
if (ValidatorHelper::isInteger($permissionId)) { |
77
|
|
|
|
78
|
|
|
/** @var integer[] $rootRoleIds */ |
79
|
|
|
$rootRoleIds = $this->repositoryRegistry |
80
|
|
|
->getUserRoleRepository() |
81
|
|
|
->getUserRoleIds($userId); |
82
|
|
|
|
83
|
|
|
if (count($rootRoleIds) > 0) { |
84
|
|
|
|
85
|
|
|
/** @var integer[] $allRoleIds */ |
86
|
|
|
$allRoleIds = $this->repositoryRegistry |
87
|
|
|
->getRoleHierarchyRepository() |
88
|
|
|
->getAllRoleIdsHierarchy($rootRoleIds); |
89
|
|
|
|
90
|
|
|
return $this->repositoryRegistry |
91
|
|
|
->getRolePermissionRepository() |
92
|
|
|
->isPermissionAssigned($permissionId, $allRoleIds); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.