src/Security/Voter/ProjectVoter.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Project;
  4. use App\Entity\ProjectPhase;
  5. use App\Entity\ProjectPhaseDocument;
  6. use App\Entity\ProjectStageDeploy;
  7. use App\Entity\User;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  10. use Symfony\Component\Security\Core\Security;
  11. class ProjectVoter extends Voter
  12. {
  13.     public const ACCESS_PROJECT 'ACCESS_PROJECT';
  14.     public const PROJECT_OWNER_OR_SUPERADMIN 'PROJECT_OWNER_OR_SUPERADMIN';
  15.     private Security $security;
  16.     public function __construct(Security $security)
  17.     {
  18.         $this->security $security;
  19.     }
  20.     protected function supports(string $attribute$subject): bool
  21.     {
  22.         return in_array($attribute, [self::ACCESS_PROJECTself::PROJECT_OWNER_OR_SUPERADMIN]) && ($subject instanceof Project || $subject instanceof ProjectStageDeploy || $subject === null);
  23.     }
  24.     /**
  25.      * @param Project $subject
  26.      */
  27.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  28.     {
  29.         /** @var User $user */
  30.         $user $token->getUser();
  31.         // if the user is anonymous, do not grant access
  32.         if (!$user instanceof User) {
  33.             return false;
  34.         }
  35.         $project null;
  36.         if ($subject instanceof Project) {
  37.             $project $subject;
  38.         }
  39.         if ($subject instanceof ProjectStageDeploy) {
  40.             $project $subject->getProjectStage()->getProject();
  41.         }
  42.         switch ($attribute) {
  43.             case self::ACCESS_PROJECT:
  44.                 if ($this->security->isGranted(User::ROLE_SUPER_ADMIN)) {
  45.                     return true;
  46.                 }
  47.                 if ($project) {
  48.                     if ($this->isOwner($user$project)) {
  49.                         return true;
  50.                     }
  51.                     if ($user->getProjects()->contains($project)) {
  52.                         return true;
  53.                     }
  54.                 }
  55.                 break;
  56.             case self::PROJECT_OWNER_OR_SUPERADMIN:
  57.                 if ($this->security->isGranted(User::ROLE_SUPER_ADMIN)) {
  58.                     return true;
  59.                 }
  60.                 if ($project) {
  61.                     if ($this->isOwner($user$project)) {
  62.                         return true;
  63.                     }
  64.                 }
  65.                 break;
  66.         }
  67.         return false;
  68.     }
  69.     private function isOwner(User $user, ?Project $project): bool
  70.     {
  71.         if ($project && (int) $project->getCreatedBy()->getId() === $user->getId()) {
  72.             return true;
  73.         }
  74.         return false;
  75.     }
  76. }