src/Security/UserCRUDVoter.php line 25

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Logics Tecnologia e Serviços LTDA.
  4.  * @author: Romeu Godoi <romeu@logics.com.br>
  5.  * Date: 04/12/19
  6.  * Time: 11:56
  7.  * @copyright Copyright (C) 2019 LogicSITE. Todos os Direitos Reservados.
  8.  * LogicSITE. Este software é de propriedade exclusiva da LOGICS TEC. E SERV. LTDA
  9.  * e seu uso só pode ser dado por usuários licenciados por escrito.
  10.  * O uso indevido desta plataforma, ou parte dela estará sujeita a penalidades
  11.  * previstas em lei, conforme legislação pertinente.
  12.  */
  13. namespace App\Security;
  14. use App\Application\Sonata\UserBundle\Entity\User;
  15. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  16. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  17. use Symfony\Component\Security\Core\Security;
  18. /**
  19.  * Class UserCRUDVoter
  20.  * @package App\Security
  21.  */
  22. class UserCRUDVoter extends Voter
  23. {
  24.     const LIST = 'ROLE_SONATA_USER_ADMIN_USER_LIST';
  25.     const VIEW 'ROLE_SONATA_USER_ADMIN_USER_VIEW';
  26.     const CREATE 'ROLE_SONATA_USER_ADMIN_USER_CREATE';
  27.     const EDIT 'ROLE_SONATA_USER_ADMIN_USER_EDIT';
  28.     const DELETE 'ROLE_SONATA_USER_ADMIN_USER_DELETE';
  29.     const ALL 'ROLE_SONATA_USER_ADMIN_USER_ALL';
  30.     
  31.     /**
  32.      * @var Security
  33.      */
  34.     private $security;
  35.     public function __construct(Security $security)
  36.     {
  37.         $this->security $security;
  38.     }
  39.     /**
  40.      * Determines if the attribute and subject are supported by this voter.
  41.      *
  42.      * @param string $attribute An attribute
  43.      * @param mixed $subject The subject to secure, e.g. an object the user wants to access or any other PHP type
  44.      *
  45.      * @return bool True if the attribute and subject are supported, false otherwise
  46.      */
  47.     protected function supports($attribute$subject)
  48.     {
  49.         // if the attribute isn't one we support, return false
  50.         if (!in_array($attribute, [self::LIST, self::VIEWself::CREATEself::EDITself::DELETEself::ALL])) {
  51.             return false;
  52.         }
  53.         // only vote on User objects inside this voter
  54.         if (!$subject instanceof User) {
  55.             return false;
  56.         }
  57.         return true;
  58.     }
  59.     /**
  60.      * Perform a single access check operation on a given attribute, subject and token.
  61.      * It is safe to assume that $attribute and $subject already passed the "supports()" method check.
  62.      *
  63.      * @param string $attribute
  64.      * @param mixed $subject
  65.      * @param TokenInterface $token
  66.      *
  67.      * @return bool
  68.      */
  69.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  70.     {
  71.         $userLogged $token->getUser();
  72.         if (!$userLogged instanceof User) {
  73.             // the user must be logged in; if not, deny access
  74.             return false;
  75.         }
  76.         // Se for um super admin do sistema retorna verdadeiro
  77.         if ($userLogged->isSuperAdmin()) {
  78.             return true;
  79.         }
  80.         // you know $subject is a User object, thanks to supports
  81.         /** @var User $obj */
  82.         $obj $subject;
  83.         switch ($attribute) {
  84.             case self::LIST:
  85.                 return $this->canList($obj$userLogged);
  86.             case self::VIEW:
  87.                 return $this->canView($obj$userLogged);
  88.             case self::CREATE:
  89.                 return $this->canCreate($obj$userLogged);
  90.             case self::EDIT:
  91.                 return $this->canEdit($obj$userLogged);
  92.             case self::DELETE:
  93.                 return $this->canDelete($obj$userLogged);
  94.             case self::ALL:
  95.                 return $this->canAll($obj$userLogged);
  96.         }
  97.         throw new \LogicException('This code should not be reached!');
  98.     }
  99.     private function canList(User $objUser $userLogged)
  100.     {
  101.         // if they can edit, they can view
  102.         if ($this->canEdit($obj$userLogged)) {
  103.             return true;
  104.         }
  105.         return false;
  106.     }
  107.     private function canCreate(User $objUser $userLogged)
  108.     {
  109.         // Se for um administrador do sistema, ok
  110.         if ($this->security->isGranted('ROLE_SUPER_ADMIN_MATRIZ')) {
  111.             return true;
  112.         }
  113.         // if they can edit, they can view
  114.         if ($this->canEdit($obj$userLogged)) {
  115.             return true;
  116.         }
  117.         return false;
  118.     }
  119.     private function canView(User $objUser $userLogged)
  120.     {
  121.         // Se for um administrador do sistema e o usuario visualizado não for, ok.
  122.         if ($this->security->isGranted('ROLE_SUPER_ADMIN_MATRIZ')) {
  123.             return true;
  124.         }
  125.         // if they can edit, they can view
  126.         if ($this->canEdit($obj$userLogged)) {
  127.             return true;
  128.         }
  129.         return false;
  130.     }
  131.     private function canEdit(User $objUser $userLogged)
  132.     {
  133.         // Se for um administrador do sistema e o usuario visualizado não for, ok.
  134.         if ($this->security->isGranted('ROLE_SUPER_ADMIN_MATRIZ')) {
  135.             return true;
  136.         }
  137.         // this assumes that the data object has himself to edit his data
  138.         return $userLogged === $obj;
  139.     }
  140.     private function canDelete(User $objUser $userLogged)
  141.     {
  142.         // Se for um administrador do sistema e o usuario visualizado não for, ok.
  143.         if ($this->security->isGranted('ROLE_SUPER_ADMIN_MATRIZ')) {
  144.             return true;
  145.         }
  146.         return false;
  147.     }
  148.     private function canAll(User $objUser $userLogged)
  149.     {
  150.         if ($obj === $userLogged) {
  151.             return true;
  152.         }
  153.         return $this->canDelete($obj$userLogged);
  154.     }
  155. }