|
3
|
1 |
Using Groups With FOSUserBundle |
|
|
2 |
=============================== |
|
|
3 |
|
|
|
4 |
The FOSUserBundle allows you to optionally use groups. You need to explicitly |
|
|
5 |
enable this functionality in your configuration by specifying the fully |
|
|
6 |
qualified class name (FQCN) of your `Group` class which must implement |
|
|
7 |
`FOS\UserBundle\Model\GroupInterface`. |
|
|
8 |
|
|
|
9 |
Below is an example configuration for enabling groups support. |
|
|
10 |
|
|
|
11 |
In YAML: |
|
|
12 |
|
|
|
13 |
``` yaml |
|
|
14 |
# app/config/config.yml |
|
|
15 |
fos_user: |
|
|
16 |
db_driver: orm |
|
|
17 |
firewall_name: main |
|
|
18 |
user_class: Acme\UserBundle\Entity\User |
|
|
19 |
group: |
|
|
20 |
group_class: Acme\UserBundle\Entity\Group |
|
|
21 |
``` |
|
|
22 |
|
|
|
23 |
Or if you prefer XML: |
|
|
24 |
|
|
|
25 |
``` xml |
|
|
26 |
# app/config/config.xml |
|
|
27 |
<fos_user:config |
|
|
28 |
db-driver="orm" |
|
|
29 |
firewall-name="main" |
|
|
30 |
user-class="Acme\UserBundle\Entity\User" |
|
|
31 |
> |
|
|
32 |
<fos_user:group group-class model="Acme\UserBundle\Entity\Group" /> |
|
|
33 |
</fos_user:config> |
|
|
34 |
``` |
|
|
35 |
### The Group class |
|
|
36 |
|
|
|
37 |
The simpliest way to create a Group class is to extend the mapped superclass |
|
|
38 |
provided by the bundle. |
|
|
39 |
|
|
|
40 |
**a) ORM Group class implementation** |
|
|
41 |
|
|
|
42 |
``` php |
|
|
43 |
// src/MyProject/MyBundle/Entity/Group.php |
|
|
44 |
<?php |
|
|
45 |
|
|
|
46 |
namespace MyProject\MyBundle\Entity; |
|
|
47 |
|
|
|
48 |
use FOS\UserBundle\Entity\Group as BaseGroup; |
|
|
49 |
use Doctrine\ORM\Mapping as ORM; |
|
|
50 |
|
|
|
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 |
``` |
|
|
65 |
|
|
|
66 |
**Note:** `Group` is a reserved keyword in SQL so it cannot be used as the table name. |
|
|
67 |
|
|
|
68 |
**b) MongoDB Group class implementation** |
|
|
69 |
|
|
|
70 |
``` php |
|
|
71 |
// src/MyProject/MyBundle/Document/Group.php |
|
|
72 |
<?php |
|
|
73 |
|
|
|
74 |
namespace MyProject\MyBundle\Document; |
|
|
75 |
|
|
|
76 |
use FOS\UserBundle\Document\Group as BaseGroup; |
|
|
77 |
use Doctrine\ODM\MongoDB\Mapping as MongoDB; |
|
|
78 |
|
|
|
79 |
/** |
|
|
80 |
* @MongoDB\Document |
|
|
81 |
*/ |
|
|
82 |
class Group extends BaseGroup |
|
|
83 |
{ |
|
|
84 |
/** |
|
|
85 |
* @MongoDB\Id(strategy="auto") |
|
|
86 |
*/ |
|
|
87 |
protected $id; |
|
|
88 |
} |
|
|
89 |
``` |
|
|
90 |
|
|
|
91 |
**c) CouchDB Group class implementation** |
|
|
92 |
|
|
|
93 |
``` php |
|
|
94 |
// src/MyProject/MyBundle/Document/Group.php |
|
|
95 |
<?php |
|
|
96 |
|
|
|
97 |
namespace MyProject\MyBundle\Document; |
|
|
98 |
|
|
|
99 |
use FOS\UserBundle\Document\Group as BaseGroup; |
|
|
100 |
use Doctrine\ODM\CouchDB\Mapping as MongoDB; |
|
|
101 |
|
|
|
102 |
/** |
|
|
103 |
* @CouchDB\Document |
|
|
104 |
*/ |
|
|
105 |
class Group extends BaseGroup |
|
|
106 |
{ |
|
|
107 |
/** |
|
|
108 |
* @CouchDB\Id |
|
|
109 |
*/ |
|
|
110 |
protected $id; |
|
|
111 |
} |
|
|
112 |
``` |
|
|
113 |
|
|
|
114 |
### Defining the User-Group relation |
|
|
115 |
|
|
|
116 |
The next step is to map the relation in your `User` class. |
|
|
117 |
|
|
|
118 |
**a) ORM User-Group mapping** |
|
|
119 |
|
|
|
120 |
``` php |
|
|
121 |
// src/MyProject/MyBundle/Entity/User.php |
|
|
122 |
<?php |
|
|
123 |
|
|
|
124 |
namespace MyProject\MyBundle\Entity; |
|
|
125 |
|
|
|
126 |
use FOS\UserBundle\Entity\User as BaseUser; |
|
|
127 |
use Doctrine\ORM\Mapping as ORM; |
|
|
128 |
|
|
|
129 |
/** |
|
|
130 |
* @ORM\Entity |
|
|
131 |
* @ORM\Table(name="fos_user") |
|
|
132 |
*/ |
|
|
133 |
class User extends BaseUser |
|
|
134 |
{ |
|
|
135 |
/** |
|
|
136 |
* @ORM\Id |
|
|
137 |
* @ORM\Column(type="integer") |
|
|
138 |
* @ORM\generatedValue(strategy="AUTO") |
|
|
139 |
*/ |
|
|
140 |
protected $id; |
|
|
141 |
|
|
|
142 |
/** |
|
|
143 |
* @ORM\ManyToMany(targetEntity="MyProject\MyBundle\Entity\Group") |
|
|
144 |
* @ORM\JoinTable(name="fos_user_user_group", |
|
|
145 |
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, |
|
|
146 |
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")} |
|
|
147 |
* ) |
|
|
148 |
*/ |
|
|
149 |
protected $groups; |
|
|
150 |
} |
|
|
151 |
``` |
|
|
152 |
|
|
|
153 |
**b) MongoDB User-Group mapping** |
|
|
154 |
|
|
|
155 |
``` php |
|
|
156 |
// src/MyProject/MyBundle/Document/User.php |
|
|
157 |
<?php |
|
|
158 |
|
|
|
159 |
namespace MyProject\MyBundle\Document; |
|
|
160 |
|
|
|
161 |
use FOS\UserBundle\Document\User as BaseUser; |
|
|
162 |
use Doctrine\ODM\MongoDB\Mapping as MongoDB; |
|
|
163 |
|
|
|
164 |
/** |
|
|
165 |
* @MongoDB\Document |
|
|
166 |
*/ |
|
|
167 |
class User extends BaseUser |
|
|
168 |
{ |
|
|
169 |
/** @MongoDB\Id(strategy="auto") */ |
|
|
170 |
protected $id; |
|
|
171 |
|
|
|
172 |
/** |
|
|
173 |
* @MongoDB\ReferenceMany(targetDocument="MyProject\MyBundle\Document\Group") |
|
|
174 |
*/ |
|
|
175 |
protected $groups; |
|
|
176 |
} |
|
|
177 |
``` |
|
|
178 |
|
|
|
179 |
**c) CouchDB User-Group mapping** |
|
|
180 |
|
|
|
181 |
``` php |
|
|
182 |
// src/MyProject/MyBundle/Document/User.php |
|
|
183 |
<?php |
|
|
184 |
|
|
|
185 |
namespace MyProject\MyBundle\Document; |
|
|
186 |
|
|
|
187 |
use FOS\UserBundle\Document\User as BaseUser; |
|
|
188 |
use Doctrine\ODM\CouchDB\Mapping as CouchDB; |
|
|
189 |
|
|
|
190 |
/** |
|
|
191 |
* @CouchDB\Document |
|
|
192 |
*/ |
|
|
193 |
class User extends BaseUser |
|
|
194 |
{ |
|
|
195 |
/** |
|
|
196 |
* @CouchDB\Id |
|
|
197 |
*/ |
|
|
198 |
protected $id; |
|
|
199 |
|
|
|
200 |
/** |
|
|
201 |
* @CouchDB\ReferenceMany(targetDocument="MyProject\MyBundle\Document\Group") |
|
|
202 |
*/ |
|
|
203 |
protected $groups; |
|
|
204 |
} |
|
|
205 |
``` |
|
|
206 |
|
|
|
207 |
### Enabling the routing for the GroupController |
|
|
208 |
|
|
|
209 |
You can import the routing file `group.xml` to use the built-in controller to |
|
|
210 |
manipulate groups. |
|
|
211 |
|
|
|
212 |
In YAML: |
|
|
213 |
|
|
|
214 |
``` yaml |
|
|
215 |
# app/config/routing.yml |
|
|
216 |
fos_user_group: |
|
|
217 |
resource: "@FOSUserBundle/Resources/config/routing/group.xml" |
|
|
218 |
prefix: /group |
|
|
219 |
|
|
|
220 |
``` |
|
|
221 |
|
|
|
222 |
About FOSUserBundle User Manager Service |
|
|
223 |
======================================== |
|
|
224 |
|
|
|
225 |
FOSUserBundle works with both ORM and ODM. To make this possible, it wraps |
|
|
226 |
all the operation on users in a UserManager. The user manager is configured |
|
|
227 |
as a service in the container. |
|
|
228 |
|
|
|
229 |
If you configure the db_driver to `orm`, this service is an instance of |
|
|
230 |
`FOS\UserBundle\Entity\UserManager`. |
|
|
231 |
|
|
|
232 |
If you configure the db_driver to `mongodb`, this service is an instance of |
|
|
233 |
`FOS\UserBundle\Document\UserManager`. |
|
|
234 |
|
|
|
235 |
If you configure the db_driver to `couchdb`, this service is an instance of |
|
|
236 |
`FOS\UserBundle\CouchDocument\UserManager`. |
|
|
237 |
|
|
|
238 |
All these classes implement `FOS\UserBundle\Model\UserManagerInterface`. |
|
|
239 |
|
|
|
240 |
### Access the User Manager service |
|
|
241 |
|
|
|
242 |
If you want to manipulate users in a way that will work as well with |
|
|
243 |
ORM and ODM, use the `fos_user.user_manager` service. |
|
|
244 |
|
|
|
245 |
``` php |
|
|
246 |
$userManager = $container->get('fos_user.user_manager'); |
|
|
247 |
``` |
|
|
248 |
|
|
|
249 |
That's the way FOSUserBundle's internal controllers are built. |
|
|
250 |
|
|
|
251 |
## Create a new User |
|
|
252 |
|
|
|
253 |
A new instance of your User class can be created by the user manager. |
|
|
254 |
|
|
|
255 |
``` php |
|
|
256 |
$user = $userManager->createUser(); |
|
|
257 |
``` |
|
|
258 |
|
|
|
259 |
`$user` is now an Entity or a Document, depending on the configuration. |
|
|
260 |
|
|
|
261 |
### Updating a User object |
|
|
262 |
|
|
|
263 |
When creating or updating a User object you need to update the encoded password |
|
|
264 |
and the canonical fields. To make it easier, the bundle comes with a Doctrine |
|
|
265 |
listener handling this for you behind the scenes. |
|
|
266 |
|
|
|
267 |
If you don't want to use the Doctrine listener, you can disable it. In this case |
|
|
268 |
you will have to call the `updateUser` method of the user manager each time |
|
|
269 |
you make a change to your entity. |
|
|
270 |
|
|
|
271 |
In YAML: |
|
|
272 |
|
|
|
273 |
``` yaml |
|
|
274 |
# app/config/config.yml |
|
|
275 |
fos_user: |
|
|
276 |
# ... |
|
|
277 |
user_class: MyProject\MyBundle\Entity\User |
|
|
278 |
``` |
|
|
279 |
|
|
|
280 |
Or if you prefer XML: |
|
|
281 |
|
|
|
282 |
``` xml |
|
|
283 |
# app/config/config.xml |
|
|
284 |
<fos_user:config |
|
|
285 |
db-driver="orm" |
|
|
286 |
firewall-name="main" |
|
|
287 |
use-listener="false" |
|
|
288 |
user-class="MyProject\MyBundle\Entity\User" |
|
|
289 |
/> |
|
|
290 |
``` |
|
|
291 |
|
|
|
292 |
The default behavior is to flush the changes when calling the `updateUser` method. |
|
|
293 |
You can disable the flush when using the ORM and the MongoDB implementations by |
|
|
294 |
passing a second argument set to `false`. |
|
|
295 |
|
|
|
296 |
An ORM example: |
|
|
297 |
|
|
|
298 |
``` php |
|
|
299 |
public function MainController extends Controller |
|
|
300 |
{ |
|
|
301 |
public function updateAction($id) |
|
|
302 |
{ |
|
|
303 |
$user = // get a user from the datastore |
|
|
304 |
|
|
|
305 |
$user->setEmail($newEmail); |
|
|
306 |
|
|
|
307 |
$this->get('fos_user.user_manager')->updateUser($user, false); |
|
|
308 |
|
|
|
309 |
// make more modifications to the database |
|
|
310 |
|
|
|
311 |
$this->getDoctrine()->getEntityManager()->flush(); |
|
|
312 |
} |
|
|
313 |
} |
|
|
314 |
``` |
|
|
315 |
|
|
|
316 |
Overriding Default User Manager |
|
|
317 |
=============================== |
|
|
318 |
|
|
|
319 |
You can replace the default implementation of the user manager by defining |
|
|
320 |
a service implementing `FOS\UserBundle\Model\UserManagerInterface` and |
|
|
321 |
setting its id in the configuration. |
|
|
322 |
|
|
|
323 |
In YAML: |
|
|
324 |
|
|
|
325 |
``` yaml |
|
|
326 |
fos_user: |
|
|
327 |
# ... |
|
|
328 |
service: |
|
|
329 |
user_manager: custom_user_manager_id |
|
|
330 |
``` |
|
|
331 |
|
|
|
332 |
Overriding Default FOSUserBundle Validation |
|
|
333 |
=========================================== |
|
|
334 |
|
|
|
335 |
The `Resources/config/validation.xml` file contains definitions for custom |
|
|
336 |
validator rules for various classes. The rules defined by FOSUserBundle are |
|
|
337 |
all in validation groups so you can choose not to use them. |
|
|
338 |
|
|
|
339 |
Form Types |
|
|
340 |
========== |
|
|
341 |
|
|
|
342 |
## The username Form Type |
|
|
343 |
|
|
|
344 |
The bundle also provides a convenient username form type. |
|
|
345 |
It appears as a text input, accepts usernames and convert them to a User instance. |
|
|
346 |
|
|
|
347 |
You can enable this feature from the configuration. |
|
|
348 |
|
|
|
349 |
In YAML: |
|
|
350 |
|
|
|
351 |
``` yaml |
|
|
352 |
# app/config/config.yml |
|
|
353 |
fos_user: |
|
|
354 |
use_username_form_type: true |
|
|
355 |
``` |
|
|
356 |
|
|
|
357 |
And then use it in your forms. |
|
|
358 |
|
|
|
359 |
``` php |
|
|
360 |
class MessageFormType extends AbstractType |
|
|
361 |
{ |
|
|
362 |
public function buildForm(FormBuilder $builder, array $options) |
|
|
363 |
{ |
|
|
364 |
$builder->add('recipient', 'fos_user_username'); |
|
|
365 |
} |
|
|
366 |
``` |
|
|
367 |
|
|
|
368 |
FOSUserBundle Emails |
|
|
369 |
==================== |
|
|
370 |
|
|
|
371 |
The default mailer relies on Swiftmailer to send the mails of the bundle. |
|
|
372 |
If you want to use another mailer in your project you can change it by defining |
|
|
373 |
your own service implementing `FOS\UserBundle\Mailer\MailerInterface` and |
|
|
374 |
setting its id in the configuration. |
|
|
375 |
|
|
|
376 |
In YAML: |
|
|
377 |
|
|
|
378 |
``` yaml |
|
|
379 |
fos_user: |
|
|
380 |
# ... |
|
|
381 |
service: |
|
|
382 |
mailer: custom_mailer_id |
|
|
383 |
``` |
|
|
384 |
|
|
|
385 |
This bundle comes with two mailer implementations. |
|
|
386 |
|
|
|
387 |
- `fos_user.mailer.default` is the default implementation, and uses Swiftmailer to send emails. |
|
|
388 |
- `fos_user.mailer.noop` does nothing and can be used if your project does not depend on Swiftmailer. |
|
|
389 |
|
|
|
390 |
FOSUserBundle Canonicalization |
|
|
391 |
============================== |
|
|
392 |
|
|
|
393 |
`Canonicalizer` services are used to canonicalize the username and the email |
|
|
394 |
fields for database storage. By default, username and email fields are |
|
|
395 |
canonicalized in the same manner using `mb_convert_case()`. You may configure |
|
|
396 |
your own class for each field provided it implements |
|
|
397 |
`FOS\UserBundle\Util\CanonicalizerInterface`. |
|
|
398 |
|
|
|
399 |
**Note:** |
|
|
400 |
|
|
|
401 |
``` |
|
|
402 |
If you do not have the mbstring extension installed you will need to define your |
|
|
403 |
own `canonicalizer`. |
|
|
404 |
``` |