src/Security/UserStatusVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Backoffice\Enum\UserStatusEnum;
  4. use AppBundle\Entity\User;
  5. use AppBundle\Enum\FeatureAccessEnum;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. use Symfony\Component\Security\Core\Security;
  9. /**
  10.  * Class UserStatusVoter
  11.  */
  12. class UserStatusVoter extends Voter
  13. {
  14.     const USER_STATUS_UNSUBSCRIBED 'user.status.unsubscribed';
  15.     /** 
  16.      * @var Security 
  17.      */
  18.     private $security;
  19.     public function __construct(Security $security)
  20.     {
  21.         $this->security $security;
  22.     }
  23.     /**
  24.      * @inheritDoc
  25.      */
  26.     protected function supports($attribute$subject)
  27.     {
  28.         return \in_array($attribute, [
  29.             self::USER_STATUS_UNSUBSCRIBED,
  30.         ]);
  31.     }
  32.     /**
  33.      * @inheritDoc
  34.      */
  35.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  36.     {
  37.         $user $token->getUser();
  38.         if (!$user instanceof User) {
  39.             return false;
  40.         }
  41.         switch ($attribute) {
  42.             case self::USER_STATUS_UNSUBSCRIBED:
  43.                 return $this->security->isGranted(FeatureAccessEnum::LAW_FIRM_ADMIN)
  44.                     && $user->getStatus() === UserStatusEnum::TAG_UNSUBSCRIBED;
  45.             default:
  46.                 throw new \LogicException('This code should not be reached');
  47.         }
  48.     }
  49. }