src/Security/Voter/UserVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  4. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use App\Entity\User;
  7. use Symfony\Component\Security\Core\Security;
  8. class UserVoter extends Voter
  9. {
  10.     const VIEW ="ver";
  11.     const EDIT ="editar";
  12.     const DELETE "eliminar";
  13.     private $security;
  14.     public function __construct(Security $security)
  15.     {
  16.         $this->security $security;
  17.     }
  18.     protected function supports($attribute$subject)
  19.     {
  20.         // if the attribute isn't one we support, return false
  21.         if (!in_array($attribute, [self::VIEWself::EDITself::DELETE])) {
  22.             return false;
  23.         }
  24.         // only vote on `User` objects
  25.         if (!$subject instanceof User) {
  26.             return false;
  27.         }
  28.         return true;
  29.     }
  30.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  31.     {
  32.         $admin $token->getUser();
  33.         // if the user is anonymous, do not grant access
  34.         if (!$admin instanceof UserInterface) {
  35.             return false;
  36.         }
  37.         // ROLE_ADMIN can do anything! The power!
  38.         if ($this->security->isGranted('ROLE_ADMIN')) {
  39.             return true;
  40.         }
  41.         // ... (check conditions and return true to grant permission) ...
  42.         switch ($attribute) {
  43.             case self::VIEW:
  44.                 return $this->canView($subject$admin);
  45.             case self::EDIT:
  46.                 return $this->canEdit($subject$admin);
  47.             case self::DELETE:
  48.                 return $this->canDelete($subject$admin);
  49.         }
  50.         return false
  51.     }
  52.     
  53.     private function canView(User $userUser $admin)
  54.     {        
  55.         return $admin->getCompany() === $user->getCompany();
  56.     }
  57.     private function canEdit(User $userUser $admin)
  58.     {
  59.         
  60.         // ROLE_ADMIN can do anything! The power!
  61.         if ( $admin->getRole() == 'ROLE_CHIEF' || $admin->getRole() == 'ROLE_SUPPLIER_CHIEF') {
  62.             
  63.             return $admin->getCompany() === $user->getCompany();
  64.         }else{
  65.             
  66.             return $admin->getId() === $user->getId();        
  67.         }
  68.         
  69.     }
  70.     private function canDelete(User $userUser $admin)
  71.     {
  72.         if ($admin->getId() !== $user->getId())
  73.         {
  74.             return $admin->getCompany() === $user->getCompany();
  75.         }
  76.         return false;
  77.         
  78.     }
  79. }