Passed
Push — master ( 52beba...2b25b9 )
by Julito
10:25
created

CCalendarEventVoter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Security\Authorization\Voter;
8
9
use Chamilo\CoreBundle\Entity\Message;
10
use Chamilo\CoreBundle\Entity\User;
11
use Chamilo\CourseBundle\Entity\CCalendarEvent;
12
use Doctrine\ORM\EntityManagerInterface;
13
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
14
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
15
use Symfony\Component\Security\Core\Security;
16
use Symfony\Component\Security\Core\User\UserInterface;
17
18
class CCalendarEventVoter extends Voter
19
{
20
    public const CREATE = 'CREATE';
21
    public const VIEW = 'VIEW';
22
    public const EDIT = 'EDIT';
23
    public const DELETE = 'DELETE';
24
25
    private EntityManagerInterface $entityManager;
26
    private Security $security;
27
28
    public function __construct(
29
        EntityManagerInterface $entityManager,
30
        Security $security
31
    ) {
32
        $this->entityManager = $entityManager;
33
        $this->security = $security;
34
    }
35
36
    protected function supports(string $attribute, $subject): bool
37
    {
38
        $options = [
39
            self::CREATE,
40
            self::VIEW,
41
            self::EDIT,
42
            self::DELETE,
43
        ];
44
45
        // if the attribute isn't one we support, return false
46
        if (!\in_array($attribute, $options, true)) {
47
            return false;
48
        }
49
50
        // only vote on Post objects inside this voter
51
        return $subject instanceof CCalendarEvent;
52
    }
53
54
    protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
55
    {
56
        /** @var User $user */
57
        $user = $token->getUser();
58
59
        if (!$user instanceof UserInterface) {
60
            return false;
61
        }
62
63
        // Admins have access to everything.
64
        if ($this->security->isGranted('ROLE_ADMIN')) {
65
            return true;
66
        }
67
68
        /** @var CCalendarEvent $event */
69
        $event = $subject;
70
71
        // @todo check permissions
72
        switch ($attribute) {
73
            case self::CREATE:
74
                return true;
75
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
76
            case self::VIEW:
77
                return true;
78
                break;
79
            case self::EDIT:
80
            case self::DELETE:
81
                if ($event->getCreator() === $user) {
82
                    return true;
83
                }
84
85
                break;
86
        }
87
88
        return false;
89
    }
90
}
91