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