Passed
Push — master ( 3ac343...373e44 )
by Angel Fernando Quiroz
08:23 queued 15s
created

TrackEAttemptQualifyVoter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Security\Authorization\Voter;
8
9
use Chamilo\CoreBundle\Entity\TrackEAttemptQualify;
10
use Chamilo\CoreBundle\Entity\User;
11
use Chamilo\CoreBundle\ServiceHelper\IsAllowedToEditHelper;
12
use Symfony\Bundle\SecurityBundle\Security;
13
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
14
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
17
/**
18
 * @extends Voter<'VIEW', TrackEAttemptQualify>
19
 */
20
class TrackEAttemptQualifyVoter extends Voter
21
{
22
    public const VIEW = 'VIEW';
23
24
    public function __construct(
25
        private readonly Security $security,
26
        private readonly IsAllowedToEditHelper $isAllowedToEditHelper,
27
    ) {}
28
29
    /**
30
     * @inheritDoc
31
     */
32
    protected function supports(string $attribute, mixed $subject): bool
33
    {
34
        $allowed = [
35
            self::VIEW,
36
        ];
37
38
        return $subject instanceof TrackEAttemptQualify && \in_array($attribute, $allowed);
39
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44
    protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool
45
    {
46
        $user = $token->getUser();
47
48
        if (!$user instanceof UserInterface) {
49
            return false;
50
        }
51
52
        if ($this->security->isGranted('ROLE_ADMIN')) {
53
            return true;
54
        }
55
56
        assert($user instanceof User);
57
        assert($subject instanceof TrackEAttemptQualify);
58
59
        $trackExercise = $subject->getTrackExercise();
60
        $session = $trackExercise->getSession();
61
        $course = $trackExercise->getCourse();
62
63
        $isAllowedToEdit = $this->isAllowedToEditHelper->check(false, true, false, true, $course, $session) || $user->isCourseTutor();
64
65
        if ($isAllowedToEdit) {
66
            return true;
67
        }
68
69
        if ($trackExercise->getUser()->getId() === $user->getId()) {
70
            return true;
71
        }
72
73
        return false;
74
    }
75
}