src/Security/Voter/StatefulEntityEditVoter.php line 13

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Security\Voter;
  4. use App\Entity\StatefulInterface;
  5. use App\Entity\User;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class StatefulEntityEditVoter extends Voter
  10. {
  11.     public const EDIT 'EDIT';
  12.     private AuthorizationCheckerInterface $authorizationChecker;
  13.     /**
  14.      * ShowVoter constructor.
  15.      */
  16.     public function __construct(AuthorizationCheckerInterface $authorizationChecker)
  17.     {
  18.         $this->authorizationChecker $authorizationChecker;
  19.     }
  20.     protected function supports(string $attribute$subject)
  21.     {
  22.         if ($attribute === self::EDIT && $subject instanceof StatefulInterface) {
  23.             return true;
  24.         }
  25.         return false;
  26.     }
  27.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  28.     {
  29.         assert($subject instanceof StatefulInterface);
  30.         // 管理者は全て編集可能
  31.         if ($this->authorizationChecker->isGranted(User::ROLE_SUPER_ADMIN)) {
  32.             return true;
  33.         }
  34.         // 編集者は下書または完了のみ
  35.         if ($this->authorizationChecker->isGranted(User::ROLE_EDITOR)) {
  36.             return $subject->getStatus()->isEditableByEditor();
  37.         }
  38.         return false;
  39.     }
  40. }