Skip to content

Commit

Permalink
Merge pull request #53 from chihiro-adachi/fix-secret-validation
Browse files Browse the repository at this point in the history
マスク値のバリデーションを修正
  • Loading branch information
KenTanaka authored Mar 29, 2023
2 parents e97f65c + 8b04cf7 commit 5b6eb0f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
4 changes: 0 additions & 4 deletions Entity/TwoFactorAuthConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,6 @@ class TwoFactorAuthConfig extends AbstractEntity
*/
private $api_secret = null;

/**
* @Assert\NotBlank()
* @Assert\Length(max=4096)
*/
private $plain_api_secret;

/**
Expand Down
34 changes: 27 additions & 7 deletions Form/Type/TwoFactorAuthConfigType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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)
Expand All @@ -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, [
Expand All @@ -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()));
}
}
}
});
}

/**
Expand Down

0 comments on commit 5b6eb0f

Please sign in to comment.