Y
Y
Yuri Matveenko2017-08-14 18:17:40
symfony
Yuri Matveenko, 2017-08-14 18:17:40

FOSUserBundle registration with the role of choice. How to implement?

From the title, I think everything is clear, but I will explain.
There is Symfony with FOSUSerBundle and when registering, I want to give the user a choice of which role he prefers. And I'm not talking about standard roles like ROLE_USER and ROLE_ADMIN.
I need to create roles. How and where?
Next, when registering, you need to make a radio switch and allow the user to select a role. How?
I can handle the switch, but I don’t know how to set the role.
Thanks

Answer the question

In order to leave comments, you need to log in

3 answer(s)
G
GTRxShock, 2017-08-14
@GTRxShock

Roles are a kind of shortcuts for users, by themselves they mean nothing and are just a text value, but with the ability to inherit and play with access differences in the code, you can do anything)

security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_MANAGER:       ROLE_USER
        ROLE_HEAD_MANAGER:  ROLE_MANAGER
        ROLE_ADMIN:         [ROLE_HEAD_MANAGER, ROLE_SONATA_ADMIN, ROLE_ALLOWED_TO_SWITCH]
        ROLE_SUPER_ADMIN:   [ROLE_ADMIN]

A
Alexey Skobkin, 2017-08-14
@skobkin

I guess you don't need roles, but groups.
Or if, after all, the roles, then here .

V
Vaalel, 2017-08-18
@Valel

Add a field to your user's entity, reload the registration form, and you're done.

<?php
namespace UserBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('Name', 'text', array('label'=>'First name'));
        $builder->add('Surname', 'text', array('label'=>'Last name'));
        $builder->add('YearOfBirth', 'text', array('label'=>'Year of birth'));
        $builder->add('Type',null, array('label'=>"Role",'placeholder'=>'Choose role'));
        $builder->add('Country', 'text', array('label'=>'Your country'));
    }
    public function getParent()
    {
        return 'fos_user_registration';
    }
    public function getName()
    {
        return 'app_user_registration';
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question