Moved the "ENFORCE_CHANGE_COMMENTS_FOR" type to the HistorySettings class

This commit is contained in:
Jan Böhmer 2024-06-25 22:59:22 +02:00
parent 6df7bc5f2a
commit 5ab6a63492
9 changed files with 147 additions and 16 deletions

5
.env
View file

@ -44,11 +44,6 @@ USE_GRAVATAR=0
# This must end with a slash! # This must end with a slash!
DEFAULT_URI="https://partdb.changeme.invalid/" DEFAULT_URI="https://partdb.changeme.invalid/"
# With this option you can configure, where users are enforced to give a change reason, which will be logged
# This is a comma separated list of values, see documentation for available values
# Leave this empty, to make all change reasons optional
ENFORCE_CHANGE_COMMENTS_FOR=""
# Disable that if you do not want that Part-DB connects to GitHub to check for available updates, or if your server can not connect to the internet # Disable that if you do not want that Part-DB connects to GitHub to check for available updates, or if your server can not connect to the internet
CHECK_FOR_UPDATES=1 CHECK_FOR_UPDATES=1

View file

@ -12,7 +12,6 @@ parameters:
partdb.title: '%env(string:settings:customization:instanceName)%' # The title shown inside of Part-DB (e.g. in the navbar and on homepage) partdb.title: '%env(string:settings:customization:instanceName)%' # The title shown inside of Part-DB (e.g. in the navbar and on homepage)
partdb.default_currency: '%env(string:BASE_CURRENCY)%' # The currency that is used inside the DB (and is assumed when no currency is set). This can not be changed later, so be sure to set it the currency used in your country partdb.default_currency: '%env(string:BASE_CURRENCY)%' # The currency that is used inside the DB (and is assumed when no currency is set). This can not be changed later, so be sure to set it the currency used in your country
partdb.locale_menu: ['en', 'de', 'it', 'fr', 'ru', 'ja', 'cs', 'da', 'zh'] # The languages that are shown in user drop down menu partdb.locale_menu: ['en', 'de', 'it', 'fr', 'ru', 'ja', 'cs', 'da', 'zh'] # The languages that are shown in user drop down menu
partdb.enforce_change_comments_for: '%env(csv:ENFORCE_CHANGE_COMMENTS_FOR)%' # The actions for which a change comment is required (e.g. "part_edit", "part_create", etc.). If this is empty, change comments are not required at all.
partdb.default_uri: '%env(string:DEFAULT_URI)%' # The default URI to use for the Part-DB instance (e.g. https://part-db.example.com/). This is used for generating links in emails partdb.default_uri: '%env(string:DEFAULT_URI)%' # The default URI to use for the Part-DB instance (e.g. https://part-db.example.com/). This is used for generating links in emails
@ -115,8 +114,6 @@ parameters:
env(REDIRECT_TO_HTTPS): 0 env(REDIRECT_TO_HTTPS): 0
env(ENFORCE_CHANGE_COMMENTS_FOR): ''
env(ERROR_PAGE_ADMIN_EMAIL): '' env(ERROR_PAGE_ADMIN_EMAIL): ''
env(ERROR_PAGE_SHOW_HELP): 1 env(ERROR_PAGE_SHOW_HELP): 1

View file

@ -79,9 +79,6 @@ services:
arguments: arguments:
$mimeTypes: '@mime_types' $mimeTypes: '@mime_types'
App\Services\LogSystem\EventCommentNeededHelper:
arguments:
$enforce_change_comments_for: '%partdb.enforce_change_comments_for%'
#################################################################################################################### ####################################################################################################################
# Attachment system # Attachment system

View file

@ -0,0 +1,49 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Form\History;
use App\Services\LogSystem\EventCommentType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* The type for the "enforceComments" setting in the HistorySettings.
*/
class EnforceEventCommentTypesType extends AbstractType
{
public function getParent(): string
{
return EnumType::class;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'multiple' => true,
'class' => EventCommentType::class,
'empty_data' => [],
]);
}
}

View file

