src/Security/UserVoter.php line 13

Open in your IDE?
  1. <?php
  2. // src/Security/UserVoter.php
  3. namespace App\Security;
  4. use App\Entity\User;
  5. use Symfony\Component\Security\Core\Security;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. /**
  9.  * @see https://symfony.com/doc/current/security/voters.html
  10.  */
  11. class UserVoter extends Voter
  12. {
  13.     // these strings are made up: you can use anything
  14.     const VIEW 'view';
  15.     const EDIT 'edit';
  16.     /** @var Security */
  17.     private $security;
  18.     public function __construct(Security $security)
  19.     {
  20.         $this->security $security;
  21.     }
  22.     /**
  23.      * Determine if the voter supports given attribute and subject
  24.      * 
  25.      * @param string $attribute
  26.      * @param $subject
  27.      * 
  28.      * @return bool
  29.      */
  30.     protected function supports($attribute$subject)
  31.     {
  32.         // if the attribute isn't one we support, return false
  33.         if (!in_array($attribute, [self::VIEWself::EDIT])) {
  34.             return false;
  35.         }
  36.         // only vote on User objects inside this voter
  37.         if (!$subject instanceof User) {
  38.             return false;
  39.         }
  40.         return true;
  41.     }
  42.     /**
  43.      * Vote to grant (return true) or deny (return false) access
  44.      *
  45.      * @param string $attribute
  46.      * @param User $subject
  47.      * @param TokenInterface $token
  48.      *
  49.      * @return bool
  50.      * 
  51.      * @throws \LogicException when an invalid attribute is provided
  52.      */
  53.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  54.     {
  55.         $currentUser $token->getUser();
  56.         if (!$currentUser instanceof User) {
  57.             // the user must be logged in; if not, deny access
  58.             return false;
  59.         }
  60.         // Admins can do anything! The power!
  61.         if ($this->security->isGranted('ROLE_ADMIN')) {
  62.             return true;
  63.         }
  64.         switch ($attribute) {
  65.             case self::VIEW:
  66.                 return $this->canView($subject$currentUser);
  67.             case self::EDIT:
  68.                 return $this->canEdit($subject$currentUser);
  69.         }
  70.         throw new \LogicException('This code should not be reached!');
  71.     }
  72.     private function canView(User $accessedUserUser $currentUser)
  73.     {
  74.         // if they can edit, they can view
  75.         if ($this->canEdit($accessedUser$currentUser)) {
  76.             return true;
  77.         }
  78.         // the Post object could have, for example, a method isPrivate()
  79.         // that checks a boolean $private property
  80.         // return !$accessedUser->isPrivate();
  81.         return false;
  82.     }
  83.     private function canEdit(User $accessedUserUser $currentUser)
  84.     {
  85.         // this assumes that the data object has a getOwner() method
  86.         // to get the entity of the user who owns this data object
  87.         return $currentUser === $accessedUser;
  88.     }
  89. }