<?php
namespace App\V4\Voters;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\Model\ModuleConfiguration\ModuleConfiguration;
use App\Security\SecurityConfig;
use App\Security\User;
use App\V4\Enum\Tag\TagConfigEnum;
use App\V4\Model\Task\Task;
use App\V4\Repository\TaskTagRepository;
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
{
public const TASK_MANAGE_LIST = 'task_manage_list';
public const TASK_SHOW_LIST = 'task_show_list';
public const TASK_ADD_EDIT = 'task_add_edit';
public const TASK_MANAGE_FIELDS = 'task_manage_fields';
public const TASK_STATE_ADD_EDIT = 'task_state_add_edit';
public const CAN_CHANGE_TASK_STATUS = 'can_change_task_status';
/**
* @var Security
*/
private $security;
/**
* @var TaskTagRepository
*/
private $taskTagRepository;
/**
* @var CollectionDataProviderInterface
*/
private $collectionDataProvider;
public function __construct(
Security $security,
TaskTagRepository $taskTagRepository,
CollectionDataProviderInterface $collectionDataProvider
) {
$this->security = $security;
$this->taskTagRepository = $taskTagRepository;
$this->collectionDataProvider = $collectionDataProvider;
}
/**
* @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,
self::CAN_CHANGE_TASK_STATUS,
], 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();
case self::CAN_CHANGE_TASK_STATUS:
return $this->canOpenCloseTask($user, $subject);
}
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;
}
private function canUseModuleTag(User $user): bool
{
return in_array(SecurityConfig::MODULE_TAG, $user->getRoles(), true);
}
/**
* @throws ResourceClassNotSupportedException
*/
private function canOpenCloseTask(User $user, $task): bool
{
if (!$this->canUseModuleTag($user)) {
return true;
}
if (!$task instanceof Task) {
return false;
}
$moduleConfigs = $this->collectionDataProvider->getCollection(ModuleConfiguration::class, null, [
'filters' => [
'moduleId' => SecurityConfig::MODULE_TAG,
'keyConf' => TagConfigEnum::CRM_TASK_PREVENT_CLOSING_WHEN_TAG_UNCLOSED,
],
]);
if (1 !== count($moduleConfigs) || "true" !== $moduleConfigs[0]->getValue()) {
return true;
}
$tags = $this->taskTagRepository->findBy(['taskId' => $task->getId()]);
foreach ($tags as $tag) {
if (!$tag->getDone()) {
return false;
}
}
return true;
}
}