mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-07-14 20:35:08 +02:00
Allow to restrict the file extensions for a attachment type.
This commit is contained in:
parent
2187f5eac2
commit
fdfb099cb5
25 changed files with 714 additions and 4 deletions
77
src/Form/AdminPages/AttachmentTypeAdminForm.php
Normal file
77
src/Form/AdminPages/AttachmentTypeAdminForm.php
Normal file
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
/**
|
||||
*
|
||||
* part-db version 0.1
|
||||
* Copyright (C) 2005 Christoph Lechner
|
||||
* http://www.cl-projects.de/
|
||||
*
|
||||
* part-db version 0.2+
|
||||
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
|
||||
* http://code.google.com/p/part-db/
|
||||
*
|
||||
* Part-DB Version 0.4+
|
||||
* Copyright (C) 2016 - 2019 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 General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*
|
||||
*/
|
||||
|
||||
namespace App\Form\AdminPages;
|
||||
|
||||
|
||||
use App\Entity\Base\NamedDBElement;
|
||||
use App\Form\AdminPages\BaseEntityAdminForm;
|
||||
use App\Services\Attachments\FileTypeFilterTools;
|
||||
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
|
||||
use Symfony\Component\Form\CallbackTransformer;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class AttachmentTypeAdminForm extends BaseEntityAdminForm
|
||||
{
|
||||
protected $filterTools;
|
||||
|
||||
public function __construct(Security $security, ParameterBagInterface $params, TranslatorInterface $trans, FileTypeFilterTools $filterTools)
|
||||
{
|
||||
$this->filterTools = $filterTools;
|
||||
parent::__construct($security, $params, $trans);
|
||||
}
|
||||
|
||||
protected function additionalFormElements(FormBuilderInterface $builder, array $options, NamedDBElement $entity)
|
||||
{
|
||||
$is_new = $entity->getID() === null;
|
||||
|
||||
$builder->add('filetype_filter', TextType::class, ['required' => false,
|
||||
'label' => $this->trans->trans('attachment_type.edit.filetype_filter'),
|
||||
'help' => $this->trans->trans('attachment_type.edit.filetype_filter.help'),
|
||||
'attr' => ['placeholder' => $this->trans->trans('attachment_type.edit.filetype_filter.placeholder')],
|
||||
'empty_data' => '',
|
||||
'disabled' => !$this->security->isGranted($is_new ? 'create' : 'edit', $entity)]);
|
||||
|
||||
//Normalize data before writing it to database
|
||||
$builder->get('filetype_filter')->addViewTransformer(new CallbackTransformer(
|
||||
function ($value) {
|
||||
return $value;
|
||||
},
|
||||
function ($value) {
|
||||
return $this->filterTools->normalizeFilterString($value);
|
||||
}
|
||||
));
|
||||
}
|
||||
}
|
|
@ -37,6 +37,7 @@ use App\Entity\Attachments\AttachmentType;
|
|||
use App\Entity\Base\StructuralDBElement;
|
||||
use App\Form\Type\StructuralEntityType;
|
||||
use App\Services\Attachments\AttachmentManager;
|
||||
use App\Validator\Constraints\AllowedFileExtension;
|
||||
use App\Validator\Constraints\UrlOrBuiltin;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
|
||||
|
@ -120,9 +121,10 @@ class AttachmentFormType extends AbstractType
|
|||
'required' => false,
|
||||
'attr' => ['class' => 'file', 'data-show-preview' => 'false', 'data-show-upload' => 'false'],
|
||||
'constraints' => [
|
||||
new AllowedFileExtension(),
|
||||
new File([
|
||||
'maxSize' => $options['max_file_size']
|
||||
])
|
||||
]),
|
||||
]
|
||||
]);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue