src/Profile/Security/Voter/SubprofileVoter.php line 13

Open in your IDE?
  1. <?php
  2. namespace App\Profile\Security\Voter;
  3. use App\Profile\Model\Profile;
  4. use App\Profile\Model\ProfileRoles;
  5. use Pimcore\Model\DataObject\Objectbrick\Data\AuctionhouseProfile;
  6. use Pimcore\Model\DataObject\Objectbrick\Data\RacetrackProfile;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. use Symfony\Component\Security\Core\Authorization\Voter\Voter;
  9. use Symfony\Component\Security\Core\Security;
  10. class SubprofileVoter extends Voter
  11. {
  12.   public const LIST = 'profile.subprofile.list';
  13.   public function __construct(protected Security $security)
  14.   {
  15.   }
  16.   protected function supports(string $attribute$subject): bool
  17.   {
  18.     if (!in_array($attribute, [self::LIST])) {
  19.       return false;
  20.     }
  21.     return true;
  22.   }
  23.   protected function voteOnAttribute(string $attribute$subjectTokenInterface $token): bool
  24.   {
  25.     $user $token->getUser();
  26.     if (!$user instanceof Profile) {
  27.       return false;
  28.     }
  29.     $subprofiles $subject;
  30.     switch ($attribute) {
  31.       case self::LIST:
  32.         return $this->canList($user$subprofiles);
  33.     }
  34.     throw new \LogicException(sprintf('Attribute `%s` not supported by object'$attribute));
  35.   }
  36.   protected function canList(Profile $profile, array $subprofiles): bool
  37.   {
  38.     if (
  39.       in_array(AuctionhouseProfile::class, $subprofiles) ||
  40.       in_array(RacetrackProfile::class, $subprofiles)) {
  41.       return
  42.         $this->security->isGranted(ProfileRoles::ROLE_CLUB_HORSE) ||
  43.         $this->security->isGranted(ProfileRoles::ROLE_CLUB_JOCKEY);
  44.     }
  45.     return true;
  46.   }
  47. }