I am implement a loging system using FOSUserBundle using groups and roles. I would like to display a dropdown list of role names (role: Bidders and role: drivers) on the user registration form of the FOSUserBundle, where new users can choose which role to apply, How can I do this? Here are my codes:
User Entity
<?php
namespace RemovalsUK\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity(repositoryClass="RemovalsUK\UserBundle\Repository\UserRepository")
*/
class User extends BaseUser
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var ArrayCollection
* @ORM\ManyToMany(targetEntity="RemovalsUK\UserBundle\Entity\Group")
* @ORM\JoinTable(name="fos_user_user_group",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
protected $groups;
/**
* @var array
* @ORM\OneToMany(targetEntity="RemovalsUK\RemovalsBundle\Entity\Post",
* mappedBy="author")
*/
protected $posts;
public function __construct()
{
parent::__construct();
// your own logic
$this->posts = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add post
*
* @param \RemovalsUK\RemovalsBundle\Entity\Post $post
*
* @return User
*/
public function addPost(\RemovalsUK\RemovalsBundle\Entity\Post $post)
{
$this->posts[] = $post;
return $this;
}
/**
* Remove post
*
* @param \RemovalsUK\RemovalsBundle\Entity\Post $post
*/
public function removePost(\RemovalsUK\RemovalsBundle\Entity\Post $post)
{
$this->posts->removeElement($post);
}
/**
* Get posts
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getPosts()
{
return $this->posts;
}
}
Group Entity
<?php
/**
* Created by PhpStorm.
* User: Ezequias
* Date: 25/06/2016
* Time: 19:09
*/
namespace RemovalsUK\UserBundle\Entity;
use FOS\UserBundle\Model\Group as BaseGroup;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="fos_group")
*/
class Group extends BaseGroup
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}
Security.yml
# To get started with security, check out the documentation:
# http://symfony.com/doc/current/book/security.html
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
# http://symfony.com/doc/current/book/security.html#where-do-users-come-from-user-providers
providers:
fos_userbundle:
id: fos_user.user_provider.username
in_memory:
memory: ~
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
login:
pattern: ^/demo/secured/login$
security: false
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_token_generator: security.csrf.token_manager
logout: true
anonymous: true
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/admin/, role: ROLE_ADMIN }
- { path: ^/create, role: ROLE_USER }
- { path: ^/EDIT, role: ROLE_USER }
- { path: ^/delete, role: ROLE_USER }
# activate different ways to authenticate
# http_basic: ~
# http://symfony.com/doc/current/book/security.html#a-configuring-how-your-users-will-authenticate
# form_login: ~
#http://symfony.com/doc/current/cookbook/security/form_login_setup.html
Observations The login is working perfectly fine, I just want to allow users to choose for instance: Role: Bidders or Role: Drivers, when they registering themselves. I'd appreciate if someone could help with this matter. thanks in advance