diff --git a/config/services.yaml b/config/services.yaml index c6f30003..3920c570 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -190,6 +190,10 @@ services: arguments: $available_themes: '%partdb.available_themes%' + App\Validator\Constraints\ValidThemeValidator: + arguments: + $available_themes: '%partdb.available_themes%' + #################################################################################################################### # Label system diff --git a/src/Entity/UserSystem/User.php b/src/Entity/UserSystem/User.php index d009d16e..56034dfd 100644 --- a/src/Entity/UserSystem/User.php +++ b/src/Entity/UserSystem/User.php @@ -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; diff --git a/src/Validator/Constraints/ValidTheme.php b/src/Validator/Constraints/ValidTheme.php new file mode 100644 index 00000000..70a32a20 --- /dev/null +++ b/src/Validator/Constraints/ValidTheme.php @@ -0,0 +1,32 @@ +. + */ + +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'; +} \ No newline at end of file diff --git a/src/Validator/Constraints/ValidThemeValidator.php b/src/Validator/Constraints/ValidThemeValidator.php new file mode 100644 index 00000000..ec437b87 --- /dev/null +++ b/src/Validator/Constraints/ValidThemeValidator.php @@ -0,0 +1,54 @@ +. + */ + +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(); + } + } +} \ No newline at end of file