added setting to limit the maximum length of a timesheet record (#2612)

This commit is contained in:
Kevin Papst
2021-06-12 01:05:33 +02:00
committed by GitHub
parent 53e01177d9
commit 7460647d58
19 changed files with 366 additions and 28 deletions

View File

@@ -211,6 +211,16 @@ class SystemConfiguration implements SystemBundleConfiguration
}
// ========== Timesheet configurations ==========
/*
public function getTimesheetBreakWarningDuration(): int
{
return (int) $this->find('timesheet.rules.break_warning_duration');
}
*/
public function getTimesheetLongRunningDuration(): int
{
return (int) $this->find('timesheet.rules.long_running_duration');
}
public function getTimesheetDefaultBeginTime(): string
{
@@ -352,6 +362,11 @@ class SystemConfiguration implements SystemBundleConfiguration
return (bool) $this->find('theme.colors_limited');
}
public function getThemeAutocompleteCharacters(): int
{
return (int) $this->find('theme.autocomplete_chars');
}
public function getThemeColorChoices(): ?array
{
$config = $this->find('theme.color_choices');

View File

@@ -365,6 +365,22 @@ final class SystemConfigurationController extends AbstractController
->setConstraints([
new GreaterThanOrEqual(['value' => 0])
]),
/*
(new Configuration())
->setName('timesheet.rules.break_warning_duration')
->setType(IntegerType::class)
->setTranslationDomain('system-configuration')
->setConstraints([
new GreaterThanOrEqual(['value' => 0])
]),
*/
(new Configuration())
->setName('timesheet.rules.long_running_duration')
->setType(IntegerType::class)
->setTranslationDomain('system-configuration')
->setConstraints([
new GreaterThanOrEqual(['value' => 0])
]),
]),
(new SystemConfigurationModel())
->setSection(SystemConfigurationModel::SECTION_LOCKDOWN)

View File

@@ -242,6 +242,15 @@ class Configuration implements ConfigurationInterface
->scalarNode('lockdown_grace_period')
->defaultNull()
->end()
->scalarNode('lockdown_grace_period')
->defaultNull()
->end()
->integerNode('break_warning_duration')
->defaultValue(0)
->end()
->integerNode('long_running_duration')
->defaultValue(0)
->end()
->end()
->end()
->end()

View File

@@ -0,0 +1,26 @@
<?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\Validator\Constraints;
final class TimesheetLongRunning extends TimesheetConstraint
{
public const LONG_RUNNING = 'kimai-timesheet-long-running-01';
protected static $errorNames = [
self::LONG_RUNNING => 'TIMESHEET_LONG_RUNNING',
];
public $message = 'Maximum duration of {{ value }} hours exceeded.';
public function getTargets()
{
return self::CLASS_CONSTRAINT;
}
}

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\Validator\Constraints;
use App\Configuration\SystemConfiguration;
use App\Entity\Timesheet as TimesheetEntity;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
final class TimesheetLongRunningValidator extends ConstraintValidator
{
private $systemConfiguration;
public function __construct(SystemConfiguration $systemConfiguration)
{
$this->systemConfiguration = $systemConfiguration;
}
/**
* @param TimesheetEntity $timesheet
* @param Constraint $constraint
*/
public function validate($timesheet, Constraint $constraint)
{
if (!($constraint instanceof TimesheetLongRunning)) {
throw new UnexpectedTypeException($constraint, TimesheetLongRunning::class);
}
if (!\is_object($timesheet) || !($timesheet instanceof TimesheetEntity)) {
throw new UnexpectedTypeException($timesheet, TimesheetEntity::class);
}
if ($timesheet->isRunning()) {
return;
}
$maxMinutes = $this->systemConfiguration->getTimesheetLongRunningDuration();
if ($maxMinutes <= 0) {
return;
}
$duration = $timesheet->getEnd()->getTimestamp() - $timesheet->getBegin()->getTimestamp();
$minutes = (int) $duration / 60;
if ($minutes < $maxMinutes) {
return;
}
$format = new \App\Utils\Duration();
$hours = $format->format($maxMinutes * 60);
// raise a violation for all entries before the start of lockdown period
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $hours)
->setTranslationDomain('validators')
->atPath('duration')
->setCode(TimesheetLongRunning::LONG_RUNNING)
->addViolation();
}
}