Passed
Push — master ( ece3c1...a0e7b2 )
by Florian
02:27
created

RegistrationVoter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 8
eloc 18
c 1
b 0
f 1
dl 0
loc 63
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A supports() 0 8 2
A voteOnAttribute() 0 18 5
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) {
0 ignored issues
show
introduced by
$user is always a sub-type of App\Entity\User.
Loading history...
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