PermissionsVoter   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 48
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A voteOnAttribute() 0 14 3
A supports() 0 8 2
1
<?php
2
3
/*
4
 * This file is part of the EloyekunlePermissionsBundle package.
5
 *
6
 * (c) Elijah Oyekunle <https://elijahoyekunle.com/>
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
namespace Eloyekunle\PermissionsBundle\Security;
13
14
use Eloyekunle\PermissionsBundle\Model\RoleInterface;
15
use Eloyekunle\PermissionsBundle\Model\UserInterface;
16
use Eloyekunle\PermissionsBundle\Util\PermissionsHandlerInterface;
17
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
18
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
19
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
20
21
class PermissionsVoter extends Voter
22
{
23
    /**
24
     * @var AccessDecisionManagerInterface
25
     */
26
    protected $decisionManager;
27
28
    /**
29
     * @var PermissionsHandlerInterface
30
     */
31
    protected $moduleHandler;
32
33 4
    public function __construct(AccessDecisionManagerInterface $accessDecisionManager, PermissionsHandlerInterface $permissionsHandler)
34
    {
35 4
        $this->decisionManager = $accessDecisionManager;
36 4
        $this->moduleHandler = $permissionsHandler;
37 4
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42 4
    protected function supports($attribute, $subject)
43
    {
44
        // Check if attribute is declared as permission.
45 4
        if (!in_array($attribute, array_keys($this->moduleHandler->getPermissions()))) {
46 2
            return false;
47
        }
48
49 2
        return true;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 2
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
56
    {
57
        // Super Admin Pass
58 2
        if ($this->decisionManager->decide($token, [RoleInterface::ROLE_SUPER_ADMIN])) {
59 1
            return true;
60
        }
61
62
        // Deny access if user is not logged in.
63 1
        $user = $token->getUser();
64 1
        if (!$user instanceof UserInterface) {
65 1
            return false;
66
        }
67
68 1
        return $user->hasPermission($attribute);
69
    }
70
}
71