RoleAuthorization   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 89
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A isGranted() 0 23 3
A getOwningRoles() 0 4 1
A getNeededRoles() 0 4 2
A checkRoles() 0 10 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Security\Authorization;
6
7
use Chubbyphp\Security\UserInterface;
8
use Psr\Log\LoggerInterface;
9
use Psr\Log\NullLogger;
10
11
final class RoleAuthorization implements AuthorizationInterface
12
{
13
    /**
14
     * @var array
15
     */
16
    private $roleHierarchyResolver;
17
18
    /**
19
     * @var LoggerInterface
20
     */
21
    private $logger;
22
23
    /**
24
     * @param RoleHierarchyResolverInterface $roleHierarchyResolver
25
     */
26 3
    public function __construct(RoleHierarchyResolverInterface $roleHierarchyResolver, LoggerInterface $logger = null)
27
    {
28 3
        $this->roleHierarchyResolver = $roleHierarchyResolver;
0 ignored issues
show
Documentation Bug introduced by
It seems like $roleHierarchyResolver of type object<Chubbyphp\Securit...archyResolverInterface> is incompatible with the declared type array of property $roleHierarchyResolver.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
29 3
        $this->logger = $logger ?? new NullLogger();
30 3
    }
31
32
    /**
33
     * @param UserInterface                  $user
34
     * @param mixed                          $attributes
35
     * @param OwnedByUserModelInterface|null $model
36
     *
37
     * @return bool
38
     */
39 3
    public function isGranted(UserInterface $user, $attributes, OwnedByUserModelInterface $model = null): bool
40
    {
41 3
        if (null !== $model && $user->getId() !== $model->getOwnedByUserId()) {
42 1
            $this->logger->info(
43 1
                'security.authorization.role: user and model owner are not the same {userId}, {ownerByUserId}',
44 1
                ['userId' => $user->getId(), 'ownerByUserId' => $model->getOwnedByUserId()]
45
            );
46
47 1
            return false;
48
        }
49
50 3
        $owningRoles = $this->getOwningRoles($user);
51 3
        $neededRoles = $this->getNeededRoles($attributes);
52
53 3
        $granted = $this->checkRoles($owningRoles, $neededRoles);
54
55 3
        $this->logger->info(
56 3
            'security.authorization.role: user {userId} granted {granted} for needed roles {neededRoles}',
57 3
            ['userId' => $user->getId(), 'granted' => $granted, 'neededRoles' => implode(', ', $neededRoles)]
58
        );
59
60 3
        return $granted;
61
    }
62
63
    /**
64
     * @param UserInterface $user
65
     *
66
     * @return array
67
     */
68 3
    private function getOwningRoles(UserInterface $user): array
69
    {
70 3
        return $this->roleHierarchyResolver->resolve($user->getRoles());
0 ignored issues
show
Bug introduced by
The method resolve cannot be called on $this->roleHierarchyResolver (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
71
    }
72
73
    /**
74
     * @param mixed $attributes
75
     *
76
     * @return array
77
     */
78 3
    private function getNeededRoles($attributes): array
79
    {
80 3
        return is_scalar($attributes) ? [$attributes] : $attributes;
81
    }
82
83
    /**
84
     * @param array $owningRoles
85
     * @param array $neededRoles
86
     *
87
     * @return bool
88
     */
89 3
    private function checkRoles(array $owningRoles, array $neededRoles): bool
90
    {
91 3
        foreach ($neededRoles as $neededRole) {
92 3
            if (!in_array($neededRole, $owningRoles, true)) {
93 3
                return false;
94
            }
95
        }
96
97 3
        return true;
98
    }
99
}
100