src/mvk/Auth/Services/VoterPermissions.php line 21

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the Pimcore X Installation by
  4.  * ercas GmbH & CO. KG <https://www.ercasdieagentur.de>
  5.  *
  6.  *  @license GPLv3
  7.  */
  8. namespace App\mvk\Auth\Services;
  9. use Pimcore\Model\DataObject\Agent;
  10. use Pimcore\Model\DataObject\BmsSubAgent;
  11. use Pimcore\Model\DataObject\SubAgent;
  12. use Psr\Container\ContainerInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  15. use Symfony\Component\Security\Core\Security ;
  16. use Symfony\Component\Security\Core\User\UserInterface;
  17. class VoterPermissions extends Voter
  18. {
  19.     private $permissionService;
  20.     public function __construct(private Security $security, private ContainerInterface $container)
  21.     {
  22.         $this->security $security;
  23.         $this->container $container;
  24.         $this->permissionService $container->get('auth.frontend_permission_toolkit_bundle');
  25.     }
  26.     const SUBJECTS = ['Policy''Policyholder''PolicyDocument''Agent''SubAgent''BmsSubAgent''AgentDocument''InsuranceApplication''InsuranceOffer''DamageDeclaration'];
  27.     const VIEWCUSTOMERLIST 'viewCustmerList';
  28.     const VIEWSETTLEMENTS  'viewSettlements';
  29.     const VIEWGDVDOWNLOAD  'viewGdvDownload';
  30.     const VIEWMYUSERS      'viewMyUsers';
  31.     const GDVDOWNLOAD      'gdvDownload';
  32.     const AGENTDOCUMENTDOWNLOAD 'agentDocumentDownload';
  33.     const ACCESS 'access';
  34.     protected function supports(string $attributemixed $subject): bool
  35.     {
  36.         /**  if the attribute isn't one we support, return false */
  37.         if (!in_array($attribute, [self::VIEWCUSTOMERLISTself::VIEWSETTLEMENTSself::VIEWGDVDOWNLOADself::VIEWMYUSERSself::GDVDOWNLOADself::AGENTDOCUMENTDOWNLOADself::ACCESS])) {
  38.             return false;
  39.         }
  40.         /**  if the subject isn't one we support, return false. (can also use instanceof )  */
  41.         if ($subject && !in_array($subject->getClassName(), self::SUBJECTS)) {
  42.             return false;
  43.         }
  44.         return true;
  45.     }
  46.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  47.     {
  48.         $user $token->getUser();
  49.         /** the user must be logged in; if not, deny access */
  50.         if (!$user instanceof UserInterface) {
  51.             return false;
  52.         }
  53.         $orginalUser$this->getOriginalUser($user);
  54.         return match($attribute) {
  55.             self::GDVDOWNLOAD => $this->canGdvDownload($attribute$orginalUser),
  56.             self::AGENTDOCUMENTDOWNLOAD => $this->canAgentDocumentDownload($attribute$orginalUser),
  57.             self::VIEWCUSTOMERLIST => $this->canViewCustmerList($attribute$orginalUser),
  58.             self::VIEWSETTLEMENTS => $this->canViewSettlements($attribute$orginalUser),
  59.             self::VIEWGDVDOWNLOAD => $this->canViewGdvDownload($attribute$orginalUser),
  60.             self::VIEWMYUSERS => $this->canViewMyUsers($attribute$orginalUser),
  61.             self::ACCESS => $this->canAccess($orginalUser$subject),
  62.             default => throw new \LogicException('This code should not be reached!')
  63.         };
  64.     }
  65.     /**
  66.      * This function checks Subjects that don't need special Treatments
  67.      */
  68.     private function checkSubject($user$subject): bool
  69.     {
  70.         $subject$this->reset($subject);
  71.         $method'get'.$user->getClassName();
  72.         if ($user instanceof SubAgent || $user instanceof BmsSubAgent) {
  73.             $user $user->getAgent();
  74.             if (!$user) {
  75.                 return false;
  76.             }
  77.             $method 'getAgent';
  78.         }
  79.         $userId$this->getUserOwners($user);
  80.         $allowedUsers$this->getAllowedUsers($subject$method);
  81.         return $this->getFinalDecision($allowedUsers$userId);
  82.     }
  83.     /**
  84.      * This function checks Subjects that  need special Treatments
  85.      */
  86.     private function checkSubjectSpecial($user$subject$attribute): bool
  87.     {
  88.         $subject$this->reset($subject);
  89.         if ($user instanceof Agent || $user instanceof SubAgent) {
  90.             $method 'getId';
  91.             if ($user instanceof SubAgent) {
  92.                 if (!$this->canAgentDocumentDownload($attribute$user)) {
  93.                     return false;
  94.                 }
  95.                 $user $user->getAgent();
  96.                 if (!$user) {
  97.                     return false;
  98.                 }
  99.             }
  100.             $userId$this->getUserOwners($user);
  101.             $allowedUsers$this->getAllowedUsers($subject$method);
  102.             return $this->getFinalDecision($allowedUsers$userId);
  103.         }
  104.         return false;
  105.     }
  106.     /**
  107.      * This function checks Related  SubAgent and BmsSubAgent to an Agent
  108.      */
  109.     private function checkRelatedAgent($user$subject)
  110.     {
  111.         if ($user instanceof Agent && $subject instanceof Agent) {
  112.             if($subject->getId() === $user->getId()) {
  113.                 return true;
  114.             }
  115.         }
  116.         if ($user instanceof Agent && ($subject instanceof SubAgent ||  $subject instanceof BmsSubAgent)) {
  117.             $user $subject->getAgent();
  118.             if (!$user) {
  119.                 return false;
  120.             }
  121.             $method 'getAgent';
  122.             $userId$this->getUserOwners($user);
  123.             $allowedUsers$this->getAllowedUsers($subject$method);
  124.             return $this->getFinalDecision($allowedUsers$userId);
  125.         }
  126.         if ($user instanceof SubAgent || $user instanceof BmsSubAgent) {
  127.             $user $user->getAgent();
  128.             if (!$user) {
  129.                 return false;
  130.             }
  131.             $method 'getAgent';
  132.             $userId$this->getUserOwners($user);
  133.             $allowedUsers$this->getAllowedUsers($subject$method);
  134.             return $this->getFinalDecision($allowedUsers$userId);
  135.         }
  136.         return false;
  137.     }
  138.     /**
  139.      * This function checks Policy Object
  140.      */
  141.     private function checkPolicy($user$subject): bool
  142.     {
  143.         return $this->checkSubject($user$subject);
  144.     }
  145.     /**
  146.      * This function checks PolicyHolder Object
  147.      */
  148.     private function checkPolicyHolder($user$subject): bool
  149.     {
  150.         $subject $subject->getPolicies();
  151.         return $this->checkPolicy($user$subject);
  152.     }
  153.     /**
  154.      * This function checks Agent Object
  155.      */
  156.     private function checkAgent($user$subject): bool
  157.     {
  158.         return $this->checkRelatedAgent($user$subject);
  159.     }
  160.     /**
  161.      * This function checks SubAgent Object
  162.      */
  163.     private function checkSubAgent($user$subject): bool
  164.     {
  165.         return $this->checkRelatedAgent($user$subject);
  166.     }
  167.     /**
  168.      * This function checks BmsSubAgent Object
  169.      */
  170.     private function checkBmsSubAgent($user$subject): bool
  171.     {
  172.         return $this->checkRelatedAgent($user$subject);
  173.     }
  174.     /**
  175.      * This function checks InsuranceApplication Object
  176.      */
  177.     private function checkInsuranceApplication($user$subject): bool
  178.     {
  179.         return $this->checkSubject($user$subject);
  180.     }
  181.     /**
  182.      * This function checks InsuranceOffer Object
  183.      */
  184.     private function checkInsuranceOffer($user$subject): bool
  185.     {
  186.         return $this->checkSubject($user$subject);
  187.     }
  188.     /**
  189.      * This function checks DamageDeclaration Object
  190.      */
  191.     private function checkDamageDeclaration($user$subject): bool
  192.     {
  193.         return $this->checkSubject($user$subject);
  194.     }
  195.     /**
  196.      * This function checks PolicyDocument Object
  197.      */
  198.     private function checkPolicyDocument($user$subject): bool
  199.     {
  200.         $subject $subject->getpolicyNr();
  201.         return $this->checkSubject($user$subject);
  202.     }
  203.     /**
  204.      * This function checks AgentDocument Object
  205.      */
  206.     private function checkAgentDocument($user$subject): bool
  207.     {
  208.         $subject $subject->getagentNr();
  209.         return $this->checkSubjectSpecial($user$subject'agentDocumentDownload');
  210.     }
  211.     /**
  212.      * This function that get called in controllers with different subjects
  213.      */
  214.     private function canAccess($user$subject): bool
  215.     {
  216.         $method'check'.$subject->getClassName();
  217.         return $this->$method($user$subject);
  218.     }
  219.     /**
  220.      * This function checks User's Role GdvDownload
  221.      */
  222.     private function canGdvDownload($attribute$user): bool
  223.     {
  224.         if ($user instanceof Agent) {
  225.             return true;
  226.         } else {
  227.             if($this->isAllowed('viewGdvDownload'$user)) {
  228.                 return true;
  229.             }
  230.         }
  231.         return false;
  232.     }
  233.     /**
  234.      * This function checks User's Role AgentDocumentDownload
  235.      */
  236.     private function canAgentDocumentDownload($attribute$user): bool
  237.     {
  238.         if ($user instanceof Agent) {
  239.             return true;
  240.         } else {
  241.             if($this->isAllowed('viewSettlements'$user)) {
  242.                 return true;
  243.             }
  244.         }
  245.         return false;
  246.     }
  247.     /**
  248.      * This function checks wether the user can view Kundenliste Pimcore Document
  249.      */
  250.     private function canViewCustmerList($attribute$user): bool
  251.     {
  252.         if ($user instanceof Agent || $user instanceof SubAgent || $user instanceof BmsSubAgent) {
  253.             return true;
  254.         }
  255.         return false;
  256.     }
  257.     /**
  258.      * This function checks wether the user can view Abrechnungen Pimcore Document
  259.      */
  260.     private function canViewSettlements($attribute$user): bool
  261.     {
  262.         return $this->canAgentDocumentDownload($attribute$user);
  263.     }
  264.     /**
  265.      * This function checks wether the user can view GDV Download Pimcore Document
  266.      */
  267.     private function canViewGdvDownload($attribute$user): bool
  268.     {
  269.         return $this->canGdvDownload($attribute$user);
  270.     }
  271.     /**
  272.      * This function checks wether the user can view  Meine Benutzer Pimcore Document
  273.      */
  274.     private function canViewMyUsers($attribute$user): bool
  275.     {
  276.         if ($user instanceof Agent) {
  277.             return true;
  278.         }
  279.         return false;
  280.     }
  281.     /**
  282.      * This function checks wether the user is granted the permission based on their Role
  283.      */
  284.     private function isAllowed($attribute$user)
  285.     {
  286.         if($this->permissionService->isAllowed($user$attribute)) {
  287.             return true;
  288.         }
  289.         return false;
  290.     }
  291.     private function getOriginalUser($user)
  292.     {
  293.         $class=  "Pimcore\Model\DataObject\\".$user->getClassName();
  294.         $user $class::getById($user->getId());
  295.         return  $user;
  296.     }
  297.     private function reset($subject)
  298.     {
  299.         if (is_array($subject) && count($subject)==1) {
  300.             $subject =  reset($subject);
  301.         }
  302.         return $subject;
  303.     }
  304.     private function getAllowedUsers($subject$method)
  305.     {
  306.         $allowedUsers=[];
  307.         if (is_array($subject)) {
  308.             foreach ($subject as $mSubject) {
  309.                 $holder $mSubject->$method();
  310.                 if ($holder) {
  311.                     $holder$this->reset($holder);
  312.                     if (is_array($holder)) {
  313.                         foreach ($holder as  $subHolder) {
  314.                             array_push($allowedUsers$subHolder->getId());
  315.                         }
  316.                     } else {
  317.                         is_int($holder) ? $holderId=$holder $holderId=$holder->getId();
  318.                         array_push($allowedUsers$holderId);
  319.                     }
  320.                 }
  321.             }
  322.         } else {
  323.             $holder $subject->$method();
  324.             if ($holder) {
  325.                 $holder$this->reset($holder);
  326.                 if (is_array($holder)) {
  327.                     $method 'getId';
  328.                     $allowedUsers $this->getAllowedUsers($holder$method);
  329.                 } else {
  330.                     $holder$this->reset($holder);
  331.                     is_int($holder) ? $holderId=$holder $holderId=$holder->getId();
  332.                     array_push($allowedUsers$holderId);
  333.                 }
  334.             }
  335.         }
  336.         return $allowedUsers;
  337.     }
  338.     private function getUserOwners($user)
  339.     {
  340.         $users=[];
  341.         if (is_array($user)) {
  342.             foreach ($user as $agent) {
  343.                 array_push($users$agent->getId());
  344.             }
  345.         } else {
  346.             array_push($users$user->getId());
  347.         }
  348.         return $users;
  349.     }
  350.     private function getFinalDecision($allowedUsers$userId)
  351.     {
  352.         if ($allowedUsers) {
  353.             if (is_array($userId)) {
  354.                 foreach ($userId as $user) {
  355.                     if (in_array($user$allowedUsers)) {
  356.                         return true;
  357.                     }
  358.                 }
  359.             } else {
  360.                 if (in_array($userId$allowedUsers)) {
  361.                     return true;
  362.                 }
  363.             }
  364.         }
  365.         return false;
  366.     }
  367. }