UserReadAuthorizationChecker   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 52
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A isGrantedToRead() 0 31 5
1
<?php
2
3
namespace App\Module\User\Read\Service;
4
5
use App\Application\Data\UserNetworkSessionData;
6
use App\Module\Authorization\Repository\AuthorizationUserRoleFinderRepository;
0 ignored issues
show
Bug introduced by
The type App\Module\Authorization...serRoleFinderRepository was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 UserReadAuthorizationChecker
15
{
16
    private ?int $loggedInUserId = null;
17
18 203
    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 203
        $this->loggedInUserId = $this->userNetworkSessionData->userId ?? null;
25
    }
26
27
    /**
28
     * Check if authenticated user is allowed to read user.
29
     *
30
     * @param int|null $userIdToRead null when check for all users
31
     * @param bool $log log if forbidden (expected false when function is called for privilege setting)
32
     *
33
     * @return bool
34
     */
35 149
    public function isGrantedToRead(?int $userIdToRead = null, bool $log = true): bool
36
    {
37 149
        if ($this->loggedInUserId === null) {
38 1
            $this->logger->error(
39 1
                'loggedInUserId not set while authorization check isGrantedToRead $userIdToRead: '
40 1
                . $userIdToRead
41 1
            );
42
43 1
            return false;
44
        }
45 148
        $authenticatedUserRoleHierarchy = $this->authorizationUserRoleFinderRepository->getRoleHierarchyByUserId(
46 148
            $this->loggedInUserId
47 148
        );
48
        // Returns array with role name as key and hierarchy as value ['role_name' => hierarchy_int]
49
        // * Lower hierarchy number means higher privileged role
50 148
        $userRoleHierarchies = $this->authorizationUserRoleFinderRepository->getUserRolesHierarchies();
51
52
        // Only managing advisor and higher privileged are allowed to see other users
53
        // If the user role hierarchy of the authenticated user is lower or equal
54
        // than the one from the managing advisor -> authorized
55 148
        if ($authenticatedUserRoleHierarchy <= $userRoleHierarchies[UserRole::MANAGING_ADVISOR->value]
56
            // or user wants to view his own profile in which case also -> authorized
57 148
            || $this->loggedInUserId === $userIdToRead) {
58 78
            return true;
59
        }
60
61 72
        if ($log === true) {
62 3
            $this->logger->notice('User ' . $this->loggedInUserId . ' tried to read user but isn\'t allowed.');
63
        }
64
65 72
        return false;
66
    }
67
}
68