src/Security/Voter/UserVoter.php line 15

Open in your IDE?
  1. <?php
  2. namespace App\Security\Voter;
  3. use App\DBAL\Types\RoleEnumType;
  4. use App\DTO\UserRatingDTO;
  5. use App\Entity\User;
  6. use App\Service\CuratorService;
  7. use App\Service\DutyService;
  8. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  9. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  10. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  11. use Symfony\Component\Security\Core\User\UserInterface;
  12. class UserVoter extends Voter
  13. {
  14.     const SHOW_PHONE 'show_phone';
  15.     const SHOW_EMAIL 'show_email';
  16.     const SHOW_FOLLOW_ME 'show_follow_me';
  17.     const SHOW_RESUME 'show_resume';
  18.     const EDIT_PROFILE 'edit_profile';
  19.     const SHOW_DETAIL_RATING 'show_detail_rating';
  20.     const SHOW_FULLY_STUDENT 'show_fully_student';
  21.     const SEND_HOMEWORK 'send_homework';
  22.     const NEUROCHAT_ACCESS 'neurochat_access';
  23.     const NEUROSUPPORT_ACCESS 'neurosupport_access';
  24.     const COORDINATOR_MESSAGE_SEND 'coordinator_message_send';
  25.     const RESUME_ACCESS 'resume_access';
  26.     private AuthorizationCheckerInterface $authorizationChecker;
  27.     private CuratorService $curatorService;
  28.     private DutyService $dutyService;
  29.     public function __construct(
  30.         AuthorizationCheckerInterface $authorizationChecker,
  31.         CuratorService $curatorService,
  32.         DutyService $dutyService
  33.     ) {
  34.         $this->authorizationChecker $authorizationChecker;
  35.         $this->curatorService $curatorService;
  36.         $this->dutyService $dutyService;
  37.     }
  38.     protected function supports($attribute$subject)
  39.     {
  40.         return in_array($attribute, [
  41.             self::SHOW_PHONEself::SHOW_EMAILself::SHOW_FOLLOW_MEself::SHOW_RESUMEself::EDIT_PROFILE,
  42.             self::SHOW_DETAIL_RATINGself::SHOW_FULLY_STUDENTself::SEND_HOMEWORK,
  43.             self::NEUROCHAT_ACCESSself::NEUROSUPPORT_ACCESSself::COORDINATOR_MESSAGE_SENDself::RESUME_ACCESS,
  44.         ]);
  45.     }
  46.     /**
  47.      * @param string                   $attribute
  48.      * @param User|UserRatingDTO|mixed $subject
  49.      *
  50.      * @return bool
  51.      */
  52.     protected function voteOnAttribute($attribute$subjectTokenInterface $token)
  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.         // ... (check conditions and return true to grant permission) ...
  61.         switch ($attribute) {
  62.             case self::SHOW_PHONE:
  63.                 return $subject->getIsShowPhone() && ((bool) trim($subject->getPhone()));
  64.                 break;
  65.             case self::SHOW_EMAIL:
  66.                 return $subject->getIsShowEmail();
  67.                 break;
  68.             case self::SHOW_FOLLOW_ME:
  69.                 return $subject->getIsShowFollowMe() && ((bool) $subject->getFollowMeLinks()->count());
  70.                 break;
  71.             case self::SHOW_RESUME:
  72.                 return $subject->getIsShowResume() && ((bool) trim($subject->getResume()));
  73.                 break;
  74.             case self::EDIT_PROFILE:
  75.                 return $subject->getId() == $user->getId();
  76.                 break;
  77.             case self::SHOW_DETAIL_RATING:
  78.                 return $user == $subject
  79.                     || $user->hasRole(RoleEnumType::ROLE_CURATOR)
  80.                     || $user->hasRole(RoleEnumType::ROLE_MODERATOR)
  81.                     || $user->hasRole(RoleEnumType::ROLE_SUPER_ADMIN)
  82.                 ;
  83.             case self::SHOW_FULLY_STUDENT:
  84.                 $user $subject ?? $user;
  85.                 $isStudent = (bool) ($user->hasRole(RoleEnumType::ROLE_STUDENT) || $user->hasRole(RoleEnumType::ROLE_VIP_STUDENT) || $user->hasRole(RoleEnumType::ROLE_VIP_VIDEO_STUDENT));
  86.                 return (bool) ($isStudent && !$this->isDemo($user));
  87.                 break;
  88.             case self::NEUROCHAT_ACCESS:
  89.                 $user $subject ?? $user;
  90.                 return $user->hasProgramWithSupport()
  91.                     && (
  92.                         (
  93.                             $this->isDemo($user)
  94.                             && $user->hasRole(RoleEnumType::ROLE_NEUROCHAT_DEMO)
  95.                         )
  96.                         || (
  97.                             !$this->isDemo($user)
  98.                             && $this->authorizationChecker->isGranted('ROLE_NEUROCHAT_ACCESS'$user)
  99.                         )
  100.                     )
  101.                 ;
  102.             case self::NEUROSUPPORT_ACCESS:
  103.                 $user $subject ?? $user;
  104.                 return (
  105.                     $this->isDemo($user)
  106.                     && $user->hasRole(RoleEnumType::ROLE_NEUROSUPPORT_DEMO)
  107.                 )
  108.                 || (
  109.                     !$this->isDemo($user)
  110.                     && $this->authorizationChecker->isGranted('ROLE_NEUROSUPPORT_ACCESS'$user)
  111.                 );
  112.             case self::SEND_HOMEWORK:
  113.                 $user $subject ?? $user;
  114.                 return !$user->hasRole(RoleEnumType::ROLE_DEMO) || $user->hasRole(RoleEnumType::ROLE_DEMO_INTENSIVE);
  115.             case self::COORDINATOR_MESSAGE_SEND:
  116.                 $user $subject ?? $user;
  117.                 return $this->dutyService->issetDutyCoordinators();
  118.             case self::RESUME_ACCESS:
  119.                 return $user->hasRole(RoleEnumType::ROLE_RESUME_ACCESS)
  120.                     || $user->hasRole(RoleEnumType::ROLE_HR);
  121.         }
  122.         return false;
  123.     }
  124.     private function isDemo(User $user)
  125.     {
  126.         return $user->hasRole(RoleEnumType::ROLE_DEMO)
  127.             || $user->hasRole(RoleEnumType::ROLE_INTRO_STUDENT)
  128.             || $user->hasRole(RoleEnumType::ROLE_LIMITED_ACCESS)
  129.             || $user->hasRole(RoleEnumType::ROLE_DEMO_INTENSIVE)
  130.         ;
  131.     }
  132. }