From 26e375f460bce2713438773e9969f2b7c45c28a5 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos <1697880+AngelFQC@users.noreply.github.com> Date: Wed, 25 Sep 2024 02:35:10 -0500 Subject: [PATCH] Add TrackEAttemptQualify voter --- .../Voter/TrackEAttemptQualifyVoter.php | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/CoreBundle/Security/Authorization/Voter/TrackEAttemptQualifyVoter.php diff --git a/src/CoreBundle/Security/Authorization/Voter/TrackEAttemptQualifyVoter.php b/src/CoreBundle/Security/Authorization/Voter/TrackEAttemptQualifyVoter.php new file mode 100644 index 0000000000..a29b6cbfd6 --- /dev/null +++ b/src/CoreBundle/Security/Authorization/Voter/TrackEAttemptQualifyVoter.php @@ -0,0 +1,75 @@ + + */ +class TrackEAttemptQualifyVoter extends Voter +{ + public const VIEW = 'VIEW'; + + public function __construct( + private readonly Security $security, + private readonly IsAllowedToEditHelper $isAllowedToEditHelper, + ) {} + + /** + * @inheritDoc + */ + protected function supports(string $attribute, mixed $subject): bool + { + $allowed = [ + self::VIEW, + ]; + + return $subject instanceof TrackEAttemptQualify && \in_array($attribute, $allowed); + } + + /** + * @inheritDoc + */ + protected function voteOnAttribute(string $attribute, mixed $subject, TokenInterface $token): bool + { + $user = $token->getUser(); + + if (!$user instanceof UserInterface) { + return false; + } + + if ($this->security->isGranted('ROLE_ADMIN')) { + return true; + } + + assert($user instanceof User); + assert($subject instanceof TrackEAttemptQualify); + + $trackExercise = $subject->getTrackExercise(); + $session = $trackExercise->getSession(); + $course = $trackExercise->getCourse(); + + $isAllowedToEdit = $this->isAllowedToEditHelper->check(false, true, false, true, $course, $session) || $user->isCourseTutor(); + + if ($isAllowedToEdit) { + return true; + } + + if ($trackExercise->getUser()->getId() === $user->getId()) { + return true; + } + + return false; + } +} \ No newline at end of file