src/Security/Voter/UserProjectVoter.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Common\RoleInterface;
  4. use App\Entity\Common\StateInterface;
  5. use App\Entity\Project;
  6. use App\Entity\User;
  7. use App\Entity\UserProject;
  8. use App\Entity\UserWorkroom;
  9. use App\Entity\Workroom;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  12. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  13. use Symfony\Component\Security\Core\User\UserInterface;
  14. /**
  15.  * Class UserProjectVoter.
  16.  */
  17. class UserProjectVoter extends Voter
  18. {
  19.     // Permissions.
  20.     const PROJECT_MANAGE_VIEW_CDP 'project_manage_view_cdp';
  21.     const PROJECT_MANAGE_VIEW_CDP_AND_WL 'project_manage_view_cdp_and_wl';
  22.     // Full list.
  23.     const PERMISSIONS = [
  24.         self::PROJECT_MANAGE_VIEW_CDP,
  25.         self::PROJECT_MANAGE_VIEW_CDP_AND_WL,
  26.     ];
  27.     private EntityManagerInterface $em;
  28.     public function __construct(EntityManagerInterface $em)
  29.     {
  30.         $this->em $em;
  31.     }
  32.     /**
  33.      * {@inheritdoc}
  34.      */
  35.     protected function supports($attribute$subject): bool
  36.     {
  37.         return in_array($attributeself::PERMISSIONS) && ($subject instanceof Project);
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     protected function voteOnAttribute($attribute$subjectTokenInterface $token): bool
  43.     {
  44.         /** @var User $user */
  45.         $user $token->getUser();
  46.         // If the user is anonymous, do not grant access.
  47.         if (!$user instanceof UserInterface) {
  48.             return false;
  49.         }
  50.         // Check conditions and provide access bases on it.
  51.         switch ($attribute) {
  52.             case self::PROJECT_MANAGE_VIEW_CDP:
  53.                 return $this->canViewCdp($subject$user);
  54.             case self::PROJECT_MANAGE_VIEW_CDP_AND_WL:
  55.                 return $this->canViewCdpAndWl($subject$user);
  56.             default:
  57.                 break;
  58.         }
  59.         return false;
  60.     }
  61.     /**
  62.      * Check that the user can view the workroom
  63.      * If the user has UserWorkroom entity we provide access, otherwise - not !!!
  64.      */
  65.     public function canViewCdp(Project $projectUser $user): bool
  66.     {
  67.         if ($project->getState() == StateInterface::STATE_ARCHIVED_INT) return false;
  68.         $userProject $this->em->getRepository(UserProject::class)->findOneBy([
  69.             'user' => $user,
  70.             'project' => $project
  71.         ]);
  72.         return ($userProject->getRole() == RoleInterface::ROLE_PROJECT_MANAGER_INT) ? true false;
  73.     }
  74.     /**
  75.      * Check that the user can view the workroom
  76.      * If the user has UserWorkroom entity we provide access, otherwise - not !!!
  77.      */
  78.     public function canViewCdpAndWl(Project $projectUser $user): bool
  79.     {
  80.         if ($project->getState() == StateInterface::STATE_ARCHIVED_INT) return false;
  81.         $userProject $this->em->getRepository(UserProject::class)->findOneBy([
  82.             'user' => $user,
  83.             'project' => $project
  84.         ]);
  85.         if ($userProject->getRole() == RoleInterface::ROLE_PROJECT_MANAGER_INT) return true;
  86.         $workroomsOfProject $project->getWorkroomsIds();
  87.         $workroomsOfUser $user->getUserWorkrooms();
  88.         foreach ($workroomsOfUser as $workroomUser) {
  89.             if (in_array($workroomUser->getWorkroom()->getId(), $workroomsOfProject)) {
  90.                 if ($workroomUser->getRole() == RoleInterface::ROLE_LEADER_WORKROOM_INT) {
  91.                     return true;
  92.                 }
  93.             }
  94.         }
  95.         return false;
  96.     }
  97. }