src/Modules/User/Security/UserVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Modules\User\Security;
  3. use App\Modules\User\Entity\UserContract;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\Security\Core\Security;
  7. class UserVoter extends Voter
  8. {
  9.     const UPDATE_USER 'update_user';
  10.     const DELETE_USER 'delete_user';
  11.     static $access = [
  12.         self::UPDATE_USER,
  13.         self::DELETE_USER,
  14.     ];
  15.     private $security;
  16.     public function __construct(Security $security)
  17.     {
  18.         $this->security $security;
  19.     }
  20.     /**
  21.      * Determines if the attribute and subject are supported by this voter.
  22.      *
  23.      * @param string $attribute An attribute
  24.      * @param mixed  $subject   The subject to secure, e.g. an object the user wants to access or any other PHP type
  25.      *
  26.      * @return bool True if the attribute and subject are supported, false otherwise
  27.      */
  28.     protected function supports($attribute$subject)
  29.     {
  30.         if (!in_array($attributeself::$access)) {
  31.             return false;
  32.         }
  33.         return true;
  34.     }
  35.     /**
  36.      * Perform a single access check operation on a given attribute, subject and token.
  37.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  38.      *
  39.      * @param string         $attribute
  40.      * @param mixed          $subject
  41.      *
  42.      * @param TokenInterface $token
  43.      *
  44.      * @return bool
  45.      */
  46.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  47.     {
  48.         $loginedUser $token->getUser();
  49.         if (!$loginedUser instanceof UserContract) {
  50.             // the user must be logged in; if not, deny access
  51.             return false;
  52.         }
  53.         $user $subject;
  54.         switch ($attribute) {
  55.             case self::UPDATE_USER:
  56.                 return $this->canUpdate($user$loginedUser);
  57.             case self::DELETE_USER:
  58.                 return $this->canDelete($user$loginedUser);
  59.         }
  60.     }
  61.     private function canUpdate(UserContract $userUserContract $loginedUser)
  62.     {
  63.         if ($this->security->isGranted('ROLE_ADMIN') ||
  64.             $user === $loginedUser
  65.         ) {
  66.             return true;
  67.         }
  68.         return false;
  69.     }
  70.     private function canDelete(UserContract $userUserContract $loginedUser)
  71.     {
  72.         if ($this->security->isGranted('ROLE_ADMIN') ||
  73.             $user === $loginedUser
  74.         ) {
  75.             return true;
  76.         }
  77.         return false;
  78.     }
  79. }