src/Security/UserVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\ApiUser;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface;
  7. class UserVoter extends Voter
  8. {
  9.     // these strings are just invented: you can use anything
  10.     const VIEW 'view';
  11.     const EDIT 'edit';
  12.     private $decisionManager;
  13.     /**
  14.      * UserVoter constructor.
  15.      * @param AccessDecisionManagerInterface $decisionManager
  16.      */
  17.     public function __construct(AccessDecisionManagerInterface $decisionManager)
  18.     {
  19.         $this->decisionManager $decisionManager;
  20.     }
  21.     /**
  22.      * @param string $attribute
  23.      * @param mixed $subject
  24.      * @return bool
  25.      */
  26.     protected function supports($attribute$subject)
  27.     {
  28.         // if the attribute isn't one we support, return false
  29.         if (!in_array(
  30.             $attribute,
  31.             array(
  32.                 self::VIEW,
  33.                 self::EDIT,
  34.             )
  35.         )) {
  36.             return false;
  37.         }
  38.         // only vote on User objects inside this voter
  39.         if (!$subject instanceof ApiUser) {
  40.             return false;
  41.         }
  42.         return true;
  43.     }
  44.     /**
  45.      * @param string $attribute
  46.      * @param mixed $subject
  47.      * @param TokenInterface $token
  48.      * @return bool
  49.      */
  50.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  51.     {
  52.         $user $token->getUser();
  53.         if (!$user instanceof ApiUser) {
  54.             // the user must be logged in; if not, deny access
  55.             return false;
  56.         }
  57.         // ROLE_SUPER_ADMIN can do anything! The power!
  58.         if ($this->decisionManager->decide(
  59.             $token,
  60.             array('ROLE_ADMIN')
  61.         )) {
  62.             return true;
  63.         }
  64.         // you know $subject is a User object, thanks to supports
  65.         /** @var ApiUser $userSubject */
  66.         $userSubject $subject;
  67.         switch ($attribute) {
  68.             case self::VIEW:
  69.                 return $this->canView(
  70.                     $userSubject,
  71.                     $user
  72.                 );
  73.             case self::EDIT:
  74.                 return $this->canEdit(
  75.                     $userSubject,
  76.                     $user
  77.                 );
  78.         }
  79.         throw new \LogicException('This code should not be reached!');
  80.     }
  81.     /**
  82.      * @param ApiUser $userSubject
  83.      * @param ApiUser $user
  84.      * @return bool
  85.      */
  86.     private function canView(ApiUser $userSubjectApiUser $user)
  87.     {
  88.         // if they can edit, they can view
  89.         if ($this->canEdit(
  90.             $userSubject,
  91.             $user
  92.         )) {
  93.             return true;
  94.         }
  95.         // the User object could have, for example, a method isPrivate()
  96.         // that checks a boolean $private property
  97.         return $user === $userSubject;
  98.     }
  99.     /**
  100.      * @param ApiUser $userSubject
  101.      * @param ApiUser $user
  102.      * @return bool
  103.      */
  104.     private function canEdit(ApiUser $userSubjectApiUser $user)
  105.     {
  106.         // this assumes that the data object has a getOwner() method
  107.         // to get the entity of the user who owns this data object
  108.         return $user === $userSubject;
  109.     }
  110. }