main: allow creation of user without password

This commit is contained in:
nobohan 2022-05-16 14:07:34 +02:00
parent f77031630f
commit cecfa1a18a
2 changed files with 16 additions and 5 deletions

View File

@ -23,6 +23,7 @@ use Chill\MainBundle\Repository\UserRepository;
use Chill\MainBundle\Templating\Listing\FilterOrderHelper; use Chill\MainBundle\Templating\Listing\FilterOrderHelper;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use RuntimeException; use RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\Extension\Core\Type\SubmitType; use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Form; use Symfony\Component\Form\Form;
use Symfony\Component\Form\FormInterface; use Symfony\Component\Form\FormInterface;
@ -45,16 +46,20 @@ class UserController extends CRUDController
private ValidatorInterface $validator; private ValidatorInterface $validator;
protected ParameterBagInterface $parameterBag;
public function __construct( public function __construct(
LoggerInterface $chillLogger, LoggerInterface $chillLogger,
ValidatorInterface $validator, ValidatorInterface $validator,
UserPasswordEncoderInterface $passwordEncoder, UserPasswordEncoderInterface $passwordEncoder,
UserRepository $userRepository UserRepository $userRepository,
ParameterBagInterface $parameterBag
) { ) {
$this->logger = $chillLogger; $this->logger = $chillLogger;
$this->userRepository = $userRepository; $this->userRepository = $userRepository;
$this->validator = $validator; $this->validator = $validator;
$this->passwordEncoder = $passwordEncoder; $this->passwordEncoder = $passwordEncoder;
$this->parameterBag = $parameterBag;
} }
/** /**
@ -307,7 +312,7 @@ class UserController extends CRUDController
protected function onPrePersist(string $action, $entity, FormInterface $form, Request $request) protected function onPrePersist(string $action, $entity, FormInterface $form, Request $request)
{ {
// for "new", encode the password // for "new", encode the password
if ('new' === $action) { if ('new' === $action && $this->parameterBag->get('chill_main.access_user_change_password')) {
$entity->setPassword($this->passwordEncoder $entity->setPassword($this->passwordEncoder
->encodePassword($entity, $form['plainPassword']->getData())); ->encodePassword($entity, $form['plainPassword']->getData()));
} }

View File

@ -19,6 +19,7 @@ use Chill\MainBundle\Form\Type\PickCivilityType;
use Chill\MainBundle\Templating\TranslatableStringHelper; use Chill\MainBundle\Templating\TranslatableStringHelper;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType; use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType; use Symfony\Component\Form\Extension\Core\Type\EmailType;
@ -35,9 +36,14 @@ class UserType extends AbstractType
{ {
private TranslatableStringHelper $translatableStringHelper; private TranslatableStringHelper $translatableStringHelper;
public function __construct(TranslatableStringHelper $translatableStringHelper) protected ParameterBagInterface $parameterBag;
{
public function __construct(
TranslatableStringHelper $translatableStringHelper,
ParameterBagInterface $parameterBag
) {
$this->translatableStringHelper = $translatableStringHelper; $this->translatableStringHelper = $translatableStringHelper;
$this->parameterBag = $parameterBag;
} }
public function buildForm(FormBuilderInterface $builder, array $options) 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, [ $builder->add('plainPassword', RepeatedType::class, [
'mapped' => false, 'mapped' => false,
'type' => PasswordType::class, 'type' => PasswordType::class,