timesheet lockdown with grace period (#1644)
This commit is contained in:
@@ -9,84 +9,52 @@
|
||||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use App\Configuration\TimesheetConfiguration;
|
||||
use App\Entity\Timesheet as TimesheetEntity;
|
||||
use App\Repository\TimesheetRepository;
|
||||
use App\Timesheet\TrackingModeService;
|
||||
use App\Validator\Constraints\Timesheet as TimesheetConstraint;
|
||||
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
class TimesheetValidator extends ConstraintValidator
|
||||
final class TimesheetValidator extends ConstraintValidator
|
||||
{
|
||||
/**
|
||||
* @var AuthorizationCheckerInterface
|
||||
* @var TimesheetConstraint[]
|
||||
*/
|
||||
protected $auth;
|
||||
/**
|
||||
* @var TimesheetConfiguration
|
||||
*/
|
||||
protected $configuration;
|
||||
/**
|
||||
* @var TrackingModeService
|
||||
*/
|
||||
protected $trackingModeService;
|
||||
/**
|
||||
* @var TimesheetRepository
|
||||
*/
|
||||
private $repository;
|
||||
private $constraints;
|
||||
|
||||
public function __construct(AuthorizationCheckerInterface $auth, TimesheetConfiguration $configuration, TrackingModeService $service, TimesheetRepository $repository)
|
||||
/**
|
||||
* @param TimesheetConstraint[] $constraints
|
||||
*/
|
||||
public function __construct(iterable $constraints)
|
||||
{
|
||||
$this->auth = $auth;
|
||||
$this->configuration = $configuration;
|
||||
$this->trackingModeService = $service;
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TimesheetEntity|mixed $value
|
||||
* @param Constraint $constraint
|
||||
*/
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
if (!($constraint instanceof TimesheetConstraint)) {
|
||||
throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\Timesheet');
|
||||
}
|
||||
|
||||
if (!\is_object($value) || !($value instanceof TimesheetEntity)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->validateBeginAndEnd($value, $this->context);
|
||||
$this->validateActivityAndProject($value, $this->context);
|
||||
$this->validatePermissions($value, $this->context);
|
||||
$this->validateActiveLimit($value, $this->context);
|
||||
$this->validateOverlapping($value, $this->context);
|
||||
$this->constraints = $constraints;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TimesheetEntity $timesheet
|
||||
* @param ExecutionContextInterface $context
|
||||
* @param Constraint $constraint
|
||||
*/
|
||||
protected function validateOverlapping(TimesheetEntity $timesheet, ExecutionContextInterface $context)
|
||||
public function validate($timesheet, Constraint $constraint)
|
||||
{
|
||||
if ($this->configuration->isAllowOverlappingRecords()) {
|
||||
return;
|
||||
if (!($constraint instanceof TimesheetConstraint)) {
|
||||
throw new UnexpectedTypeException($constraint, Timesheet::class);
|
||||
}
|
||||
|
||||
if (!$this->repository->hasRecordForTime($timesheet)) {
|
||||
return;
|
||||
if (!\is_object($timesheet) || !($timesheet instanceof TimesheetEntity)) {
|
||||
throw new UnexpectedTypeException($timesheet, TimesheetEntity::class);
|
||||
}
|
||||
|
||||
$context->buildViolation('You already have an entry for this time.')
|
||||
->atPath('begin')
|
||||
->setTranslationDomain('validators')
|
||||
->setCode(TimesheetConstraint::RECORD_OVERLAPPING)
|
||||
->addViolation();
|
||||
$this->validateBeginAndEnd($timesheet, $this->context);
|
||||
$this->validateActivityAndProject($timesheet, $this->context);
|
||||
$this->validateActiveLimit($timesheet, $this->context);
|
||||
|
||||
foreach ($this->constraints as $constraint) {
|
||||
$this->context
|
||||
->getValidator()
|
||||
->inContext($this->context)
|
||||
->validate($timesheet, $constraint, [Constraint::DEFAULT_GROUP]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,35 +66,6 @@ class TimesheetValidator extends ConstraintValidator
|
||||
// TODO check active entries against hard_limit
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TimesheetEntity $timesheet
|
||||
* @param ExecutionContextInterface $context
|
||||
*/
|
||||
protected function validatePermissions(TimesheetEntity $timesheet, ExecutionContextInterface $context)
|
||||
{
|
||||
// special case that would otherwise need to be validated in several controllers:
|
||||
// an entry is edited and the end date is removed (or duration deleted) would restart the record,
|
||||
// which might be disallowed for the current user
|
||||
if ($context->getViolations()->count() == 0 && null === $timesheet->getEnd()) {
|
||||
$mode = $this->trackingModeService->getActiveMode();
|
||||
$path = 'start';
|
||||
if ($mode->canEditEnd()) {
|
||||
$path = 'end';
|
||||
} elseif ($mode->canEditDuration()) {
|
||||
$path = 'duration';
|
||||
}
|
||||
if (!$this->auth->isGranted('start', $timesheet)) {
|
||||
$context->buildViolation('You are not allowed to start this timesheet record.')
|
||||
->atPath($path)
|
||||
->setTranslationDomain('validators')
|
||||
->setCode(TimesheetConstraint::START_DISALLOWED)
|
||||
->addViolation();
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TimesheetEntity $timesheet
|
||||
* @param ExecutionContextInterface $context
|
||||
@@ -150,18 +89,6 @@ class TimesheetValidator extends ConstraintValidator
|
||||
->setCode(TimesheetConstraint::END_BEFORE_BEGIN_ERROR)
|
||||
->addViolation();
|
||||
}
|
||||
|
||||
if (false === $this->configuration->isAllowFutureTimes()) {
|
||||
// allow configured default rounding time + 1 minute - see #1295
|
||||
$allowedDiff = ($this->configuration->getDefaultRoundingBegin() * 60) + 60;
|
||||
if ((time() + $allowedDiff) < $timesheet->getBegin()->getTimestamp()) {
|
||||
$context->buildViolation('The begin date cannot be in the future.')
|
||||
->atPath('begin')
|
||||
->setTranslationDomain('validators')
|
||||
->setCode(TimesheetConstraint::BEGIN_IN_FUTURE_ERROR)
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user