src/Security/Voter/OficinaVoter.php line 8

  1. <?php
  2. namespace App\Security\Voter;
  3. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  4. use App\Entity\Oficina;
  5. class OficinaVoter extends Voter
  6. {
  7.     
  8.     public function supports($attribute$subject): bool
  9.     {
  10. /*        if ($subject) {
  11.              var_dump(get_class($subject));echo "-";
  12.              var_dump(($subject instanceof User));echo "#";
  13.         }     */   
  14.         
  15.         return $subject instanceof Oficina && in_array($attribute, array(
  16.             'ROLE_APP_ADMIN_OFICINA_EDIT',
  17.             'ROLE_APP_ADMIN_OFICINA_LIST',
  18.         ));
  19.     }
  20.     
  21.     protected function voteOnAttribute($attribute$object$token): bool
  22.     {
  23. //         echo $attribute;
  24. //         return true;
  25.         
  26.         $user $token->getUser();
  27.         
  28.         if ($user->hasRole('ROLE_SUPER_ADMIN') or $user->hasRole('ROLE_ADMIN')) { //Acceso de super admin o administrador
  29.             return true;
  30.         }
  31.         
  32.         // los managers solo puede editar su oficina
  33.         if ((in_array($attribute, ['ROLE_APP_ADMIN_OFICINA_EDIT']) and $user->hasRole('ROLE_MANAGER')  and $object->getId() == $user->getOficinaGestion()->getId()) or in_array($attribute, ['ROLE_APP_ADMIN_OFICINA_LIST'])) {
  34.             return true;
  35.         }
  36.         
  37.         return false;
  38.     }
  39.     
  40. }