groups.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. Using Groups With FOSUserBundle
  2. ===============================
  3. FOSUserBundle allows you to associate groups to your users. Groups are a
  4. way to group a collection of roles. The roles of a group will be granted
  5. to all users belonging to it.
  6. .. note::
  7. Symfony supports role inheritance so inheriting roles from groups is
  8. not always needed. If the role inheritance is enough for your use case,
  9. it is better to use it instead of groups as it is more efficient (loading
  10. the groups triggers the database).
  11. To use the groups, you need to explicitly enable this functionality in your
  12. configuration. The only mandatory configuration is the fully qualified class
  13. name (FQCN) of your ``Group`` class which must implement ``FOS\UserBundle\Model\GroupInterface``.
  14. Below is an example configuration for enabling groups support.
  15. .. configuration-block::
  16. .. code-block:: yaml
  17. # app/config/config.yml
  18. fos_user:
  19. db_driver: orm
  20. firewall_name: main
  21. user_class: AppBundle\Entity\User
  22. group:
  23. group_class: AppBundle\Entity\Group
  24. .. code-block:: xml
  25. <!-- app/config/config.xml -->
  26. <?xml version="1.0" encoding="UTF-8" ?>
  27. <container xmlns="http://symfony.com/schema/dic/services"
  28. xmlns:fos-user="http://friendsofsymfony.github.io/schema/dic/user"
  29. >
  30. <fos_user:config
  31. db-driver="orm"
  32. firewall-name="main"
  33. user-class="AppBundle\Entity\User"
  34. >
  35. <fos_user:group group-class="AppBundle\Entity\Group" />
  36. </fos_user:config>
  37. </container>
  38. The Group class
  39. ---------------
  40. The simplest way to create a Group class is to extend the mapped superclass
  41. provided by the bundle.
  42. a) ORM Group class implementation
  43. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  44. .. configuration-block::
  45. .. code-block:: php-annotations
  46. <?php
  47. // src/AppBundle/Entity/Group.php
  48. namespace AppBundle\Entity;
  49. use FOS\UserBundle\Entity\Group as BaseGroup;
  50. use Doctrine\ORM\Mapping as ORM;
  51. /**
  52. * @ORM\Entity
  53. * @ORM\Table(name="fos_group")
  54. */
  55. class Group extends BaseGroup
  56. {
  57. /**
  58. * @ORM\Id
  59. * @ORM\Column(type="integer")
  60. * @ORM\GeneratedValue(strategy="AUTO")
  61. */
  62. protected $id;
  63. }
  64. .. code-block:: yaml
  65. # src/AppBundle/Resources/config/doctrine/Group.orm.yml
  66. AppBundle\Entity\Group:
  67. type: entity
  68. table: fos_group
  69. id:
  70. id:
  71. type: integer
  72. generator:
  73. strategy: AUTO
  74. .. note::
  75. ``Group`` is a reserved keyword in SQL so it cannot be used as the table name.
  76. b) MongoDB Group class implementation
  77. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  78. .. code-block:: php
  79. <?php
  80. // src/AppBundle/Document/Group.php
  81. namespace AppBundle\Document;
  82. use FOS\UserBundle\Document\Group as BaseGroup;
  83. use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
  84. /**
  85. * @MongoDB\Document
  86. */
  87. class Group extends BaseGroup
  88. {
  89. /**
  90. * @MongoDB\Id(strategy="auto")
  91. */
  92. protected $id;
  93. }
  94. c) CouchDB Group class implementation
  95. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  96. .. code-block:: php
  97. <?php
  98. // src/AppBundle/CouchDocument/Group.php
  99. namespace AppBundle\CouchDocument;
  100. use FOS\UserBundle\Document\Group as BaseGroup;
  101. use Doctrine\ODM\CouchDB\Mapping\Annotations as CouchDB;
  102. /**
  103. * @CouchDB\Document
  104. */
  105. class Group extends BaseGroup
  106. {
  107. /**
  108. * @CouchDB\Id
  109. */
  110. protected $id;
  111. }
  112. Defining the User-Group relation
  113. --------------------------------
  114. The next step is to map the relation in your ``User`` class.
  115. a) ORM User-Group mapping
  116. ~~~~~~~~~~~~~~~~~~~~~~~~~
  117. .. configuration-block::
  118. .. code-block:: php-annotations
  119. <?php
  120. // src/AppBundle/Entity/User.php
  121. namespace AppBundle\Entity;
  122. use FOS\UserBundle\Entity\User as BaseUser;
  123. use Doctrine\ORM\Mapping as ORM;
  124. /**
  125. * @ORM\Entity
  126. * @ORM\Table(name="fos_user")
  127. */
  128. class User extends BaseUser
  129. {
  130. /**
  131. * @ORM\Id
  132. * @ORM\Column(type="integer")
  133. * @ORM\GeneratedValue(strategy="AUTO")
  134. */
  135. protected $id;
  136. /**
  137. * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Group")
  138. * @ORM\JoinTable(name="fos_user_user_group",
  139. * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
  140. * inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
  141. * )
  142. */
  143. protected $groups;
  144. }
  145. .. code-block:: yaml
  146. # src/AppBundle/Resources/config/doctrine/User.orm.yml
  147. AppBundle\Entity\User:
  148. type: entity
  149. table: fos_user
  150. id:
  151. id:
  152. type: integer
  153. generator:
  154. strategy: AUTO
  155. manyToMany:
  156. groups:
  157. targetEntity: Group
  158. joinTable:
  159. name: fos_user_group
  160. joinColumns:
  161. user_id:
  162. referencedColumnName: id
  163. inverseJoinColumns:
  164. group_id:
  165. referencedColumnName: id
  166. .. code-block:: xml
  167. <?xml version="1.0" encoding="UTF-8"?>
  168. <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
  169. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  170. xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
  171. http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">
  172. <entity name="AppBundle\Entity\User" table="fos_user">
  173. <id name="id" column="id" type="integer">
  174. <generator strategy="AUTO" />
  175. </id>
  176. <many-to-many field="groups" target-entity="Group">
  177. <join-table name="fos_user_group">
  178. <join-columns>
  179. <join-column name="user_id" referenced-column-name="id"/>
  180. </join-columns>
  181. <inverse-join-columns>
  182. <join-column name="group_id" referenced-column-name="id" />
  183. </inverse-join-columns>
  184. </join-table>
  185. </many-to-many>
  186. </entity>
  187. </doctrine-mapping>
  188. b) MongoDB User-Group mapping
  189. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  190. .. code-block:: php
  191. <?php
  192. // src/AppBundle/Document/User.php
  193. namespace AppBundle\Document;
  194. use FOS\UserBundle\Document\User as BaseUser;
  195. use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
  196. /**
  197. * @MongoDB\Document
  198. */
  199. class User extends BaseUser
  200. {
  201. /** @MongoDB\Id(strategy="auto") */
  202. protected $id;
  203. /**
  204. * @MongoDB\ReferenceMany(targetDocument="AppBundle\Document\Group")
  205. */
  206. protected $groups;
  207. }
  208. c) CouchDB User-Group mapping
  209. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  210. .. code-block:: php
  211. <?php
  212. // src/AppBundle/CouchDocument/User.php
  213. namespace AppBundle\CouchDocument;
  214. use FOS\UserBundle\Document\User as BaseUser;
  215. use Doctrine\ODM\CouchDB\Mapping\Annotations as CouchDB;
  216. /**
  217. * @CouchDB\Document
  218. */
  219. class User extends BaseUser
  220. {
  221. /**
  222. * @CouchDB\Id
  223. */
  224. protected $id;
  225. /**
  226. * @CouchDB\ReferenceMany(targetDocument="AppBundle\CouchDocument\Group")
  227. */
  228. protected $groups;
  229. }
  230. Enabling the routing for the GroupController
  231. --------------------------------------------
  232. You can import the routing file ``group.xml`` to use the built-in controller to
  233. manipulate groups.
  234. .. code-block:: yaml
  235. # app/config/routing.yml
  236. fos_user_group:
  237. resource: "@FOSUserBundle/Resources/config/routing/group.xml"
  238. prefix: /group