@ -22,14 +22,16 @@ declare(strict_types=1);
*/ */
namespace App\Services\LogSystem; namespace App\Services\LogSystem;
use App\Settings\SystemSettings\HistorySettings;
/** /**
* This service is used to check if a log change comment is needed for a given operation type. * This service is used to check if a log change comment is needed for a given operation type.
* It is configured using the "enforce_change_comments_for" config parameter. * It is configured using the "enforce_change_comments_for" config parameter.
* @see \App\Tests\Services\LogSystem\EventCommentNeededHelperTest * @see \App\Tests\Services\LogSystem\EventCommentNeededHelperTest
*/ */
class EventCommentNeededHelper final class EventCommentNeededHelper
{ {
public function __construct(protected array $enforce_change_comments_for) public function __construct(private readonly HistorySettings $settings)
{ {
} }
@ -39,6 +41,6 @@ class EventCommentNeededHelper
*/ */
public function isCommentNeeded(EventCommentType $comment_type): bool public function isCommentNeeded(EventCommentType $comment_type): bool
{ {
return in_array($comment_type, $this->enforce_change_comments_for, true); return in_array($comment_type, $this->settings->enforceComments, true);
} }
} }

View file

@ -23,11 +23,14 @@ declare(strict_types=1);
namespace App\Services\LogSystem; namespace App\Services\LogSystem;
use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/** /**
* This enum represents the different types of event comments that could be required, by the system. * This enum represents the different types of event comments that could be required, by the system.
* They are almost only useful when working with the EventCommentNeededHelper service. * They are almost only useful when working with the EventCommentNeededHelper service.
*/ */
enum EventCommentType: string enum EventCommentType: string implements TranslatableInterface
{ {
case PART_EDIT = 'part_edit'; case PART_EDIT = 'part_edit';
case PART_CREATE = 'part_create'; case PART_CREATE = 'part_create';
@ -36,4 +39,9 @@ enum EventCommentType: string
case DATASTRUCTURE_EDIT = 'datastructure_edit'; case DATASTRUCTURE_EDIT = 'datastructure_edit';
case DATASTRUCTURE_CREATE = 'datastructure_create'; case DATASTRUCTURE_CREATE = 'datastructure_create';
case DATASTRUCTURE_DELETE = 'datastructure_delete'; case DATASTRUCTURE_DELETE = 'datastructure_delete';
public function trans(TranslatorInterface $translator, ?string $locale = null): string
{
return $translator->trans('settings.system.history.enforceComments.type.' . $this->value, locale: $locale);
}
} }

View file

@ -23,7 +23,11 @@ declare(strict_types=1);
namespace App\Settings\SystemSettings; namespace App\Settings\SystemSettings;
use App\Form\History\EnforceEventCommentTypesType;
use App\Services\LogSystem\EventCommentType;
use Jbtronics\SettingsBundle\Metadata\EnvVarMode; use Jbtronics\SettingsBundle\Metadata\EnvVarMode;
use Jbtronics\SettingsBundle\ParameterTypes\ArrayType;
use Jbtronics\SettingsBundle\ParameterTypes\EnumType;
use Jbtronics\SettingsBundle\Settings\Settings; use Jbtronics\SettingsBundle\Settings\Settings;
use Jbtronics\SettingsBundle\Settings\SettingsParameter; use Jbtronics\SettingsBundle\Settings\SettingsParameter;
use Symfony\Component\Translation\TranslatableMessage as TM; use Symfony\Component\Translation\TranslatableMessage as TM;
@ -53,4 +57,25 @@ class HistorySettings
envVar: "bool:HISTORY_SAVE_REMOVED_DATA", envVarMode: EnvVarMode::OVERWRITE envVar: "bool:HISTORY_SAVE_REMOVED_DATA", envVarMode: EnvVarMode::OVERWRITE
)] )]
public bool $saveRemovedData = true; public bool $saveRemovedData = true;
/** @var EventCommentType[] */
#[SettingsParameter(
type: ArrayType::class,
label: new TM("settings.system.history.enforceComments"),
description: new TM("settings.system.history.enforceComments.description"),
options: ['type' => EnumType::class, 'nullable' => false, 'options' => ['class' => EventCommentType::class]],
formType: EnforceEventCommentTypesType::class,
envVar: "ENFORCE_CHANGE_COMMENTS_FOR", envVarMode: EnvVarMode::OVERWRITE, envVarMapper: [self::class, 'mapEnforceComments']
)]
public array $enforceComments = [];
public static function mapEnforceComments(string $value): array
{
if (trim($value) === '') {
return [];
}
$explode = explode(',', $value);
return array_map(fn(string $type) => EventCommentType::from($type), $explode);
}
} }

