<?php
namespace App\Profile\Security\Voter;
use App\Profile\Model\Profile;
use App\Profile\Model\ProfileRoles;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
use Symfony\Component\Security\Core\Security;
class TimelinePostVoter extends Voter
{
public const CREATE = 'profile.timeline.post.create';
public const CREATE_SERVICE = 'profile.timeline.post.create.service';
public function __construct(protected Security $security)
{
}
protected function supports(string $attribute, $subject): bool
{
if (!in_array($attribute, [self::CREATE, self::CREATE_SERVICE])) {
return false;
}
return true;
}
protected function voteOnAttribute(string $attribute, $subject, TokenInterface $token): bool
{
$user = $this->security->getUser();
if (!$user instanceof Profile) {
return false;
}
switch ($attribute) {
case self::CREATE:
return $this->canCreate($user);
break;
case self::CREATE_SERVICE:
return $this->canCreateService($user);
break;
}
throw new \LogicException(sprintf('Attribute `%s` not supported by object', $attribute));
}
protected function canCreate(Profile $profile): bool
{
return $this->security->isGranted(ProfileRoles::ROLE_USER);
}
protected function canCreateService(Profile $profile): bool
{
return $this->security->isGranted(ProfileRoles::ROLE_CLUB_BUSINESS) || $this->security->isGranted(ProfileRoles::ROLE_CLUB_JOCKEY);
}
}