Gabriel Muñumel
Gabriel Muñumel

Reputation: 1876

FOSUserBundle - Creating new user specifying role

Would like to know how can achieve the creation of a new user but specifying his role. At the moment, when i created a new user the program automatically associated with ROLE_ADMIN role.

Any information/code/example is very welcome.

Upvotes: 0

Views: 14727

Answers (3)

Gabriel Muñumel
Gabriel Muñumel

Reputation: 1876

Ok. I solved my problem. Basically i follow this guide, and changed some aspects.

  1. On my model, i added the field for roles: protected $roles = array();
  2. Changed my Form/Type/RegistrationFormType.php and put the following control:

Code:

$user = new UserAdmin();
    $builder->add('roles', 'collection', array(
        'type'   => 'choice',
        'options'  => array(
            'choices'  => array(
                'nashville' => 'Nashville',
                'paris'     => 'Paris',
                'berlin'    => 'Berlin',
                'london'    => 'London',
            ),
        ),
));

Now when i opened my register view it shows a dropdownlist. In this case the options are: Nashville, Paris, Berlin, London.

Upvotes: 6

Sifeng
Sifeng

Reputation: 719

A solution of James Morris could be helpful: http://blog.jmoz.co.uk/symfony2-fosuserbundle-roles

More discuss see: Symfony2 FOSUserBundle Role entities

Upvotes: 1

Zeljko
Zeljko

Reputation: 5158

This is the code I used:

    $admin = new User() ;
    $admin->setEmail("[email protected]") ;
    $admin->setUsername("mitke") ;
    $admin->setPlainPassword("123123123") ;
    $admin->setEnabled(true) ;
    $admin->setSuperAdmin(true) ;

    $user1 = new User() ;
    $user1->setEmail("[email protected]") ;
    $user1->setUsername("user1") ;
    $user1->setPlainPassword("123123123") ;
    $user1->setEnabled(true) ;
    $user1->setRoles( array(User::ROLE_DEFAULT) ) ;

Maybe it can help you, it works for me.

Question: Does all users you create using /register has admin role? Maybe FOS decides that first registered user will be admin by default.

Upvotes: 4

Related Questions