UserDeleteAuthorizationChecker   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 79.17%

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 58
ccs 19
cts 24
cp 0.7917
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
B isGrantedToDelete() 0 37 7
1
<?php
2
3
namespace App\Module\User\Delete\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 UserDeleteAuthorizationChecker
15
{
16
    private ?int $loggedInUserId = null;
17
18 14
    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 14
        $this->loggedInUserId = $this->userNetworkSessionData->userId ?? null;
25
    }
26
27
    /**
28
     * Check if authenticated user is allowed to delete user.
29
     *
30
     * @param int $userIdToDelete
31
     * @param bool $log log if forbidden (expected false when function is called for privilege setting)
32
     *
33
     * @return bool
34
     */
35 9
    public function isGrantedToDelete(
36
        int $userIdToDelete,
37
        bool $log = true,
38
    ): bool {
39 9
        if ($this->loggedInUserId === null) {
40
            $this->logger->error(
41
                'loggedInUserId not set while authorization check isGrantedToDelete $userIdToDelete: '
42
                . $userIdToDelete
43
            );
44
45
            return false;
46
        }
47 9
        $authenticatedUserRoleHierarchy = $this->authorizationUserRoleFinderRepository->getRoleHierarchyByUserId(
48 9
            $this->loggedInUserId
49 9
        );
50 9
        $userToDeleteRoleHierarchy = $this->authorizationUserRoleFinderRepository->getRoleHierarchyByUserId($userIdToDelete);
51
52
        // Returns array with role name as key and hierarchy as value ['role_name' => hierarchy_int]
53
        // * Lower hierarchy number means higher privileged role
54 9
        $userRoleHierarchies = $this->authorizationUserRoleFinderRepository->getUserRolesHierarchies();
55
56
        // Only managing_advisor and higher are allowed to delete user and only if the user is advisor or lower or their own
57 9
        if (($authenticatedUserRoleHierarchy <= $userRoleHierarchies[UserRole::MANAGING_ADVISOR->value]
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($authenticatedUserRoleH...UserRole::ADMIN->value], Probably Intended Meaning: $authenticatedUserRoleHi...serRole::ADMIN->value])
Loading history...
58 7
                && ($userToDeleteRoleHierarchy >= $userRoleHierarchies[UserRole::ADVISOR->value]
59 7
                    || $userIdToDelete === $this->loggedInUserId))
60
            // or authenticated user is admin
61 9
            || $authenticatedUserRoleHierarchy <= $userRoleHierarchies[UserRole::ADMIN->value]) {
62 6
            return true;
63
        }
64
65 3
        if ($log === true) {
66 2
            $this->logger->notice(
67 2
                'User ' . $this->loggedInUserId . ' tried to delete user but isn\'t allowed.'
68 2
            );
69
        }
70
71 3
        return false;
72
    }
73
}
74