Passed
Push — master ( 8bd912...d93388 )
by Alan
06:58 queued 02:20
created

ResourceAccessChecker::getEffectiveRoles()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 4
nop 1
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Security;
15
16
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ApiPlatform\Core\Security\ExpressionLanguage. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolverInterface;
18
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
19
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
20
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
21
use Symfony\Component\Security\Core\Role\Role;
22
use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
23
24
/**
25
 * Checks if the logged user has sufficient permissions to access the given resource.
26
 *
27
 * @author Kévin Dunglas <[email protected]>
28
 */
29
final class ResourceAccessChecker implements ResourceAccessCheckerInterface
30
{
31
    private $expressionLanguage;
32
    private $authenticationTrustResolver;
33
    private $roleHierarchy;
34
    private $tokenStorage;
35
    private $authorizationChecker;
36
37
    public function __construct(ExpressionLanguage $expressionLanguage = null, AuthenticationTrustResolverInterface $authenticationTrustResolver = null, RoleHierarchyInterface $roleHierarchy = null, TokenStorageInterface $tokenStorage = null, AuthorizationCheckerInterface $authorizationChecker = null)
38
    {
39
        $this->expressionLanguage = $expressionLanguage;
40
        $this->authenticationTrustResolver = $authenticationTrustResolver;
41
        $this->roleHierarchy = $roleHierarchy;
42
        $this->tokenStorage = $tokenStorage;
43
        $this->authorizationChecker = $authorizationChecker;
44
    }
45
46
    public function isGranted(string $resourceClass, string $expression, array $extraVariables = []): bool
47
    {
48
        if (null === $this->tokenStorage || null === $this->authenticationTrustResolver) {
49
            throw new \LogicException('The "symfony/security" library must be installed to use the "security" attribute.');
50
        }
51
        if (null === $token = $this->tokenStorage->getToken()) {
52
            throw new \LogicException('The current token must be set to use the "security" attribute (is the URL behind a firewall?).');
53
        }
54
        if (null === $this->expressionLanguage) {
55
            throw new \LogicException('The "symfony/expression-language" library must be installed to use the "security".');
56
        }
57
58
        return (bool) $this->expressionLanguage->evaluate($expression, array_merge($extraVariables, $this->getVariables($token)));
59
    }
60
61
    /**
62
     * @copyright Fabien Potencier <[email protected]>
63
     *
64
     * @see https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Core/Authorization/Voter/ExpressionVoter.php
65
     */
66
    private function getVariables(TokenInterface $token): array
67
    {
68
        return [
69
            'token' => $token,
70
            'user' => $token->getUser(),
71
            'roles' => $this->getEffectiveRoles($token),
72
            'trust_resolver' => $this->authenticationTrustResolver,
73
            // needed for the is_granted expression function
74
            'auth_checker' => $this->authorizationChecker,
75
        ];
76
    }
77
78
    /**
79
     * @return string[]
80
     */
81
    private function getEffectiveRoles(TokenInterface $token): array
82
    {
83
        if (null === $this->roleHierarchy) {
84
            return method_exists($token, 'getRoleNames') ? $token->getRoleNames() : array_map('strval', $token->getRoles());
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Securi...enInterface::getRoles() has been deprecated: since Symfony 4.3, use the getRoleNames() method instead ( Ignorable by Annotation )

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

84
            return method_exists($token, 'getRoleNames') ? $token->getRoleNames() : array_map('strval', /** @scrutinizer ignore-deprecated */ $token->getRoles());

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
85
        }
86
87
        if (method_exists($this->roleHierarchy, 'getReachableRoleNames')) {
88
            return $this->roleHierarchy->getReachableRoleNames($token->getRoleNames());
0 ignored issues
show
Bug introduced by
The method getRoleNames() does not exist on Symfony\Component\Securi...on\Token\TokenInterface. Did you maybe mean getRoles()? ( Ignorable by Annotation )

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

88
            return $this->roleHierarchy->getReachableRoleNames($token->/** @scrutinizer ignore-call */ getRoleNames());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
89
        }
90
91
        return array_map(function (Role $role): string {
92
            return $role->getRole();
93
        }, $this->roleHierarchy->getReachableRoles($token->getRoles()));
0 ignored issues
show
Deprecated Code introduced by
The function Symfony\Component\Securi...enInterface::getRoles() has been deprecated: since Symfony 4.3, use the getRoleNames() method instead ( Ignorable by Annotation )

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

93
        }, $this->roleHierarchy->getReachableRoles(/** @scrutinizer ignore-deprecated */ $token->getRoles()));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
94
    }
95
}
96