|
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
|
|
|
} |