src/Security/Voter/ManageProjectVoter.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Common\RoleInterface;
  4. use App\Entity\Project;
  5. use App\Entity\User;
  6. use App\Entity\UserProject;
  7. use App\Entity\UserWorkroom;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  11. use Symfony\Component\HttpFoundation\Session\Session;
  12. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  13. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  14. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  15. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  16. use Symfony\Component\Security\Core\User\UserInterface;
  17. class ManageProjectVoter extends Voter
  18. {
  19.     private EntityManagerInterface $entityManager;
  20.     private RequestStack $requestStack;
  21.     public const ACTION_PROJECT 'action_project';
  22.     public const ACTION_PROJECT_CDP_WL 'action_project_cdp_wl';
  23.     protected array $attributes = [
  24.         self::ACTION_PROJECT,
  25.         self::ACTION_PROJECT_CDP_WL,
  26.     ];
  27.     protected AuthorizationCheckerInterface $authChecker;
  28.     public function __construct(AuthorizationCheckerInterface $authCheckerEntityManagerInterface $entityManagerRequestStack $requestStack)
  29.     {
  30.         $this->authChecker $authChecker;
  31.         $this->entityManager $entityManager;
  32.         $this->requestStack $requestStack;
  33.     }
  34.     private function getCurrentProjectId()
  35.     {
  36.         $session $this->requestStack->getSession();
  37.         return $session->get('project_id');
  38.     }
  39.     protected function supports($attribute$subject): bool
  40.     {
  41.         if (!\in_array($attribute$this->attributes)) {
  42.             return false;
  43.         }
  44.         return true;
  45.     }
  46.     /**
  47.      * @param mixed $subject
  48.      */
  49.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  50.     {
  51.         $user $token->getUser();
  52.         if (!$user instanceof UserInterface) {
  53.             return false;
  54.         }
  55.         switch ($attribute) {
  56.             case self::ACTION_PROJECT:
  57.                 return $this->canDoActionOnProject($user->getId());
  58.             case self::ACTION_PROJECT_CDP_WL:
  59.                 return $this->canDoActionOnProjectByCdpAndWl($user->getId());
  60.             default:
  61.                 return false;
  62.         }
  63.     }
  64.     public function canDoActionOnProject(int $id): bool
  65.     {
  66.         $projectId $this->getCurrentProjectId();
  67.         return true == $this->entityManager->getRepository(UserProject::class)->checkIfProjectManagerInProject($id$projectId);
  68.     }
  69.     public function canDoActionOnProjectByCdpAndWl(int $id): bool
  70.     {
  71.         $projectId $this->getCurrentProjectId();
  72.         if ($this->entityManager->getRepository(UserProject::class)->checkIfProjectManagerInProject($id$projectId)) {
  73.             return true;
  74.         }
  75.         $repoProject $this->entityManager->getRepository(Project::class);
  76.         /** @var Project $project */
  77.         $project $repoProject->find($projectId);
  78.         $repoUser $this->entityManager->getRepository(User::class);
  79.         /** @var User $user */
  80.         $user $repoUser->find($id);
  81.         $workroomsOfProject $project->getWorkroomsIds();
  82.         $workroomsOfUser $user->getUserWorkrooms();
  83.         foreach ($workroomsOfUser as $workroomUser) {
  84.             if (in_array($workroomUser->getWorkroom()->getId(), $workroomsOfProject)) {
  85.                 if ($workroomUser->getRole() == RoleInterface::ROLE_LEADER_WORKROOM_INT) {
  86.                     return true;
  87.                 }
  88.             }
  89.         }
  90.         return false;
  91.     }
  92. }