<?php
namespace App\V4\Voters;
use App\Security\User;
use App\V4\Entity\CustomAction;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class CustomActionVoter extends Voter
{
const CUSTOM_ACTION_SHOW_LIST = 'custom_action_show_list';
const CUSTOM_ACTION_ADD_EDIT = 'custom_action_add_edit';
const CUSTOM_ACTION_SHOW = 'custom_action_show';
/**
* @param $attribute
* @param $subject
*
* @return bool
*/
protected function supports($attribute, $subject): bool
{
if ($subject instanceof CustomAction && in_array($attribute, [
self::CUSTOM_ACTION_SHOW,
], true)) {
return true;
}
return in_array($attribute, [
self::CUSTOM_ACTION_SHOW_LIST,
self::CUSTOM_ACTION_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::CUSTOM_ACTION_SHOW_LIST:
return $this->canShowListCustomAction($user);
case self::CUSTOM_ACTION_ADD_EDIT:
return $this->canAddEditCustomAction($user);
case self::CUSTOM_ACTION_SHOW:
return $this->canAccessCustomAction($user);
}
throw new LogicException('This should never happen');
}
/**
* @param User $user
*
* @return bool
*/
private function canShowListCustomAction(User $user): bool
{
return $user->isSuperAdmin();
}
/**
* @param User $user
*
* @return bool
*/
private function canAddEditCustomAction(User $user): bool
{
return $user->isSuperAdmin();
}
/**
* @param User $user
*
* @return bool
*/
private function canAccessCustomAction(User $user): bool
{
return $user->isSuperAdmin();
}
}