src/Security/Voter/HelpQuestionVoter.php line 17

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\DBAL\Types\RoleEnumType;
  5. use App\Entity\Lesson;
  6. use App\Entity\User;
  7. use App\Service\DutyService;
  8. use App\Service\HelpQuestionService;
  9. use DateTimeImmutable;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. use Symfony\Component\Security\Core\User\UserInterface;
  13. class HelpQuestionVoter extends Voter
  14. {
  15.     public const WRITE_QUESTION 'WRITE_HELP_QUESTION';
  16.     public const READ_LIST 'READ_HELP_LIST';
  17.     private const ACCESSED_ROLES = [
  18.         RoleEnumType::ROLE_CURATOR,
  19.         RoleEnumType::ROLE_MODERATOR,
  20.         RoleEnumType::ROLE_SUPER_ADMIN,
  21.         RoleEnumType::ROLE_ADMIN,
  22.     ];
  23.     private HelpQuestionService $helpQuestionService;
  24.     private DutyService $dutyService;
  25.     public function __construct(HelpQuestionService $helpQuestionServiceDutyService $dutyService)
  26.     {
  27.         $this->helpQuestionService $helpQuestionService;
  28.         $this->dutyService $dutyService;
  29.     }
  30.     protected function supports($attribute$subject): bool
  31.     {
  32.         return in_array($attribute, [self::WRITE_QUESTIONself::READ_LIST])
  33.             && $subject instanceof Lesson;
  34.     }
  35.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  36.     {
  37.         /** @var User $user */
  38.         $user $token->getUser();
  39.         if (!$user instanceof UserInterface) {
  40.             return false;
  41.         }
  42.         foreach (self::ACCESSED_ROLES as $role) {
  43.             if ($user->hasRole($role)) {
  44.                 return true;
  45.             }
  46.         }
  47.         $helpQuestionDisabledUntilDate DateTimeImmutable::createFromFormat('Y-m-d'$_ENV['OPTION_HELP_QUESTION_DISABLED_UNTIL_DATE']);
  48.         switch ($attribute) {
  49.             case self::READ_LIST:
  50.                 return $this->helpQuestionService->userHasAccessToHelpQuestion($user$subject);
  51.             case self::WRITE_QUESTION:
  52.                 return $this->helpQuestionService->userHasAccessToHelpQuestion($user$subject)
  53.                     && $user->hasCuratorSupportsInRocket()
  54.                     && $this->dutyService->issetDutyCurators()
  55.                     && $helpQuestionDisabledUntilDate $now = new DateTimeImmutable();
  56.         }
  57.         return false;
  58.     }
  59. }