src/Security/Voter/StatefulEntityShowVoter.php line 17

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\ClinicalQuestion\ClinicalQuestion;
  5. use App\Entity\Consultation\Report;
  6. use App\Entity\StatefulInterface;
  7. use App\Entity\User;
  8. use App\Model\Enum\Status;
  9. use Carbon\CarbonImmutable;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. class StatefulEntityShowVoter extends Voter
  14. {
  15.     public const SHOW 'SHOW';
  16.     private AuthorizationCheckerInterface $authorizationChecker;
  17.     /**
  18.      * ShowVoter constructor.
  19.      */
  20.     public function __construct(AuthorizationCheckerInterface $authorizationChecker)
  21.     {
  22.         $this->authorizationChecker $authorizationChecker;
  23.     }
  24.     protected function supports(string $attribute$subject)
  25.     {
  26.         if ($attribute === self::SHOW && $subject instanceof StatefulInterface) {
  27.             return true;
  28.         }
  29.         return false;
  30.     }
  31.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  32.     {
  33.         assert($subject instanceof StatefulInterface);
  34.         if ($this->authorizationChecker->isGranted(User::ROLE_NVC)) {
  35.             return true;
  36.         }
  37.         // 公開ステータス出なければ不可
  38.         if ($subject->getStatus() !== Status::PUBLISHED()) {
  39.             return false;
  40.         }
  41.         // ゲスト会員の時
  42.         if (!$this->authorizationChecker->isGranted('ROLE_MEMBER')) {
  43.             if ($subject instanceof Report) {
  44.                 // 報告日をチェック
  45.                 if (!$subject->getReportedDate()) {
  46.                     return false;
  47.                 }
  48.                 if ($subject->getReportedDate() < new CarbonImmutable(Report::GUEST_SHOW_LIMIT)) {
  49.                     return false;
  50.                 }
  51.             }
  52.             // ゲストはCQを閲覧できない。
  53.             if ($subject instanceof ClinicalQuestion) {
  54.                 return false;
  55.             }
  56.         }
  57.         return true;
  58.     }
  59. }