make sure that timezone is properly validated (#2663)

This commit is contained in:
Kevin Papst
2021-07-13 23:23:18 +02:00
committed by GitHub
parent 43a31efeac
commit 7d052eba00
9 changed files with 68 additions and 62 deletions

View File

@@ -9,18 +9,9 @@
namespace App\Validator\Constraints;
use App\Validator\TimesheetBudgetUsedValidator;
final class TimesheetBudgetUsedConstraint extends TimesheetConstraint
final class TimesheetBudgetUsed extends TimesheetConstraint
{
public const BUDGET_SPENT = 'kimai-timesheet-budget-used-01';
// same messages, so we can re-use the validation translation!
public $messageRate = 'The budget is completely used.';
public $messageTime = 'The budget is completely used.';
public function validatedBy()
{
return TimesheetBudgetUsedValidator::class;
}
}

View File

@@ -7,7 +7,7 @@
* file that was distributed with this source code.
*/
namespace App\Validator;
namespace App\Validator\Constraints;
use App\Activity\ActivityStatisticService;
use App\Configuration\SystemConfiguration;
@@ -18,7 +18,6 @@ use App\Repository\TimesheetRepository;
use App\Timesheet\RateServiceInterface;
use App\Utils\Duration;
use App\Utils\LocaleHelper;
use App\Validator\Constraints\TimesheetBudgetUsedConstraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
@@ -48,8 +47,8 @@ final class TimesheetBudgetUsedValidator extends ConstraintValidator
*/
public function validate($timesheet, Constraint $constraint)
{
if (!($constraint instanceof TimesheetBudgetUsedConstraint)) {
throw new UnexpectedTypeException($constraint, TimesheetBudgetUsedConstraint::class);
if (!($constraint instanceof TimesheetBudgetUsed)) {
throw new UnexpectedTypeException($constraint, TimesheetBudgetUsed::class);
}
if (!\is_object($timesheet) || !($timesheet instanceof Timesheet)) {
@@ -127,7 +126,7 @@ final class TimesheetBudgetUsedValidator extends ConstraintValidator
}
}
private function checkActivity(TimesheetBudgetUsedConstraint $constraint, Timesheet $timesheet, int $duration, float $rate): bool
private function checkActivity(TimesheetBudgetUsed $constraint, Timesheet $timesheet, int $duration, float $rate): bool
{
$activity = $timesheet->getActivity();
@@ -156,7 +155,7 @@ final class TimesheetBudgetUsedValidator extends ConstraintValidator
return false;
}
private function checkProject(TimesheetBudgetUsedConstraint $constraint, Timesheet $timesheet, int $duration, float $rate): bool
private function checkProject(TimesheetBudgetUsed $constraint, Timesheet $timesheet, int $duration, float $rate): bool
{
$project = $timesheet->getProject();
@@ -185,7 +184,7 @@ final class TimesheetBudgetUsedValidator extends ConstraintValidator
return false;
}
private function checkCustomer(TimesheetBudgetUsedConstraint $constraint, Timesheet $timesheet, int $duration, float $rate): bool
private function checkCustomer(TimesheetBudgetUsed $constraint, Timesheet $timesheet, int $duration, float $rate): bool
{
$customer = $timesheet->getProject()->getCustomer();
@@ -214,7 +213,7 @@ final class TimesheetBudgetUsedValidator extends ConstraintValidator
return false;
}
private function addBudgetViolation(TimesheetBudgetUsedConstraint $constraint, Timesheet $timesheet, string $field, float $budget, float $rate)
private function addBudgetViolation(TimesheetBudgetUsed $constraint, Timesheet $timesheet, string $field, float $budget, float $rate)
{
// using the locale of the assigned user is not the best solution, but allows to be independent from the request stack
$helper = new LocaleHelper($timesheet->getUser()->getLanguage());
@@ -235,7 +234,7 @@ final class TimesheetBudgetUsedValidator extends ConstraintValidator
;
}
private function addTimeBudgetViolation(TimesheetBudgetUsedConstraint $constraint, string $field, int $budget, int $duration)
private function addTimeBudgetViolation(TimesheetBudgetUsed $constraint, string $field, int $budget, int $duration)
{
$durationFormat = new Duration();

View File

@@ -11,19 +11,19 @@ namespace App\Validator\Constraints;
use App\Entity\Timesheet as TimesheetEntity;
use App\Timesheet\LockdownService;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
final class TimesheetLockdownValidator extends ConstraintValidator
{
private $auth;
private $lockdownService;
private $security;
public function __construct(AuthorizationCheckerInterface $auth, LockdownService $lockdownService)
public function __construct(Security $security, LockdownService $lockdownService)
{
$this->auth = $auth;
$this->security = $security;
$this->lockdownService = $lockdownService;
}
@@ -50,7 +50,7 @@ final class TimesheetLockdownValidator extends ConstraintValidator
}
// lockdown never takes effect for users with special permission
if ($this->auth->isGranted('lockdown_override_timesheet')) {
if (null !== $this->security->getUser() && $this->security->isGranted('lockdown_override_timesheet')) {
return;
}
@@ -67,7 +67,10 @@ final class TimesheetLockdownValidator extends ConstraintValidator
}
}
$allowEditInGracePeriod = $this->auth->isGranted('lockdown_grace_timesheet');
$allowEditInGracePeriod = false;
if (null !== $this->security->getUser() && $this->security->isGranted('lockdown_grace_timesheet')) {
$allowEditInGracePeriod = true;
}
if ($this->lockdownService->isEditable($timesheet, $now, $allowEditInGracePeriod)) {
return;

View File

@@ -11,26 +11,20 @@ namespace App\Validator\Constraints;
use App\Entity\Timesheet as TimesheetEntity;
use App\Timesheet\TrackingModeService;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
final class TimesheetRestartValidator extends ConstraintValidator
{
/**
* @var TrackingModeService
*/
private $trackingModeService;
/**
* @var AuthorizationCheckerInterface
*/
private $auth;
private $security;
public function __construct(TrackingModeService $service, AuthorizationCheckerInterface $auth)
public function __construct(Security $security, TrackingModeService $service)
{
$this->security = $security;
$this->trackingModeService = $service;
$this->auth = $auth;
}
/**
@@ -58,7 +52,7 @@ final class TimesheetRestartValidator extends ConstraintValidator
return;
}
if ($this->auth->isGranted('start', $timesheet)) {
if (null !== $this->security->getUser() && $this->security->isGranted('start', $timesheet)) {
return;
}
@@ -76,7 +70,5 @@ final class TimesheetRestartValidator extends ConstraintValidator
->setTranslationDomain('validators')
->setCode(TimesheetRestart::START_DISALLOWED)
->addViolation();
return;
}
}