diff --git a/.env b/.env index f99bdc01..8687566d 100644 --- a/.env +++ b/.env @@ -44,11 +44,6 @@ USE_GRAVATAR=0 # This must end with a slash! 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 CHECK_FOR_UPDATES=1 diff --git a/config/parameters.yaml b/config/parameters.yaml index 58f8c223..7ff1b68e 100644 --- a/config/parameters.yaml +++ b/config/parameters.yaml @@ -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.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.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 @@ -115,8 +114,6 @@ parameters: env(REDIRECT_TO_HTTPS): 0 - env(ENFORCE_CHANGE_COMMENTS_FOR): '' - env(ERROR_PAGE_ADMIN_EMAIL): '' env(ERROR_PAGE_SHOW_HELP): 1 diff --git a/config/services.yaml b/config/services.yaml index 26d74cea..695deb11 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -79,9 +79,6 @@ services: arguments: $mimeTypes: '@mime_types' - App\Services\LogSystem\EventCommentNeededHelper: - arguments: - $enforce_change_comments_for: '%partdb.enforce_change_comments_for%' #################################################################################################################### # Attachment system diff --git a/src/Form/History/EnforceEventCommentTypesType.php b/src/Form/History/EnforceEventCommentTypesType.php new file mode 100644 index 00000000..8bb095b9 --- /dev/null +++ b/src/Form/History/EnforceEventCommentTypesType.php @@ -0,0 +1,49 @@ +. + */ + +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' => [], + ]); + } +} \ No newline at end of file diff --git a/src/Services/LogSystem/EventCommentNeededHelper.php b/src/Services/LogSystem/EventCommentNeededHelper.php index 0fffd734..cacf525f 100644 --- a/src/Services/LogSystem/EventCommentNeededHelper.php +++ b/src/Services/LogSystem/EventCommentNeededHelper.php @@ -22,14 +22,16 @@ declare(strict_types=1); */ 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. * It is configured using the "enforce_change_comments_for" config parameter. * @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 { - return in_array($comment_type, $this->enforce_change_comments_for, true); + return in_array($comment_type, $this->settings->enforceComments, true); } } diff --git a/src/Services/LogSystem/EventCommentType.php b/src/Services/LogSystem/EventCommentType.php index ec247e0a..d68c03b2 100644 --- a/src/Services/LogSystem/EventCommentType.php +++ b/src/Services/LogSystem/EventCommentType.php @@ -23,11 +23,14 @@ declare(strict_types=1); 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. * 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_CREATE = 'part_create'; @@ -36,4 +39,9 @@ enum EventCommentType: string case DATASTRUCTURE_EDIT = 'datastructure_edit'; case DATASTRUCTURE_CREATE = 'datastructure_create'; 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); + } } diff --git a/src/Settings/SystemSettings/HistorySettings.php b/src/Settings/SystemSettings/HistorySettings.php index 96353be7..4208cb73 100644 --- a/src/Settings/SystemSettings/HistorySettings.php +++ b/src/Settings/SystemSettings/HistorySettings.php @@ -23,7 +23,11 @@ declare(strict_types=1); namespace App\Settings\SystemSettings; +use App\Form\History\EnforceEventCommentTypesType; +use App\Services\LogSystem\EventCommentType; use Jbtronics\SettingsBundle\Metadata\EnvVarMode; +use Jbtronics\SettingsBundle\ParameterTypes\ArrayType; +use Jbtronics\SettingsBundle\ParameterTypes\EnumType; use Jbtronics\SettingsBundle\Settings\Settings; use Jbtronics\SettingsBundle\Settings\SettingsParameter; use Symfony\Component\Translation\TranslatableMessage as TM; @@ -53,4 +57,25 @@ class HistorySettings envVar: "bool:HISTORY_SAVE_REMOVED_DATA", envVarMode: EnvVarMode::OVERWRITE )] 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); + } } \ No newline at end of file diff --git a/tests/Services/LogSystem/EventCommentNeededHelperTest.php b/tests/Services/LogSystem/EventCommentNeededHelperTest.php index f365bb63..070cf071 100644 --- a/tests/Services/LogSystem/EventCommentNeededHelperTest.php +++ b/tests/Services/LogSystem/EventCommentNeededHelperTest.php @@ -24,13 +24,17 @@ namespace App\Tests\Services\LogSystem; use App\Services\LogSystem\EventCommentNeededHelper; use App\Services\LogSystem\EventCommentType; +use App\Settings\SystemSettings\HistorySettings; use PHPUnit\Framework\TestCase; class EventCommentNeededHelperTest extends TestCase { 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_EDIT)); $this->assertFalse($service->isCommentNeeded(EventCommentType::DATASTRUCTURE_EDIT)); diff --git a/translations/messages.en.xlf b/translations/messages.en.xlf index 95f3ad07..fc055205 100644 --- a/translations/messages.en.xlf +++ b/translations/messages.en.xlf @@ -12473,5 +12473,59 @@ Please note, that you can not impersonate a disabled user. If you try you will g Global theme + + + settings.system.history.enforceComments + Enforce comments for action types + + + + + settings.system.history.enforceComments.description + With this option, you can specify for which actions, users are enforced to give a reason, which will be logged in history. + + + + + settings.system.history.enforceComments.type.part_edit + Part edit + + + + + settings.system.history.enforceComments.type.part_create + Part creation + + + + + settings.system.history.enforceComments.type.part_delete + Part deletion + + + + + settings.system.history.enforceComments.type.part_stock_operation + Part stock operation + + + + + settings.system.history.enforceComments.type.datastructure_edit + Data structure edit + + + + + settings.system.history.enforceComments.type.datastructure_create + Data structure creation + + + + + settings.system.history.enforceComments.type.datastructure_delete + Data structure deletion + +