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

EventVoter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
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
25
    /**
26
     * @var ManagerRegistry
27
     */
28
    private $doctrine;
29
30
    /**
31
     * EventVoter constructor.
32
     */
33
    public function __construct(ManagerRegistry $doctrine)
34
    {
35
        $this->doctrine = $doctrine;
36
    }
37
38
    /**
39
     * Determines if the attribute and subject are supported by this voter.
40
     *
41
     * @param string $attribute An attribute
42
     * @param Event  $subject   The subject to secure, e.g. an object the user wants to access or any other PHP type
43
     *
44
     * @return bool True if the attribute and subject are supported, false otherwise
45
     */
46
    protected function supports($attribute, $subject)
47
    {
48
        // if the attribute isn't one we support, return false
49
        if (!in_array($attribute, [self::EVENT_VIEW, self::EVENT_UPDATE])) {
50
            return false;
51
        }
52
53
        return $subject instanceof Event;
54
    }
55
56
    /**
57
     * Perform a single access check operation on a given attribute, subject and token.
58
     * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
59
     *
60
     * @param string $attribute
61
     * @param Event  $subject
62
     *
63
     * @return bool
64
     */
65
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
66
    {
67
        $email = $token->getUser();
68
        /** @var User|null $user */
69
        $user = $this->doctrine->getRepository(User::class)->findOneBy(['email' => $email]);
70
        if (null === $user) {
71
            return false;
72
        }
73
74
        $matchingRegistration = $user->getRegistrationFor($subject);
75
        if (null === $matchingRegistration) {
76
            return false;
77
        }
78
79
        if ($user instanceof User) {
0 ignored issues
show
introduced by
$user is always a sub-type of App\Entity\User.
Loading history...
80
            switch ($attribute) {
81
                case self::EVENT_VIEW:
82
                case self::EVENT_UPDATE:
83
                    return $matchingRegistration->getIsOrganizer();
84
            }
85
        }
86
87
        throw new \LogicException('Attribute '.$attribute.' unknown!');
88
    }
89
}
90