allow to configure timezone for the lockdown period (#2593)

This commit is contained in:
Kevin Papst
2021-06-02 00:05:44 +02:00
committed by GitHub
parent c1eb5ba90a
commit 286b63e2c8
16 changed files with 163 additions and 41 deletions

View File

@@ -208,6 +208,11 @@ class SystemConfiguration implements SystemBundleConfiguration
return (string) $this->find('timesheet.rules.lockdown_grace_period');
}
public function getTimesheetLockdownTimeZone(): ?string
{
return $this->find('timesheet.rules.lockdown_period_timezone');
}
public function isTimesheetLockdownActive(): bool
{
return !empty($this->find('timesheet.rules.lockdown_period_start')) && !empty($this->find('timesheet.rules.lockdown_period_end'));

View File

@@ -22,6 +22,7 @@ use App\Form\Type\LanguageType;
use App\Form\Type\MinuteIncrementType;
use App\Form\Type\RoundingModeType;
use App\Form\Type\SkinType;
use App\Form\Type\TimezoneType;
use App\Form\Type\TrackingModeType;
use App\Form\Type\WeekDaysType;
use App\Form\Type\YesNoType;
@@ -37,7 +38,6 @@ use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\CurrencyType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
@@ -232,18 +232,32 @@ final class SystemConfigurationController extends AbstractController
$dateFormat = 'D, d M Y H:i:s';
if ($this->configurations->isTimesheetLockdownActive()) {
$userTimezone = $this->getDateTimeFactory()->getTimezone();
$timezone = $this->configurations->getTimesheetLockdownTimeZone();
if ($timezone !== null) {
$timezone = new \DateTimeZone($timezone);
}
if ($timezone === null) {
$timezone = $userTimezone;
}
try {
if (!empty($this->configurations->getTimesheetLockdownPeriodStart())) {
$lockdownStartHelp = $this->getDateTimeFactory()->createDateTime($this->configurations->getTimesheetLockdownPeriodStart());
$lockdownStartHelp = new \DateTime($this->configurations->getTimesheetLockdownPeriodStart(), $timezone);
$lockdownStartHelp->setTimezone($userTimezone);
$lockdownStartHelp = $lockdownStartHelp->format($dateFormat);
}
if (!empty($this->configurations->getTimesheetLockdownPeriodEnd())) {
$lockdownEndHelp = $this->getDateTimeFactory()->createDateTime($this->configurations->getTimesheetLockdownPeriodEnd());
$lockdownEndHelp = new \DateTime($this->configurations->getTimesheetLockdownPeriodEnd(), $timezone);
if (!empty($this->configurations->getTimesheetLockdownGracePeriod())) {
$lockdownGraceHelp = clone $lockdownEndHelp;
$lockdownGraceHelp->modify($this->configurations->getTimesheetLockdownGracePeriod());
$lockdownGraceHelp->setTimezone($userTimezone);
$lockdownGraceHelp = $lockdownGraceHelp->format($dateFormat);
}
$lockdownEndHelp->setTimezone($userTimezone);
$lockdownEndHelp = $lockdownEndHelp->format($dateFormat);
}
} catch (\Exception $ex) {
@@ -276,27 +290,6 @@ final class SystemConfigurationController extends AbstractController
->setName('timesheet.rules.allow_overbooking_budget')
->setType(CheckboxType::class)
->setTranslationDomain('system-configuration'),
(new Configuration())
->setName('timesheet.rules.lockdown_period_start')
->setOptions(['help' => $lockdownStartHelp])
->setType(TextType::class)
->setRequired(false)
->setConstraints([new DateTimeFormat()])
->setTranslationDomain('system-configuration'),
(new Configuration())
->setName('timesheet.rules.lockdown_period_end')
->setOptions(['help' => $lockdownEndHelp])
->setType(TextType::class)
->setRequired(false)
->setConstraints([new DateTimeFormat()])
->setTranslationDomain('system-configuration'),
(new Configuration())
->setName('timesheet.rules.lockdown_grace_period')
->setOptions(['help' => $lockdownGraceHelp])
->setType(TextType::class)
->setRequired(false)
->setConstraints([new DateTimeFormat()])
->setTranslationDomain('system-configuration'),
(new Configuration())
->setName('timesheet.active_entries.hard_limit')
->setType(IntegerType::class)
@@ -327,6 +320,36 @@ final class SystemConfigurationController extends AbstractController
new GreaterThanOrEqual(['value' => 0])
]),
]),
(new SystemConfigurationModel())
->setSection(SystemConfigurationModel::SECTION_LOCKDOWN)
->setConfiguration([
(new Configuration())
->setName('timesheet.rules.lockdown_period_start')
->setOptions(['help' => $lockdownStartHelp])
->setType(TextType::class)
->setRequired(false)
->setConstraints([new DateTimeFormat()])
->setTranslationDomain('system-configuration'),
(new Configuration())
->setName('timesheet.rules.lockdown_period_end')
->setOptions(['help' => $lockdownEndHelp])
->setType(TextType::class)
->setRequired(false)
->setConstraints([new DateTimeFormat()])
->setTranslationDomain('system-configuration'),
(new Configuration())
->setName('timesheet.rules.lockdown_period_timezone')
->setType(TimezoneType::class)
->setRequired(false)
->setTranslationDomain('system-configuration'),
(new Configuration())
->setName('timesheet.rules.lockdown_grace_period')
->setOptions(['help' => $lockdownGraceHelp])
->setType(TextType::class)
->setRequired(false)
->setConstraints([new DateTimeFormat()])
->setTranslationDomain('system-configuration'),
]),
(new SystemConfigurationModel())
->setSection(SystemConfigurationModel::SECTION_ROUNDING)
->setConfiguration([

View File

@@ -235,6 +235,9 @@ class Configuration implements ConfigurationInterface
->scalarNode('lockdown_period_end')
->defaultNull()
->end()
->scalarNode('lockdown_period_timezone')
->defaultNull()
->end()
->scalarNode('lockdown_grace_period')
->defaultNull()
->end()

View File

@@ -21,12 +21,12 @@ use App\Form\Type\LanguageType;
use App\Form\Type\ReportType;
use App\Form\Type\SkinType;
use App\Form\Type\ThemeLayoutType;
use App\Form\Type\TimezoneType;
use App\Reporting\ReportingService;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Validator\Constraints\Range;

View File

@@ -11,13 +11,13 @@ namespace App\Form;
use App\Entity\Customer;
use App\Form\Type\MailType;
use App\Form\Type\TimezoneType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\CurrencyType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

View File

@@ -12,6 +12,7 @@ namespace App\Form\Model;
class SystemConfiguration
{
public const SECTION_ROUNDING = 'rounding';
public const SECTION_LOCKDOWN = 'lockdown_period';
public const SECTION_TIMESHEET = 'timesheet';
public const SECTION_FORM_INVOICE = 'invoice';
public const SECTION_FORM_CUSTOMER = 'form_customer';

View File

@@ -0,0 +1,37 @@
<?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\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TimezoneType as BaseTimezoneType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TimezoneType extends AbstractType
{
public function getBlockPrefix()
{
return 'timezone_type';
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'intl' => false,
]);
}
/**
* {@inheritdoc}
*/
public function getParent()
{
return BaseTimezoneType::class;
}
}

View File

@@ -12,11 +12,11 @@ namespace App\Form;
use App\Entity\User;
use App\Form\Type\AvatarType;
use App\Form\Type\LanguageType;
use App\Form\Type\TimezoneType;
use App\Form\Type\YesNoType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TimezoneType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

View File

@@ -55,10 +55,17 @@ final class LockdownService
$lockedStart = $this->configuration->getTimesheetLockdownPeriodStart();
$lockedEnd = $this->configuration->getTimesheetLockdownPeriodEnd();
$gracePeriod = $this->configuration->getTimesheetLockdownGracePeriod();
$timezone = $this->configuration->getTimesheetLockdownTimeZone();
if ($timezone === null) {
$timezone = $timesheetStart->getTimezone();
} else {
$timezone = new \DateTimeZone($timezone);
}
try {
$lockdownStart = new \DateTime($lockedStart, $timesheetStart->getTimezone());
$lockdownEnd = new \DateTime($lockedEnd, $timesheetStart->getTimezone());
$lockdownStart = new \DateTime($lockedStart, $timezone);
$lockdownEnd = new \DateTime($lockedEnd, $timezone);
$lockdownGrace = clone $lockdownEnd;
if (!empty($gracePeriod)) {
$lockdownGrace->modify($gracePeriod);

View File

@@ -72,6 +72,7 @@ class SystemConfigurationControllerTest extends ControllerBaseTest
{
return [
['form[name=system_configuration_form_timesheet]', $this->createUrl('/admin/system-config/update/timesheet')],
['form[name=system_configuration_form_lockdown_period]', $this->createUrl('/admin/system-config/update/lockdown_period')],
['form[name=system_configuration_form_invoice]', $this->createUrl('/admin/system-config/update/invoice')],
['form[name=system_configuration_form_rounding]', $this->createUrl('/admin/system-config/update/rounding')],
['form[name=system_configuration_form_form_customer]', $this->createUrl('/admin/system-config/update/form_customer')],
@@ -102,9 +103,6 @@ class SystemConfigurationControllerTest extends ControllerBaseTest
['name' => 'timesheet.rules.allow_future_times', 'value' => false],
['name' => 'timesheet.rules.allow_overlapping_records', 'value' => false],
['name' => 'timesheet.rules.allow_overbooking_budget', 'value' => false],
['name' => 'timesheet.rules.lockdown_period_start', 'value' => null],
['name' => 'timesheet.rules.lockdown_period_end', 'value' => null],
['name' => 'timesheet.rules.lockdown_grace_period', 'value' => null],
['name' => 'timesheet.active_entries.hard_limit', 'value' => 99],
['name' => 'timesheet.active_entries.soft_limit', 'value' => 77],
]
@@ -124,6 +122,41 @@ class SystemConfigurationControllerTest extends ControllerBaseTest
$this->assertEquals(77, $configService->find('timesheet.active_entries.soft_limit'));
}
public function testUpdateLockdownPeriodConfig()
{
$client = $this->getClientForAuthenticatedUser(User::ROLE_SUPER_ADMIN);
$this->assertAccessIsGranted($client, '/admin/system-config/');
$configService = static::$kernel->getContainer()->get(SystemConfiguration::class);
$this->assertNull($configService->find('timesheet.rules.lockdown_period_start'));
$this->assertNull($configService->find('timesheet.rules.lockdown_period_end'));
$this->assertNull($configService->find('timesheet.rules.lockdown_period_timezone'));
$this->assertNull($configService->find('timesheet.rules.lockdown_grace_period'));
$form = $client->getCrawler()->filter('form[name=system_configuration_form_lockdown_period]')->form();
$client->submit($form, [
'system_configuration_form_lockdown_period' => [
'configuration' => [
['name' => 'timesheet.rules.lockdown_period_start', 'value' => 'first day of last month 01:23:45'],
['name' => 'timesheet.rules.lockdown_period_end', 'value' => 'last day of last month 23:01:45'],
['name' => 'timesheet.rules.lockdown_period_timezone', 'value' => 'Africa/Bangui'],
['name' => 'timesheet.rules.lockdown_grace_period', 'value' => '+ 12 hours'],
]
]
]);
$this->assertIsRedirect($client, $this->createUrl('/admin/system-config/'));
$client->followRedirect();
$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertHasFlashSaveSuccess($client);
$configService = static::$kernel->getContainer()->get(SystemConfiguration::class);
$this->assertEquals('first day of last month 01:23:45', $configService->find('timesheet.rules.lockdown_period_start'));
$this->assertEquals('last day of last month 23:01:45', $configService->find('timesheet.rules.lockdown_period_end'));
$this->assertEquals('Africa/Bangui', $configService->find('timesheet.rules.lockdown_period_timezone'));
$this->assertEquals('+ 12 hours', $configService->find('timesheet.rules.lockdown_grace_period'));
}
public function testUpdateTimesheetConfigValidation()
{
$this->assertFormHasValidationError(
@@ -138,9 +171,6 @@ class SystemConfigurationControllerTest extends ControllerBaseTest
['name' => 'timesheet.rules.allow_future_times', 'value' => 1],
['name' => 'timesheet.rules.allow_overlapping_records', 'value' => 1],
['name' => 'timesheet.rules.allow_overbooking_budget', 'value' => 1],
['name' => 'timesheet.rules.lockdown_period_start', 'value' => 'first day of last month'],
['name' => 'timesheet.rules.lockdown_period_end', 'value' => 'first day of last month'],
['name' => 'timesheet.rules.lockdown_grace_period', 'value' => '+10 days'],
['name' => 'timesheet.active_entries.hard_limit', 'value' => -1],
['name' => 'timesheet.active_entries.soft_limit', 'value' => -1],
]
@@ -148,8 +178,8 @@ class SystemConfigurationControllerTest extends ControllerBaseTest
],
[
'#system_configuration_form_timesheet_configuration_0_value', // mode
'#system_configuration_form_timesheet_configuration_8_value', // hard_limit
'#system_configuration_form_timesheet_configuration_9_value', // soft_limit
'#system_configuration_form_timesheet_configuration_5_value', // hard_limit
'#system_configuration_form_timesheet_configuration_6_value', // soft_limit
],
true
);

View File

@@ -375,9 +375,6 @@ class TimesheetControllerTest extends ControllerBaseTest
['name' => 'timesheet.rules.allow_future_times', 'value' => true],
['name' => 'timesheet.rules.allow_overlapping_records', 'value' => false],
['name' => 'timesheet.rules.allow_overbooking_budget', 'value' => true],
['name' => 'timesheet.rules.lockdown_period_start', 'value' => null],
['name' => 'timesheet.rules.lockdown_period_end', 'value' => null],
['name' => 'timesheet.rules.lockdown_grace_period', 'value' => null],
['name' => 'timesheet.active_entries.hard_limit', 'value' => 1],
['name' => 'timesheet.active_entries.soft_limit', 'value' => 1],
]

View File

@@ -207,6 +207,7 @@ class AppExtensionTest extends TestCase
'lockdown_period_end' => null,
'lockdown_grace_period' => null,
'allow_overbooking_budget' => true,
'lockdown_period_timezone' => null,
],
'default_begin' => 'now',
'duration_increment' => null,

