role field added to user form. Works with select or radio button, not with checkbox...

This commit is contained in:
Julie Lenaerts 2022-03-09 15:49:50 +01:00
parent 1453fdcd18
commit 3ebeda9840
3 changed files with 26 additions and 12 deletions

View File

@ -228,7 +228,7 @@ class User implements AdvancedUserInterface
$roles = $this->roles;
$roles[] = 'ROLE_USER';
return array_unique($roles);
return $roles;
}
public function getSalt(): ?string

View File

@ -19,6 +19,7 @@ use Chill\MainBundle\Templating\TranslatableStringHelper;
use Doctrine\ORM\EntityRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
@ -59,6 +60,16 @@ class UserType extends AbstractType
return $qb;
},
])
->add('roles', ChoiceType::class, [
'required' => true,
'multiple' => true,
'expanded' => true,
'label' => 'Roles',
'choices' => [
'Usager' => 'ROLE_USER',
'Administrateur' => 'ROLE_ADMIN',
],
])
->add('mainScope', EntityType::class, [
'label' => 'Main scope',
'required' => false,
@ -94,6 +105,18 @@ class UserType extends AbstractType
},
]);
$builder->get('roles')
->addModelTransformer(new CallbackTransformer(
function ($rolesArray) {
// transform the array to a string
return count($rolesArray)? $rolesArray[0]: null;
},
function ($rolesString) {
// transform the string back to an array
return [$rolesString];
}
));
if ($options['is_creation']) {
$builder->add('plainPassword', RepeatedType::class, [
'mapped' => false,

View File

@ -22,19 +22,10 @@ use Symfony\Contracts\Translation\TranslatorInterface;
*/
class SectionMenuBuilder implements LocalMenuBuilderInterface
{
/**
* @var AuthorizationCheckerInterface
*/
protected $authorizationChecker;
protected AuthorizationCheckerInterface $authorizationChecker;
/**
* @var TranslatorInterface
*/
protected $translator;
protected TranslatorInterface $translator;
/**
* SectionMenuBuilder constructor.
*/
public function __construct(AuthorizationCheckerInterface $authorizationChecker, TranslatorInterface $translator)
{
$this->authorizationChecker = $authorizationChecker;