src/Security/Voter/SurveyVoter.php line 11

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\AccountingFirm;
  4. use App\Entity\Survey;
  5. use App\Entity\User;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  8. class SurveyVoter extends Voter
  9. {
  10.     const ACCESS 'SURVEY_ACCESS';
  11.     const EDIT 'SURVEY_EDIT';
  12.     const DELETE 'SURVEY_DELETE';
  13.     const NEW = 'SURVEY_NEW';
  14.     
  15.     protected function supports(string $attributemixed $subject): bool
  16.     {
  17.         return in_array($attribute, [self::ACCESSself::EDITself::DELETEself::NEW])
  18.             && ($subject === null || $subject instanceof Survey);
  19.     }
  20.     protected function voteOnAttribute(string $attributemixed $subjectTokenInterface $token): bool
  21.     {
  22.         $user $token->getUser();
  23.         if (!$user instanceof User) {
  24.             return false;
  25.         }
  26.         if (in_array('ROLE_ADMIN'$user->getRoles())) {
  27.             return true;
  28.         }
  29.         switch ($attribute) {
  30.             case self::ACCESS:
  31.                 return $this->canAccessSurvey($user);
  32.             case self::EDIT:
  33.             case self::DELETE:
  34.                 return $this->canEditSurvey($user$subject);
  35.             case self::NEW:
  36.                 return $this->canNewSurvey($user);
  37.         }
  38.         return false;
  39.     }
  40.     private function canAccessSurvey(User $user): bool
  41.     {
  42.         $accountingFirm $user->getAccountingFirm();
  43.         if (!$accountingFirm) {
  44.             return false;
  45.         }
  46.         return $this->canAccessSurveyModule($accountingFirm);
  47.     }
  48.     private function canEditSurvey(User $user, ?Survey $survey): bool
  49.     {
  50.         if (!$this->canAccessSurvey($user)) {
  51.             return false;
  52.         }
  53.         if (!$survey) {
  54.             return false;
  55.         }
  56.         $accountingFirm $survey->getAccountingFirm();
  57.         if (!$this->canAccessSurveyClient($accountingFirm)) {
  58.             return false;
  59.         }
  60.         return $survey->getAccountingFirm() === $user->getAccountingFirm();
  61.     }
  62.     private function canAccessSurveyModule(AccountingFirm $accountingFirm): bool
  63.     {
  64.         return $accountingFirm->getSurveyClientActivate() === true || $accountingFirm->getWebsiteCreatedAt() !== null;
  65.     }
  66.     private function canNewSurvey(User $user): bool
  67.     {
  68.         $accountingFirm $user->getAccountingFirm();
  69.         if (!$accountingFirm) {
  70.             return false;
  71.         }
  72.         if (!$this->canAccessSurveyClient($accountingFirm)) {
  73.             return false;
  74.         }
  75.         return $accountingFirm->getSurveyClientActivate() === true;
  76.     }
  77.     //verify if token survey and widget url are not null
  78.     private function canAccessSurveyClient(AccountingFirm $accountingFirm): bool
  79.     {
  80.         return $accountingFirm->getSurveyToken() !== null && $accountingFirm->getSurveyWidgetUrl() !== null;
  81.     }
  82. }