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\Registration; |
16
|
|
|
use App\Entity\User; |
17
|
|
|
use Doctrine\Persistence\ManagerRegistry; |
18
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; |
19
|
|
|
use Symfony\Component\Security\Core\Authorization\Voter\Voter; |
20
|
|
|
|
21
|
|
|
class RegistrationVoter extends Voter |
22
|
|
|
{ |
23
|
|
|
const REGISTRATION_UPDATE = 'registration_update'; |
24
|
|
|
const REGISTRATION_DELETE = 'registration_delete'; |
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::REGISTRATION_UPDATE, self::REGISTRATION_DELETE])) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $subject instanceof Registration; |
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 Registration $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 ($user instanceof User) { |
|
|
|
|
76
|
|
|
switch ($attribute) { |
77
|
|
|
case self::REGISTRATION_UPDATE: |
78
|
|
|
case self::REGISTRATION_DELETE: |
79
|
|
|
return $subject->getUser() === $user; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
throw new \LogicException('Attribute '.$attribute.' unknown!'); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|