Completed
Pull Request — master (#24)
by
unknown
02:45
created

HandleCommandVoter::getDecisionManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace League\Tactician\Bundle\Security\Voter;
4
5
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
6
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
7
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
8
9
/**
10
 * Voter for security checks on handling commands.
11
 *
12
 * @author Ron Rademaker
13
 */
14
class HandleCommandVoter extends Voter
15
{
16
    /**
17
     * The decision manager.
18
     *
19
     * @var AccessDecisionManagerInterface
20
     */
21
    private $decisionManager;
22
23
    /**
24
     * Default required role.
25
     *
26
     * @var string
27
     */
28
    private $defaultRole;
29
30
    /**
31
     * Command - Require role mapping
32
     *
33
     * @var array
34
     */
35
    private $commandRoleMapping;
36
37
    /**
38
     * Create a new HandleCommandVoter.
39
     *
40
     * @param AccessDecisionManagerInterface $decisionManager
41
     * @param array $commandRoleMapping
42
     * @param string $defaultRole
43
     */
44
    public function __construct(AccessDecisionManagerInterface $decisionManager, array $commandRoleMapping = [], $defaultRole = null)
45
    {
46
        $this->decisionManager = $decisionManager;
47
        $this->commandRoleMapping = $commandRoleMapping;
48
        $this->defaultRole = $defaultRole;
49
    }
50
51
    /**
52
     * The voter supports checking handle commands
53
     *
54
     * @param string $attribute
55
     * @param object $subject
56
     * @return bool
57
     */
58
    protected function supports($attribute, $subject)
59
    {
60
        return $attribute === 'handle' && is_object($subject);
61
    }
62
63
    /**
64
     * Checks if the currently logged on user may handle $subject.
65
     *
66
     * @param type $attribute
67
     * @param type $subject
68
     * @param TokenInterface $token
69
     *
70
     * @return bool
71
     */
72
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
73
    {
74
        $allowedRoles = $this->getAllowedRoles(get_class($subject));
75
76
        if (count($allowedRoles) > 0) {
77
            return $this->getDecisionManager()->decide($token, $allowedRoles);
78
        } else {
79
            // no required role is action allowed
80
            return true;
81
        }
82
    }
83
84
    /**
85
     * Gets the roles allowed to handle a command of $type
86
     *
87
     * @param string $type
88
     * @return array
89
     */
90
    private function getAllowedRoles($type)
91
    {
92
        if (false === $type && isset($this->defaultRole)) {
93
            return [$this->defaultRole];
94
        } elseif (false === $type) {
95
            return [];
96
        } elseif (array_key_exists($type, $this->commandRoleMapping)) {
97
            return $this->commandRoleMapping[$type];
98
        } else {
99
            return $this->getAllowedRoles(get_parent_class($type));
100
        }
101
    }
102
103
    /**
104
     * Returns the decisionManager.
105
     *
106
     * @return AccessDecisionManagerInterface
107
     */
108
    private function getDecisionManager()
109
    {
110
        return $this->decisionManager;
111
    }
112
}
113