adapta list of tasks for a person

This commit is contained in:
Julien Fastré 2021-10-29 16:26:19 +02:00
parent 4017f8db48
commit db15a3d53c
10 changed files with 224 additions and 344 deletions

View File

@ -17,6 +17,7 @@
*/
namespace Chill\CalendarBundle\Menu;
use Chill\MainBundle\Entity\User;
use Chill\MainBundle\Routing\LocalMenuBuilderInterface;
use Knp\Menu\MenuItem;
use Chill\TaskBundle\Templating\UI\CountNotificationTask;
@ -72,7 +73,9 @@ class UserMenuBuilder implements LocalMenuBuilderInterface
{
$user = $this->tokenStorage->getToken()->getUser();
if ($this->authorizationChecker->isGranted('ROLE_USER')){
if ($this->authorizationChecker->isGranted('ROLE_USER')
&& $user instanceof User
) {
$menu->addChild("My calendar list", [
'route' => 'chill_calendar_calendar_list',
'routeParameters' => [

View File

@ -165,8 +165,8 @@ final class SingleTaskController extends AbstractController
}
if ($entityType === 'person') {
return $this->redirectToRoute('chill_task_singletask_list', [
'person_id' => $task->getPerson()->getId()
return $this->redirectToRoute('chill_task_singletask_by-person_list', [
'id' => $task->getPerson()->getId()
]);
} elseif ($entityType === 'course') {
return $this->redirectToRoute('chill_task_singletask_by-course_list', [
@ -399,16 +399,15 @@ final class SingleTaskController extends AbstractController
$this->addFlash('success', $this->translator
->trans("The task has been successfully removed."));
if($task->getContext() instanceof Person){
if ($task->getContext() instanceof Person) {
return $this->redirect($this->generateUrl(
'chill_task_singletask_list',
$request->query->get('list_params', [
'person_id' => $person->getId()
])));
'chill_task_singletask_by-person_list',
[ 'id' => $task->getPerson()->getId() ]
));
} else {
return $this->redirect($this->generateUrl(
'chill_task_singletask_by-course_list',
['id' => $course->getId()]
['id' => $task->getCourse()->getId()]
));
}
}
@ -753,13 +752,14 @@ final class SingleTaskController extends AbstractController
* "/{_locale}/task/single-task/by-course/{id}",
* name="chill_task_singletask_by-course_list")
*/
public function listCourseTasks(
AccompanyingPeriod $course,
FormFactoryInterface $formFactory,
Request $request
): Response
{
$this->denyAccessUnlessGranted(TaskVoter::SHOW, $course);
$filterOrder = $this->buildFilterOrder();
$flags = \array_merge(
$filterOrder->getCheckboxData('status'),
@ -771,6 +771,8 @@ final class SingleTaskController extends AbstractController
$flags
);
$paginator = $this->paginatorFactory->create($nb);
if (0 < $nb) {
$tasks = $this->singleTaskAclAwareRepository->findByCourse(
$course,
$filterOrder->getQueryString(),
@ -782,6 +784,9 @@ final class SingleTaskController extends AbstractController
'endDate' => 'DESC',
]
);
} else {
$tasks = [];
}
return $this->render(
'@ChillTask/SingleTask/AccompanyingCourse/list.html.twig',
@ -793,4 +798,52 @@ final class SingleTaskController extends AbstractController
]);
}
/**
* @Route(
* "/{_locale}/task/single-task/by-person/{id}",
* name="chill_task_singletask_by-person_list")
*/
public function listPersonTasks(
Person $person
): Response {
$this->denyAccessUnlessGranted(TaskVoter::SHOW, $person);
$filterOrder = $this->buildFilterOrder();
$flags = \array_merge(
$filterOrder->getCheckboxData('status'),
\array_map(fn ($i) => 'state_'.$i, $filterOrder->getCheckboxData('states'))
);
$nb = $this->singleTaskAclAwareRepository->countByPerson(
$person,
$filterOrder->getQueryString(),
$flags
);
$paginator = $this->paginatorFactory->create($nb);
if (0 < $nb) {
$tasks = $this->singleTaskAclAwareRepository->findByPerson(
$person,
$filterOrder->getQueryString(),
$flags,
$paginator->getCurrentPageFirstItemNumber(),
$paginator->getItemsPerPage(),
[
'startDate' => 'DESC',
'endDate' => 'DESC',
]
);
} else {
$tasks = [];
}
return $this->render(
'@ChillTask/SingleTask/Person/list.html.twig',
[
'tasks' => $tasks,
'person' => $person,
'paginator' => $paginator,
'filter_order' => $filterOrder
]);
}
}

View File

@ -69,17 +69,22 @@ class LoadTaskACL extends AbstractFixture implements OrderedFixtureInterface
->setRole(TaskVoter::UPDATE)
->setScope($scope);
$permissionsGroup->addRoleScope($roleScopeUpdate);
$roleScopeCreate = (new RoleScope())
->setRole(TaskVoter::CREATE)
$roleScopeCreateP = (new RoleScope())
->setRole(TaskVoter::CREATE_PERSON)
->setScope($scope);
$permissionsGroup->addRoleScope($roleScopeCreate);
$permissionsGroup->addRoleScope($roleScopeCreateP);
$roleScopeCreateC = (new RoleScope())
->setRole(TaskVoter::CREATE_COURSE)
->setScope($scope);
$permissionsGroup->addRoleScope($roleScopeCreateC);
$roleScopeDelete = (new RoleScope())
->setRole(TaskVoter::DELETE)
->setScope($scope);
$permissionsGroup->addRoleScope($roleScopeDelete);
$manager->persist($roleScopeUpdate);
$manager->persist($roleScopeCreate);
$manager->persist($roleScopeCreateP);
$manager->persist($roleScopeCreateC);
$manager->persist($roleScopeDelete);
}

View File

@ -62,7 +62,8 @@ class ChillTaskExtension extends Extension implements PrependExtensionInterface
$container->prependExtensionConfig('security', array(
'role_hierarchy' => array(
TaskVoter::UPDATE => [TaskVoter::SHOW],
TaskVoter::CREATE => [TaskVoter::SHOW]
TaskVoter::CREATE_COURSE => [TaskVoter::SHOW],
TaskVoter::CREATE_PERSON => [TaskVoter::SHOW],
)
));
}

View File

@ -76,9 +76,9 @@ class MenuBuilder implements LocalMenuBuilderInterface
if ($this->authorizationChecker->isGranted(TaskVoter::SHOW, $person)) {
$menu->addChild(
$this->translator->trans('Tasks'), [
'route' => 'chill_task_singletask_list',
'route' => 'chill_task_singletask_by-person_list',
'routeParameters' =>
[ 'person_id' => $person->getId() ]
[ 'id' => $person->getId() ]
])
->setExtra('order', 400);
}

View File

@ -5,6 +5,7 @@ namespace Chill\TaskBundle\Repository;
use Chill\MainBundle\Security\Authorization\AuthorizationHelperInterface;
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\TaskBundle\Entity\SingleTask;
use Chill\TaskBundle\Security\Authorization\TaskVoter;
use Doctrine\ORM\EntityManagerInterface;
@ -78,6 +79,33 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
->getQuery()->getSingleScalarResult();
}
public function findByPerson(
Person $person,
?string $pattern = null,
?array $flags = [],
?int $start = 0,
?int $limit = 50,
?array $orderBy = []
): array {
$qb = $this->buildQueryByPerson($person, $pattern, $flags);
$qb = $this->addACL($qb, $person);
return $this->getResult($qb, $start, $limit, $orderBy);
}
public function countByPerson(
Person $person,
?string $pattern = null,
?array $flags = []
): int {
$qb = $this->buildQueryByPerson($person, $pattern, $flags);
return $this
->addACL($qb, $person)
->select('COUNT(t)')
->getQuery()->getSingleScalarResult();
}
public function buildQueryByCourse(
AccompanyingPeriod $course,
?string $pattern = null,
@ -91,6 +119,19 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
;
}
public function buildQueryByPerson(
Person $person,
?string $pattern = null,
?array $flags = []
): QueryBuilder
{
$qb = $this->buildBaseQuery($pattern, $flags);
return $qb
->andWhere($qb->expr()->eq('t.person', ':person'))
->setParameter('person', $person);
}
public function buildQueryMyTasks(
?string $pattern = null,
?array $flags = []
@ -123,7 +164,7 @@ final class SingleTaskAclAwareRepository implements SingleTaskAclAwareRepository
return $qb->getQuery()->getResult();
}
public function addACL(
private function addACL(
QueryBuilder $qb,
$entity
): QueryBuilder {

View File

@ -3,6 +3,7 @@
namespace Chill\TaskBundle\Repository;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
interface SingleTaskAclAwareRepositoryInterface
{
@ -25,4 +26,18 @@ interface SingleTaskAclAwareRepositoryInterface
?array $flags = []
): int;
public function findByPerson(
Person $person,
?string $pattern = null,
?array $flags = [],
?int $start = 0,
?int $limit = 50,
?array $orderBy = []
): array;
public function countByPerson(
Person $person,
?string $pattern = null,
?array $flags = []
): int;
}

View File

@ -21,6 +21,7 @@
{{ chill_pagination(paginator) }}
{% if is_granted('CHILL_TASK_TASK_CREATE_FOR_COURSE', person) %}
<ul class="record_actions sticky-form-buttons">
<li>
{% if accompanyingCourse is not null %}
@ -30,6 +31,7 @@
{% endif %}
</li>
</ul>
{% endif %}
</div>
{% endblock %}

View File

@ -1,251 +1,44 @@
{% macro date_status(title, tasks, count, paginator, status, isSingleStatus, person, user) %}
{% if tasks|length > 0 %}
<h3>{{ title|trans }}</h3>
{% extends '@ChillPerson/Person/layout.html.twig' %}
<table class="table table-bordered border-dark chill-task-list">
<tbody>
{% for task in tasks %}
<tr>
<td>
<div>
<span class="chill-task-list__row__title">{{ task.title }}</span>
</div>
{% set activeRouteKey = '' %}
{% if person is null %}
<div>
<span class="chill-task-list__row__person-for">{{ 'For person'|trans }}&nbsp;:</span>
<span class="chill-task-list__row__person">
<a href="{{ path('chill_person_view', {person_id : task.person.Id}) }}">{{ task.person}}</a>
</span>
</div>
{% endif %}
{% block title 'Tasks for {{ name }}'|trans({ '{{ name }}' : person|chill_entity_render_string }) %}
<div>
<span class="chill-task-list__row__type">{{ task_workflow_metadata(task, 'definition.name')|trans }}</span>
</div>
{% block personcontent %}
<div class="col-md-10 col-xxl">
<div>
{% for place in workflow_marked_places(task) %}
<span class="task-status box type-{{ task.type }} place-{{ place }}">{{ place|trans }}</span>
{% endfor %}
{% if task.assignee is not null %}
<div class="chill-task-list__row__assignee">
<span class="chill_task-list__row__assignee_by">{{ 'By'|trans }}&nbsp;:</span>
{{ task.assignee.username }}</div>
{% endif %}
</div>
<h1>{{ block('title') }}</h1>
{% if task.startDate is not null or task.warningDate is not null or task.endDate is not null %}
<div class="chill-task-list__row__dates">
<ul class="record_actions column">
{% if task.startDate is not null %}
<li title="{{ 'Start'|trans|escape('html_attr') }}">
<i class="fa fa-play"></i>
{{ task.startDate|format_date('medium') }}
</li>
{% endif %}
{% if task.warningDate is not null %}
<li title="{{ 'Warning'|trans|escape('html_attr') }}">
<i class="fa fa-exclamation-triangle"></i>
{{ task.warningDate|format_date('medium') }}
</li>
{% endif %}
{% if task.endDate is not null %}
<li title="{{ 'End'|trans|escape('html_attr') }}">
<i class="fa fa-hourglass-end"></i>
{{ task.endDate|format_date('medium') }}
</li>
{% endif %}
</ul>
</div>
{% endif %}
{{ filter_order|chill_render_filter_order_helper }}
</td>
<td>
<ul class="record_actions">
{% if workflow_transitions(task)|length > 0 %}
<li>
<div class="btn-group">
<a class="btn btn-task-exchange dropdown-toggle" href="#" role="button" id="taskExchange" data-bs-toggle="dropdown" aria-expanded="false">
{{'Change task status'|trans}}
</a>
<ul class="dropdown-menu" aria-labelledby="taskExchange">
{% for transition in workflow_transitions(task) %}
<li>
<a class="dropdown-item" href="{{ path('chill_task_task_transition', { 'taskId': task.id, 'transition': transition.name, 'kind': 'single-task', 'list_params': app.request.query.all }) }}" class="{{ task_workflow_metadata(task, 'transition.class', transition)|e('html_attr') }}">
{{ task_workflow_metadata(task, 'transition.verb', transition)|trans }}
</a>
</li>
{% endfor %}
</ul>
</div>
</li>
{% endif %}
<li>
<a href="{{ path('chill_task_single_task_show', { 'id': task.id, 'list_params': app.request.query.all }) }}" class="btn btn-show "></a>
</li>
{% if is_granted('CHILL_TASK_TASK_UPDATE', task) %}
<li>
<a href="{{ path('chill_task_single_task_edit', { 'id': task.id, 'list_params': app.request.query.all }) }}" class="btn btn-update "></a>
</li>
{% endif %}
{% if is_granted('CHILL_TASK_TASK_DELETE', task) %}
<li>
<a href="{{ path('chill_task_single_task_delete', { 'id': task.id, 'list_params': app.request.query.all } ) }}" class="btn btn-delete "></a>
</li>
{% endif %}
</ul>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{% if isSingleStatus %}
{% if tasks|length < paginator.getTotalItems %}
{{ chill_pagination(paginator) }}
{% endif %}
<!-- lien retour -->
<ul class="record_actions">
<li>
{% if person is not null %}
<a href="{{ path('chill_task_singletask_list', {'person_id': person.id}) }}" class="btn btn-cancel">
{{ 'Back to the list' | trans }}
</a>
{% endif %}
{% if user is not null %}
<a href="{{ path('chill_task_singletask_list') }}" class="btn btn-cancel">
{{ 'Back to the list' | trans }}
</a>
{% endif %}
</li>
<li>
{% if person is not null %}
<a href="{{ path('chill_task_single_task_new', {'person_id': person.id}) }}" class="btn btn-create">
{{ 'Add a new task' | trans }}
</a>
{% endif %}
{% if user is not null %}
<a href="{{ path('chill_task_single_task_new') }}" class="btn btn-create">
{{ 'Add a new task' | trans }}
</a>
{% endif %}
</li>
</ul>
{% if tasks|length == 0 %}
<p class="chill-no-data-statement">{{ 'Any tasks'|trans }}</p>
{% else %}
<ul class="record_actions">
<li>
<a href="{{ path('chill_task_singletask_list', app.request.query.all|merge({ 'status': [ status ] })) }}" class="btn btn-misc">
{{ 'See more' | trans }}
</a>
</li>
</ul>
<div class="flex-table chill-task-list">
{% for task in tasks %}
{% include 'ChillTaskBundle:SingleTask/List:index_item.html.twig' with { 'showContext' : false } %}
{% endfor %}
</div>
{% endif %}
{% endif %}
{% endmacro %}
{{ chill_pagination(paginator) }}
{% import _self as helper %}
<h1>{{ app.request.query.get('title', null)|escape('html')|default('Task list'|trans) }}</h1>
{% if false == app.request.query.boolean('hide_form', false) %}
<h2>{{ 'Filter the tasks'|trans }}</h2>
{{ form_start(form) }}
{{ form_row(form.user_id) }}
{% if form.status is defined %}
{{ form_row(form.status) }}
{% endif %}
{% if form.types is defined %}
{{ form_row(form.types) }}
{% endif %}
{% if form.person_id is defined %}
{{ form_row(form.person_id) }}
{% endif %}
{% if form.center_id is defined %}
{{ form_row(form.center_id) }}
{% endif %}
<ul class="record_actions">
<li>
<button type="submit" class="btn btn-submit">{{ 'Filter'|trans }}</button>
</li>
</ul>
{{ form_end(form)}}
{% endif %}
{% if tasks_count == 0 %}
<p class="chill-no-data-statement">{{ "There is no tasks."|trans }}</p>
{% if person is not null and is_granted('CHILL_TASK_TASK_CREATE', person) %}
<ul class="record_actions">
<li>
{% if person is not null %}
<a href="{{ path('chill_task_single_task_new', {'person_id': person.id}) }}" class="btn btn-create">
{{ 'Add a new task' | trans }}
</a>
{% endif %}
</li>
</ul>
{% endif %}
{% else %}
{% if false == app.request.query.boolean('hide_form', false) %}
<h2>{{ 'Tasks'|trans }}</h2>
{% endif %}
{# TODO reimplement right to create task. #}
{# {% if person is not null and is_granted('CHILL_TASK_TASK_CREATE', person) %} #}
<ul class="record_actions">
<li>
{% if person is not null %}
<a href="{{ path('chill_task_single_task_new', {'person_id': person.id}) }}" class="btn btn-create">
{{ 'Add a new task' | trans }}
</a>
{% endif %}
</li>
</ul>
{# {% endif %} #}
{% if single_task_ended_tasks is defined %}
{{ helper.date_status('Tasks with expired deadline', single_task_ended_tasks, single_task_ended_count, single_task_ended_paginator, 'ended', isSingleStatus, person) }}
{% endif %}
{% if single_task_warning_tasks is defined %}
{{ helper.date_status('Tasks with warning deadline reached', single_task_warning_tasks, single_task_warning_count, single_task_warning_paginator, 'warning', isSingleStatus, person) }}
{% endif %}
{% if single_task_current_tasks is defined %}
{{ helper.date_status('Current tasks', single_task_current_tasks, single_task_current_count, single_task_current_paginator, 'current', isSingleStatus, person) }}
{% endif %}
{% if single_task_not_started_tasks is defined %}
{{ helper.date_status('Tasks not started', single_task_not_started_tasks, single_task_not_started_count, single_task_not_started_paginator, 'not_started', isSingleStatus, person) }}
{% endif %}
{% if single_task_closed_tasks is defined %}
{{ helper.date_status('Closed tasks', single_task_closed_tasks, single_task_closed_count, single_task_closed_paginator, 'closed', isSingleStatus, person) }}
{% endif %}
{% if isSingleStatus == false %}
{% if is_granted('CHILL_TASK_TASK_CREATE_FOR_PERSON', person) %}
<ul class="record_actions sticky-form-buttons">
<li>
{% if person is not null and is_granted('CHILL_TASK_TASK_CREATE', person) %}
<a href="{{ path('chill_task_single_task_new', {'person_id': person.id}) }}" class="btn btn-create">
{{ 'Create' | trans }}
</a>
{% endif %}
</li>
</ul>
{% endif %}
{% endif %}
</div>
{% endblock %}
{% block css %}
{{ encore_entry_link_tags('page_task_list') }}
{% endblock %}
{% block js %}
{{ encore_entry_script_tags('page_task_list') }}
{% endblock %}

View File

@ -41,16 +41,18 @@ use Chill\TaskBundle\Security\Authorization\AuthorizationEvent;
final class TaskVoter extends AbstractChillVoter implements ProvideRoleHierarchyInterface
{
const CREATE = 'CHILL_TASK_TASK_CREATE';
const UPDATE = 'CHILL_TASK_TASK_UPDATE';
const SHOW = 'CHILL_TASK_TASK_SHOW';
const CREATE_COURSE = 'CHILL_TASK_TASK_CREATE_FOR_COURSE';
const CREATE_PERSON = 'CHILL_TASK_TASK_CREATE_FOR_PERSON';
const DELETE = 'CHILL_TASK_TASK_DELETE';
const SHOW = 'CHILL_TASK_TASK_SHOW';
const UPDATE = 'CHILL_TASK_TASK_UPDATE';
const ROLES = [
self::CREATE,
self::UPDATE,
self::CREATE_COURSE,
self::CREATE_PERSON,
self::DELETE,
self::SHOW,
self::DELETE
self::UPDATE,
];
protected AuthorizationHelper $authorizationHelper;
@ -84,8 +86,8 @@ final class TaskVoter extends AbstractChillVoter implements ProvideRoleHierarchy
$this->voter = $voterFactory
->generate(AbstractTask::class)
->addCheckFor(AbstractTask::class, self::ROLES)
->addCheckFor(Person::class, [self::SHOW, self::CREATE])
->addCheckFor(AccompanyingPeriod::class, [self::SHOW, self::CREATE])
->addCheckFor(Person::class, [self::SHOW, self::CREATE_PERSON])
->addCheckFor(AccompanyingPeriod::class, [self::SHOW, self::CREATE_COURSE])
->addCheckFor(null, [self::SHOW])
->build()
;
@ -147,41 +149,6 @@ final class TaskVoter extends AbstractChillVoter implements ProvideRoleHierarchy
// do regular check.
return $this->voter->voteOnAttribute($attribute, $subject, $token);
if ($subject instanceof AbstractTask) {
$associated = $subject->getPerson() ?? $subject->getCourse();
if ($associated === null) {
throw new \LogicException("You should associate a person with task "
. "in order to check autorizations");
}
$person = $subject->getPerson();
} elseif ($subject instanceof Person) {
// subject is null. We check that at least one center is reachable
$centers = $this->authorizationHelper->getReachableCenters($token->getUser(), new Role($attribute));
return count($centers) > 0;
}
if (!$this->accessDecisionManager->decide($token, [PersonVoter::SEE], $person)) {
return false;
}
$center = $this->centerResolverDispatcher->resolveCenter($subject);
if (NULL === $center) {
return false;
} elseif ($associated instanceof AccompanyingPeriod && !$this->accessDecisionManager->decide($token, [AccompanyingPeriodVoter::SEE], $associated)) {
return false;
} elseif ($associated instanceof AccompanyingPeriod && !$this->accessDecisionManager->decide($token, [AccompanyingPeriodVoter::SEE], $associated)) {
return false;
}
return $this->authorizationHelper->userHasAccess(
$token->getUser(),
$subject,
$attribute
);
}
public function getRoles()