fix return path in tasks

This commit is contained in:
Julien Fastré 2021-10-28 00:50:18 +02:00
parent 97dbc4bc16
commit e63d645f8a
8 changed files with 75 additions and 168 deletions

View File

@ -160,13 +160,17 @@ final class SingleTaskController extends AbstractController
$this->addFlash('success', $this->translator->trans("The task is created")); $this->addFlash('success', $this->translator->trans("The task is created"));
if ($request->query->has('returnPath')) {
return $this->redirect($request->query->get('returnPath'));
}
if ($entityType === 'person') { if ($entityType === 'person') {
return $this->redirectToRoute('chill_task_singletask_list', [ return $this->redirectToRoute('chill_task_singletask_list', [
'person_id' => $task->getPerson()->getId() 'person_id' => $task->getPerson()->getId()
]); ]);
} elseif ($entityType === 'course') { } elseif ($entityType === 'course') {
return $this->redirectToRoute('chill_task_singletask_courselist', [ return $this->redirectToRoute('chill_task_singletask_by-course_list', [
'course_id' => $task->getCourse()->getId() 'id' => $task->getCourse()->getId()
]); ]);
} }
} else { } else {
@ -179,13 +183,13 @@ final class SingleTaskController extends AbstractController
return $this->render('@ChillTask/SingleTask/Person/new.html.twig', array( return $this->render('@ChillTask/SingleTask/Person/new.html.twig', array(
'form' => $form->createView(), 'form' => $form->createView(),
'task' => $task, 'task' => $task,
'person' => $person, 'person' => $task->getPerson(),
)); ));
case 'course': case 'course':
return $this->render('@ChillTask/SingleTask/AccompanyingCourse/new.html.twig', array( return $this->render('@ChillTask/SingleTask/AccompanyingCourse/new.html.twig', array(
'form' => $form->createView(), 'form' => $form->createView(),
'task' => $task, 'task' => $task,
'accompanyingCourse' => $course, 'accompanyingCourse' => $task->getCourse(),
)); ));
default: default:
throw new \LogicException("entity context not supported"); throw new \LogicException("entity context not supported");
@ -198,68 +202,23 @@ final class SingleTaskController extends AbstractController
* name="chill_task_single_task_show" * name="chill_task_single_task_show"
* ) * )
*/ */
public function showAction($id, Request $request) public function showAction(SingleTask $task, Request $request)
{ {
$this->denyAccessUnlessGranted(TaskVoter::SHOW, $task);
$em = $this->getDoctrine()->getManager(); if ($person = $task->getContext() instanceof Person) {
$task = $em->getRepository(SingleTask::class)->find($id);
if (!$task) {
throw $this->createNotFoundException('Unable to find Task entity.');
}
if ($task->getPerson() !== null) {
$personId = $task->getPerson()->getId();
if ($personId === null) {
return new Response("You must provide a person_id", Response::HTTP_BAD_REQUEST);
}
$person = $this->getDoctrine()->getManager()
->getRepository(Person::class)
->find($personId);
if ($person === null) {
throw $this->createNotFoundException("Invalid person id");
}
$event = new PrivacyEvent($person, array( $event = new PrivacyEvent($person, array(
'element_class' => SingleTask::class, 'element_class' => SingleTask::class,
'element_id' => $task->getId(), 'element_id' => $task->getId(),
'action' => 'show' 'action' => 'show'
)); ));
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
} }
if ($task->getCourse() !== null)
{
$courseId = $task->getCourse()->getId();
if ($courseId === null) {
return new Response("You must provide a course_id", Response::HTTP_BAD_REQUEST);
}
$course = $this->getDoctrine()->getManager()
->getRepository(AccompanyingPeriod::class)
->find($courseId);
if ($course === null)
{
throw $this->createNotFoundException("Invalid course id");
}
}
$this->denyAccessUnlessGranted(TaskVoter::SHOW, $task, 'You are not '
. 'allowed to view this task');
$timeline = $this->timelineBuilder $timeline = $this->timelineBuilder
->getTimelineHTML('task', array('task' => $task)); ->getTimelineHTML('task', array('task' => $task));
if ($task->getContext() instanceof Person) {
if($task->getContext() instanceof Person){
return $this->render('@ChillTask/SingleTask/Person/show.html.twig', array( return $this->render('@ChillTask/SingleTask/Person/show.html.twig', array(
'task' => $task, 'task' => $task,
'timeline' => $timeline 'timeline' => $timeline
@ -281,52 +240,16 @@ final class SingleTaskController extends AbstractController
* ) * )
*/ */
public function editAction( public function editAction(
$id, SingleTask $task,
Request $request Request $request
) { ) {
$em = $this->getDoctrine()->getManager();
$task = $em->getRepository(SingleTask::class)->find($id);
if ($task->getContext() instanceof Person) {
$personId = $task->getPerson()->getId();
if ($personId === null) {
return new Response("You must provide a person_id", Response::HTTP_BAD_REQUEST);
}
$person = $this->getDoctrine()->getManager()
->getRepository(Person::class)
->find($personId);
if ($person === null) {
throw $this->createNotFoundException("Invalid person id");
}
} else {
$courseId = $task->getCourse()->getId();
if ($courseId === null) {
return new Response("You must provide a course_id", Response::HTTP_BAD_REQUEST);
}
$course = $this->getDoctrine()->getManager()
->getRepository(AccompanyingPeriod::class)
->find($courseId);
if ($course === null) {
throw $this->createNotFoundException("Invalid accompanying period id");
}
}
$this->denyAccessUnlessGranted(TaskVoter::UPDATE, $task, 'You are not ' $this->denyAccessUnlessGranted(TaskVoter::UPDATE, $task, 'You are not '
. 'allowed to edit this task'); . 'allowed to edit this task');
if (!$task) {
throw $this->createNotFoundException('Unable to find Task entity.');
}
$event = (new UIEvent('single-task', $task)) $event = (new UIEvent('single-task', $task))
->setForm($this->setCreateForm($task, new Role(TaskVoter::UPDATE))) ->setForm($this->setCreateForm($task, new Role(TaskVoter::UPDATE)))
; ;
$this->eventDispatcher->dispatch(UIEvent::EDIT_FORM, $event); $this->eventDispatcher->dispatch(UIEvent::EDIT_FORM, $event);
$form = $event->getForm(); $form = $event->getForm();
@ -343,7 +266,7 @@ final class SingleTaskController extends AbstractController
$this->addFlash('success', $this->translator $this->addFlash('success', $this->translator
->trans("The task has been updated")); ->trans("The task has been updated"));
if($task->getContext() instanceof Person){ if ($person = $task->getContext() instanceof Person) {
$event = new PrivacyEvent($person, array( $event = new PrivacyEvent($person, array(
'element_class' => SingleTask::class, 'element_class' => SingleTask::class,
'element_id' => $task->getId(), 'element_id' => $task->getId(),
@ -351,14 +274,20 @@ final class SingleTaskController extends AbstractController
)); ));
$this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event);
if ($request->query->has('returnPath')) {
return $this->redirect($request->query->get('returnPath'));
}
return $this->redirectToRoute( return $this->redirectToRoute(
'chill_task_singletask_list', 'chill_task_singletask_list',
$request->query->get('list_params', [])
); );
} else { } else {
if ($request->query->has('returnPath')) {
return $this->redirect($request->query->get('returnPath'));
}
return $this->redirectToRoute( return $this->redirectToRoute(
'chill_task_singletask_courselist', 'chill_task_singletask_by-course_list', ['id' => $task->getCourse()->getId()]
$request->query->get('list_params', [])
); );
} }
} else { } else {
@ -372,7 +301,7 @@ final class SingleTaskController extends AbstractController
return $event->getResponse(); return $event->getResponse();
} }
if($task->getContext() instanceof Person){ if ($person = $task->getContext() instanceof Person) {
$event = new PrivacyEvent($person, array( $event = new PrivacyEvent($person, array(
'element_class' => SingleTask::class, 'element_class' => SingleTask::class,
'element_id' => $task->getId(), 'element_id' => $task->getId(),
@ -388,7 +317,7 @@ final class SingleTaskController extends AbstractController
return $this->render('@ChillTask/SingleTask/AccompanyingCourse/edit.html.twig', array( return $this->render('@ChillTask/SingleTask/AccompanyingCourse/edit.html.twig', array(
'task' => $task, 'task' => $task,
'form' => $form->createView(), 'form' => $form->createView(),
'accompanyingCourse' => $course 'accompanyingCourse' => $task->getCourse()
)); ));
} }
@ -478,10 +407,9 @@ final class SingleTaskController extends AbstractController
]))); ])));
} else { } else {
return $this->redirect($this->generateUrl( return $this->redirect($this->generateUrl(
'chill_task_singletask_courselist', 'chill_task_singletask_by-course_list',
$request->query->get('list_params', [ ['id' => $course->getId()]
'course_id' => $course->getId() ));
])));
} }
} }
} }
@ -510,7 +438,6 @@ final class SingleTaskController extends AbstractController
protected function setCreateForm(SingleTask $task, Role $role) protected function setCreateForm(SingleTask $task, Role $role)
{ {
$form = $this->createForm(SingleTaskType::class, $task, [ $form = $this->createForm(SingleTaskType::class, $task, [
'center' => $this->centerResolverDispatcher->resolveCenter($task),
'role' => $role, 'role' => $role,
]); ]);
@ -817,39 +744,24 @@ final class SingleTaskController extends AbstractController
/** /**
* @Route( * @Route(
* "/{_locale}/task/single-task/courselist", * "/{_locale}/task/single-task/by-course/{id}",
* name="chill_task_singletask_courselist") * name="chill_task_singletask_by-course_list")
*/ */
public function listCourseTasks( public function listCourseTasks(
AccompanyingPeriodRepository $courseRepository, AccompanyingPeriod $course,
SingleTaskRepository $taskRepository, SingleTaskRepository $taskRepository,
FormFactoryInterface $formFactory, FormFactoryInterface $formFactory,
Request $request Request $request
): Response ): Response
{ {
if (!empty($request->query->get('course_id', NULL))) {
$courseId = $request->query->getInt('course_id', 0);
$course = $courseRepository->find($courseId);
if ($course === null) {
throw $this->createNotFoundException("This accompanying course ' $courseId ' does not exist.");
}
}
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
if($course === NULL) {
throw $this->createNotFoundException('Accompanying course not found');
}
$tasks = $taskRepository $tasks = $taskRepository
->findBy( ->findBy(
array('course' => $course) array('course' => $course)
); );
$form = $formFactory->createNamed(null, SingleTaskListType::class, null, [ $form = $formFactory->createNamed(null, SingleTaskListType::class, null, [
'accompanyingCourse' => $course, 'accompanyingCourse' => $course,

View File

@ -253,11 +253,6 @@ abstract class AbstractTask implements HasScopeInterface, HasCenterInterface
public function getContext() public function getContext()
{ {
// if ($this->getCourse() instanceof AccompanyingPeriod){
// return $this->getCourse();
// } else {
// return $this->getPerson();
// }
return $this->getPerson() ?? $this->getCourse(); return $this->getPerson() ?? $this->getCourse();
} }

View File

@ -19,6 +19,7 @@ namespace Chill\TaskBundle\Form;
use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher; use Chill\MainBundle\Security\Resolver\CenterResolverDispatcher;
use Chill\MainBundle\Security\Resolver\ScopeResolverDispatcher; use Chill\MainBundle\Security\Resolver\ScopeResolverDispatcher;
use Chill\TaskBundle\Security\Authorization\TaskVoter;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Form\FormBuilderInterface;
@ -60,7 +61,7 @@ class SingleTaskType extends AbstractType
->add('assignee', UserPickerType::class, [ ->add('assignee', UserPickerType::class, [
'required' => false, 'required' => false,
'center' => $center, 'center' => $center,
'role' => $options['role'], 'role' => TaskVoter::SHOW,
'placeholder' => 'Not assigned' 'placeholder' => 'Not assigned'
]) ])
->add('startDate', ChillDateType::class, [ ->add('startDate', ChillDateType::class, [
@ -87,8 +88,6 @@ class SingleTaskType extends AbstractType
public function configureOptions(OptionsResolver $resolver) public function configureOptions(OptionsResolver $resolver)
{ {
$resolver $resolver
->setRequired('center')
->setAllowedTypes('center', [ Center::class, 'array', 'null' ])
->setRequired('role') ->setRequired('role')
->setAllowedTypes('role', [ Role::class, 'string' ]) ->setAllowedTypes('role', [ Role::class, 'string' ])
; ;

View File

@ -24,7 +24,7 @@ use Chill\TaskBundle\Security\Authorization\TaskVoter;
use Symfony\Component\Translation\TranslatorInterface; use Symfony\Component\Translation\TranslatorInterface;
/** /**
* *
* *
* @author Julien Fastré <julien.fastre@champs-libres.coop> * @author Julien Fastré <julien.fastre@champs-libres.coop>
*/ */
@ -35,13 +35,13 @@ class MenuBuilder implements LocalMenuBuilderInterface
* @var TranslatorInterface * @var TranslatorInterface
*/ */
protected $translator; protected $translator;
/** /**
* *
* @var AuthorizationCheckerInterface * @var AuthorizationCheckerInterface
*/ */
protected $authorizationChecker; protected $authorizationChecker;
public function __construct( public function __construct(
AuthorizationCheckerInterface $authorizationChecker, AuthorizationCheckerInterface $authorizationChecker,
TranslatorInterface $translator) TranslatorInterface $translator)
@ -50,7 +50,7 @@ class MenuBuilder implements LocalMenuBuilderInterface
$this->authorizationChecker = $authorizationChecker; $this->authorizationChecker = $authorizationChecker;
} }
public function buildMenu($menuId, MenuItem $menu, array $parameters) public function buildMenu($menuId, MenuItem $menu, array $parameters)
{ {
switch($menuId) { switch($menuId) {
@ -69,15 +69,15 @@ class MenuBuilder implements LocalMenuBuilderInterface
} }
public function buildPersonMenu($menu, $parameters){ public function buildPersonMenu($menu, $parameters){
//var $person \Chill\PersonBundle\Entity\Person */ //var $person \Chill\PersonBundle\Entity\Person */
$person = $parameters['person'] ?? null; $person = $parameters['person'] ?? null;
if ($this->authorizationChecker->isGranted(TaskVoter::SHOW, $person)) { if ($this->authorizationChecker->isGranted(TaskVoter::SHOW, $person)) {
$menu->addChild( $menu->addChild(
$this->translator->trans('Tasks'), [ $this->translator->trans('Tasks'), [
'route' => 'chill_task_singletask_list', 'route' => 'chill_task_singletask_list',
'routeParameters' => 'routeParameters' =>
[ 'person_id' => $person->getId() ] [ 'person_id' => $person->getId() ]
]) ])
->setExtra('order', 400); ->setExtra('order', 400);
@ -85,18 +85,18 @@ class MenuBuilder implements LocalMenuBuilderInterface
} }
public function buildAccompanyingCourseMenu($menu, $parameters){ public function buildAccompanyingCourseMenu($menu, $parameters){
$course = $parameters['accompanyingCourse']; $course = $parameters['accompanyingCourse'];
// if ($this->authorizationChecker->isGranted(TaskVoter::SHOW, $course)) { if ($this->authorizationChecker->isGranted(TaskVoter::SHOW, $course)) {
$menu->addChild( $menu->addChild(
$this->translator->trans('Tasks'), [ $this->translator->trans('Tasks'), [
'route' => 'chill_task_singletask_courselist', 'route' => 'chill_task_singletask_by-course_list',
'routeParameters' => 'routeParameters' =>
[ 'course_id' => $course->getId() ] [ 'id' => $course->getId() ]
]) ])
->setExtra('order', 400); ->setExtra('order', 400);
// } }
} }

View File

@ -3,7 +3,7 @@
{% set activeRouteKey = 'chill_task_task_list' %} {% set activeRouteKey = 'chill_task_task_list' %}
{% set course = task.course %} {% set course = task.course %}
{% block title 'Remove task'|trans %} {% block title 'Remove task'|trans %}
{% block content %} {% block content %}
@ -11,8 +11,8 @@
{ {
'title' : 'Remove task'|trans, 'title' : 'Remove task'|trans,
'confirm_question' : 'Are you sure you want to remove the task "%title%" ?'|trans({ '%title%' : task.title } ), 'confirm_question' : 'Are you sure you want to remove the task "%title%" ?'|trans({ '%title%' : task.title } ),
'cancel_route' : 'chill_task_singletask_courselist', 'cancel_route' : 'chill_task_singletask_by-course_list',
'cancel_parameters' : app.request.query.get('list_params', { } ), 'cancel_parameters' : {'id' : task.course.id },
'form' : delete_form, 'form' : delete_form,
} ) }} } ) }}

View File

@ -120,20 +120,20 @@
{% endif %} {% endif %}
<li> <li>
<a href="{{ path('chill_task_single_task_show', { 'id': task.id, 'list_params': app.request.query.all }) }}" class="btn btn-show "></a> <a href="{{ chill_path_add_return_path('chill_task_single_task_show', { 'id': task.id }) }}" class="btn btn-show "></a>
</li> </li>
{# {% if is_granted('CHILL_TASK_TASK_UPDATE', task) %} #} {% if is_granted('CHILL_TASK_TASK_UPDATE', task) %}
<li> <li>
<a href="{{ path('chill_task_single_task_edit', { 'id': task.id, 'list_params': app.request.query.all }) }}" class="btn btn-update "></a> <a href="{{ chill_path_add_return_path('chill_task_single_task_edit', { 'id': task.id }) }}" class="btn btn-update "></a>
</li> </li>
{# {% endif %} #} {% endif %}
{# {% if is_granted('CHILL_TASK_TASK_DELETE', task) %} #} {% if is_granted('CHILL_TASK_TASK_DELETE', task) %}
<li> <li>
<a href="{{ path('chill_task_single_task_delete', { 'id': task.id, 'list_params': app.request.query.all } ) }}" class="btn btn-delete "></a> <a href="{{ chill_path_add_return_path('chill_task_single_task_delete', { 'id': task.id } ) }}" class="btn btn-delete "></a>
</li> </li>
{# {% endif %} #} {% endif %}
</ul> </ul>
</div> </div>

View File

@ -18,7 +18,7 @@
<ul class="record_actions sticky-form-buttons"> <ul class="record_actions sticky-form-buttons">
<li class="cancel"> <li class="cancel">
<a class="btn btn-cancel" href={% if task.person is not null %} "{{ path('chill_task_singletask_list', { 'person_id': task.person.id, 'list_params': app.request.query.get('list_params', {} )} ) }}" {% else %} "{{ chill_return_path_or('chill_task_singletask_courselist', {'course_id': task.course.id}) }}" {% endif %}> <a class="btn btn-cancel" href={% if task.person is not null %} "{{ path('chill_task_singletask_list', { 'person_id': task.person.id, 'list_params': app.request.query.get('list_params', {} )} ) }}" {% else %} "{{ chill_return_path_or('chill_task_singletask_by-course_list', {'id': task.course.id}) }}" {% endif %}>
{{'Cancel'|trans}} {{'Cancel'|trans}}
</a> </a>
</li> </li>

View File

@ -70,8 +70,8 @@
<ul class="record_actions sticky-form-buttons"> <ul class="record_actions sticky-form-buttons">
<li class="cancel"> <li class="cancel">
<a class="btn btn-cancel" href={% if task.person is not null %} "{{ path('chill_task_singletask_list', { 'person_id': task.person.id, 'list_params': app.request.query.get('list_params', {} )} ) }}" {% else %} "{{ chill_return_path_or('chill_task_singletask_courselist', {'course_id': task.course.id}) }}" {% endif %}> <a class="btn btn-cancel" href={% if task.person is not null %} "{{ chill_return_path_or('chill_task_singletask_list', { 'person_id': task.person.id } ) }}" {% else %} "{{ chill_return_path_or('chill_task_singletask_by-course_list', {'id': task.course.id}) }}" {% endif %}>
{{'Back to the list'|trans}} {{'Cancel'|trans}}
</a> </a>
</li> </li>
@ -94,19 +94,20 @@
</li> </li>
{% endif %} {% endif %}
{% if is_granted('CHILL_TASK_TASK_DELETE', task) %}
<li>
<a href="{{ chill_path_forward_return_path('chill_task_single_task_delete', { 'id': task.id } ) }}" class="btn btn-delete">
{{ 'Delete'|trans }}
</a>
</li>
{% endif %}
{% if is_granted('CHILL_TASK_TASK_UPDATE', task) %} {% if is_granted('CHILL_TASK_TASK_UPDATE', task) %}
<li> <li>
<a class="btn btn-update" href="{{ path('chill_task_single_task_edit', { 'id': task.id, 'list_params': app.request.query.all['list_params'] }) }}"> <a class="btn btn-update" href="{{ chill_path_forward_return_path('chill_task_single_task_edit', { 'id': task.id }) }}">
{{ 'Edit the task'|trans }} {{ 'Edit the task'|trans }}
</a> </a>
</li> </li>
{% endif %} {% endif %}
{% if is_granted('CHILL_TASK_TASK_CREATE', task) %}
<li>
<a href="{{ path('chill_task_single_task_delete', { 'id': task.id, 'list_params': app.request.query.all['list_params'] } ) }}" class="btn btn-delete">
{{ 'Delete'|trans }}
</a>
</li>
{% endif %}
</ul></div> </ul></div>