Passed
Push — master ( aebfcf...8c8482 )
by Florian
02:35
created

EventVoter::supports()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the vseth-semesterly-reports 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\Organisation;
16
use App\Model\User;
17
use App\Security\Voter\Base\BaseVoter;
18
use Doctrine\Common\Persistence\ManagerRegistry;
19
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
20
21
class EventVoter extends BaseVoter
22
{
23
    /** @var ManagerRegistry */
24
    private $doctrine;
25
26
    /**
27
     * EntryVoter constructor.
28
     */
29
    public function __construct(ManagerRegistry $doctrine)
30
    {
31
        $this->doctrine = $doctrine;
32
    }
33
34
    /**
35
     * @param string $attribute An attribute
36
     * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
37
     *
38
     * @return bool True if the attribute and subject are supported, false otherwise
39
     */
40
    protected function supports($attribute, $subject)
41
    {
42
        return $subject instanceof Event;
43
    }
44
45
    /**
46
     * Perform a single access check operation on a given attribute, subject and token.
47
     * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
48
     *
49
     * @param string $attribute
50
     * @param Event $subject
51
     *
52
     * @return bool
53
     */
54
    protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
55
    {
56
        if (\in_array(User::ROLE_ADMIN, $token->getRoleNames(), true)) {
0 ignored issues
show
Bug introduced by
The method getRoleNames() does not exist on Symfony\Component\Securi...on\Token\TokenInterface. Did you maybe mean getRoles()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

56
        if (\in_array(User::ROLE_ADMIN, $token->/** @scrutinizer ignore-call */ getRoleNames(), true)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
57
            return true;
58
        }
59
60
        $organisation = $this->doctrine->getRepository(Organisation::class)->findOneBy(['email' => $token->getUser()->getUsername()]);
61
62
        return $subject->getOrganisation() === $organisation;
63
    }
64
}
65