From cecfa1a18a01babb12531240037d998f4078a892 Mon Sep 17 00:00:00 2001 From: nobohan Date: Mon, 16 May 2022 14:07:34 +0200 Subject: [PATCH] main: allow creation of user without password --- .../ChillMainBundle/Controller/UserController.php | 9 +++++++-- src/Bundle/ChillMainBundle/Form/UserType.php | 12 +++++++++--- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Bundle/ChillMainBundle/Controller/UserController.php b/src/Bundle/ChillMainBundle/Controller/UserController.php index 068bd9f73..b344a0b2e 100644 --- a/src/Bundle/ChillMainBundle/Controller/UserController.php +++ b/src/Bundle/ChillMainBundle/Controller/UserController.php @@ -23,6 +23,7 @@ use Chill\MainBundle\Repository\UserRepository; use Chill\MainBundle\Templating\Listing\FilterOrderHelper; use Psr\Log\LoggerInterface; use RuntimeException; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Form; use Symfony\Component\Form\FormInterface; @@ -45,16 +46,20 @@ class UserController extends CRUDController private ValidatorInterface $validator; + protected ParameterBagInterface $parameterBag; + public function __construct( LoggerInterface $chillLogger, ValidatorInterface $validator, UserPasswordEncoderInterface $passwordEncoder, - UserRepository $userRepository + UserRepository $userRepository, + ParameterBagInterface $parameterBag ) { $this->logger = $chillLogger; $this->userRepository = $userRepository; $this->validator = $validator; $this->passwordEncoder = $passwordEncoder; + $this->parameterBag = $parameterBag; } /** @@ -307,7 +312,7 @@ class UserController extends CRUDController protected function onPrePersist(string $action, $entity, FormInterface $form, Request $request) { // for "new", encode the password - if ('new' === $action) { + if ('new' === $action && $this->parameterBag->get('chill_main.access_user_change_password')) { $entity->setPassword($this->passwordEncoder ->encodePassword($entity, $form['plainPassword']->getData())); } diff --git a/src/Bundle/ChillMainBundle/Form/UserType.php b/src/Bundle/ChillMainBundle/Form/UserType.php index c2567df4c..72047ce61 100644 --- a/src/Bundle/ChillMainBundle/Form/UserType.php +++ b/src/Bundle/ChillMainBundle/Form/UserType.php @@ -19,6 +19,7 @@ use Chill\MainBundle\Form\Type\PickCivilityType; use Chill\MainBundle\Templating\TranslatableStringHelper; use Doctrine\ORM\EntityRepository; use Symfony\Bridge\Doctrine\Form\Type\EntityType; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\EmailType; @@ -35,9 +36,14 @@ class UserType extends AbstractType { private TranslatableStringHelper $translatableStringHelper; - public function __construct(TranslatableStringHelper $translatableStringHelper) - { + protected ParameterBagInterface $parameterBag; + + public function __construct( + TranslatableStringHelper $translatableStringHelper, + ParameterBagInterface $parameterBag + ) { $this->translatableStringHelper = $translatableStringHelper; + $this->parameterBag = $parameterBag; } public function buildForm(FormBuilderInterface $builder, array $options) @@ -105,7 +111,7 @@ class UserType extends AbstractType }, ]); - if ($options['is_creation']) { + if ($options['is_creation'] && $this->parameterBag->get('chill_main.access_user_change_password')) { $builder->add('plainPassword', RepeatedType::class, [ 'mapped' => false, 'type' => PasswordType::class,