|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the TheAlternativeZurich/events project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Florian Moser <[email protected]> |
|
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 App\Security\Voter; |
|
13
|
|
|
|
|
14
|
|
|
use App\Entity\Event; |
|
15
|
|
|
use App\Entity\User; |
|
16
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
|
17
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
|
18
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
|
19
|
|
|
|
|
20
|
|
|
class EventVoter extends Voter |
|
21
|
|
|
{ |
|
22
|
|
|
const EVENT_VIEW = 'event_view'; |
|
23
|
|
|
const EVENT_UPDATE = 'event_update'; |
|
24
|
|
|
const EVENT_CREATE = 'êvent_create'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var ManagerRegistry |
|
28
|
|
|
*/ |
|
29
|
|
|
private $doctrine; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* EventVoter constructor. |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct(ManagerRegistry $doctrine) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->doctrine = $doctrine; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Determines if the attribute and subject are supported by this voter. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $attribute An attribute |
|
43
|
|
|
* @param Event $subject The subject to secure, e.g. an object the user wants to access or any other PHP type |
|
44
|
|
|
* |
|
45
|
|
|
* @return bool True if the attribute and subject are supported, false otherwise |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function supports($attribute, $subject) |
|
48
|
|
|
{ |
|
49
|
|
|
// if the attribute isn't one we support, return false |
|
50
|
|
|
if (!in_array($attribute, [self::EVENT_CREATE, self::EVENT_VIEW, self::EVENT_UPDATE])) { |
|
51
|
|
|
return false; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $subject instanceof Event; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Perform a single access check operation on a given attribute, subject and token. |
|
59
|
|
|
* It is safe to assume that $attribute and $subject already passed the "supports()" method check. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $attribute |
|
62
|
|
|
* @param Event $subject |
|
63
|
|
|
* |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) |
|
67
|
|
|
{ |
|
68
|
|
|
$email = $token->getUser(); |
|
69
|
|
|
/** @var User|null $user */ |
|
70
|
|
|
$user = $this->doctrine->getRepository(User::class)->findOneBy(['email' => $email]); |
|
71
|
|
|
if (null === $user) { |
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
if (self::EVENT_CREATE) { |
|
76
|
|
|
return $user->getIsEmailConfirmed(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$matchingRegistration = $user->getRegistrationFor($subject); |
|
80
|
|
|
if (null === $matchingRegistration) { |
|
81
|
|
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ($user instanceof User) { |
|
|
|
|
|
|
85
|
|
|
switch ($attribute) { |
|
86
|
|
|
case self::EVENT_VIEW: |
|
87
|
|
|
case self::EVENT_UPDATE: |
|
88
|
|
|
return $matchingRegistration->getIsOrganizer(); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
throw new \LogicException('Attribute '.$attribute.' unknown!'); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|