Added an option to enforce log comments for certain actions

This implements issue #220
This commit is contained in:
Jan Böhmer 2023-04-08 20:43:19 +02:00
parent 5f2408b791
commit 29af14f588
14 changed files with 179 additions and 11 deletions

View file

@ -24,6 +24,7 @@ namespace App\Form\AdminPages;
use App\Entity\Base\AbstractNamedDBElement;
use App\Services\Attachments\FileTypeFilterTools;
use App\Services\LogSystem\EventCommentNeededHelper;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
@ -33,10 +34,10 @@ class AttachmentTypeAdminForm extends BaseEntityAdminForm
{
protected FileTypeFilterTools $filterTools;
public function __construct(Security $security, FileTypeFilterTools $filterTools)
public function __construct(Security $security, FileTypeFilterTools $filterTools, EventCommentNeededHelper $eventCommentNeededHelper)
{
$this->filterTools = $filterTools;
parent::__construct($security);
parent::__construct($security, $eventCommentNeededHelper);
}
protected function additionalFormElements(FormBuilderInterface $builder, array $options, AbstractNamedDBElement $entity): void

View file

@ -31,6 +31,7 @@ use App\Form\ParameterType;
use App\Form\Type\MasterPictureAttachmentType;
use App\Form\Type\RichTextEditorType;
use App\Form\Type\StructuralEntityType;
use App\Services\LogSystem\EventCommentNeededHelper;
use FOS\CKEditorBundle\Form\Type\CKEditorType;
use function get_class;
use Symfony\Component\Form\AbstractType;
@ -46,10 +47,12 @@ use Symfony\Component\Security\Core\Security;
class BaseEntityAdminForm extends AbstractType
{
protected Security $security;
protected EventCommentNeededHelper $eventCommentNeededHelper;
public function __construct(Security $security)
public function __construct(Security $security, EventCommentNeededHelper $eventCommentNeededHelper)
{
$this->security = $security;
$this->eventCommentNeededHelper = $eventCommentNeededHelper;
}
public function configureOptions(OptionsResolver $resolver): void
@ -141,7 +144,7 @@ class BaseEntityAdminForm extends AbstractType
$builder->add('log_comment', TextType::class, [
'label' => 'edit.log_comment',
'mapped' => false,
'required' => false,
'required' => $this->eventCommentNeededHelper->isCommentNeeded($is_new ? 'datastructure_create': 'datastructure_edit'),
'empty_data' => null,
]);

View file

@ -37,6 +37,7 @@ use App\Form\Type\RichTextEditorType;
use App\Form\Type\SIUnitType;
use App\Form\Type\StructuralEntityType;
use App\Form\WorkaroundCollectionType;
use App\Services\LogSystem\EventCommentNeededHelper;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
@ -54,17 +55,20 @@ class PartBaseType extends AbstractType
{
protected Security $security;
protected UrlGeneratorInterface $urlGenerator;
protected EventCommentNeededHelper $event_comment_needed_helper;
public function __construct(Security $security, UrlGeneratorInterface $urlGenerator)
public function __construct(Security $security, UrlGeneratorInterface $urlGenerator, EventCommentNeededHelper $event_comment_needed_helper)
{
$this->security = $security;
$this->urlGenerator = $urlGenerator;
$this->event_comment_needed_helper = $event_comment_needed_helper;
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
/** @var Part $part */
$part = $builder->getData();
$new_part = null === $part->getID();
$status_choices = [
'm_status.unknown' => '',
@ -250,7 +254,7 @@ class PartBaseType extends AbstractType
$builder->add('log_comment', TextType::class, [
'label' => 'edit.log_comment',
'mapped' => false,
'required' => false,
'required' => $this->event_comment_needed_helper->isCommentNeeded($new_part ? 'part_create' : 'part_edit'),
'empty_data' => null,
]);

View file

@ -0,0 +1,60 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 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/>.
*/
namespace App\Services\LogSystem;
/**
* 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.
*/
class EventCommentNeededHelper
{
protected array $enforce_change_comments_for;
public const VALID_OPERATION_TYPES = [
'part_edit',
'part_create',
'part_delete',
'part_stock_operation',
'datastructure_edit',
'datastructure_create',
'datastructure_delete',
];
public function __construct(array $enforce_change_comments_for)
{
$this->enforce_change_comments_for = $enforce_change_comments_for;
}
/**
* Checks if a log change comment is needed for the given operation type
* @param string $comment_type
* @return bool
*/
public function isCommentNeeded(string $comment_type): bool
{
//Check if the comment type is valid
if (! in_array($comment_type, self::VALID_OPERATION_TYPES, true)) {
throw new \InvalidArgumentException('The comment type "'.$comment_type.'" is not valid!');
}
return in_array($comment_type, $this->enforce_change_comments_for, true);
}
}

View file

@ -0,0 +1,43 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 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/>.
*/
namespace App\Twig;
use App\Services\LogSystem\EventCommentNeededHelper;
use Twig\Extension\AbstractExtension;
final class MiscExtension extends AbstractExtension
{
private EventCommentNeededHelper $eventCommentNeededHelper;
public function __construct(EventCommentNeededHelper $eventCommentNeededHelper)
{
$this->eventCommentNeededHelper = $eventCommentNeededHelper;
}
public function getFunctions()
{
return [
new \Twig\TwigFunction('event_comment_needed',
fn(string $operation_type) => $this->eventCommentNeededHelper->isCommentNeeded($operation_type)
),
];
}
}