1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overwatch\TestBundle\Security; |
4
|
|
|
|
5
|
|
|
use Overwatch\UserBundle\Entity\User; |
6
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
7
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface; |
8
|
|
|
use Symfony\Component\Security\Core\Role\Role; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* TestGroupVoter |
12
|
|
|
*/ |
13
|
|
|
class TestGroupVoter implements VoterInterface |
14
|
|
|
{ |
15
|
|
|
const VIEW = 'view'; |
16
|
|
|
const EDIT = 'edit'; |
17
|
|
|
|
18
|
23 |
|
public function supportsAttribute($attribute) |
19
|
|
|
{ |
20
|
23 |
|
return in_array($attribute, [ |
21
|
23 |
|
self::VIEW, |
22
|
23 |
|
self::EDIT, |
23
|
23 |
|
]); |
24
|
|
|
} |
25
|
|
|
|
26
|
74 |
|
public function supportsClass($class) |
27
|
|
|
{ |
28
|
74 |
|
$supportedClass = 'Overwatch\TestBundle\Entity\TestGroup'; |
29
|
74 |
|
return $supportedClass === $class || is_subclass_of($class, $supportedClass); |
30
|
|
|
} |
31
|
|
|
|
32
|
74 |
|
public function vote(TokenInterface $token, $group, array $attributes) |
33
|
|
|
{ |
34
|
|
|
// check if class of this object is supported by this voter |
35
|
74 |
|
if (!$this->supportsClass(get_class($group))) { |
36
|
72 |
|
return VoterInterface::ACCESS_ABSTAIN; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// check if the given attribute is covered by this voter |
40
|
23 |
|
if (!$this->supportsAttribute($attributes[0])) { |
41
|
1 |
|
return VoterInterface::ACCESS_ABSTAIN; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// get current logged in user |
45
|
22 |
|
$user = $token->getUser(); |
46
|
|
|
|
47
|
|
|
//allow the token to have ROLE_SUPER_ADMIN before we check the user, for testing |
48
|
22 |
|
if (in_array(new Role('ROLE_SUPER_ADMIN'), $token->getRoles())) { |
49
|
13 |
|
return VoterInterface::ACCESS_GRANTED; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// make sure there is a user object (i.e. that the user is logged in) |
53
|
11 |
|
if (!$user instanceof User) { |
54
|
9 |
|
return VoterInterface::ACCESS_DENIED; |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
switch ($attributes[0]) { |
58
|
2 |
|
case self::VIEW: |
59
|
1 |
|
if ($user->hasGroup($group->getName())) { |
60
|
1 |
|
return VoterInterface::ACCESS_GRANTED; |
61
|
|
|
} |
62
|
1 |
|
break; |
63
|
|
|
|
64
|
1 |
|
case self::EDIT: |
65
|
1 |
|
if ($user->hasGroup($group->getName()) && $user->hasRole('ROLE_ADMIN')) { |
66
|
1 |
|
return VoterInterface::ACCESS_GRANTED; |
67
|
|
|
} |
68
|
1 |
|
break; |
69
|
2 |
|
} |
70
|
|
|
|
71
|
2 |
|
return VoterInterface::ACCESS_DENIED; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|