src/Security/Voter/SatisfactionSurveys/SatisfactionSurveyCustomerResponseSecurityVoter.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter\SatisfactionSurveys;
  3. use App\Entity\SatisfactionSurveys\SatisfactionSurveyCustomerResponse;
  4. use App\Entity\User;
  5. use App\Enum\MenuRolesAssociatedEnum;
  6. use App\Enum\MenuRolesManagerEnum;
  7. use App\Enum\UserRolesEnum;
  8. use App\Enum\VotersEnum;
  9. use App\Repository\SatisfactionSurveys\SatisfactionSurveyCustomerResponseRepository;
  10. use LogicException;
  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. final class SatisfactionSurveyCustomerResponseSecurityVoter extends Voter
  15. {
  16.     private Security $security;
  17.     private array $voters;
  18.     private SatisfactionSurveyCustomerResponseRepository $sscrr;
  19.     public function __construct(Security $securitySatisfactionSurveyCustomerResponseRepository $sscrr)
  20.     {
  21.         $this->security $security;
  22.         $this->voters = [
  23.             VotersEnum::LIST_SATISFACTION_SURVEY_CUSTOMER_RESPONSE,
  24.             VotersEnum::LIST_SATISFACTION_SURVEY_CUSTOMER_RESPONSE_ASSOCIATED,
  25.             VotersEnum::READ,
  26.             VotersEnum::OWN,
  27.             VotersEnum::EXPORT_SATISFACTION_SURVEY_CUSTOMER_RESPONSE,
  28.             VotersEnum::EXPORT_SATISFACTION_SURVEY_CUSTOMER_RESPONSE_ASSOCIATED,
  29.         ];
  30.         $this->sscrr $sscrr;
  31.     }
  32.     protected function supports(string $attribute$subject): bool
  33.     {
  34.         // first check the $subject and last if the $attribute is supported,
  35.         // because there are attributes (with subject) used as well by other voters (like UPDATE, ...)
  36.         if ($subject && !$subject instanceof SatisfactionSurveyCustomerResponse) {
  37.             // only vote on these objects
  38.             return false;
  39.         }
  40.         if (in_array($attribute$this->voters)) {
  41.             // if the attribute is one we support
  42.             return true;
  43.         }
  44.         return false;
  45.     }
  46.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  47.     {
  48.         $user $token->getUser();
  49.         if (!$user instanceof User) {
  50.             // the user must be logged in; if not, deny access
  51.             return false;
  52.         }
  53.         switch ($attribute) {
  54.             case VotersEnum::LIST_SATISFACTION_SURVEY_CUSTOMER_RESPONSE:
  55.                 return $this->canList();
  56.             case VotersEnum::LIST_SATISFACTION_SURVEY_CUSTOMER_RESPONSE_ASSOCIATED:
  57.                 return $this->canListAssociated();
  58.             case VotersEnum::READ:
  59.                 return $this->canRead();
  60.             case VotersEnum::OWN:
  61.                 return $this->isOwner($subject$user);
  62.             case VotersEnum::EXPORT_SATISFACTION_SURVEY_CUSTOMER_RESPONSE:
  63.                 return $this->canExport();
  64.             case VotersEnum::EXPORT_SATISFACTION_SURVEY_CUSTOMER_RESPONSE_ASSOCIATED:
  65.                 return $this->canExportAssociated();
  66.         }
  67.         throw new LogicException('This code should not be reached!');
  68.     }
  69.     private function canList(): bool
  70.     {
  71.         return $this->isAdminUser();
  72.     }
  73.     private function canListAssociated(): bool
  74.     {
  75.         return $this->isAssociatedUser();
  76.     }
  77.     private function canRead(): bool
  78.     {
  79.         return $this->isAdminUser() || $this->isAssociatedUser();
  80.     }
  81.     private function isOwner(SatisfactionSurveyCustomerResponse $satisfactionSurveyCustomerResponseUser $user): bool
  82.     {
  83.         if ($this->security->isGranted(UserRolesEnum::ROLE_ADMIN_LONG) || $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_SATISFACTION_SURVEYS)) {
  84.             return true;
  85.         } elseif ($this->security->isGranted(UserRolesEnum::ROLE_COORDINATOR_LONG)) {
  86.             $satisfactionSurveyCustomerResponses $this->sscrr->getByProvincesSortedByBegin($user->getWorkProvinces());
  87.             /** @var SatisfactionSurveyCustomerResponse $item */
  88.             foreach ($satisfactionSurveyCustomerResponses as $item) {
  89.                 if ($satisfactionSurveyCustomerResponse->getId() === $item->getId()) {
  90.                     return true;
  91.                 }
  92.             }
  93.         } elseif ($satisfactionSurveyCustomerResponse->getGarage() && $satisfactionSurveyCustomerResponse->getGarage()->getOwner() && $satisfactionSurveyCustomerResponse->getGarage()->getOwner()->getId() === $user->getId()) {
  94.             return true;
  95.         }
  96.         return false;
  97.     }
  98.     private function canExport(): bool
  99.     {
  100.         return $this->isAdminUser();
  101.     }
  102.     private function canExportAssociated(): bool
  103.     {
  104.         return $this->isAssociatedUser();
  105.     }
  106.     private function isAssociatedUser(): bool
  107.     {
  108.         return $this->security->isGranted(MenuRolesAssociatedEnum::ROLE_MENU_SATISFACTION_SURVEYS_ASOCIATED);
  109.     }
  110.     private function isAdminUser(): bool
  111.     {
  112.         return $this->security->isGranted(MenuRolesManagerEnum::ROLE_MENU_SATISFACTION_SURVEYS);
  113.     }
  114. }