<?php
namespace App\V4\Voters;
use App\Security\User;
use App\V4\Entity\CustomerEventService;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class CustomerEventServiceVoter extends Voter
{
const CUSTOMER_EVENT_SERVICE_SHOW_LIST = 'customer_event_service_show_list';
const CUSTOMER_EVENT_SERVICE_ADD_EDIT = 'customer_event_service_add_edit';
const CUSTOMER_EVENT_SERVICE_SHOW = 'customer_event_service_show';
/**
* @param $attribute
* @param $subject
*
* @return bool
*/
protected function supports($attribute, $subject): bool
{
if ($subject instanceof CustomerEventService && in_array($attribute, [
self::CUSTOMER_EVENT_SERVICE_SHOW,
], true)) {
return true;
}
return in_array($attribute, [
self::CUSTOMER_EVENT_SERVICE_SHOW_LIST,
self::CUSTOMER_EVENT_SERVICE_ADD_EDIT,
], true);
}
/**
* @param $attribute
* @param $subject
* @param TokenInterface $token
*
* @return bool
*
* @throws LogicException
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token): bool
{
$user = $token->getUser();
if (!$user instanceof User) {
return false;
}
switch ($attribute) {
case self::CUSTOMER_EVENT_SERVICE_SHOW_LIST:
return $this->canShowListCustomerEventService($user);
case self::CUSTOMER_EVENT_SERVICE_ADD_EDIT:
return $this->canAddEditCustomerEventService($user);
case self::CUSTOMER_EVENT_SERVICE_SHOW:
return $this->canAccessCustomerEventService($user);
}
throw new LogicException('This should never happen');
}
/**
* @param User $user
*
* @return bool
*/
private function canShowListCustomerEventService(User $user): bool
{
return $user->isSuperAdmin();
}
/**
* @param User $user
*
* @return bool
*/
private function canAddEditCustomerEventService(User $user): bool
{
return $user->isSuperAdmin();
}
/**
* @param User $user
*
* @return bool
*/
private function canAccessCustomerEventService(User $user): bool
{
return $user->isSuperAdmin();
}
}