diff --git a/Entity/TwoFactorAuthConfig.php b/Entity/TwoFactorAuthConfig.php index b2cabbe..94b9149 100644 --- a/Entity/TwoFactorAuthConfig.php +++ b/Entity/TwoFactorAuthConfig.php @@ -53,10 +53,6 @@ class TwoFactorAuthConfig extends AbstractEntity */ private $api_secret = null; - /** - * @Assert\NotBlank() - * @Assert\Length(max=4096) - */ private $plain_api_secret; /** diff --git a/Form/Type/TwoFactorAuthConfigType.php b/Form/Type/TwoFactorAuthConfigType.php index ae7f255..00cd14f 100644 --- a/Form/Type/TwoFactorAuthConfigType.php +++ b/Form/Type/TwoFactorAuthConfigType.php @@ -19,8 +19,12 @@ use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormError; +use Symfony\Component\Form\FormEvent; +use Symfony\Component\Form\FormEvents; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints as Assert; +use Symfony\Component\Validator\Validator\ValidatorInterface; class TwoFactorAuthConfigType extends AbstractType { @@ -29,14 +33,17 @@ class TwoFactorAuthConfigType extends AbstractType */ protected $eccubeConfig; + protected ValidatorInterface $validator; + /** * TwoFactorAuthConfigType constructor. * * @param EccubeConfig $eccubeConfig */ - public function __construct(EccubeConfig $eccubeConfig) + public function __construct(EccubeConfig $eccubeConfig, ValidatorInterface $validator) { $this->eccubeConfig = $eccubeConfig; + $this->validator = $validator; } public function buildForm(FormBuilderInterface $builder, array $options) @@ -60,12 +67,6 @@ public function buildForm(FormBuilderInterface $builder, array $options) 'constraints' => [ new Assert\NotBlank(), new Assert\Length(['max' => $this->eccubeConfig['eccube_stext_len']]), - new Assert\Regex( - [ - 'pattern' => '/^[a-zA-Z0-9]+$/i', - 'message' => 'form_error.graph_only', - ] - ), ], ]) ->add('from_phone_number', TextType::class, [ @@ -89,6 +90,25 @@ public function buildForm(FormBuilderInterface $builder, array $options) ]), ], ]); + + $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) { + $form = $event->getForm(); + $data = $event->getData(); + + if ($data['plain_api_secret'] !== $this->eccubeConfig['eccube_default_password']) { + $errors = $this->validator->validate($data['plain_api_secret'], [ + new Assert\Regex([ + 'pattern' => '/^[a-zA-Z0-9]+$/i', + 'message' => 'form_error.graph_only', + ]), + ]); + if ($errors) { + foreach ($errors as $error) { + $form['plain_api_secret']->addError(new FormError($error->getMessage())); + } + } + } + }); } /**