mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-27 20:28:54 +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
|
@ -34,6 +34,7 @@ namespace App\Controller\AdminPages;
|
|||
|
||||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Entity\Attachments\AttachmentTypeAttachment;
|
||||
use App\Form\AdminPages\AttachmentTypeAdminForm;
|
||||
use App\Form\AdminPages\BaseEntityAdminForm;
|
||||
use App\Services\EntityExporter;
|
||||
use App\Services\EntityImporter;
|
||||
|
@ -52,7 +53,7 @@ class AttachmentTypeController extends BaseAdminController
|
|||
|
||||
protected $entity_class = AttachmentType::class;
|
||||
protected $twig_template = 'AdminPages/AttachmentTypeAdmin.html.twig';
|
||||
protected $form_class = BaseEntityAdminForm::class;
|
||||
protected $form_class = AttachmentTypeAdminForm::class;
|
||||
protected $route_base = 'attachment_type';
|
||||
protected $attachment_class = AttachmentTypeAttachment::class;
|
||||
|
||||
|
|
|
@ -56,6 +56,7 @@ use App\Entity\Base\StructuralDBElement;
|
|||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use App\Validator\Constraints\ValidFileFilter;
|
||||
|
||||
/**
|
||||
* Class AttachmentType.
|
||||
|
@ -91,6 +92,7 @@ class AttachmentType extends StructuralDBElement
|
|||
/**
|
||||
* @var string
|
||||
* @ORM\Column(type="text")
|
||||
* @ValidFileFilter
|
||||
*/
|
||||
protected $filetype_filter = "";
|
||||
|
||||
|
@ -112,6 +114,8 @@ class AttachmentType extends StructuralDBElement
|
|||
|
||||
/**
|
||||
* Gets an filter, which file types are allowed for attachment files.
|
||||
* Must be in the format of <input type=file> accept attribute
|
||||
* (See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers).
|
||||
* @return string
|
||||
*/
|
||||
public function getFiletypeFilter(): string
|
||||
|
@ -129,8 +133,6 @@ class AttachmentType extends StructuralDBElement
|
|||
return $this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns the ID as an string, defined by the element class.
|
||||
* This should have a form like P000014, for a part with ID 14.
|
||||
|
|
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']
|
||||
])
|
||||
]),
|
||||
]
|
||||
]);
|
||||
|
||||
|
|
184
src/Services/Attachments/FileTypeFilterTools.php
Normal file
184
src/Services/Attachments/FileTypeFilterTools.php
Normal file
|
@ -0,0 +1,184 @@
|
|||
<?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\Services\Attachments;
|
||||
|
||||
use App\Entity\Attachments\Attachment;
|
||||
use Symfony\Component\Mime\MimeTypesInterface;
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
/**
|
||||
* An servive that helps working with filetype filters (based on the format <input type=file> accept uses.
|
||||
* See https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/file#Unique_file_type_specifiers for
|
||||
* more details.
|
||||
* @package App\Services\Attachments
|
||||
*/
|
||||
class FileTypeFilterTools
|
||||
{
|
||||
|
||||
//The file extensions that will be used for the 'video/*', 'image/*', 'audio/*' placeholders
|
||||
//These file formats can be directly played in common browesers
|
||||
//Source: https://www.chromium.org/audio-video
|
||||
protected const IMAGE_EXTS = Attachment::PICTURE_EXTS;
|
||||
protected const VIDEO_EXTS = ['mp4', 'ogv', 'ogg', 'webm'];
|
||||
protected const AUDIO_EXTS = ['mp3', 'flac', 'ogg', 'oga', 'wav', 'm4a', 'opus'];
|
||||
|
||||
protected $mimeTypes;
|
||||
protected const ALLOWED_MIME_PLACEHOLDERS = ['image/*', 'audio/*', 'video/*'];
|
||||
protected $cache;
|
||||
|
||||
public function __construct(MimeTypesInterface $mimeTypes, CacheInterface $cache)
|
||||
{
|
||||
$this->mimeTypes = $mimeTypes;
|
||||
$this->cache = $cache;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a filetype filter string is valid.
|
||||
* @param string $filter The filter string that should be validated.
|
||||
* @return bool Returns true, if the string is valid.
|
||||
*/
|
||||
public function validateFilterString(string $filter) : bool
|
||||
{
|
||||
$filter = trim($filter);
|
||||
//An empty filter is valid (means no filter applied)
|
||||
if ($filter === '') {
|
||||
return true;
|
||||
}
|
||||
|
||||
$elements = explode(',', $filter);
|
||||
//Check for each element if it is valid:
|
||||
foreach ($elements as $element) {
|
||||
$element = trim($element);
|
||||
if (!preg_match('/^\.\w+$/', $element) // .ext is allowed
|
||||
&& !preg_match('/^[-\w.]+\/[-\w.]+/', $element) //Explicit MIME type is allowed
|
||||
&& !in_array($element, static::ALLOWED_MIME_PLACEHOLDERS, false)) { //image/* is allowed
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//If no element was invalid, the whole string is valid
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Normalize a filter string. All extensions are converted to lowercase, too much whitespaces are removed.
|
||||
* The filter string is not validated.
|
||||
* @param string $filter The filter string that should be normalized.
|
||||
* @return string The normalized filter string
|
||||
*/
|
||||
public function normalizeFilterString(string $filter) : string
|
||||
{
|
||||
$filter = trim($filter);
|
||||
//Replace other separators, with , so we can split it properly
|
||||
$filter = str_replace(';', ',', $filter);
|
||||
//Make everything lower case
|
||||
$filter = strtolower($filter);
|
||||
|
||||
$elements = explode(',', $filter);
|
||||
//Check for each element if it is valid:
|
||||
foreach ($elements as $key => &$element) {
|
||||
$element = trim($element);
|
||||
//Remove empty elements
|
||||
if ($element === '') {
|
||||
unset($elements[$key]);
|
||||
}
|
||||
|
||||
//Convert *.jpg to .jpg
|
||||
if (strpos($element, '*.') === 0) {
|
||||
$element = str_replace('*.', '.', $element);
|
||||
}
|
||||
|
||||
//Convert image to image/*
|
||||
if ($element === 'image' || $element === 'image/') {
|
||||
$element = 'image/*';
|
||||
} elseif ($element === 'video' || $element === 'video/') {
|
||||
$element = 'video/*';
|
||||
} elseif ($element === 'audio' || $element === 'audio/') {
|
||||
$element = 'audio/*';
|
||||
} elseif (!preg_match('/^[-\w.]+\/[-\w.*]+/', $element) && strpos($element, '.') !== 0) {
|
||||
//Convert jpg to .jpg
|
||||
$element = '.' . $element;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$elements = array_unique($elements);
|
||||
|
||||
return implode($elements, ',');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of all file extensions that matches the given filter string
|
||||
* @param string $filter A valid filetype filter string.
|
||||
* @return string[] An array of allowed extensions ['txt', 'csv', 'gif']
|
||||
*/
|
||||
public function resolveFileExtensions(string $filter) : array
|
||||
{
|
||||
$filter = trim($filter);
|
||||
|
||||
return $this->cache->get('filter_exts_' . md5($filter), function (ItemInterface $item) use ($filter) {
|
||||
$elements = explode(',', $filter);
|
||||
$extensions = [];
|
||||
|
||||
foreach ($elements as $element) {
|
||||
$element = trim($element);
|
||||
if (strpos($element, '.') === 0) {
|
||||
//We found an explicit specified file extension -> add it to list
|
||||
$extensions[] = substr($element, 1);
|
||||
} elseif ($element === 'image/*') {
|
||||
$extensions = array_merge($extensions, static::IMAGE_EXTS);
|
||||
} elseif ($element === 'audio/*') {
|
||||
$extensions = array_merge($extensions, static::AUDIO_EXTS);
|
||||
} elseif ($element === 'image/*') {
|
||||
$extensions = array_merge($extensions, static::VIDEO_EXTS);
|
||||
} elseif (preg_match('/^[-\w.]+\/[-\w.*]+/', $element)) {
|
||||
$extensions = array_merge($extensions, $this->mimeTypes->getExtensions($element));
|
||||
}
|
||||
}
|
||||
return array_unique($extensions);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the given extension matches the filter.
|
||||
* @param string $filter The filter which should be used for checking.
|
||||
* @param string $extension The extension that should be checked.
|
||||
* @return bool Returns true, if the extension is allowed with the given filter.
|
||||
*/
|
||||
public function isExtensionAllowed(string $filter, string $extension) : bool
|
||||
{
|
||||
$extension = strtolower($extension);
|
||||
return empty($filter) || in_array($extension, $this->resolveFileExtensions($filter), false);
|
||||
}
|
||||
}
|
44
src/Validator/Constraints/AllowedFileExtension.php
Normal file
44
src/Validator/Constraints/AllowedFileExtension.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?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\Validator\Constraints;
|
||||
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
* @package App\Validator\Constraints
|
||||
*/
|
||||
class AllowedFileExtension extends Constraint
|
||||
{
|
||||
|
||||
}
|
91
src/Validator/Constraints/AllowedFileExtensionValidator.php
Normal file
91
src/Validator/Constraints/AllowedFileExtensionValidator.php
Normal file
|
@ -0,0 +1,91 @@
|
|||
<?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\Validator\Constraints;
|
||||
|
||||
|
||||
use App\Entity\Attachments\Attachment;
|
||||
use App\Services\Attachments\FileTypeFilterTools;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
class AllowedFileExtensionValidator extends ConstraintValidator
|
||||
{
|
||||
|
||||
protected $filterTools;
|
||||
|
||||
public function __construct(FileTypeFilterTools $filterTools)
|
||||
{
|
||||
$this->filterTools = $filterTools;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the passed value is valid.
|
||||
*
|
||||
* @param mixed $value The value that should be validated
|
||||
* @param Constraint $constraint The constraint for the validation
|
||||
*/
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
if (!$constraint instanceof AllowedFileExtension) {
|
||||
throw new UnexpectedTypeException($constraint, AllowedFileExtension::class);
|
||||
}
|
||||
|
||||
if ($value instanceof UploadedFile) {
|
||||
if ($this->context->getObject() instanceof Attachment) {
|
||||
/** @var Attachment $attachment */
|
||||
$attachment = $this->context->getObject();
|
||||
} elseif ($this->context->getObject() instanceof FormInterface) {
|
||||
$attachment = $this->context->getObject()->getParent()->getData();
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
$attachment_type = $attachment->getAttachmentType();
|
||||
|
||||
//Only validate if the attachment type has specified an filetype filter:
|
||||
if ($attachment_type === null || empty($attachment_type->getFiletypeFilter())) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!$this->filterTools->isExtensionAllowed(
|
||||
$attachment_type->getFiletypeFilter(),
|
||||
$value->getClientOriginalExtension()
|
||||
)) {
|
||||
$this->context->buildViolation('validator.file_ext_not_allowed')->addViolation();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -37,6 +37,7 @@ use Symfony\Component\Validator\Constraint;
|
|||
/**
|
||||
* This constraint restricts a user in that way that it can not lock itself out of the user system
|
||||
* @package App\Validator\Constraints
|
||||
* @Annotation
|
||||
*/
|
||||
class NoLockout extends Constraint
|
||||
{
|
||||
|
|
|
@ -38,6 +38,7 @@ use Symfony\Component\Validator\Constraints\Url;
|
|||
/**
|
||||
* Constraints the field that way that the content is either a url or a path to a builtin ressource (like %FOOTPRINTS%)
|
||||
* @package App\Validator\Constraints
|
||||
* @Annotation
|
||||
*/
|
||||
class UrlOrBuiltin extends Url
|
||||
{
|
||||
|
|
44
src/Validator/Constraints/ValidFileFilter.php
Normal file
44
src/Validator/Constraints/ValidFileFilter.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?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\Validator\Constraints;
|
||||
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* @Annotation
|
||||
* @package App\Validator\Constraints
|
||||
*/
|
||||
class ValidFileFilter extends Constraint
|
||||
{
|
||||
|
||||
}
|
77
src/Validator/Constraints/ValidFileFilterValidator.php
Normal file
77
src/Validator/Constraints/ValidFileFilterValidator.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\Validator\Constraints;
|
||||
|
||||
|
||||
use App\Services\Attachments\FileTypeFilterTools;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedValueException;
|
||||
|
||||
class ValidFileFilterValidator extends ConstraintValidator
|
||||
{
|
||||
|
||||
protected $filterTools;
|
||||
|
||||
public function __construct(FileTypeFilterTools $filterTools)
|
||||
{
|
||||
$this->filterTools = $filterTools;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the passed value is valid.
|
||||
*
|
||||
* @param mixed $value The value that should be validated
|
||||
* @param Constraint $constraint The constraint for the validation
|
||||
*/
|
||||
public function validate($value, Constraint $constraint)
|
||||
{
|
||||
if (!$constraint instanceof ValidFileFilter) {
|
||||
throw new UnexpectedTypeException($constraint, ValidFileFilter::class);
|
||||
}
|
||||
|
||||
if (null === $value || '' === $value) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!is_string($value)) {
|
||||
// throw this exception if your validator cannot handle the passed type so that it can be marked as invalid
|
||||
throw new UnexpectedValueException($value, 'string');
|
||||
}
|
||||
|
||||
if (!$this->filterTools->validateFilterString($value)) {
|
||||
$this->context->buildViolation('validator.file_type_filter.invalid')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue