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 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. 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.     
  20.     public function __construct(EntityManagerInterface $emKernelInterface $kernel)
  21.     {
  22.         $this->em $em;
  23.         $this->environment $kernel->getEnvironment();
  24.     }
  25.     
  26.     protected function supports(string $attribute$subject)
  27.     {
  28.         // if the attribute isn't one we support, return false
  29.         /*if (!in_array($attribute, [self::VIEW, self::EDIT])) {
  30.             return false;
  31.         }
  32.         // only vote on `Post` objects
  33.         if (!$subject instanceof Post) {
  34.             return false;
  35.         }*/
  36.         return true;
  37.     }
  38.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  39.     {
  40.         $user $token->getUser();
  41.         if (!$user instanceof User) {
  42.             // the user must be logged in; if not, deny access
  43.             //return false;
  44.         }
  45.         
  46.         $items explode ":" $attribute);
  47.         
  48.         if (isset($items[0]) && isset($items[1]))
  49.         {
  50.             $idealaction null;
  51.             
  52.             $actionentity $this->em->getRepository(SecurityAction::class)->findOneBy(array('entity' => $items[0], 'action' => $items[1], 'state' => 0));
  53.             if (!$actionentity)
  54.             {
  55.                 $entity = new SecurityAction();
  56.                 $entity->setEntity($items[0]);
  57.                 $entity->setAction($items[1]);
  58.                 $entity->setLogging(1);
  59.                 $entity->setState(0);
  60.                 $this->em->persist($entity);
  61.                 $this->em->flush();
  62.                 $idealaction $entity;
  63.             }
  64.             else
  65.             {
  66.                 $idealaction $actionentity;
  67.             }
  68.             
  69.             if ($idealaction->getLogging() != 0)
  70.             {
  71.                 if ($this->environment === 'dev') {
  72.                     $log  = new UserLog();
  73.                     $log->setEntity($items[0]);
  74.                     $log->setAction($items[1]);
  75.                     $log->setOrganization($user->getOrganization());
  76.                     if ($subject != null) {
  77.                         $log->setElement($subject->getId());
  78.                     }
  79.                     $log->setState(0);
  80.                     $this->em->persist($log);
  81.                     $this->em->flush();
  82.                 }
  83.             }
  84.             return true;
  85.             $userfunction $user->getUserfunction();
  86.             foreach ($userfunction->getRoles() as $role) {
  87.                 $profile $role->getProfile();
  88.                 foreach ($profile->getActionprofiles() as $actionprofiles) {
  89.                     foreach ($actionprofiles->getSecurityactions() as $securityactions)
  90.                     {
  91.                         if($securityactions->getId() == $idealaction->getId())
  92.                         {
  93.                             if ($items[0] == "Plant") {
  94.                                 if ($subject != null)
  95.                                 {
  96.                                     //$subject->getId()
  97.                                     if ($role->getPlantrights()->getName() == "AllOrSelection.Selection")
  98.                                     {
  99.                                         
  100.                                         if (($role->getPlant() != null) && ($role->getPlant()->getId() == $subject->getId()))
  101.                                         {
  102.                                             return true;
  103.                                         }
  104.                                     }
  105.                                     else
  106.                                     {
  107.                                         return true;
  108.                                     }
  109.                                 }
  110.                                 else
  111.                                 {
  112.                                     return true;
  113.                                 }
  114.                             }
  115.                             else
  116.                             {
  117.                                 if ($items[0] == "WorkCenter") {
  118.                                     
  119.                                     if ($subject != null)
  120.                                     {
  121.                                         //If WorkCenterCounter find workcenterID and workcenter
  122.                                         //...
  123.                                         $workcenterid $subject->getId();
  124.                                         $workcenter $this->em->getRepository(WorkCenter::class)->findOneById($workcenterid);
  125.                                         if ($role->getWorkcenterrights()->getName() == "AllOrSelection.Selection")
  126.                                         {
  127.                                             if (($role->getWorkcenter() != null) && ($role->getWorkcenter()->getId() == $workcenterid))
  128.                                             {
  129.                                                 return true;
  130.                                             }
  131.                                         }
  132.                                         else
  133.                                         {
  134.                                             if ($role->getPlantrights()->getName() == "AllOrSelection.Selection")
  135.                                             {
  136.                                                 if ($role->getPlant() != null)
  137.                                                 {
  138.                                                     if ($workcenter->getPlant()->getId() == $role->getPlant()->getId())
  139.                                                     {
  140.                                                         return true;
  141.                                                     }
  142.                                                 }
  143.                                             }
  144.                                             else
  145.                                             {
  146.                                                 return true;
  147.                                             }
  148.                                         }
  149.                                     }
  150.                                     else
  151.                                     {
  152.                                         return true;
  153.                                     }
  154.                                 }
  155.                                 else
  156.                                 {
  157.                                     return true;
  158.                                 }
  159.                             }
  160.                         }
  161.                     }
  162.                 }
  163.             }
  164.             return false;
  165.         }
  166.         
  167.         return true;
  168.         throw new LogicException('This code should not be reached!');
  169.     }
  170.     private function canView($postUser $user)
  171.     {
  172.         // if they can edit, they can view
  173.         if ($this->canEdit($post$user)) {
  174.             return true;
  175.         }
  176.         // the Post object could have, for example, a method `isPrivate()`
  177.         return !$post->isPrivate();
  178.     }
  179.     private function canEdit($postUser $user)
  180.     {
  181.         // this assumes that the Post object has a `getOwner()` method
  182.         return $user === $post->getOwner();
  183.     }
  184. }