mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-20 17:15:51 +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
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -31,3 +31,8 @@ yarn-error.log
|
|||
###> liip/imagine-bundle ###
|
||||
/public/media/cache/
|
||||
###< liip/imagine-bundle ###
|
||||
|
||||
###> phpunit/phpunit ###
|
||||
/phpunit.xml
|
||||
.phpunit.result.cache
|
||||
###< phpunit/phpunit ###
|
||||
|
|
|
@ -104,6 +104,10 @@ services:
|
|||
$footprints_path: 'public/img/footprints'
|
||||
$models_path: null
|
||||
|
||||
App\Services\Attachments\FileTypeFilterTools:
|
||||
arguments:
|
||||
$mimeTypes: '@mime_types'
|
||||
|
||||
App\Services\TranslationExtractor\PermissionExtractor:
|
||||
tags:
|
||||
- { name: 'translation.extractor', alias: 'permissionExtractor'}
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -2,4 +2,8 @@
|
|||
|
||||
{% block card_title %}
|
||||
<i class="fas fa-file-alt fa-fw"></i> {% trans %}attachment_type.caption{% endtrans %}
|
||||
{% endblock %}
|
||||
|
||||
{% block additional_controls %}
|
||||
{{ form_row(form.filetype_filter) }}
|
||||
{% endblock %}
|
|
@ -38,6 +38,10 @@ use Doctrine\ORM\EntityManagerInterface;
|
|||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
use Symfony\Component\Security\Core\Security;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
abstract class AbstractAdminControllerTest extends WebTestCase
|
||||
{
|
||||
protected static $base_path = 'not_valid';
|
||||
|
@ -62,6 +66,7 @@ abstract class AbstractAdminControllerTest extends WebTestCase
|
|||
|
||||
/**
|
||||
* @dataProvider readDataProvider
|
||||
* @group slow
|
||||
* Tests if you can access the /new part which is used to list all entities. Checks if permissions are working
|
||||
*/
|
||||
public function testListEntries(string $user, bool $read)
|
||||
|
@ -81,6 +86,7 @@ abstract class AbstractAdminControllerTest extends WebTestCase
|
|||
|
||||
/**
|
||||
* @dataProvider readDataProvider
|
||||
* @group slow
|
||||
* Tests if it possible to access an specific entity. Checks if permissions are working.
|
||||
*/
|
||||
public function testReadEntity(string $user, bool $read)
|
||||
|
@ -110,6 +116,7 @@ abstract class AbstractAdminControllerTest extends WebTestCase
|
|||
|
||||
/**
|
||||
* Tests if deleting an entity is working.
|
||||
* @group slow
|
||||
* @dataProvider deleteDataProvider
|
||||
*/
|
||||
public function testDeleteEntity(string $user, bool $delete)
|
||||
|
|
|
@ -34,6 +34,10 @@ namespace App\Tests\Controller\AdminPages;
|
|||
|
||||
use App\Entity\Attachments\AttachmentType;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class AttachmentTypeControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/attachment_type';
|
||||
|
|
|
@ -33,6 +33,10 @@ namespace App\Tests\Controller\AdminPages;
|
|||
|
||||
use App\Entity\Parts\Category;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class CategoryControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/category';
|
||||
|
|
|
@ -35,6 +35,10 @@ namespace App\Tests\Controller\AdminPages;
|
|||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Entity\Devices\Device;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class DeviceControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/device';
|
||||
|
|
|
@ -35,6 +35,10 @@ namespace App\Tests\Controller\AdminPages;
|
|||
use App\Entity\Attachments\AttachmentType;
|
||||
use App\Entity\Parts\Footprint;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class FootprintControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/footprint';
|
||||
|
|
|
@ -36,6 +36,10 @@ use App\Entity\Attachments\AttachmentType;
|
|||
use App\Entity\Devices\Device;
|
||||
use App\Entity\Parts\Manufacturer;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class ManufacturerControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/manufacturer';
|
||||
|
|
|
@ -37,6 +37,10 @@ use App\Entity\Devices\Device;
|
|||
use App\Entity\Parts\Manufacturer;
|
||||
use App\Entity\Parts\MeasurementUnit;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class MeasurementUnitControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/measurement_unit';
|
||||
|
|
|
@ -38,6 +38,10 @@ use App\Entity\Parts\Manufacturer;
|
|||
use App\Entity\Parts\Storelocation;
|
||||
use Symfony\Component\HttpKernel\HttpCache\Store;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class StorelocationControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/store_location';
|
||||
|
|
|
@ -37,6 +37,10 @@ use App\Entity\Devices\Device;
|
|||
use App\Entity\Parts\Manufacturer;
|
||||
use App\Entity\Parts\Supplier;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller\AdminPages
|
||||
*/
|
||||
class SupplierControllerTest extends AbstractAdminControllerTest
|
||||
{
|
||||
protected static $base_path = '/en' . '/supplier';
|
||||
|
|
|
@ -36,6 +36,10 @@ use Doctrine\ORM\EntityManagerInterface;
|
|||
use Proxies\__CG__\App\Entity\UserSystem\User;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
/**
|
||||
* @group slow
|
||||
* @package App\Tests\Controller
|
||||
*/
|
||||
class RedirectControllerTest extends WebTestCase
|
||||
{
|
||||
protected $em;
|
||||
|
@ -65,6 +69,7 @@ class RedirectControllerTest extends WebTestCase
|
|||
/**
|
||||
* Test if a certain request to an url will be redirected.
|
||||
* @dataProvider urlMatchDataProvider
|
||||
* @group slow
|
||||
*/
|
||||
public function testUrlMatch($url, $expect_redirect)
|
||||
{
|
||||
|
@ -95,6 +100,7 @@ class RedirectControllerTest extends WebTestCase
|
|||
/**
|
||||
* Test if the user is redirected to the localized version of a page, based on his settings.
|
||||
* @dataProvider urlAddLocaleDataProvider
|
||||
* @group slow
|
||||
* @depends testUrlMatch
|
||||
* @param $user_locale
|
||||
* @param $input_path
|
||||
|
@ -124,6 +130,7 @@ class RedirectControllerTest extends WebTestCase
|
|||
/**
|
||||
* Test if the user is redirected to password change page if he should do that
|
||||
* @depends testAddLocale
|
||||
* @group slow
|
||||
* @testWith ["de"]
|
||||
* ["en"]
|
||||
*/
|
||||
|
|
127
tests/Services/Attachments/FileTypeFilterToolsTest.php
Normal file
127
tests/Services/Attachments/FileTypeFilterToolsTest.php
Normal file
|
@ -0,0 +1,127 @@
|
|||
<?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\Tests\Services\Attachments;
|
||||
|
||||
use App\Services\Attachments\FileTypeFilterTools;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
class FileTypeFilterToolsTest extends WebTestCase
|
||||
{
|
||||
protected static $service;
|
||||
|
||||
public static function setUpBeforeClass()
|
||||
{
|
||||
self::bootKernel();
|
||||
self::$service = self::$container->get(FileTypeFilterTools::class);
|
||||
}
|
||||
|
||||
public function validateDataProvider() : array
|
||||
{
|
||||
return [
|
||||
['', true], //Empty string is valid
|
||||
['.jpeg,.png, .gif', true], //Only extensions are valid
|
||||
['image/*, video/*, .mp4, video/x-msvideo, application/vnd.amazon.ebook', true],
|
||||
['application/vnd.amazon.ebook, audio/opus', true],
|
||||
|
||||
['*.notvalid, .png', false], //No stars in extension
|
||||
['test.png', false], //No full filename
|
||||
['application/*', false], //Only certain placeholders are allowed
|
||||
['.png;.png,.jpg', false], //Wrong separator
|
||||
['.png .jpg .gif', false]
|
||||
];
|
||||
}
|
||||
|
||||
public function normalizeDataProvider() : array
|
||||
{
|
||||
return [
|
||||
['', ''],
|
||||
['.jpeg,.png,.gif', '.jpeg,.png,.gif'],
|
||||
['.jpeg, .png, .gif,', '.jpeg,.png,.gif'],
|
||||
['jpg, *.gif', '.jpg,.gif'],
|
||||
['video, image/', 'video/*,image/*'],
|
||||
['video/*', 'video/*'],
|
||||
['video/x-msvideo,.jpeg', 'video/x-msvideo,.jpeg'],
|
||||
['.video', '.video'],
|
||||
//Remove duplicate entries
|
||||
['png, .gif, .png,', '.png,.gif'],
|
||||
];
|
||||
}
|
||||
|
||||
public function extensionAllowedDataProvider() : array
|
||||
{
|
||||
return [
|
||||
['', 'txt', true],
|
||||
['', 'everything_should_match', true],
|
||||
|
||||
['.jpg,.png', 'jpg', true],
|
||||
['.jpg,.png', 'png', true],
|
||||
['.jpg,.png', 'txt', false],
|
||||
|
||||
['image/*', 'jpeg', true],
|
||||
['image/*', 'png', true],
|
||||
['image/*', 'txt', false],
|
||||
|
||||
['application/pdf,.txt', 'pdf', true],
|
||||
['application/pdf,.txt', 'txt', true],
|
||||
['application/pdf,.txt', 'jpg', false],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the validateFilterString method
|
||||
* @dataProvider validateDataProvider
|
||||
* @param string $filter
|
||||
* @param bool $expected
|
||||
*/
|
||||
public function testValidateFilterString(string $filter, bool $expected)
|
||||
{
|
||||
$this->assertEquals($expected, self::$service->validateFilterString($filter));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider normalizeDataProvider
|
||||
* @param string $filter
|
||||
* @param string $expected
|
||||
*/
|
||||
public function testNormalizeFilterString(string $filter, string $expected)
|
||||
{
|
||||
$this->assertEquals($expected, self::$service->normalizeFilterString($filter));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider extensionAllowedDataProvider
|
||||
*/
|
||||
public function testIsExtensionAllowed(string $filter, string $extension, bool $expected)
|
||||
{
|
||||
$this->assertEquals($expected, self::$service->isExtensionAllowed($filter, $extension), $expected);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue