src/Common/Security/Voter/MemberApplicationVoter.php line 18

Open in your IDE?
  1. <?php
  2. namespace PaperKite\Common\Security\Voter;
  3. use Lightbulb\Symfony\Exception\NotFoundException;
  4. use PaperKite\Common\Entity\CommonUserInterface;
  5. use PaperKite\Common\Entity\Enum\MemberApplicationProcessingStatusEnumType;
  6. use PaperKite\Common\Entity\Member\MemberApplication;
  7. use PaperKite\Common\Service\Member\MemberApplicationReaderService;
  8. use PaperKite\CompanyApi\Entity\CompanyUserInterface;
  9. use PaperKite\EmployeeApi\Entity\EmployeeInterface;
  10. use PaperKite\HealthMutualApi\Entity\HealthMutualUserInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Security\Core\User\UserInterface;
  15. class MemberApplicationVoter extends Voter
  16. {
  17.     public const LIST = 'MEMBER_APPLICATION_LIST';
  18.     public const VIEW 'MEMBER_APPLICATION_VIEW';
  19.     public const EDIT 'MEMBER_APPLICATION_EDIT';
  20.     public const LITE_VIEW 'MEMBER_APPLICATION_LITE_VIEW';
  21.     public const COMPANY_EDIT 'MEMBER_APPLICATION_COMPANY_EDIT';
  22.     public const HEALTH_MUTUAL_EDIT 'MEMBER_APPLICATION_HEALTH_MUTUAL_EDIT';
  23.     public function __construct(
  24.         private Security $security,
  25.         private MemberApplicationReaderService $memberApplicationReaderService,
  26.     ) {
  27.     }
  28.     protected function supports(string $attribute$subject): bool
  29.     {
  30.         // https://symfony.com/doc/current/security/voters.html
  31.         return in_array(
  32.             $attribute,
  33.             [
  34.                 self::VIEW,
  35.                 self::EDIT,
  36.                 self::LITE_VIEW,
  37.                 self::COMPANY_EDIT,
  38.                 self::HEALTH_MUTUAL_EDIT,
  39.             ]
  40.         );
  41.     }
  42.     /**
  43.      * @param string $subject Member application identifier
  44.      *
  45.      * @throws NotFoundException
  46.      */
  47.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  48.     {
  49.         /** @var CommonUserInterface $user */
  50.         $user $token->getUser();
  51.         // if the user is anonymous, do not grant access
  52.         if (!$user instanceof UserInterface) {
  53.             return false;
  54.         }
  55.         if ($this->security->isGranted('ROLE_SUPER_ADMIN')) {
  56.             return true;
  57.         }
  58.         $memberApplication $this->memberApplicationReaderService->getById($subject);
  59.         return match ($attribute) {
  60.             self::VIEW => $this->viewCheck($user),
  61.             self::EDIT => $this->editCheck($user$memberApplication),
  62.             self::LITE_VIEW => $this->liteViewCheck($user$memberApplication),
  63.             self::COMPANY_EDIT => $this->companyEditCheck($user$memberApplication),
  64.             self::HEALTH_MUTUAL_EDIT => $this->healthMutualEditCheck($user$memberApplication),
  65.             default => false,
  66.         };
  67.     }
  68.     private function viewCheck(CommonUserInterface $user): bool
  69.     {
  70.         if ($user instanceof EmployeeInterface) {
  71.             return true;
  72.         }
  73.         return false;
  74.     }
  75.     private function editCheck(CommonUserInterface $userMemberApplication $memberApplication): bool
  76.     {
  77.         if ($user instanceof EmployeeInterface) {
  78.             // No edition of closed/exported member application
  79.             if (true === in_array($memberApplication->getProcessingStatus(), [
  80.                 MemberApplicationProcessingStatusEnumType::STATUS_TO_BE_EXPORTED,
  81.                 MemberApplicationProcessingStatusEnumType::STATUS_CLOSED,
  82.             ], true)) {
  83.                 return false;
  84.             }
  85.             return true;
  86.         }
  87.         return false;
  88.     }
  89.     private function liteViewCheck(CommonUserInterface $userMemberApplication $memberApplication): bool
  90.     {
  91.         if ($user instanceof HealthMutualUserInterface) {
  92.             if ($memberApplication->getHealthMutual() === $user->getHealthMutual()) {
  93.                 return true;
  94.             }
  95.         }
  96.         if ($user instanceof CompanyUserInterface) {
  97.             if ($memberApplication->getCompany() === $user->getCompany()) {
  98.                 return true;
  99.             }
  100.         }
  101.         return false;
  102.     }
  103.     private function companyEditCheck(CommonUserInterface $userMemberApplication $memberApplication): bool
  104.     {
  105.         if ($user instanceof CompanyUserInterface) {
  106.             if ($memberApplication->getCompany() === $user->getCompany()) {
  107.                 return true;
  108.             }
  109.         }
  110.         return false;
  111.     }
  112.     private function healthMutualEditCheck(CommonUserInterface $userMemberApplication $memberApplication): bool
  113.     {
  114.         if ($user instanceof HealthMutualUserInterface) {
  115.             if ($memberApplication->getHealthMutual() === $user->getHealthMutual()) {
  116.                 return true;
  117.             }
  118.         }
  119.         return false;
  120.     }
  121. }