src/Security/Voter/Scholar/Front/TrainingVoter.php line 21

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the nellapp package.
  4. *
  5. * (c) Benjamin Georgeault
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace App\Security\Voter\Scholar\Front;
  11. use App\Entity\Scholar\Training\Training;
  12. use App\Service\Scholar\ScholarNavigationSecurityService;
  13. use Nellapp\Bundle\SDKBundle\Auth\Entity\UserInterface;
  14. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  15. use Symfony\Component\Security\Core\Authorization\Voter\VoterInterface;
  16. class TrainingVoter implements VoterInterface
  17. {
  18. public function __construct(
  19. private ScholarNavigationSecurityService $scholarNavigationSecurityService,
  20. )
  21. {
  22. }
  23. private function isSubjectValid($subject): bool
  24. {
  25. if (!is_array($subject)) {
  26. return false;
  27. }
  28. if (
  29. !array_key_exists('training', $subject)
  30. || array_key_exists('module', $subject)
  31. || array_key_exists('lesson', $subject)
  32. ) {
  33. return false;
  34. }
  35. $training = $subject['training'];
  36. return $training instanceof Training;
  37. }
  38. public function vote(TokenInterface $token, $subject, array $attributes): int
  39. {
  40. $user = $token->getUser();
  41. if (!$user instanceof UserInterface) {
  42. return false;
  43. }
  44. if (!$this->isSubjectValid($subject)) {
  45. return self::ACCESS_ABSTAIN;
  46. }
  47. if (!in_array('FRONT_ACCESS', $attributes)) {
  48. return self::ACCESS_ABSTAIN;
  49. }
  50. return $this->scholarNavigationSecurityService
  51. ->canAccessTraining($subject['training']) ? self::ACCESS_GRANTED : self::ACCESS_DENIED;
  52. }
  53. }