src/Security/Voter/CreateProjectVoter.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Common\RoleInterface;
  4. use App\Entity\Common\TypeProjectInterface;
  5. use App\Entity\Project;
  6. use App\Entity\User;
  7. use App\Entity\UserProject;
  8. use App\Entity\UserWorkroom;
  9. use App\Repository\UserProjectRepository;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  12. use Symfony\Component\HttpFoundation\RequestStack;
  13. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  14. use Symfony\Component\HttpFoundation\Session\Session;
  15. use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;
  16. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  17. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  18. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  19. use Symfony\Component\Security\Core\User\UserInterface;
  20. class CreateProjectVoter extends Voter
  21. {
  22.     private int $maxPrivateProjects;
  23.     public const CREATE_PUBLIC_PROJECT 'create_public_project';
  24.     public const CREATE_PRIVATE_PROJECT 'create_private_project';
  25.     protected array $attributes = [
  26.         self::CREATE_PUBLIC_PROJECT,
  27.         self::CREATE_PRIVATE_PROJECT
  28.     ];
  29.     protected AuthorizationCheckerInterface $authChecker;
  30.     public function __construct(
  31.         private UserProjectRepository $userProjectRepository,
  32.         ParameterBagInterface $parameterBag
  33.     ) {
  34.         $this->maxPrivateProjects $parameterBag->get('MAX_PRIVATE_PROJECTS');
  35.     }
  36.     protected function supports($attribute$subject): bool
  37.     {
  38.         if (!\in_array($attribute$this->attributes)) {
  39.             return false;
  40.         }
  41.         return true;
  42.     }
  43.     /**
  44.      * @param mixed $subject
  45.      */
  46.     protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  47.     {
  48.         $user $token->getUser();
  49.         if (!$user instanceof UserInterface) {
  50.             return false;
  51.         }
  52.         switch ($attribute) {
  53.             case self::CREATE_PUBLIC_PROJECT:
  54.                 return $this->canCreatePublicProject($user);
  55.             case self::CREATE_PRIVATE_PROJECT:
  56.                 return $this->canCreatePrivateProject($user);
  57.             default:
  58.                 return false;
  59.         }
  60.     }
  61.     private function canCreatePublicProject(User $user): bool
  62.     {
  63.         return $user->hasRole(User::ROLE_PROJECT_MANAGER);
  64.     }
  65.     
  66.     private function canCreatePrivateProject(User $user): bool
  67.     {
  68.         return $user->hasRole(User::ROLE_PROJECT_MANAGER)
  69.             || ($user->hasRole(User::ROLE_LEADER_WORKROOM
  70.                 && $this->userProjectRepository->getCountPrivateProjects($user) < $this->maxPrivateProjects);
  71.     }
  72. }