src/Security/UserVoter.php line 11

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