src/Security/AccessVoter.php line 17

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 LogicException;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\HttpKernel\KernelInterface;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  13. class AccessVoter extends Voter
  14. {
  15.     // these strings are just invented: you can use anything
  16.     const VIEW 'view';
  17.     const EDIT 'edit';
  18.     private $em;
  19.     private $environment;
  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.             if ($user->getEnabled() != true)
  55.             {
  56.                 return false;
  57.             }
  58.             if ($user->getLocked() == true)
  59.             {
  60.                 return false;
  61.             }
  62.                 
  63.             
  64.             $actionentity $this->em->getRepository(SecurityAction::class)->findOneBy(array('entity' => $items[0], 'action' => $items[1], 'state' => 0));
  65.             if (!$actionentity)
  66.             {
  67.                 $entity = new SecurityAction();
  68.                 $entity->setEntity($items[0]);
  69.                 $entity->setAction($items[1]);
  70.                 $entity->setLogging(1);
  71.                 $entity->setState(0);
  72.                 $this->em->persist($entity);
  73.                 $this->em->flush();
  74.                 $idealaction $entity;
  75.             }
  76.             else
  77.             {
  78.                 $idealaction $actionentity;
  79.             }
  80.             
  81.             if ($idealaction->getLogging() != 0)
  82.             {
  83.                 //if ($this->environment === 'dev') {
  84.                     $log  = new UserLog();
  85.                     $log->setEntity($items[0]);
  86.                     $log->setAction($items[1]);
  87.                     $log->setOrganization($user->getOrganization());
  88.                     if ($subject != null) {
  89.                         $log->setElement($subject->getId());
  90.                     }
  91.                     $log->setState(0);
  92.                     $this->em->persist($log);
  93.                     
  94.                     if($idealaction != null){
  95.                         $idealaction->setExecutionAction($idealaction->getExecutionAction() + 1);
  96.                     }
  97.                     $this->em->persist($idealaction);
  98.                     
  99.                     $this->em->flush();
  100.                 //}
  101.             }
  102.             
  103.             if ($this->params->get('app.desactive_security') == 1)
  104.             {
  105.                 return true;
  106.             }
  107.             
  108.             
  109.             $userfunction $user->getUserfunction();
  110.             foreach ($userfunction->getRoles() as $role) {
  111.                 $profile $role->getProfile();
  112.                 foreach ($profile->getActionprofiles() as $actionprofiles) {
  113.                     foreach ($actionprofiles->getSecurityactions() as $securityactions)
  114.                     {
  115.                         if($securityactions->getId() == $idealaction->getId())
  116.                         {
  117.                             if ($items[0] == "Plant") {
  118.                                 if ($subject != null)
  119.                                 {
  120.                                     //$subject->getId()
  121.                                     if ($role->getPlantrights()->getName() == "AllOrSelection.Selection")
  122.                                     {
  123.                                         
  124.                                         if (($role->getPlant() != null) && ($role->getPlant()->getId() == $subject->getId()))
  125.                                         {
  126.                                             return true;
  127.                                         }
  128.                                     }
  129.                                     else
  130.                                     {
  131.                                         return true;
  132.                                     }
  133.                                 }
  134.                                 else
  135.                                 {
  136.                                     return true;
  137.                                 }
  138.                             }
  139.                             else
  140.                             {
  141.                                 if ($items[0] == "WorkCenter") {
  142.                                     
  143.                                     if ($subject != null)
  144.                                     {
  145.                                         //If WorkCenterCounter find workcenterID and workcenter
  146.                                         //...
  147.                                         $workcenterid $subject->getId();
  148.                                         $workcenter $this->em->getRepository(WorkCenter::class)->findOneById($workcenterid);
  149.                                         if ($role->getWorkcenterrights()->getName() == "AllOrSelection.Selection")
  150.                                         {
  151.                                             if (($role->getWorkcenter() != null) && ($role->getWorkcenter()->getId() == $workcenterid))
  152.                                             {
  153.                                                 return true;
  154.                                             }
  155.                                         }
  156.                                         else
  157.                                         {
  158.                                             if ($role->getPlantrights()->getName() == "AllOrSelection.Selection")
  159.                                             {
  160.                                                 if ($role->getPlant() != null)
  161.                                                 {
  162.                                                     if ($workcenter->getPlant()->getId() == $role->getPlant()->getId())
  163.                                                     {
  164.                                                         return true;
  165.                                                     }
  166.                                                 }
  167.                                             }
  168.                                             else
  169.                                             {
  170.                                                 return true;
  171.                                             }
  172.                                         }
  173.                                     }
  174.                                     else
  175.                                     {
  176.                                         return true;
  177.                                     }
  178.                                 }
  179.                                 else
  180.                                 {
  181.                                     return true;
  182.                                 }
  183.                             }
  184.                         }
  185.                     }
  186.                 }
  187.             }
  188.             return false;
  189.         }
  190.         
  191.         return true;
  192.         throw new LogicException('This code should not be reached!');
  193.     }
  194.     private function canView($postUser $user)
  195.     {
  196.         // if they can edit, they can view
  197.         if ($this->canEdit($post$user)) {
  198.             return true;
  199.         }
  200.         // the Post object could have, for example, a method `isPrivate()`
  201.         return !$post->isPrivate();
  202.     }
  203.     private function canEdit($postUser $user)
  204.     {
  205.         // this assumes that the Post object has a `getOwner()` method
  206.         return $user === $post->getOwner();
  207.     }
  208. }