<?php
namespace App\V4\Voters;
use App\Security\User;
use LogicException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class ListingVoter extends Voter
{
const LISTING_MANAGE_LIST = 'listing_manage_list';
/**
* @param $attribute
* @param $subject
*
* @return bool
*/
protected function supports($attribute, $subject): bool
{
return in_array($attribute, [
self::LISTING_MANAGE_LIST,
], 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::LISTING_MANAGE_LIST:
return $this->canListingManageList($user);
}
throw new LogicException('This should never happen');
}
/**
* @param User $user
*
* @return bool
*/
private function canListingManageList(User $user): bool
{
return $user->isAdmin();
}
}