|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chubbyphp\Security\Authorization; |
|
6
|
|
|
|
|
7
|
|
|
use Chubbyphp\Security\UserInterface; |
|
8
|
|
|
use Psr\Log\LoggerInterface; |
|
9
|
|
|
use Psr\Log\NullLogger; |
|
10
|
|
|
|
|
11
|
|
|
final class RoleAuthorization implements AuthorizationInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var array |
|
15
|
|
|
*/ |
|
16
|
|
|
private $roleHierarchyResolver; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var LoggerInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private $logger; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param RoleHierarchyResolverInterface $roleHierarchyResolver |
|
25
|
|
|
*/ |
|
26
|
3 |
|
public function __construct(RoleHierarchyResolverInterface $roleHierarchyResolver, LoggerInterface $logger = null) |
|
27
|
|
|
{ |
|
28
|
3 |
|
$this->roleHierarchyResolver = $roleHierarchyResolver; |
|
|
|
|
|
|
29
|
3 |
|
$this->logger = $logger ?? new NullLogger(); |
|
30
|
3 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param UserInterface $user |
|
34
|
|
|
* @param mixed $attributes |
|
35
|
|
|
* @param OwnedByUserModelInterface|null $model |
|
36
|
|
|
* |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
3 |
|
public function isGranted(UserInterface $user, $attributes, OwnedByUserModelInterface $model = null): bool |
|
40
|
|
|
{ |
|
41
|
3 |
|
if (null !== $model && $user->getId() !== $model->getOwnedByUserId()) { |
|
42
|
1 |
|
$this->logger->info( |
|
43
|
1 |
|
'security.authorization.role: user and model owner are not the same {userId}, {ownerByUserId}', |
|
44
|
1 |
|
['userId' => $user->getId(), 'ownerByUserId' => $model->getOwnedByUserId()] |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
1 |
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
3 |
|
$owningRoles = $this->getOwningRoles($user); |
|
51
|
3 |
|
$neededRoles = $this->getNeededRoles($attributes); |
|
52
|
|
|
|
|
53
|
3 |
|
$granted = $this->checkRoles($owningRoles, $neededRoles); |
|
54
|
|
|
|
|
55
|
3 |
|
$this->logger->info( |
|
56
|
3 |
|
'security.authorization.role: user {userId} granted {granted} for needed roles {neededRoles}', |
|
57
|
3 |
|
['userId' => $user->getId(), 'granted' => $granted, 'neededRoles' => implode(', ', $neededRoles)] |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
3 |
|
return $granted; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param UserInterface $user |
|
65
|
|
|
* |
|
66
|
|
|
* @return array |
|
67
|
|
|
*/ |
|
68
|
3 |
|
private function getOwningRoles(UserInterface $user): array |
|
69
|
|
|
{ |
|
70
|
3 |
|
return $this->roleHierarchyResolver->resolve($user->getRoles()); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param mixed $attributes |
|
75
|
|
|
* |
|
76
|
|
|
* @return array |
|
77
|
|
|
*/ |
|
78
|
3 |
|
private function getNeededRoles($attributes): array |
|
79
|
|
|
{ |
|
80
|
3 |
|
return is_scalar($attributes) ? [$attributes] : $attributes; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param array $owningRoles |
|
85
|
|
|
* @param array $neededRoles |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
3 |
|
private function checkRoles(array $owningRoles, array $neededRoles): bool |
|
90
|
|
|
{ |
|
91
|
3 |
|
foreach ($neededRoles as $neededRole) { |
|
92
|
3 |
|
if (!in_array($neededRole, $owningRoles, true)) { |
|
93
|
3 |
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
3 |
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..