FosUserBundle et utilisation des rôles
Bonjour,
Je souhaiterais pouvoir récupérer la liste des rôles dans le formulaire d'enregistrement des utilisateurs.
Une idée ?
Merci.
P.S : Je n'ai pas crée d'entité "role"
________________________________________________________
Je réponds à ma question :
Entité User : https://github.com/FriendsOfSymfony/...s/doc/index.md
Entité Group : https://github.com/FriendsOfSymfony/.../doc/groups.md
User
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| <?php
namespace MyApp\SecurityBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="users")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="MyApp\SecurityBundle\Entity\Group")
* @ORM\JoinTable(name="users_groups",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
*/
protected $groups;
public function __construct()
{
parent::__construct();
$this->groups = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Add groups
*
* @param MyApp\SecurityBundle\Entity\Group $groups
*/
public function addGroups(\MyApp\SecurityBundle\Entity\Group $groups)
{
$this->groups[] = $groups;
}
/**
* Get groups
*
* @return Doctrine\Common\Collections\Collection
*/
public function getGroups()
{
return $this->groups;
}
} |
Group
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <?php
namespace MyApp\SecurityBundle\Entity;
use FOS\UserBundle\Entity\Group as BaseGroup;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="groups")
*/
class Group extends BaseGroup
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\generatedValue(strategy="AUTO")
*/
protected $id;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
} |
Il suffit d'ajouter les rôles dans le champ roles de la table groupe avec serialize() et remplir le champ name.
L'utilisation est la même pour checker les roles avec is_granted()