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
|
|
|
* Command - Require role mapping |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $commandRoleMapping; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a new HandleCommandVoter. |
32
|
|
|
* |
33
|
|
|
* @param AccessDecisionManagerInterface $decisionManager |
34
|
|
|
* @param array $commandRoleMapping |
35
|
|
|
* @param string $defaultRole |
|
|
|
|
36
|
|
|
*/ |
37
|
24 |
|
public function __construct(AccessDecisionManagerInterface $decisionManager, array $commandRoleMapping = []) |
38
|
|
|
{ |
39
|
24 |
|
$this->decisionManager = $decisionManager; |
40
|
24 |
|
$this->commandRoleMapping = $commandRoleMapping; |
41
|
24 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* The voter supports checking handle commands |
45
|
|
|
* |
46
|
|
|
* @param string $attribute |
47
|
|
|
* @param object $subject |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
24 |
|
protected function supports($attribute, $subject) |
51
|
|
|
{ |
52
|
24 |
|
return $attribute === 'handle' && is_object($subject); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Checks if the currently logged on user may handle $subject. |
57
|
|
|
* |
58
|
|
|
* @param type $attribute |
59
|
|
|
* @param type $subject |
60
|
|
|
* @param TokenInterface $token |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
15 |
|
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) |
65
|
|
|
{ |
66
|
15 |
|
$allowedRoles = $this->getAllowedRoles(get_class($subject)); |
67
|
|
|
|
68
|
15 |
|
if (count($allowedRoles) > 0) { |
69
|
6 |
|
return $this->decisionManager->decide($token, $allowedRoles); |
70
|
|
|
} else { |
71
|
|
|
// default conclusion is access denied |
72
|
9 |
|
return false; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets the roles allowed to handle a command of $type |
78
|
|
|
* |
79
|
|
|
* @param string $type |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
15 |
|
private function getAllowedRoles($type) |
83
|
|
|
{ |
84
|
15 |
|
if (array_key_exists($type, $this->commandRoleMapping)) { |
85
|
6 |
|
return $this->commandRoleMapping[$type]; |
86
|
|
|
} else { |
87
|
9 |
|
return []; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.