use dialog to duplicate timesheet (#2567)

* fix: display time budget in customer listing if set
* updated api methods with annotations
* do not require checkboxes for system configurations
* open dialog for duplicated timesheets
This commit is contained in:
Kevin Papst
2021-05-12 22:32:08 +02:00
committed by GitHub
parent f81f9ba492
commit f2b32b211b
16 changed files with 271 additions and 230 deletions

View File

@@ -11,6 +11,7 @@ declare(strict_types=1);
namespace App\API;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Event\RecentActivityEvent;
use App\Event\TimesheetDuplicatePostEvent;
@@ -36,7 +37,6 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Swagger\Annotations as SWG;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\Validator\Constraints;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
@@ -280,20 +280,13 @@ class TimesheetController extends BaseApiController
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*
* @Security("is_granted('view', id)")
*/
public function getAction(int $id): Response
public function getAction(Timesheet $id): Response
{
$data = $this->repository->find($id);
if (null === $data) {
throw new NotFoundException();
}
if (!$this->isGranted('view', $data)) {
throw new AccessDeniedHttpException('You are not allowed to view this timesheet');
}
$view = new View($data, 200);
$timesheet = $id; // cannot be changed due to BC reasons, routes use 'id'
$view = new View($timesheet, 200);
$view->getContext()->setGroups(self::GROUPS_ENTITY);
return $this->viewHandler->handle($view);
@@ -384,34 +377,27 @@ class TimesheetController extends BaseApiController
* )
* )
* @SWG\Parameter(
* name="body",
* in="body",
* required=true,
* @SWG\Schema(ref="#/definitions/TimesheetEditForm")
* )
* @SWG\Parameter(
* name="id",
* in="path",
* type="integer",
* description="Timesheet record ID to update",
* required=true,
* )
* @SWG\Parameter(
* name="body",
* in="body",
* required=true,
* @SWG\Schema(ref="#/definitions/TimesheetEditForm")
* )
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*
* @Security("is_granted('edit', id)")
*/
public function patchAction(Request $request, int $id): Response
public function patchAction(Request $request, Timesheet $id): Response
{
$timesheet = $this->repository->find($id);
if (null === $timesheet) {
throw new NotFoundException();
}
if (!$this->isGranted('edit', $timesheet)) {
throw new AccessDeniedHttpException('You are not allowed to update this timesheet');
}
$timesheet = $id;
$event = new TimesheetMetaDefinitionEvent($timesheet);
$this->dispatcher->dispatch($event);
@@ -463,20 +449,12 @@ class TimesheetController extends BaseApiController
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*
* @Security("is_granted('delete', id)")
*/
public function deleteAction(int $id): Response
public function deleteAction(Timesheet $id): Response
{
$timesheet = $this->repository->find($id);
if (null === $timesheet) {
throw new NotFoundException();
}
if (!$this->isGranted('delete', $timesheet)) {
throw $this->createAccessDeniedException('You are not allowed to delete this timesheet');
}
$this->service->deleteTimesheet($timesheet);
$this->service->deleteTimesheet($id);
$view = new View(null, Response::HTTP_NO_CONTENT);
@@ -585,22 +563,14 @@ class TimesheetController extends BaseApiController
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*
* @Security("is_granted('stop', id)")
*/
public function stopAction(int $id): Response
public function stopAction(Timesheet $id): Response
{
$timesheet = $this->repository->find($id);
$this->service->stopTimesheet($id);
if (null === $timesheet) {
throw new NotFoundException();
}
if (!$this->isGranted('stop', $timesheet)) {
throw new AccessDeniedHttpException('You are not allowed to stop this timesheet');
}
$this->service->stopTimesheet($timesheet);
$view = new View($timesheet, 200);
$view = new View($id, 200);
$view->getContext()->setGroups(self::GROUPS_ENTITY);
return $this->viewHandler->handle($view);
@@ -627,19 +597,12 @@ class TimesheetController extends BaseApiController
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*
* @Security("is_granted('start', id)")
*/
public function restartAction(int $id, ParamFetcherInterface $paramFetcher): Response
public function restartAction(Timesheet $id, ParamFetcherInterface $paramFetcher): Response
{
$timesheet = $this->repository->find($id);
if (null === $timesheet) {
throw new NotFoundException();
}
if (!$this->isGranted('start', $timesheet)) {
throw new AccessDeniedHttpException('You are not allowed to re-start this timesheet');
}
$timesheet = $id;
/** @var User $user */
$user = $this->getUser();
@@ -714,25 +677,16 @@ class TimesheetController extends BaseApiController
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*
* @Security("is_granted('duplicate', id)")
*/
public function duplicateAction(int $id): Response
public function duplicateAction(Timesheet $id): Response
{
$timesheet = $this->repository->find($id);
if (null === $timesheet) {
throw new NotFoundException();
}
if (!$this->isGranted('duplicate', $timesheet)) {
throw new AccessDeniedHttpException('You are not allowed to duplicate this timesheet');
}
$timesheet = $id;
$copyTimesheet = clone $timesheet;
$this->dispatcher->dispatch(new TimesheetDuplicatePreEvent($copyTimesheet, $timesheet));
$this->service->saveNewTimesheet($copyTimesheet);
$this->dispatcher->dispatch(new TimesheetDuplicatePostEvent($copyTimesheet, $timesheet));
$view = new View($copyTimesheet, 200);
@@ -759,21 +713,12 @@ class TimesheetController extends BaseApiController
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*
* @Security("is_granted('edit_export', id)")
*/
public function exportAction(int $id): Response
public function exportAction(Timesheet $id): Response
{
$timesheet = $this->repository->find($id);
if (null === $timesheet) {
throw new NotFoundException();
}
if (!$this->isGranted('edit_export', $timesheet)) {
throw new AccessDeniedHttpException(
sprintf('You are not allowed to %s this timesheet', ($timesheet->isExported() ? 'unlock' : 'lock'))
);
}
$timesheet = $id;
$timesheet->setExported(!$timesheet->isExported());
$this->service->updateTimesheet($timesheet);
@@ -804,19 +749,12 @@ class TimesheetController extends BaseApiController
*
* @ApiSecurity(name="apiUser")
* @ApiSecurity(name="apiToken")
*
* @Security("is_granted('edit', id)")
*/
public function metaAction(int $id, ParamFetcherInterface $paramFetcher): Response
public function metaAction(Timesheet $id, ParamFetcherInterface $paramFetcher): Response
{
$timesheet = $this->repository->find($id);
if (null === $timesheet) {
throw new NotFoundException();
}
if (!$this->isGranted('edit', $timesheet)) {
throw new AccessDeniedHttpException('You are not allowed to update this timesheet');
}
$timesheet = $id;
$event = new TimesheetMetaDefinitionEvent($timesheet);
$this->dispatcher->dispatch($event);

View File

@@ -13,6 +13,8 @@ use App\Configuration\SystemConfiguration;
use App\Entity\MetaTableTypeInterface;
use App\Entity\Tag;
use App\Entity\Timesheet;
use App\Event\TimesheetDuplicatePostEvent;
use App\Event\TimesheetDuplicatePreEvent;
use App\Event\TimesheetMetaDefinitionEvent;
use App\Event\TimesheetMetaDisplayEvent;
use App\Export\ServiceExport;
@@ -208,6 +210,32 @@ abstract class TimesheetAbstractController extends AbstractController
]);
}
protected function duplicate(Timesheet $timesheet, Request $request, string $renderTemplate): Response
{
$copyTimesheet = clone $timesheet;
$form = $this->getDuplicateForm($timesheet);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
try {
$this->dispatcher->dispatch(new TimesheetDuplicatePreEvent($copyTimesheet, $timesheet));
$this->service->saveNewTimesheet($copyTimesheet);
$this->dispatcher->dispatch(new TimesheetDuplicatePostEvent($copyTimesheet, $timesheet));
$this->flashSuccess('action.update.success');
return $this->redirectToRoute($this->getTimesheetRoute());
} catch (\Exception $ex) {
$this->flashUpdateException($ex);
}
}
return $this->render($renderTemplate, [
'timesheet' => $copyTimesheet,
'form' => $form->createView(),
]);
}
protected function export(Request $request, string $exporterId): Response
{
$query = new TimesheetQuery();
@@ -425,12 +453,12 @@ abstract class TimesheetAbstractController extends AbstractController
]);
}
protected function getCreateForm(Timesheet $entry): FormInterface
protected function generateCreateForm(Timesheet $entry, string $formClass, string $action): FormInterface
{
$mode = $this->getTrackingMode();
return $this->createForm($this->getCreateFormClassName(), $entry, [
'action' => $this->generateUrl($this->getCreateRoute()),
return $this->createForm($formClass, $entry, [
'action' => $action,
'include_rate' => $this->isGranted('edit_rate', $entry),
'include_exported' => $this->isGranted('edit_export', $entry),
'include_user' => $this->includeUserInForms('create'),
@@ -495,11 +523,6 @@ abstract class TimesheetAbstractController extends AbstractController
return 'edit_rate_own_timesheet';
}
protected function getCreateFormClassName(): string
{
return TimesheetEditForm::class;
}
protected function getEditFormClassName(): string
{
return TimesheetEditForm::class;
@@ -525,11 +548,6 @@ abstract class TimesheetAbstractController extends AbstractController
return 'timesheet_edit';
}
protected function getCreateRoute(): string
{
return 'timesheet_create';
}
protected function getMultiUpdateRoute(): string
{
return 'timesheet_multi_update';
@@ -544,4 +562,8 @@ abstract class TimesheetAbstractController extends AbstractController
{
return $this->getTrackingMode()->canSeeBeginAndEndTimes();
}
abstract protected function getDuplicateForm(Timesheet $entry): FormInterface;
abstract protected function getCreateForm(Timesheet $entry): FormInterface;
}

View File

@@ -11,11 +11,13 @@ namespace App\Controller;
use App\Entity\Timesheet;
use App\Event\TimesheetMetaDisplayEvent;
use App\Form\TimesheetEditForm;
use App\Repository\ActivityRepository;
use App\Repository\ProjectRepository;
use App\Repository\Query\TimesheetQuery;
use App\Repository\TagRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
@@ -30,12 +32,8 @@ class TimesheetController extends TimesheetAbstractController
* @Route(path="/", defaults={"page": 1}, name="timesheet", methods={"GET"})
* @Route(path="/page/{page}", requirements={"page": "[1-9]\d*"}, name="timesheet_paginated", methods={"GET"})
* @Security("is_granted('view_own_timesheet')")
*
* @param int $page
* @param Request $request
* @return Response
*/
public function indexAction($page, Request $request)
public function indexAction(int $page, Request $request): Response
{
$query = new TimesheetQuery();
$query->setPage($page);
@@ -47,12 +45,8 @@ class TimesheetController extends TimesheetAbstractController
/**
* @Route(path="/export/{exporter}", name="timesheet_export", methods={"GET"})
* @Security("is_granted('export_own_timesheet')")
*
* @param Request $request
* @param string $exporter
* @return Response
*/
public function exportAction(Request $request, string $exporter)
public function exportAction(Request $request, string $exporter): Response
{
return $this->export($request, $exporter);
}
@@ -60,21 +54,26 @@ class TimesheetController extends TimesheetAbstractController
/**
* @Route(path="/{id}/edit", name="timesheet_edit", methods={"GET", "POST"})
* @Security("is_granted('edit', entry)")
*
* @param Timesheet $entry
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function editAction(Timesheet $entry, Request $request)
public function editAction(Timesheet $entry, Request $request): Response
{
return $this->edit($entry, $request, 'timesheet/edit.html.twig');
}
/**
* @Route(path="/{id}/duplicate", name="timesheet_duplicate", methods={"GET", "POST"})
* @Security("is_granted('duplicate', entry)")
*/
public function duplicateAction(Timesheet $entry, Request $request): Response
{
return $this->duplicate($entry, $request, 'timesheet/edit.html.twig');
}
/**
* @Route(path="/multi-update", name="timesheet_multi_update", methods={"POST"})
* @Security("is_granted('edit_own_timesheet')")
*/
public function multiUpdateAction(Request $request)
public function multiUpdateAction(Request $request): Response
{
return $this->multiUpdate($request, 'timesheet/multi-update.html.twig');
}
@@ -83,7 +82,7 @@ class TimesheetController extends TimesheetAbstractController
* @Route(path="/multi-delete", name="timesheet_multi_delete", methods={"POST"})
* @Security("is_granted('delete_own_timesheet')")
*/
public function multiDeleteAction(Request $request)
public function multiDeleteAction(Request $request): Response
{
return $this->multiDelete($request);
}
@@ -91,14 +90,19 @@ class TimesheetController extends TimesheetAbstractController
/**
* @Route(path="/create", name="timesheet_create", methods={"GET", "POST"})
* @Security("is_granted('create_own_timesheet')")
*
* @param Request $request
* @param ProjectRepository $projectRepository
* @param ActivityRepository $activityRepository
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
*/
public function createAction(Request $request, ProjectRepository $projectRepository, ActivityRepository $activityRepository, TagRepository $tagRepository)
public function createAction(Request $request, ProjectRepository $projectRepository, ActivityRepository $activityRepository, TagRepository $tagRepository): Response
{
return $this->create($request, 'timesheet/edit.html.twig', $projectRepository, $activityRepository, $tagRepository);
}
protected function getCreateForm(Timesheet $entry): FormInterface
{
return $this->generateCreateForm($entry, TimesheetEditForm::class, $this->generateUrl('timesheet_create'));
}
protected function getDuplicateForm(Timesheet $entry): FormInterface
{
return $this->generateCreateForm($entry, TimesheetEditForm::class, $this->generateUrl('timesheet_duplicate', ['id' => $entry->getId()]));
}
}

View File

@@ -23,7 +23,6 @@ use App\Repository\TagRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
@@ -43,7 +42,7 @@ class TimesheetTeamController extends TimesheetAbstractController
* @param Request $request
* @return Response
*/
public function indexAction($page, Request $request)
public function indexAction(int $page, Request $request): Response
{
$query = new TimesheetQuery();
$query->setPage($page);
@@ -54,12 +53,8 @@ class TimesheetTeamController extends TimesheetAbstractController
/**
* @Route(path="/export/{exporter}", name="admin_timesheet_export", methods={"GET"})
*
* @param Request $request
* @param string $exporter
* @return Response
*/
public function exportAction(Request $request, string $exporter)
public function exportAction(Request $request, string $exporter): Response
{
return $this->export($request, $exporter);
}
@@ -67,26 +62,26 @@ class TimesheetTeamController extends TimesheetAbstractController
/**
* @Route(path="/{id}/edit", name="admin_timesheet_edit", methods={"GET", "POST"})
* @Security("is_granted('edit', entry)")
*
* @param Timesheet $entry
* @param Request $request
* @return RedirectResponse|Response
*/
public function editAction(Timesheet $entry, Request $request)
public function editAction(Timesheet $entry, Request $request): Response
{
return $this->edit($entry, $request, 'timesheet-team/edit.html.twig');
}
/**
* @Route(path="/{id}/duplicate", name="admin_timesheet_duplicate", methods={"GET", "POST"})
* @Security("is_granted('duplicate', entry)")
*/
public function duplicateAction(Timesheet $entry, Request $request): Response
{
return $this->duplicate($entry, $request, 'timesheet-team/edit.html.twig');
}
/**
* @Route(path="/create", name="admin_timesheet_create", methods={"GET", "POST"})
* @Security("is_granted('create_other_timesheet')")
*
* @param Request $request
* @param ProjectRepository $projectRepository
* @param ActivityRepository $activityRepository
* @return RedirectResponse|Response
*/
public function createAction(Request $request, ProjectRepository $projectRepository, ActivityRepository $activityRepository, TagRepository $tagRepository)
public function createAction(Request $request, ProjectRepository $projectRepository, ActivityRepository $activityRepository, TagRepository $tagRepository): Response
{
return $this->create($request, 'timesheet-team/edit.html.twig', $projectRepository, $activityRepository, $tagRepository);
}
@@ -94,11 +89,8 @@ class TimesheetTeamController extends TimesheetAbstractController
/**
* @Route(path="/create_mu", name="admin_timesheet_create_multiuser", methods={"GET", "POST"})
* @Security("is_granted('create_other_timesheet')")
*
* @param Request $request
* @return RedirectResponse|Response
*/
public function createForMultiUserAction(Request $request)
public function createForMultiUserAction(Request $request): Response
{
$entry = new MultiUserTimesheet();
$entry->setUser($this->getUser());
@@ -176,7 +168,7 @@ class TimesheetTeamController extends TimesheetAbstractController
* @Route(path="/multi-update", name="admin_timesheet_multi_update", methods={"POST"})
* @Security("is_granted('edit_other_timesheet')")
*/
public function multiUpdateAction(Request $request)
public function multiUpdateAction(Request $request): Response
{
return $this->multiUpdate($request, 'timesheet-team/multi-update.html.twig');
}
@@ -185,7 +177,7 @@ class TimesheetTeamController extends TimesheetAbstractController
* @Route(path="/multi-delete", name="admin_timesheet_multi_delete", methods={"POST"})
* @Security("is_granted('delete_other_timesheet')")
*/
public function multiDeleteAction(Request $request)
public function multiDeleteAction(Request $request): Response
{
return $this->multiDelete($request);
}
@@ -195,6 +187,16 @@ class TimesheetTeamController extends TimesheetAbstractController
$query->setCurrentUser($this->getUser());
}
protected function getCreateForm(Timesheet $entry): FormInterface
{
return $this->generateCreateForm($entry, TimesheetAdminEditForm::class, $this->generateUrl('admin_timesheet_create'));
}
protected function getDuplicateForm(Timesheet $entry): FormInterface
{
return $this->generateCreateForm($entry, TimesheetAdminEditForm::class, $this->generateUrl('admin_timesheet_duplicate', ['id' => $entry->getId()]));
}
protected function getPermissionEditExport(): string
{
return 'edit_export_other_timesheet';
@@ -205,11 +207,6 @@ class TimesheetTeamController extends TimesheetAbstractController
return 'edit_rate_other_timesheet';
}
protected function getCreateFormClassName(): string
{
return TimesheetAdminEditForm::class;
}
protected function getEditFormClassName(): string
{
return TimesheetAdminEditForm::class;
@@ -234,11 +231,6 @@ class TimesheetTeamController extends TimesheetAbstractController
return 'admin_timesheet_edit';
}
protected function getCreateRoute(): string
{
return 'admin_timesheet_create';
}
protected function getMultiUpdateRoute(): string
{
return 'admin_timesheet_multi_update';

View File

@@ -12,9 +12,12 @@ namespace App\EventSubscriber\Actions;
use App\Entity\Timesheet;
use App\Event\PageActionsEvent;
/**
* @internal
*/
abstract class AbstractTimesheetSubscriber extends AbstractActionsSubscriber
{
protected function timesheetActions(PageActionsEvent $event, string $routeListing, string $routeEdit): void
protected function timesheetActions(PageActionsEvent $event, string $routeEdit, string $routeDuplicate): void
{
$payload = $event->getPayload();
@@ -35,7 +38,8 @@ abstract class AbstractTimesheetSubscriber extends AbstractActionsSubscriber
}
if ($this->isGranted('duplicate', $timesheet)) {
$event->addAction('copy', ['url' => $this->path('duplicate_timesheet', ['id' => $timesheet->getId()]), 'class' => 'api-link', 'attr' => ['data-payload' => '{"copy": "all"}', 'data-event' => 'kimai.timesheetStart kimai.timesheetUpdate', 'data-method' => 'PATCH', 'data-msg-error' => 'action.update.error', 'data-msg-success' => 'action.update.success']]);
$class = $event->isView('edit') ? '' : 'modal-ajax-form';
$event->addAction('copy', ['url' => $this->path($routeDuplicate, ['id' => $timesheet->getId()]), 'class' => $class]);
}
if ($event->countActions() > 0) {

View File

@@ -20,6 +20,6 @@ class TimesheetSubscriber extends AbstractTimesheetSubscriber
public function onActions(PageActionsEvent $event): void
{
$this->timesheetActions($event, 'timesheet', 'timesheet_edit');
$this->timesheetActions($event, 'timesheet_edit', 'timesheet_duplicate');
}
}

View File

@@ -20,6 +20,6 @@ class TimesheetTeamSubscriber extends AbstractTimesheetSubscriber
public function onActions(PageActionsEvent $event): void
{
$this->timesheetActions($event, 'admin_timesheet', 'admin_timesheet_edit');
$this->timesheetActions($event, 'admin_timesheet_edit', 'admin_timesheet_duplicate');
}
}

View File

@@ -45,7 +45,7 @@ class SystemConfigurationType extends AbstractType
}
$required = $preference->isRequired();
if (CheckboxType::class == $preference->getType()) {
if (CheckboxType::class == $preference->getType() || YesNoType::class == $preference->getType()) {
$required = false;
}

View File

@@ -9,7 +9,6 @@
namespace App\Voter;
use App\Configuration\SystemConfiguration;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Security\RolePermissionManager;
@@ -48,9 +47,6 @@ final class TimesheetVoter extends Voter
'duplicate'
];
private $configuration;
private $cacheAllowCopy;
private $permissionManager;
private $lockdownService;
@@ -59,11 +55,10 @@ final class TimesheetVoter extends Voter
private $editExported;
private $now;
public function __construct(RolePermissionManager $permissionManager, LockdownService $lockdownService, SystemConfiguration $configuration)
public function __construct(RolePermissionManager $permissionManager, LockdownService $lockdownService)
{
$this->permissionManager = $permissionManager;
$this->lockdownService = $lockdownService;
$this->configuration = $configuration;
}
/**
@@ -209,17 +204,6 @@ final class TimesheetVoter extends Voter
protected function canDuplicate(User $user, Timesheet $timesheet): bool
{
if ($this->cacheAllowCopy === null) {
$this->cacheAllowCopy = $this->configuration->isTimesheetAllowOverlappingRecords();
}
// This is a quickfix, because the API cannot open dialogs. if the method is copied to the timesheet controller,
// we could open the edit dialog instead of directly saving the copied entry.
// Probably add a new permission is required to differentiate between API and UI.
if (!$this->cacheAllowCopy) {
return false;
}
if (!$this->isAllowedInLockdown($user, $timesheet)) {
return false;
}