0

I'm developing a web application now, i want create 3 roles for 3 entities (Student, teacher, School).

When the user click on inscription he is redirected to a first step he can choose between 3 choices (3 radios boxes) student, teacher or school, after the confirmation he is redirected to another page that contain a form with diferents inputs depending on the first step.

How to do that with Symfony 3? Can I do that with ForUserBundle?

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Mounir Zairig
  • 73
  • 1
  • 8

2 Answers2

0

In the controller of the first step decide where to redirect based on the radio value :

$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
    if( studentRadio is selected ){
        $this->render("student.html.twig")
    } else if (...) {
        ...
    }
    ....
}

FOSUserBundle makes it easy to add roles because the BaseUser class has a method to add roles : $user->addRole("ROLE_STUDENT");

Role name must begin with ROLE_ and must be all uppercase and you can create as many as you want

EDIT :

Roles are just string values that are stored within a $roles field within your User class then you can require a certain role to access certain pages in the security.yml file, when you want to acces a page that requires a role, symfony will check that field to see if the user has the required role

You can create three classes : Student, Teacher and School and have each one of them have a field named $account wich is the default User class from FOSUserBundle and link them with @ORM\OneToOne association and add AccountType in the FormType of those 3 classes, this way in the database each class has a table for it and each object of those class is linked to an account and all accounts are stored in one table

In the first step make the user choose a role, then the website directs him to either Student registration, Teacher registration or School registration depending on what he chose, in their controllers don't forget to use ->addRole("your role"), example : $student->getAccount->addRole("ROLE_STUDENT");

I had a project that i worked on where i have a Person entity with Account entity (which is the default FOSUserBundle User class), the registration was done by PersonType and not by AccountType because i couldn't add the roles i wanted after the default FOSUserBundle's registration form was submitted (i didn't want to modify the FOSUserBundle's controller that handles registration for simplicity reasons)

check out this question i asked that talks about it : Symfony fosuserbundle add Account entity to a person entity

Associations are explained here : http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html

Roles are explained here : http://symfony.com/doc/current/security.html

Community
  • 1
  • 1
paris93
  • 212
  • 1
  • 2
  • 16
  • Hi paris93, thank you for this answer, i just wana ask how can i configure FosUserBundle with 3 roles depending on 3 entities, i already work with this bundle with juste one entity (user) created by default. now what is the perfect method i create the 3 entities and after i configurate FosUserBundle with this entities ? or i create this logique first from FosUserBunle ? – Mounir Zairig Apr 18 '17 at 13:34
  • 1
    i edited my answer to answer your question, i hope it helps, i hope if it helps you would vote me up i'm a bit low on reputation as you can see :( – paris93 Apr 18 '17 at 15:55
  • Hi @paris93, i juste wanna ask about the AccountType if you create this file manually or we don't need to create it in the AppBundle\Form, and in the SchoolType when i add this line : ->add('account',AccountType::class) i must add the use of AccountType ? and how can i get the password input in the registration page ? – Mounir Zairig May 03 '17 at 23:38
  • 1
    yes i created it manually, https://github.com/mkhinini-motaz/pfa/blob/dev2/src/AppBundle/Form there is an example of AccountType file, it's called CompteType(AccountType in french) and it's used by a AbonneType (Abonne = Subscriber in french) ,when AccountType extends from the FOSUserBundle's RegistrationFormType ( with getParent() method ) it will have the password input in it, and no you don't need the "use" because normally AccountType and SchoolType will be in the same folder : AppBundle/Form/ , i hope you can help me with some votes up – paris93 May 04 '17 at 09:17
  • Bonjour merci pour votre réponse, je crois que vous parlez français ça me facilitera la tâche pour communiquer, bon j'ai fait comme ce que vous m'avez dit, j'ai créée trois entités School, Student et Professor dans le même bundle Social Bundle, après j'ai installé FosUserBundle donc dans le bundle AppBundle j'ai crée l'entité User comme ce qu'on a sur la documentation, – Mounir Zairig May 04 '17 at 09:47
  • après j'ai crée l'attribut $account dans les 3 entités School, Student et Professor et je l'ai mi en relation OneToOne avec l'entité User. Maintenant j'ai crée le SchoolType dans lequel je doit précisé le AccountType ici je suis vraiment bloquer, est ce que c'est le UserType que je doit crée dans le Bundle AppBundle ? et il doit hériter de la registrationFormType ? – Mounir Zairig May 04 '17 at 09:47
  • 1
    AccountType = UserType, j'ai juste changé le nom pour des raison de clarité, next time please speak in english there might be some people who need to read this post, if you need help in french i have no problem helping you, it's also easier for me but not everyone in this website would understand us, just leave your email and i'll contact you in private and help you with everything you want – paris93 May 04 '17 at 10:16
  • ok thanks, and i will speak english next time, this is my email : mounirzairig@gmail.com i really need your help to unblock the situation – Mounir Zairig May 04 '17 at 10:20
  • sent you an email – paris93 May 04 '17 at 10:56
0

You could extend the FOS User class with School, Teacher, Student. You could then store user choices in the session and at the end just build the adequate user from the data you got over those steps. However, you could make all of those individual steps on the same page with some clever front end magic or ajax calls that return new forms based on the data if that is needed. If you ask me, the simpler the better. For example you create an object on the role select and update it in every one of the next steps.

  • Hi Marko, thank you for this answer, i just wana ask how can i configure FosUserBundle with 3 roles depending on 3 entities, i already work with this bundle with juste one entity (user) created by default. now what is the perfect method i create the 3 entities and after i configurate FosUserBundle with this entities ? or i create this logique first from FosUserBunle ? and how can i do that ? – Mounir Zairig Apr 18 '17 at 13:35
  • Roles come in form of an array, because this gives great flexibility to your application, role can be anything like a simple permission to have your signature shown on the forum to some high level privilages, what you are looking for is not replacing the ROLE_USER but rather adding a role. Role user just tells you someone is registered, all others are perks and permissions would be handled by additional roles. You can see how to add a role [here](http://stackoverflow.com/questions/20659377/assign-a-role-with-fosuserbundle) but the short version is for example: `$user->addRole("ROLE_ADMIN");` – Marko Živković Apr 19 '17 at 09:41