src/Security/BridgeVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\User;
  4. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  5. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  6. use Symfony\Component\DependencyInjection\ContainerInterface;
  7. use Symfony\Component\Security\Core\Role\RoleHierarchyInterface;
  8. use Symfony\Component\Security\Core\Role\Role;
  9. use Symfony\Component\Security\Core\Security;
  10. use App\Mask\UserRolesMask;
  11. use Lexik\Bundle\JWTAuthenticationBundle\Security\User\JWTUser;
  12. use Doctrine\ORM\EntityManagerInterface;
  13. class BridgeVoter extends Voter {
  14. // these strings are just invented: you can use anything
  15.     const READ 'READ';
  16.     const EDIT 'EDIT';
  17.     private $container;
  18.     private $security;
  19.     private $userRolesMask;
  20.     private $entityManager;
  21.     private $roleHierarchy;
  22.     // modifs 12/12 Guillaume
  23.     private $entites_sans_partage;
  24.     private $user_admin_editions;
  25.     public function __construct(
  26.         ContainerInterface $container,
  27.         Security $security,
  28.         UserRolesMask $userRolesMask,
  29.         EntityManagerInterface $entityManager,
  30.         RoleHierarchyInterface $roleHierarchy
  31.     ) {
  32.         $this->security $security;
  33.         $this->container $container;
  34.         $this->userRolesMask $userRolesMask;
  35.         $this->entityManager $entityManager;
  36.         $this->roleHierarchy $roleHierarchy;
  37.         // modifs 12/12 Guillaume
  38.         $userNameTemp $this->container->getParameter('user_admin_editions');
  39.         if($userNameTemp !='')
  40.             $this->user_admin_editions $this->entityManager->getRepository("App\Entity\User")->findOneBy(array('username' => $userNameTemp));
  41.         $entites_sans_partage $this->container->getParameter('entites_sans_partage');
  42.         $this->entites_sans_partage explode(',',$entites_sans_partage);
  43.         if(!is_array($this->entites_sans_partage))
  44.             $this->entites_sans_partage = array($this->entites_sans_partage);
  45.     }
  46.     protected function supports$attribute$subject ) {
  47.         $supportsAttribute in_array($attribute, ['READ''EDIT']);
  48.         return $supportsAttribute;
  49.     }
  50.     protected function voteOnAttribute$attribute$subjectTokenInterface $token ) {
  51.         $disable_data_voter $this->container->getParameter('disable_data_voter');
  52.         if($disable_data_voter == "true") {
  53.             return true;
  54.         }
  55.         // Le voter ne s'applique que pour les entités qui ont des propriétés owner ET entity
  56.         if ( ! method_exists $subject 'getOwner' ) || !method_exists $subject 'getEntity' ) )
  57.         {
  58.             return true;
  59.         }
  60.         $JWTuser $token->getUser();
  61.         if ( ! $JWTuser instanceof JWTUser )
  62.         {
  63.             // the user must be logged in; if not, deny access
  64.             return false;
  65.         }
  66.         $username $JWTuser->getUsername();
  67.         $user $this->entityManager->getRepository('App\\Entity\\User')->findOneBy(['username' => $username]);
  68.         switch ( $attribute )
  69.         {
  70.             case self::READ:
  71.                 return $this->canView$subject$user );
  72.             case self::EDIT:
  73.                 return $this->canEdit$subject$user );
  74.         }
  75.         throw new \LogicException'This code should not be reached!' );
  76.     }
  77.     private function canView$objectUser $user ) {
  78.     // if they can edit, they can view
  79.         $owner $object->getOwner();
  80.         $entity $object->getEntity();
  81.         $currentEntity $user->getEntity();
  82.         $currentEntityId '';
  83.         $user_admin_editions $this->container->getParameter('user_admin_editions');
  84.         if(isset($currentEntity))
  85.             $currentEntityId = (string) $currentEntity->getId();
  86.         if ( $this->canEdit$object$user ) )
  87.         {
  88.             return true;
  89.         }
  90.         // Ici on checke les roles
  91.         $refRoles $this->userRolesMask::ENTITY_ROLES;
  92.         $class get_class($object);
  93.         $classTrimmed substr($class11);
  94.         if(isset($refRoles[$classTrimmed])) {
  95.             $baseRole $refRoles[$classTrimmed] . '_RO';
  96.             // On récupère les rôles hérités (ou sous-rôles) du user
  97.             $userRoles $this->roleHierarchy->getReachableRoleNames($this->security->getUser()->getRoles());
  98.             if (in_array($baseRole$userRoles)) {
  99.                 $ownerId '';
  100.                 $userId '';
  101.                 $entityId '';
  102.                 if(isset($owner)) {
  103.                     $ownerId $owner->getId();
  104.                     // Cas spécial du user admin éditions dont tous doivent pouvoir voir les données en lecture seule
  105.                     $ownerName $owner->getUsername();
  106.                     if($user_admin_editions == $ownerName)
  107.                         return true;
  108.                 }
  109.                 if(isset($user))
  110.                     $userId $user->getId();
  111.                 if(isset($entity))
  112.                     $entityId $entity->getId();
  113.                 // return ($owner === $user || ($entity === $currentEntity && isset($currentEntity) && !in_array($currentEntityId,$this->entites_sans_partage)));
  114.                 return ($ownerId == $userId || ($entityId == $currentEntityId && isset($currentEntity) && !in_array($currentEntityId,$this->entites_sans_partage)));
  115.             }
  116.         }
  117.         return false;
  118.     }
  119.     private function canEdit$objectUser $user ) {
  120.         // die('coucdcou');
  121.         $owner $object->getOwner();
  122.         $entity $object->getEntity();
  123.         $currentEntity $user->getEntity();
  124.         $currentEntityId '';
  125.         if(isset($currentEntity))
  126.             $currentEntityId = (string) $currentEntity->getId();
  127.         // modifs 12/12 Guillaume
  128.         /*$username = $user->getUsername();
  129.         $userDoctrine = $this->entityManager->getRepository('App\\Entity\\User')->findOneBy(['username' => $username]);
  130.         $userEntity = $userDoctrine->getEntity();*/
  131.         // Ici on checke les roles
  132.         $refRoles $this->userRolesMask::ENTITY_ROLES;
  133.         $class get_class($object);
  134.         $classTrimmed substr($class11);
  135.         if(isset($refRoles[$classTrimmed])) {
  136.             $baseRole $refRoles[$classTrimmed] . '_RW';
  137.             // On récupère les rôles hérités (ou sous-rôles) du user
  138.             $userRoles $this->roleHierarchy->getReachableRoleNames($this->security->getUser()->getRoles());
  139.             if (in_array($baseRole$userRoles)) {
  140.                 // print_r($userRoles);
  141.                 $ownerId '';
  142.                 $userId '';
  143.                 $entityId '';
  144.                 if(isset($owner))
  145.                     $ownerId $owner->getId();
  146.                 if(isset($user))
  147.                     $userId $user->getId();
  148.                 if(isset($entity))
  149.                     $entityId $entity->getId();
  150.                 //return ($owner === $user || $entity === $currentEntity);
  151.                 return ($ownerId == $userId || ($entityId == $currentEntityId && isset($currentEntity) && !in_array($currentEntityId,$this->entites_sans_partage)));
  152.             }
  153.         }
  154.         return false;
  155.     }
  156. }