src/Security/AccessVoter.php line 16

Open in your IDE?
  1. <?php
  2. namespace App\Security;
  3. use App\Entity\Kernel\User;
  4. use App\Entity\Kernel\SecurityAction;
  5. use App\Entity\Kernel\UserLog;
  6. use App\Entity\Manufacturing\WorkCenter;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\HttpKernel\KernelInterface;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  12. class AccessVoter extends Voter
  13. {
  14.     // these strings are just invented: you can use anything
  15.     const VIEW 'view';
  16.     const EDIT 'edit';
  17.     private $em;
  18.     private $environment;
  19.     private $params;
  20.     
  21.     public function __construct(EntityManagerInterface $emKernelInterface $kernelContainerBagInterface $params)
  22.     {
  23.         $this->em $em;
  24.         $this->environment $kernel->getEnvironment();
  25.         $this->params $params;
  26.     }
  27.     
  28.     protected function supports(string $attribute$subject)
  29.     {
  30.         // if the attribute isn't one we support, return false
  31.         /*if (!in_array($attribute, [self::VIEW, self::EDIT])) {
  32.             return false;
  33.         }
  34.         // only vote on `Post` objects
  35.         if (!$subject instanceof Post) {
  36.             return false;
  37.         }*/
  38.         return true;
  39.     }
  40.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  41.     {
  42.         $user $token->getUser();
  43.         if (!$user instanceof User) {
  44.             // the user must be logged in; if not, deny access
  45.             //return false;
  46.         }
  47.         
  48.         $items explode ":" $attribute);
  49.         
  50.         if (isset($items[0]) && isset($items[1]))
  51.         {
  52.             $idealaction null;
  53.             
  54.             $actionentity $this->em->getRepository(SecurityAction::class)->findOneBy(array('entity' => $items[0], 'action' => $items[1], 'state' => 0));
  55.             if (!$actionentity)
  56.             {
  57.         $entity  = new SecurityAction();
  58.         $entity->setEntity($items[0]);
  59.         $entity->setAction($items[1]);
  60.                 $entity->setLogging(1);
  61.                 $entity->setState(0);
  62.         $this->em->persist($entity);
  63.         $this->em->flush();
  64.         $idealaction $entity;
  65.             }
  66.             else
  67.             {
  68.                 $idealaction $actionentity;
  69.             }
  70.             
  71.             if ($idealaction->getLogging() != 0)
  72.             {
  73.                 if ($this->environment === 'dev') {
  74.                     $log  = new UserLog();
  75.                     $log->setEntity($items[0]);
  76.                     $log->setAction($items[1]);
  77.                     $log->setOrganization($user->getOrganization());
  78.                     if ($subject != null)
  79.                     {
  80.                         $log->setElement($subject->getId());
  81.                     }
  82.                     $log->setState(0);
  83.                     $securityAction $this->em->getRepository(SecurityAction::class)
  84.                         ->findOneBy(array('entity' => $items[0], 'action' => $items[1], 'state' => 0));
  85.                     if($securityAction != null){
  86.                         $securityAction->setExecutionAction($securityAction->getExecutionAction() + 1);
  87.                     }
  88.                     $this->em->persist($log);
  89.                     $this->em->flush();
  90.                 }
  91.             }
  92.             
  93.             if ($this->params->get('app.desactive_security') == 1)
  94.             {
  95.                 return true;
  96.             }
  97.             
  98.             
  99.             //return true;
  100.             
  101.             
  102.                         
  103.             $userfunction $user->getUserfunction();
  104.             
  105.             $query $this->em->createQuery(
  106.             "SELECT e.id as id FROM App\Entity\Kernel\UserFunction  a
  107.                 JOIN a.roles b
  108.                 JOIN b.profile c
  109.                 JOIN c.actionprofiles d
  110.                 JOIN d.securityactions e
  111.                 WHERE (a.id = ?1)");
  112.             $query $query->setParameter(1$userfunction->getId());
  113.             $iterator $query->getResult();
  114.             //return true;
  115.             foreach($iterator as $actionid)
  116.             {
  117.                 if($actionid["id"] == $idealaction->getId())
  118.                 {
  119.                     return true;
  120.                 }
  121.             }
  122.             return false;
  123.             
  124.         /*
  125.             foreach ($userfunction->getRoles() as $role)
  126.             {
  127.                 $profile = $role->getProfile();
  128.                 foreach ($profile->getActionprofiles() as $actionprofiles)
  129.                 {
  130.                     foreach ($actionprofiles->getSecurityactions() as $securityactions)
  131.                     {
  132.                         if($securityactions->getId() == $idealaction->getId())
  133.                         {
  134.                             if ($items[0] == "Plant") {
  135.                                 if ($subject != null)
  136.                                 {
  137.                                     //$subject->getId()
  138.                                     if ($role->getPlantrights()->getName() == "AllOrSelection.Selection")
  139.                                     {
  140.                                         
  141.                                         if (($role->getPlant() != null) && ($role->getPlant()->getId() == $subject->getId()))
  142.                                         {
  143.                                             return true;
  144.                                         }
  145.                                     }
  146.                                     else
  147.                                     {
  148.                                         return true;
  149.                                     }
  150.                                 }
  151.                                 else
  152.                                 {
  153.                                     return true;
  154.                                 }
  155.                             }
  156.                             else
  157.                             {
  158.                                 if ($items[0] == "WorkCenter") {
  159.                                     
  160.                                     if ($subject != null)
  161.                                     {
  162.                                         //If WorkCenterCounter find workcenterID and workcenter
  163.                                         //...
  164.                                         $workcenterid = $subject->getId();
  165.                                         $workcenter = $this->em->getRepository(WorkCenter::class)->findOneById($workcenterid);
  166.                                         if ($role->getWorkcenterrights()->getName() == "AllOrSelection.Selection")
  167.                                         {
  168.                                             if (($role->getWorkcenter() != null) && ($role->getWorkcenter()->getId() == $workcenterid))
  169.                                             {
  170.                                                 return true;
  171.                                             }
  172.                                         }
  173.                                         else
  174.                                         {
  175.                                             if ($role->getPlantrights()->getName() == "AllOrSelection.Selection")
  176.                                             {
  177.                                                 if ($role->getPlant() != null)
  178.                                                 {
  179.                                                     if ($workcenter->getPlant()->getId() == $role->getPlant()->getId())
  180.                                                     {
  181.                                                         return true;
  182.                                                     }
  183.                                                 }
  184.                                             }
  185.                                             else
  186.                                             {
  187.                                                 return true;
  188.                                             }
  189.                                         }
  190.                                     }
  191.                                     else
  192.                                     {
  193.                                         return true;
  194.                                     }
  195.                                 }
  196.                                 else
  197.                                 {
  198.                                     return true;
  199.                                 }
  200.                             }
  201.                         }
  202.                     }
  203.                 }
  204.             }
  205.             return false;
  206.          * * 
  207.          */
  208.         }
  209.          
  210.         
  211.         return true;
  212.         
  213.         throw new \LogicException('This code should not be reached!');
  214.     }
  215.     private function canView($postUser $user)
  216.     {
  217.         // if they can edit, they can view
  218.         if ($this->canEdit($post$user)) {
  219.             return true;
  220.         }
  221.         // the Post object could have, for example, a method `isPrivate()`
  222.         return !$post->isPrivate();
  223.     }
  224.     private function canEdit($postUser $user)
  225.     {
  226.         // this assumes that the Post object has a `getOwner()` method
  227.         return $user === $post->getOwner();
  228.     }
  229. }