View file

@ -24,13 +24,17 @@ namespace App\Tests\Services\LogSystem;
use App\Services\LogSystem\EventCommentNeededHelper; use App\Services\LogSystem\EventCommentNeededHelper;
use App\Services\LogSystem\EventCommentType; use App\Services\LogSystem\EventCommentType;
use App\Settings\SystemSettings\HistorySettings;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
class EventCommentNeededHelperTest extends TestCase class EventCommentNeededHelperTest extends TestCase
{ {
public function testIsCommentNeeded(): void public function testIsCommentNeeded(): void
{ {
$service = new EventCommentNeededHelper([EventCommentType::PART_CREATE, EventCommentType::PART_EDIT]); $settings = new HistorySettings();
$settings->enforceComments = [EventCommentType::PART_CREATE, EventCommentType::PART_EDIT];
$service = new EventCommentNeededHelper($settings);
$this->assertTrue($service->isCommentNeeded(EventCommentType::PART_CREATE)); $this->assertTrue($service->isCommentNeeded(EventCommentType::PART_CREATE));
$this->assertTrue($service->isCommentNeeded(EventCommentType::PART_EDIT)); $this->assertTrue($service->isCommentNeeded(EventCommentType::PART_EDIT));
$this->assertFalse($service->isCommentNeeded(EventCommentType::DATASTRUCTURE_EDIT)); $this->assertFalse($service->isCommentNeeded(EventCommentType::DATASTRUCTURE_EDIT));

View file

@ -12473,5 +12473,59 @@ Please note, that you can not impersonate a disabled user. If you try you will g
<target>Global theme</target> <target>Global theme</target>
</segment> </segment>
</unit> </unit>
<unit id="4hNoijr" name="settings.system.history.enforceComments">
<segment>
<source>settings.system.history.enforceComments</source>
<target>Enforce comments for action types</target>
</segment>
</unit>
<unit id="zeiSg.P" name="settings.system.history.enforceComments.description">
<segment>
<source>settings.system.history.enforceComments.description</source>
<target>With this option, you can specify for which actions, users are enforced to give a reason, which will be logged in history.</target>
</segment>
</unit>
<unit id="iEq8463" name="settings.system.history.enforceComments.type.part_edit">
<segment>
<source>settings.system.history.enforceComments.type.part_edit</source>
<target>Part edit</target>
</segment>
</unit>
<unit id="Ns_O1ZC" name="settings.system.history.enforceComments.type.part_create">
<segment>
<source>settings.system.history.enforceComments.type.part_create</source>
<target>Part creation</target>
</segment>
</unit>
<unit id="bkYgIFK" name="settings.system.history.enforceComments.type.part_delete">
<segment>
<source>settings.system.history.enforceComments.type.part_delete</source>
<target>Part deletion</target>
</segment>
</unit>
<unit id="SKOeTBp" name="settings.system.history.enforceComments.type.part_stock_operation">
<segment>
<source>settings.system.history.enforceComments.type.part_stock_operation</source>
<target>Part stock operation</target>
</segment>
</unit>
<unit id="SvD2GFR" name="settings.system.history.enforceComments.type.datastructure_edit">
<segment>
<source>settings.system.history.enforceComments.type.datastructure_edit</source>
<target>Data structure edit</target>
</segment>
</unit>
<unit id="YGcoYaK" name="settings.system.history.enforceComments.type.datastructure_create">
<segment>
<source>settings.system.history.enforceComments.type.datastructure_create</source>
<target>Data structure creation</target>
</segment>
</unit>
<unit id="Xt6S9iR" name="settings.system.history.enforceComments.type.datastructure_delete">
<segment>
<source>settings.system.history.enforceComments.type.datastructure_delete</source>
<target>Data structure deletion</target>
</segment>
</unit>
</file> </file>
</xliff> </xliff>