|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Module\User\Create\Service; |
|
4
|
|
|
|
|
5
|
|
|
use App\Application\Data\UserNetworkSessionData; |
|
6
|
|
|
use App\Module\Authorization\Repository\AuthorizationUserRoleFinderRepository; |
|
|
|
|
|
|
7
|
|
|
use App\Module\User\AssignRole\Service\UserAssignRoleAuthorizationChecker; |
|
8
|
|
|
use App\Module\User\Enum\UserRole; |
|
9
|
|
|
use Psr\Log\LoggerInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Check if authenticated user is permitted to do actions |
|
13
|
|
|
* Roles: newcomer < advisor < managing_advisor < administrator. |
|
14
|
|
|
*/ |
|
15
|
|
|
final class UserCreateAuthorizationChecker |
|
16
|
|
|
{ |
|
17
|
|
|
private ?int $loggedInUserId = null; |
|
18
|
|
|
|
|
19
|
9 |
|
public function __construct( |
|
20
|
|
|
private readonly UserNetworkSessionData $userNetworkSessionData, |
|
21
|
|
|
private readonly AuthorizationUserRoleFinderRepository $authorizationUserRoleFinderRepository, |
|
22
|
|
|
private readonly UserAssignRoleAuthorizationChecker $userAssignRoleAuthorizationChecker, |
|
23
|
|
|
private readonly LoggerInterface $logger, |
|
24
|
|
|
) { |
|
25
|
|
|
// Fix error $userId must not be accessed before initialization |
|
26
|
9 |
|
$this->loggedInUserId = $this->userNetworkSessionData->userId ?? null; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Check if the authenticated user is allowed to create |
|
31
|
|
|
* Important to have user role in the object. |
|
32
|
|
|
* |
|
33
|
|
|
* @param array $userValues |
|
34
|
|
|
* |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
9 |
|
public function isGrantedToCreate(array $userValues): bool |
|
38
|
|
|
{ |
|
39
|
9 |
|
if ($this->loggedInUserId === null) { |
|
40
|
|
|
$this->logger->error( |
|
41
|
|
|
'loggedInUserId not set while authorization check isGrantedToCreate: ' |
|
42
|
|
|
. json_encode($userValues, JSON_PARTIAL_OUTPUT_ON_ERROR) |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
return false; |
|
46
|
|
|
} |
|
47
|
9 |
|
$authenticatedUserRoleHierarchy = $this->authorizationUserRoleFinderRepository->getRoleHierarchyByUserId( |
|
48
|
9 |
|
$this->loggedInUserId |
|
49
|
9 |
|
); |
|
50
|
|
|
// Returns array with role name as key and hierarchy as value ['role_name' => hierarchy_int] |
|
51
|
|
|
// * Lower hierarchy number means higher privileged role |
|
52
|
9 |
|
$userRoleHierarchies = $this->authorizationUserRoleFinderRepository->getUserRolesHierarchies(); |
|
53
|
|
|
|
|
54
|
|
|
// Newcomer and advisor are not allowed to do anything from other users - only user edit his own profile |
|
55
|
|
|
// Managing advisor may change users |
|
56
|
9 |
|
if ($authenticatedUserRoleHierarchy <= $userRoleHierarchies[UserRole::MANAGING_ADVISOR->value]) { |
|
57
|
|
|
// Managing advisors can do everything with users except setting a role higher than advisor |
|
58
|
8 |
|
if ($this->userAssignRoleAuthorizationChecker->userRoleIsGranted( |
|
59
|
8 |
|
$userValues['user_role_id'] ?? null, |
|
60
|
8 |
|
null, |
|
61
|
8 |
|
$authenticatedUserRoleHierarchy, |
|
62
|
8 |
|
$userRoleHierarchies |
|
63
|
8 |
|
) === true |
|
64
|
|
|
) { |
|
65
|
7 |
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// If the user role of the user managing advisors or higher wants to change is empty, allowed |
|
69
|
|
|
// It's the validation's job to check if the value is valid |
|
70
|
1 |
|
if ($userValues['user_role_id'] === null) { |
|
71
|
|
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
// There is no need to check if user wants to create his own user as he can't be logged in if the user doesn't exist |
|
75
|
|
|
|
|
76
|
2 |
|
$this->logger->notice( |
|
77
|
2 |
|
'User ' . $this->loggedInUserId . ' tried to create user but isn\'t allowed.' |
|
78
|
2 |
|
); |
|
79
|
|
|
|
|
80
|
2 |
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths