Use new ValidTheme constraint on theme field.

The old choice constraint needed an const, which is not available anymore, as you can configure the themes via parameters
This commit is contained in:
Jan Böhmer 2023-02-01 23:50:54 +01:00
parent f1c3a52c8a
commit 3c3a03a179
4 changed files with 92 additions and 1 deletions

View file

@ -190,6 +190,10 @@ services:
arguments:
$available_themes: '%partdb.available_themes%'
App\Validator\Constraints\ValidThemeValidator:
arguments:
$available_themes: '%partdb.available_themes%'
####################################################################################################################
# Label system

View file

@ -29,6 +29,7 @@ use App\Entity\PriceInformations\Currency;
use App\Security\Interfaces\HasPermissionsInterface;
use App\Validator\Constraints\Selectable;
use App\Validator\Constraints\ValidPermission;
use App\Validator\Constraints\ValidTheme;
use Jbtronics\TFAWebauthn\Model\LegacyU2FKeyInterface;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;
use Webauthn\PublicKeyCredentialUserEntity;
@ -77,7 +78,7 @@ class User extends AttachmentContainingDBElement implements UserInterface, HasPe
/**
* @var string|null The theme
* @ORM\Column(type="string", name="config_theme", nullable=true)
* @Assert\Choice(choices=User::AVAILABLE_THEMES)
* @ValidTheme()
*/
protected ?string $theme = null;

View file

@ -0,0 +1,32 @@
<?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\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* A constraint to validate the theme setting of the user.
* @Annotation
*/
class ValidTheme extends Constraint
{
public string $message = 'validator.selected_theme_is_invalid';
}

View file

@ -0,0 +1,54 @@
<?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\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class ValidThemeValidator extends ConstraintValidator
{
private array $available_themes;
public function __construct(array $available_themes)
{
$this->available_themes = $available_themes;
}
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof ValidTheme) {
throw new UnexpectedTypeException($constraint, ValidTheme::class);
}
//Empty values are allowed
if (null === $value || '' === $value) {
return;
}
//If a value is set, it must be a value from the available themes list
if (!in_array($value, $this->available_themes, true)) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $value)
->addViolation();
}
}
}