ClientReadAuthorizationChecker   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 16
dl 0
loc 52
ccs 18
cts 18
cp 1
rs 10
c 1
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A isGrantedToRead() 0 31 6
A __construct() 0 6 1
1
<?php
2
3
namespace App\Module\Client\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 the authenticated user is permitted to do actions.
12
 * Roles: newcomer < advisor < managing_advisor < administrator.
13
 */
14
final class ClientReadAuthorizationChecker
15
{
16
    private ?int $loggedInUserId = null;
17
18 32
    public function __construct(
19
        private readonly AuthorizationUserRoleFinderRepository $authorizationUserRoleFinderRepository,
20
        private readonly UserNetworkSessionData $userNetworkSessionData,
21
        private readonly LoggerInterface $logger,
22
    ) {
23 32
        $this->loggedInUserId = $this->userNetworkSessionData->userId;
24
    }
25
26
    /**
27
     * Check if the authenticated user is allowed to read client.
28
     *
29
     * @param int|null $ownerId
30
     * @param string|\DateTimeImmutable|null $deletedAt
31
     * @param bool $log log if forbidden (expected false when function is called for privilege setting)
32
     *
33
     * @return bool
34
     */
35 22
    public function isGrantedToRead(
36
        ?int $ownerId,
0 ignored issues
show
Unused Code introduced by
The parameter $ownerId is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

36
        /** @scrutinizer ignore-unused */ ?int $ownerId,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
        string|\DateTimeImmutable|null $deletedAt = null,
38
        bool $log = true,
39
    ): bool {
40 22
        if ($this->loggedInUserId !== null) {
41 22
            $authenticatedUserRoleHierarchy = $this->authorizationUserRoleFinderRepository->getRoleHierarchyByUserId(
42 22
                $this->loggedInUserId
43 22
            );
44
            // Returns array with role name as key and hierarchy as value ['role_name' => hierarchy_int]
45
            // * Lower hierarchy number means higher privileged role
46 22
            $userRoleHierarchies = $this->authorizationUserRoleFinderRepository->getUserRolesHierarchies();
47
48
            // Newcomer are allowed to see all clients regardless of owner if not deleted
49 22
            if ($authenticatedUserRoleHierarchy <= $userRoleHierarchies[UserRole::NEWCOMER->value]
50 22
                && $deletedAt === null
51
            ) {
52 19
                return true;
53
            }
54
            // Managing advisors can see all clients including deleted ones
55 5
            if ($authenticatedUserRoleHierarchy <= $userRoleHierarchies[UserRole::MANAGING_ADVISOR->value]) {
56 3
                return true;
57
            }
58
        }
59 2
        if ($log === true) {
60 2
            $this->logger->notice(
61 2
                'User ' . $this->loggedInUserId . ' tried to read client but isn\'t allowed.'
62 2
            );
63
        }
64
65 2
        return false;
66
    }
67
}
68