improve permission handling for quick entry controller (#3081)

This commit is contained in:
Kevin Papst
2022-01-20 22:24:19 +01:00
committed by GitHub
parent 49bb9980b8
commit 97c23f75dd
3 changed files with 75 additions and 20 deletions

View File

@@ -25,7 +25,7 @@ use Symfony\Component\Routing\Annotation\Route;
* Controller used to enter times in weekly form.
*
* @Route(path="/quick_entry")
* @Security("is_granted('weekly_own_timesheet') and is_granted('edit_own_timesheet')")
* @Security("is_granted('quick-entry')")
*/
class QuickEntryController extends AbstractController
{
@@ -45,14 +45,6 @@ class QuickEntryController extends AbstractController
*/
public function quickEntry(Request $request, ?string $begin = null)
{
$mode = $this->timesheetService->getActiveTrackingMode();
if (!$mode->canEditDuration() && !$mode->canEditEnd()) {
$this->flashError('Not allowed');
return $this->redirectToRoute('homepage');
}
$factory = $this->getDateTimeFactory();
if ($begin === null) {
$begin = $factory->createDateTime();

View File

@@ -10,7 +10,6 @@
namespace App\EventSubscriber;
use App\Event\ConfigureMainMenuEvent;
use App\Timesheet\TrackingModeService;
use App\Twig\IconExtension;
use App\Utils\MenuItemModel;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
@@ -22,12 +21,10 @@ use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
final class MenuSubscriber implements EventSubscriberInterface
{
private $security;
private $trackingModeService;
public function __construct(AuthorizationCheckerInterface $security, TrackingModeService $trackingModeService)
public function __construct(AuthorizationCheckerInterface $security)
{
$this->security = $security;
$this->trackingModeService = $trackingModeService;
}
public static function getSubscribedEvents(): array
@@ -55,13 +52,10 @@ final class MenuSubscriber implements EventSubscriberInterface
$timesheets->setChildRoutes(['timesheet_export', 'timesheet_edit', 'timesheet_create', 'timesheet_multi_update']);
$menu->addItem($timesheets);
if ($auth->isGranted('weekly_own_timesheet') && $auth->isGranted('edit_own_timesheet')) {
$mode = $this->trackingModeService->getActiveMode();
if ($mode->canEditDuration() || $mode->canEditEnd()) {
$menu->addItem(
new MenuItemModel('quick_entry', 'quick_entry.title', 'quick_entry', [], $icons->icon('weekly-times'))
);
}
if ($auth->isGranted('quick-entry')) {
$menu->addItem(
new MenuItemModel('quick_entry', 'quick_entry.title', 'quick_entry', [], $icons->icon('weekly-times'))
);
}
$menu->addItem(

View File

@@ -0,0 +1,69 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Voter;
use App\Entity\User;
use App\Security\RolePermissionManager;
use App\Timesheet\TrackingModeService;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
final class QuickEntryVoter extends Voter
{
private $permissionManager;
private $trackingModeService;
public function __construct(RolePermissionManager $permissionManager, TrackingModeService $trackingModeService)
{
$this->permissionManager = $permissionManager;
$this->trackingModeService = $trackingModeService;
}
/**
* @param string $attribute
* @param mixed $subject
* @return bool
*/
protected function supports($attribute, $subject)
{
return 'quick-entry' === $attribute;
}
/**
* @param string $attribute
* @param User $subject
* @param TokenInterface $token
* @return bool
*/
protected function voteOnAttribute($attribute, $subject, TokenInterface $token)
{
$user = $token->getUser();
if (!($user instanceof User)) {
return false;
}
if (!$this->permissionManager->hasRolePermission($user, 'weekly_own_timesheet')) {
return false;
}
if (!$this->permissionManager->hasRolePermission($user, 'edit_own_timesheet')) {
return false;
}
$mode = $this->trackingModeService->getActiveMode();
if ($mode->canEditDuration() || $mode->canEditEnd()) {
return true;
}
return false;
}
}