src/Profile/Subprofiles/Controller/Voter/SubprofileAdminVoter.php line 10

Open in your IDE?
  1. <?php
  2. namespace App\Profile\Subprofiles\Controller\Voter;
  3. use App\Profile\Model\Profile;
  4. use App\Profile\Model\ProfileRoles;
  5. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  6. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  7. class SubprofileAdminVoter extends Voter {
  8.   const VIEW 'view';
  9.   protected function supports(string $attributemixed $subject): bool
  10.   {
  11.     if (!in_array($attribute, [self::VIEW])) {
  12.       return false;
  13.     }
  14.     return true;
  15.   }
  16.   protected function voteOnAttribute(string $attribute$subjectTokenInterface $token)
  17.   {
  18.     /** @var Profile */
  19.     $user $token->getUser();
  20.     switch ($attribute) {
  21.       case self::VIEW:
  22.         return $this->canView($subject$user);
  23.     }
  24.     throw new \LogicException('This code should not be reached');
  25.   }
  26.   private function canView($subject$user) {
  27.     $roles $user->getRoles();
  28.     if (in_array(ProfileRoles::ROLE_CLUB_HORSE$roles)) {
  29.       return true;
  30.     }
  31.     $subprofiles $user->getActiveSubprofileTypes();
  32.     if (in_array($subject$subprofiles)) {
  33.       return true;
  34.     }
  35.     return false;
  36.   }
  37. }