View File

@@ -286,6 +286,7 @@ class ConfigurationTest extends TestCase
'lockdown_period_end' => null,
'lockdown_grace_period' => null,
'allow_overbooking_budget' => true,
'lockdown_period_timezone' => null,
],
'duration_increment' => null,
'time_increment' => null,

View File

@@ -20,7 +20,7 @@ use PHPUnit\Framework\TestCase;
*/
class LockdownServiceTest extends TestCase
{
protected function createService(?string $start, ?string $end, ?string $grace)
protected function createService(?string $start, ?string $end, ?string $grace, ?string $timezone = null)
{
$loader = $this->createMock(ConfigLoaderInterface::class);
$config = new SystemConfiguration($loader, [
@@ -28,6 +28,7 @@ class LockdownServiceTest extends TestCase
'rules' => [
'lockdown_period_start' => $start,
'lockdown_period_end' => $end,
'lockdown_period_timezone' => $timezone,
'lockdown_grace_period' => $grace,
],
]

View File

@@ -74,6 +74,14 @@
<source>label.timesheet.rules.allow_overlapping_records</source>
<target>Erlaube überlappende Zeiteinträge</target>
</trans-unit>
<trans-unit id="lockdown_period">
<source>lockdown_period</source>
<target>Gesperrter Zeitraum</target>
</trans-unit>
<trans-unit id="label.timesheet.rules.lockdown_period_timezone">
<source>label.timesheet.rules.lockdown_period_timezone</source>
<target>Zeitzone (falls leer, wird Zeitzone des jeweiligen Benutzers verwendet)</target>
</trans-unit>
<trans-unit id="label.timesheet.rules.lockdown_period_start">
<source>label.timesheet.rules.lockdown_period_start</source>
<target>Start des gesperrten Zeitraums (relatives PHP Datumsformat)</target>

View File

@@ -74,6 +74,14 @@
<source>label.timesheet.rules.allow_overlapping_records</source>
<target>Allow overlapping time entries</target>
</trans-unit>
<trans-unit id="lockdown_period">
<source>lockdown_period</source>
<target>Lockdown period</target>
</trans-unit>
<trans-unit id="label.timesheet.rules.lockdown_period_timezone">
<source>label.timesheet.rules.lockdown_period_timezone</source>
<target>Time zone (if empty, time zone of the respective user is used)</target>
</trans-unit>
<trans-unit id="label.timesheet.rules.lockdown_period_start">
<source>label.timesheet.rules.lockdown_period_start</source>
<target>Lockdown period start (PHP relative date to now)</target>