src/Security/Voters/UserVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voters;
  3. use App\Entity\User;
  4. use App\Security\SecurityTrait;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. use Symfony\Component\Security\Core\Security;
  8. class UserVoter extends Voter
  9. {
  10.     use SecurityTrait;
  11.     const LIST = "list_user";
  12.     const ADD "add_user";
  13.     const EDIT "edit_user";
  14.     const READ "read_user";
  15.     const REMOVE "remove_user";
  16.     const PASSWORD_EMAIL "password_email_user";
  17.     private $security;
  18.     public function __construct(Security $security)
  19.     {
  20.         $this->security $security;
  21.     }
  22.     protected function supports($attribute$subject)
  23.     {
  24.         return in_array($attribute, [self::LIST, self::ADDself::PASSWORD_EMAIL]) ||
  25.             (in_array($attribute, [
  26.                 self::EDIT,
  27.                 self::READ,
  28.                 self::REMOVE
  29.             ]) && $subject instanceof User);
  30.     }
  31.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  32.     {
  33.         $user $token->getUser();
  34.         if (!$user instanceof User) {
  35.             return false;
  36.         }
  37.         switch ($attribute) {
  38.             case self::LIST:
  39.                 return $this->isAdmin();
  40.             case self::ADD:
  41.                 return $this->isAdmin();
  42.             case self::EDIT:
  43.                 return $this->isAdmin();
  44.             case self::READ:
  45.                 return $this->isAdmin();
  46.             case self::REMOVE:
  47.                 return $this->isAdmin();
  48.             case self::PASSWORD_EMAIL:
  49.                 return $this->isAdmin();
  50.         }
  51.         throw new \LogicException('This code should not be reached!');
  52.     }
  53. }