<?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;
use Symfony\Component\Security\Core\Security;
class TaskVoter extends Voter
{
const TASK_MANAGE_LIST = 'task_manage_list';
const TASK_SHOW_LIST = 'task_show_list';
const TASK_ADD_EDIT = 'task_add_edit';
const TASK_MANAGE_FIELDS = 'task_manage_fields';
const TASK_STATE_ADD_EDIT = 'task_state_add_edit';
/**
* @var Security
*/
private $security;
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* @param $attribute
* @param $subject
*
* @return bool
*/
protected function supports($attribute, $subject): bool
{
return in_array($attribute, [
self::TASK_MANAGE_LIST,
self::TASK_SHOW_LIST,
self::TASK_ADD_EDIT,
self::TASK_MANAGE_FIELDS,
self::TASK_STATE_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::TASK_MANAGE_LIST:
return $this->canTaskManageList();
case self::TASK_SHOW_LIST:
return $this->canTaskShowList();
case self::TASK_ADD_EDIT:
return $this->canTaskAddEdit();
case self::TASK_MANAGE_FIELDS:
return $this->canTaskManageFields();
case self::TASK_STATE_ADD_EDIT:
return $this->canTaskStateAddEdit();
}
throw new LogicException('This should never happen');
}
private function canTaskManageList(): bool
{
return $this->canAccessTask();
}
private function canTaskShowList(): bool
{
return $this->canAccessTask();
}
private function canTaskAddEdit(): bool
{
return $this->canAccessTask();
}
private function canTaskManageFields(): bool
{
return $this->canAccessTask();
}
private function canTaskStateAddEdit(): bool
{
return $this->canAccessTask();
}
/**
* @TODO: Si le client n'a pas le droit "task show", il peut tout de même voir les actions qu'il gère.
*
* @return bool
*/
private function canAccessTask(): bool
{
return true;
}
}