How to let the user decide what role he wants when he registers in Symfony

How to let the user decide what role he wants when he registers in Symfony

I have a User in my project, I want it to be able to chose an additional role when he registers that would be ROLE_PROFESSOR, so the actual would be a ROLE_USER no matter what he choses, but he would additionally be a ROLE_PROFESSOR. However I’m having issues trying to give the User the PROFESSOR role. This is I’ve done so far :

My Registration page :

{% extends 'base.html.twig' %}

{% block title %}Hello RegistrationController!{% endblock %}

{% block body %}

<div class="container">
    <div class="row">
        <div class="col-md-10 ml-md-auto">
            <div class="card bg-light mb-3 mt-5" style="800px;">
                <div class="card-body">
                    <div class="card-header mb-3">Registration Form</div>
                    {{ form_start(form) }}
                    <div class="form_group">
                        <div class="col-md-12 mb-3">
                            {{ form_row(form.nom, {'attr' : {'class':'form-control'}}) }}
                        </div>
                    </div>
                    <div class="form_group">
                        <div class="col-md-12 mb-3">
                            {{ form_row(form.prenom, {'attr': {'class':'form-control'}}) }}
                        </div>
                    </div>
                    <div class="form_group">
                        <div class="col-md-12 mb-3">
                            {{ form_row(form.username, {'attr': {'class':'form-control'}}) }}
                        </div>
                    </div>
                    <div class="form_group">
                        <div class="col-md-12 mb-3">
                            {{form_row(form.password.first, {'attr' : {'class':'form-control'}}) }}
                        </div>
                    </div>
                    <div class="form_group">
                        <div class="col-md-12 mb-3">
                            {{form_row(form.password.second, {'attr': {'class':'form-control'}}) }}
                        </div>
                    </div>
                    <div class="form_group">
                        <div class="col-md-12 mb-3">
                            {{ form_row(form.roles, {'attr':{'class':'form-control'}}) }}
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-md-8 col-md-offset-4" style="margin-top:5px;">
                            <button class="btn btn-primary" type="submit">
                                <i class="fa fa-btn fa-user">Register</i>
                            </button>
                        </div>
                    </div>
                    {{form_end(form) }}
                </div>
            </div>
        </div>
    </div>
</div>

{% endblock %}

This is what my User class looks like :

<?php

namespace AppEntity;

use AppRepositoryUserRepository;
use DoctrineORMMapping as ORM;
use SymfonyComponentSecurityCoreUserUserInterface;

/**
 * @ORMEntity(repositoryClass=UserRepository::class)
 */
class User implements UserInterface
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=180, unique=true)
     */
    private $username;

    /**
     * @ORMColumn(type="json")
     */
    private $roles = [];

    /**
     * @var string The hashed password
     * @ORMColumn(type="string")
     */
    private $password;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $nom;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $prenom;

    public function getId(): ?int
    {
        return $this->id;
    }

    /**
     * A visual identifier that represents this user.
     *
     * @see UserInterface
     */
    public function getUsername(): string
    {
        return (string) $this->username;
    }

    public function setUsername(string $username): self
    {
        $this->username = $username;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getRoles(): array
    {
        $roles = $this->roles;
        // guarantee every user at least has ROLE_USER
        $roles[] = 'ROLE_USER';

        return array_unique($roles);
    }

    public function setRoles(array $roles): self
    {
        $this->roles = $roles;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getPassword(): string
    {
        return (string) $this->password;
    }

    public function setPassword(string $password): self
    {
        $this->password = $password;

        return $this;
    }

    /**
     * @see UserInterface
     */
    public function getSalt()
    {
        // not needed when using the "bcrypt" algorithm in security.yaml
    }

    /**
     * @see UserInterface
     */
    public function eraseCredentials()
    {
        // If you store any temporary, sensitive data on the user, clear it here
        // $this->plainPassword = null;
    }

    public function getNom(): ?string
    {
        return $this->nom;
    }

    public function setNom(string $nom): self
    {
        $this->nom = $nom;

        return $this;
    }

    public function getPrenom(): ?string
    {
        return $this->prenom;
    }

    public function setPrenom(string $prenom): self
    {
        $this->prenom = $prenom;

        return $this;
    }
}

My UserType:

<?php

namespace AppForm;

use AppEntityUser;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeChoiceType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentValidatorConstraintsLength;
use SymfonyComponentValidatorConstraintsNotBlank;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormExtensionCoreTypePasswordType;
use SymfonyComponentFormExtensionCoreTypeRepeatedType;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('username', TextType::class)
            ->add('password', RepeatedType::class, [
                'type' => PasswordType::class,
                'first_options' => ['label' => 'Password'],
                'second_options' => ['label' => 'Confirm Password']
            ])
            ->add('nom', TextType::class, [
                'constraints' => [
                    new NotBlank([
                        'message' => 'Please enter a name'
                    ])
                ]
            ])
            ->add('prenom', TextType::class, [
                'constraints' => [
                    new NotBlank([
                        'message' => 'Please enter a first name'
                    ])
                ]
            ])
            ->add('roles', ChoiceType::class, [
                'multiple' => true,
                'required' => true,
                'expanded' => true,
                'choices' => [
                    'Utilisateur' => 'ROLE_USER',
                    'Professeur' => 'ROLE_PROFESSEUR'
                ]
            ])
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => User::class,
        ]);
    }
}

And finally, my RegistrationController :

<?php

namespace AppController;

use AppEntityUser;
use AppFormUserType;
use SymfonyComponentHttpFoundationRequest;
use SymfonyComponentRoutingAnnotationRoute;
use SymfonyBundleFrameworkBundleControllerAbstractController;
use SymfonyComponentSecurityCoreEncoderUserPasswordEncoderInterface;

class RegistrationController extends AbstractController
{   
    private $passwordEncoder;

    public function __construct(UserPasswordEncoderInterface $passwordEncoder)
    {
        $this->passwordEncoder = $passwordEncoder;
    }
    /**
     * @Route("/registration", name="registration")
     */
    public function index(Request $request)
    {
        $user = new User();

        $form = $this->createForm(UserType::class, $user);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()){
            $user->setPassword($this->passwordEncoder->encodePassword($user, $user->getPassword()));

            $user->setRoles(['ROLE_USER']);

            $em = $this->getDoctrine()->getManager();
            $em->persist($user);
            $em->flush();

            return $this->redirectToRoute('app_login');
        }

        return $this->render('registration/index.html.twig', [
            'form' => $form->createView()
        ]);
    }
}

I’d say the problem is coming from the RegistrationController, especially this line: setRoles(['ROLE_USER']), it doesn’t take in consideration the choice made by the user maybe? Other than that I don’t really know what I could do here.

Source: Symfony Questions

Leave a Reply

Your email address will not be published. Required fields are marked *