src/Security/Voter/UserVoter.php line 22

Open in your IDE?
  1. <?php
  2. /*
  3.  * To change this license header, choose License Headers in Project Properties.
  4.  * To change this template file, choose Tools | Templates
  5.  * and open the template in the editor.
  6.  */
  7. namespace App\Security\Voter;
  8. use App\Entity\Security\User;
  9. use LogicException;
  10. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  11. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  12. use Symfony\Component\Security\Core\Security;
  13. /**
  14.  * Description of UserVoter
  15.  *
  16.  * @author NOUTCHEU Blaise
  17.  */
  18. class UserVoter extends Voter {
  19.     // these strings are just invented: you can use anything
  20.     const VIEW 'view';
  21.     const EDIT_PASSWORD 'ROLE_SECURITY_USER_EDIT_PASSWORD';
  22.     private $security;
  23.     public function __construct(Security $security) {
  24.         $this->security $security;
  25.     }
  26.     private function canView(User $user_User $user) {
  27.         if ($this->canEditPassword($user_$user)) {
  28.             return true;
  29.         }
  30.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  31.             return true;
  32.         }
  33.         if ($this->security->isGranted('ROLE_DEV')) {
  34.             return true;
  35.         }
  36.         
  37.         return false;
  38.     }
  39.     private function canEditPassword(User $user_User $user) {
  40.         return $user === $user_;
  41.     }
  42.     protected function supports(string $attribute$subject): bool {
  43.         // if the attribute isn't one we support, return false
  44.         if (!in_array($attribute, [self::VIEWself::EDIT_PASSWORD])) {
  45.             return false;
  46.         }
  47.         // only vote on `User` objects
  48.         if (!$subject instanceof User) {
  49.             return false;
  50.         }
  51.         return true;
  52.         
  53.     }
  54.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool {
  55.         
  56.         $user $token->getUser();
  57.         // ROLE_SUPER_ADMIN can do anything! The power!
  58.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  59.             return true;
  60.         }
  61.         if ($this->security->isGranted('ROLE_DEV')) {
  62.             return true;
  63.         }
  64.         if (!$user instanceof User) {
  65.             // the user must be logged in; if not, deny access
  66.             return false;
  67.         }
  68.         // you know $subject is a User object, thanks to `supports()`
  69.         /** @var User $user_ */
  70.         $user_ $subject;
  71.         switch ($attribute) {
  72.             case self::VIEW:
  73.                 return $this->canView($user_$user);
  74.             case self::EDIT_PASSWORD:
  75.                 return $this->canEditPassword($user_$user);
  76.         }
  77.         throw new LogicException('This code should not be reached!');
  78.     }
  79. }