1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Module\UserActivity\ReadAuthorization\Service; |
4
|
|
|
|
5
|
|
|
use App\Application\Data\UserNetworkSessionData; |
6
|
|
|
use App\Module\Authorization\Repository\AuthorizationUserRoleFinderRepository; |
|
|
|
|
7
|
|
|
use App\Module\User\Enum\UserRole; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Check if authenticated user is permitted to do actions |
12
|
|
|
* Roles: newcomer < advisor < managing_advisor < administrator. |
13
|
|
|
*/ |
14
|
|
|
final class UserActivityReadAuthorizationChecker |
15
|
|
|
{ |
16
|
|
|
private ?int $loggedInUserId = null; |
17
|
|
|
|
18
|
3 |
|
public function __construct( |
19
|
|
|
private readonly UserNetworkSessionData $userNetworkSessionData, |
20
|
|
|
private readonly AuthorizationUserRoleFinderRepository $authorizationUserRoleFinderRepository, |
21
|
|
|
private readonly LoggerInterface $logger, |
22
|
|
|
) { |
23
|
|
|
// Fix error $userId must not be accessed before initialization |
24
|
3 |
|
$this->loggedInUserId = $this->userNetworkSessionData->userId ?? null; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Check if the authenticated user is allowed to read user activity. |
29
|
|
|
* |
30
|
|
|
* @param int $userIdToRead |
31
|
|
|
* @param bool $log log if forbidden |
32
|
|
|
* |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
2 |
|
public function isGrantedToReadUserActivity( |
36
|
|
|
int $userIdToRead, |
37
|
|
|
bool $log = true, |
38
|
|
|
): bool { |
39
|
2 |
|
if ($this->loggedInUserId === null) { |
40
|
|
|
$this->logger->error( |
41
|
|
|
'loggedInUserId not set while authorization check isGrantedToReadUserActivity $userIdToRead: ' |
42
|
|
|
. $userIdToRead |
43
|
|
|
); |
44
|
|
|
|
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
2 |
|
$authenticatedUserRoleHierarchy = $this->authorizationUserRoleFinderRepository->getRoleHierarchyByUserId( |
49
|
2 |
|
$this->loggedInUserId |
50
|
2 |
|
); |
51
|
|
|
// Returns array with role name as key and hierarchy as value ['role_name' => hierarchy_int] |
52
|
|
|
// * Lower hierarchy number means higher privileged role |
53
|
2 |
|
$userRoleHierarchies = $this->authorizationUserRoleFinderRepository->getUserRolesHierarchies(); |
54
|
|
|
|
55
|
2 |
|
$userToReadRoleHierarchy = $this->authorizationUserRoleFinderRepository->getRoleHierarchyByUserId($userIdToRead); |
56
|
|
|
|
57
|
|
|
// Only managing advisors are allowed to see user activity, but only if target user role is not higher than also managing advisor |
58
|
2 |
|
if (($authenticatedUserRoleHierarchy <= $userRoleHierarchies[UserRole::MANAGING_ADVISOR->value] |
59
|
1 |
|
&& $userToReadRoleHierarchy >= $userRoleHierarchies[UserRole::MANAGING_ADVISOR->value]) |
60
|
|
|
// or authenticated user is admin |
61
|
2 |
|
|| $authenticatedUserRoleHierarchy <= $userRoleHierarchies[UserRole::ADMIN->value] |
62
|
|
|
// or user wants to view his own activity |
63
|
2 |
|
|| $this->loggedInUserId === $userIdToRead) { |
64
|
2 |
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
if ($log === true) { |
68
|
1 |
|
$this->logger->notice( |
69
|
1 |
|
"User $this->loggedInUserId tried to read activity of user $userIdToRead but isn't allowed." |
70
|
1 |
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
1 |
|
return false; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
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