src/Security/Voter/DemandFileVoter.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\CompanyDemand;
  4. use App\Entity\Demand;
  5. use App\Entity\DemandFile;
  6. use App\Entity\User;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. class DemandFileVoter extends Voter
  10. {
  11.     protected function supports(string $attribute$subject): bool
  12.     {
  13.         // replace with your own logic
  14.         // https://symfony.com/doc/current/security/voters.html
  15.         return in_array($attribute, ['DEMAND_FILE_REMOVE'], true)
  16.             && $subject instanceof DemandFile;
  17.     }
  18.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  19.     {
  20.         $user $token->getUser();
  21.         // if the user is anonymous, do not grant access
  22.         if (!$user instanceof User) {
  23.             return false;
  24.         }
  25.         /** @var DemandFile $demandFile */
  26.         $demandFile $subject;
  27.         $demand $demandFile
  28.             ->getDemand()
  29.         ;
  30.         /** @var CompanyDemand|null
  31.          * $companyDemandSelected
  32.          */
  33.         $companyDemandSelected $demand->getCompanyDemands()->filter(function (CompanyDemand $companyDemand): bool {
  34.             return $companyDemand->getSelected() === true;
  35.         })->first();
  36.         /** @var CompanyDemand $companyAssigned */
  37.         $companyAssigned $demand->getCompanyDemands()->filter(function (CompanyDemand $companyDemand) use ($user): bool {
  38.             return $companyDemand->getCompany() === $user->getCompanies()->first();
  39.         })->first();
  40.         // ... (check conditions and return true to grant permission) ...
  41.         switch ($attribute) {
  42.             case 'DEMAND_FILE_REMOVE':
  43.                 // logic to determine if the user can VIEW
  44.                 // return true or false
  45.                 if ($demand->getUser() === $user) {
  46.                     if (
  47.                         $demand->getStatus() <= Demand::IN_PENDING_STATUS ||
  48.                         $demand->getStatus() === Demand::ON_GOING_STATUS ||
  49.                         $demand->getStatus() === Demand::COMPANY_POSTED_FEEDBACK_STATUS ||
  50.                         $demand->getStatus() === Demand::COMPLETE_STATUS
  51.                     ) {
  52.                         return true;
  53.                     }
  54.                 }
  55.                 if (in_array('ROLE_ADMIN'$user->getRoles(), true)) {
  56.                     return true;
  57.                 }
  58.                 if ($companyAssigned->getCompany()->getUser() === $user) {
  59.                     if (
  60.                         $demand->getStatus() === Demand::INTERVENTION_CONFIRMATION_STATUS ||
  61.                         $demand->getStatus() === Demand::SELECTED_COMPANY_STATUS
  62.                     ) {
  63.                         return true;
  64.                     }
  65.                 }
  66.                 if ($companyDemandSelected?->getCompany()?->getUser() === $user) {
  67.                     if (
  68.                         $demand->getStatus() === Demand::ON_GOING_STATUS ||
  69.                         $demand->getStatus() === Demand::INTERVENTION_CONFIRMATION_STATUS ||
  70.                         $demand->getStatus() === Demand::SELECTED_COMPANY_STATUS ||
  71.                         $demand->getStatus() === Demand::USER_POSTED_FEEDBACK_STATUS ||
  72.                         $demand->getStatus() === Demand::COMPLETE_STATUS
  73.                     ) {
  74.                         return true;
  75.                     }
  76.                 }
  77.                 return false;
  78.         }
  79.         return false;
  80.     }
  81. }