src/Security/Voter/ProfileVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\Entity\Profile\Profile;
  4. use App\Entity\User\User;
  5. use App\Enums\Constants;
  6. use App\Repository\User\RoleRepository;
  7. use App\Service\AuthenticationService;
  8. use App\Service\ProfileService;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. class ProfileVoter extends Voter
  13. {
  14.     private const PROFILE_ACCESS 'PROFILE_ACCESS';
  15.     private const PROFILE_OWNER 'PROFILE_OWNER';
  16.     private const PROFILE_ADMIN 'PROFILE_ADMIN';
  17.     private const PROFILE_EDITOR 'PROFILE_EDITOR';
  18.     private const PROFILE_ANALYTIC 'PROFILE_ANALYTIC';
  19.     /**
  20.      * @var ProfileService
  21.      */
  22.     private $profileService;
  23.     /**
  24.      * @var RoleRepository
  25.      */
  26.     private $roleRepository;
  27.     /** @var AuthenticationService */
  28.     private $authenticationService;
  29.     public function __construct(
  30.         ProfileService $profileService,
  31.         RoleRepository $roleRepository,
  32.         AuthenticationService $authenticationService
  33.     )
  34.     {
  35.         $this->profileService $profileService;
  36.         $this->roleRepository $roleRepository;
  37.         $this->authenticationService $authenticationService;
  38.     }
  39.     protected function supports($attribute$subject)
  40.     {
  41.         return in_array($attribute, [
  42.             self::PROFILE_ACCESS,
  43.             self::PROFILE_OWNER,
  44.             self::PROFILE_ADMIN,
  45.             self::PROFILE_EDITOR,
  46.             self::PROFILE_ANALYTIC,
  47.         ], true) && $subject instanceof Profile;
  48.     }
  49.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  50.     {
  51.         if($_ENV['CWR_ENVIROMENT'] === "local"){
  52.             return true;
  53.         }
  54.         /** @var User $user */
  55.         $user $token->getUser();
  56.         // if the user is anonymous, do not grant access
  57.         if (!$user instanceof UserInterface) {
  58.             return false;
  59.         }
  60.         if($this->authenticationService->userIsMmpzServiceAdmin($user)){
  61.             return true;
  62.         }
  63.        switch ($attribute) {
  64.             case self::PROFILE_ACCESS:
  65.                 return $this->profileService->userCanAccessProfile($user$subject);
  66.             case self::PROFILE_OWNER:
  67.                 $ownerRole $this->roleRepository->findOneByName(Constants::PROFILE_ROLE_NAME_OWNER);
  68.                 return
  69.                     $this->profileService->roleSupportedBetweenUserAndProfile($user$subject$ownerRole);
  70.             case self::PROFILE_ADMIN:
  71.                 $adminRole $this->roleRepository->findOneByName(Constants::PROFILE_ROLE_NAME_ADMIN);
  72.                 return
  73.                     $this->profileService->roleSupportedBetweenUserAndProfile($user$subject$adminRole);
  74.             case self::PROFILE_EDITOR:
  75.                 $editorRole $this->roleRepository->findOneByName(Constants::PROFILE_ROLE_NAME_EDITOR);
  76.                 return
  77.                     $this->profileService->checkTheRolByUserInAProfile($subject$user);
  78.             case self::PROFILE_ANALYTIC:
  79.                 $role $this->profileService->checkTheRolByUserInAProfile($subject$user);
  80.                 if($role->getName() !== Constants::PROFILE_ROLE_NAME_EDITOR){
  81.                     return true;
  82.                 }
  83.         }
  84.         return false;
  85.     }
  86. }