1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Security; |
4
|
|
|
|
5
|
|
|
use App\Document\Access; |
6
|
|
|
use App\Document\Application; |
7
|
|
|
use App\Document\User; |
8
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
9
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
10
|
|
|
use Symfony\Component\Security\Core\Security; |
11
|
|
|
|
12
|
|
|
class ApplicationVoter extends Voter |
13
|
|
|
{ |
14
|
|
|
private $security; |
15
|
|
|
|
16
|
|
|
public function __construct(Security $security) |
17
|
|
|
{ |
18
|
|
|
$this->security = $security; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
protected function supports($attribute, $subject) |
22
|
|
|
{ |
23
|
|
|
if (!in_array($attribute, [Access::ACCESS_USER, Access::ACCESS_MASTER, Access::ACCESS_OWNER])) { |
24
|
|
|
return false; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (!$subject instanceof Application) { |
28
|
|
|
return false; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return true; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) |
35
|
|
|
{ |
36
|
|
|
if ($this->security->isGranted(User::ROLE_MANAGER)) { |
37
|
|
|
return true; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
$user = $token->getUser(); |
41
|
|
|
|
42
|
|
|
if (!$user instanceof User) { |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** @var Application $application */ |
47
|
|
|
$application = $subject; |
48
|
|
|
|
49
|
|
|
$access = null; |
50
|
|
|
|
51
|
|
|
foreach ($application->getAccesses() as $item) { |
52
|
|
|
if ($item->getUser() === $user) { |
53
|
|
|
$access = $item->getAccess(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (!$access) { |
58
|
|
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
switch ($attribute) { |
62
|
|
|
case Access::ACCESS_USER: |
63
|
|
|
return true; |
64
|
|
|
case Access::ACCESS_MASTER: |
65
|
|
|
return in_array($access, [Access::ACCESS_MASTER, Access::ACCESS_OWNER]); |
66
|
|
|
case Access::ACCESS_OWNER: |
67
|
|
|
return Access::ACCESS_OWNER === $access; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw new \LogicException('This code should not be reached!'); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|