Reputation: 1876
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
Reputation: 1876
Ok. I solved my problem. Basically i follow this guide, and changed some aspects.
protected $roles = array();
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
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
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