@@ -77,24 +77,15 @@ the user provider uses :doc:`Doctrine </doctrine>` to retrieve them.
77
77
use App\Entity\User;
78
78
use Symfony\Config\SecurityConfig;
79
79
80
- $container->loadFromExtension('security', [
81
- 'providers' => [
82
- 'users' => [
83
- 'entity' => [
84
- // the class of the entity that represents users
85
- 'class' => User::class,
86
- // the property to query by - e.g. email, username, etc
87
- 'property' => 'email',
88
-
89
- // optional: if you're using multiple Doctrine entity
90
- // managers, this option defines which one to use
91
- //'manager_name' => 'customer',
92
- ],
93
- ],
94
- ],
95
-
80
+ return static function (SecurityConfig $security): void {
96
81
// ...
97
- ]);
82
+
83
+ $security->provider('app_user_provider')
84
+ ->entity()
85
+ ->class(User::class)
86
+ ->property('email')
87
+ ;
88
+ };
98
89
99
90
.. _authenticating-someone-with-a-custom-entity-provider :
100
91
@@ -185,18 +176,16 @@ To finish this, remove the ``property`` key from the user provider in
185
176
186
177
// config/packages/security.php
187
178
use App\Entity\User;
179
+ use Symfony\Config\SecurityConfig;
188
180
189
- $container->loadFromExtension('security', [
190
- 'providers' => [
191
- 'users' => [
192
- 'entity' => [
193
- 'class' => User::class,
194
- ],
195
- ],
196
- ],
197
-
181
+ return static function (SecurityConfig $security): void {
198
182
// ...
199
- ]);
183
+
184
+ $security->provider('app_user_provider')
185
+ ->entity()
186
+ ->class(User::class)
187
+ ;
188
+ };
200
189
201
190
Now, whenever Symfony uses the user provider, the ``loadUserByIdentifier() ``
202
191
method on your ``UserRepository `` will be called.
@@ -217,24 +206,78 @@ including their passwords. Make sure the passwords are hashed properly. See
217
206
After setting up hashing, you can configure all the user information in
218
207
``security.yaml ``:
219
208
220
- .. code-block :: yaml
209
+ .. configuration-block ::
210
+
211
+ .. code-block :: yaml
212
+
213
+ # config/packages/security.yaml
214
+ security :
215
+ providers :
216
+ backend_users :
217
+ memory :
218
+ users :
219
+ john_admin : { password: '$2y$13$jxGxc ... IuqDju', roles: ['ROLE_ADMIN'] }
220
+ jane_admin : { password: '$2y$13$PFi1I ... rGwXCZ', roles: ['ROLE_ADMIN', 'ROLE_SUPER_ADMIN'] }
221
+
222
+ # ...
223
+
224
+ .. code-block :: xml
225
+
226
+ <!-- config/packages/security.xml -->
227
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
228
+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
229
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
230
+ xmlns : srv =" http://symfony.com/schema/dic/services"
231
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
232
+ https://symfony.com/schema/dic/services/services-1.0.xsd
233
+ http://symfony.com/schema/dic/security
234
+ https://symfony.com/schema/dic/security/security-1.0.xsd" >
235
+
236
+ <config >
237
+ <!-- ... -->
238
+
239
+ <provider name =" app_user_provider2" >
240
+ <memory >
241
+ <user identifier =" john_admin" password =" $2y$13$jxGxc ... IuqDju" roles =" ROLE_ADMIN" />
242
+ <user identifier =" jane_admin" password =" $2y$13$PFi1I ... rGwXCZ" roles =" ROLE_ADMIN" />
243
+ </memory >
244
+ </provider >
245
+ </config >
246
+ </srv : container >
247
+
248
+ .. code-block :: php
249
+
250
+ // config/packages/security.php
251
+ use App\Entity\User;
252
+ use Symfony\Config\SecurityConfig;
221
253
222
- # config/packages/security.yaml
223
- security :
224
- providers :
225
- backend_users :
226
- memory :
227
- users :
228
- john_admin : { password: '$2y$13$jxGxc ... IuqDju', roles: ['ROLE_ADMIN'] }
229
- jane_admin : { password: '$2y$13$PFi1I ... rGwXCZ', roles: ['ROLE_ADMIN', 'ROLE_SUPER_ADMIN'] }
254
+ return static function (SecurityConfig $security): void {
255
+ // ...
230
256
231
- # ...
257
+ $memoryProvider = $security->provider('app_user_provider')->memory();
258
+ $memoryProvider
259
+ ->user('john_admin')
260
+ ->password('$2y$13$jxGxc ... IuqDju')
261
+ ->roles(['ROLE_ADMIN'])
262
+ ;
263
+
264
+ $memoryProvider
265
+ ->user('jane_admin')
266
+ ->password('$2y$13$PFi1I ... rGwXCZ')
267
+ ->roles(['ROLE_ADMIN', 'ROLE_SUPER_ADMIN'])
268
+ ;
269
+ };
232
270
233
271
.. caution ::
234
272
235
273
When using a ``memory `` provider, and not the ``auto `` algorithm, you have
236
274
to choose an encoding without salt (i.e. ``bcrypt ``).
237
275
276
+ .. note ::
277
+
278
+ You cannot provide multiple roles to a memory user by using the XML config
279
+ format.
280
+
238
281
.. _security-chain-user-provider :
239
282
240
283
Chain User Provider
@@ -246,27 +289,99 @@ providers are configured is important because Symfony will look for users
246
289
starting from the first provider and will keep looking for in the other
247
290
providers until the user is found:
248
291
249
- .. code -block :: yaml
292
+ .. configuration -block ::
250
293
251
- # config/packages/security.yaml
252
- security :
253
- # ...
254
- providers :
255
- backend_users :
256
- ldap :
257
- # ...
294
+ .. code-block :: yaml
258
295
259
- legacy_users :
260
- entity :
261
- # ...
296
+ # config/packages/security.yaml
297
+ security :
298
+ # ...
299
+ providers :
300
+ backend_users :
301
+ ldap :
302
+ # ...
303
+
304
+ legacy_users :
305
+ entity :
306
+ # ...
307
+
308
+ users :
309
+ entity :
310
+ # ...
262
311
263
- users :
264
- entity :
265
- # ...
312
+ all_users :
313
+ chain :
314
+ providers : ['legacy_users', 'users', 'backend_users']
266
315
267
- all_users :
268
- chain :
269
- providers : ['legacy_users', 'users', 'backend_users']
316
+ .. code-block :: xml
317
+
318
+ <!-- config/packages/security.xml -->
319
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
320
+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
321
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
322
+ xmlns : srv =" http://symfony.com/schema/dic/services"
323
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
324
+ https://symfony.com/schema/dic/services/services-1.0.xsd
325
+ http://symfony.com/schema/dic/security
326
+ https://symfony.com/schema/dic/security/security-1.0.xsd" >
327
+
328
+ <config >
329
+ <!-- ... -->
330
+
331
+ <provider name =" backend_users" >
332
+ <ldap service =" ..." base-dn =" ..." />
333
+ </provider >
334
+
335
+ <provider name =" legacy_users" >
336
+ <entity >
337
+ <!-- ... -->
338
+ </entity >
339
+ </provider >
340
+
341
+ <provider name =" users" >
342
+ <entity >
343
+ <!-- ... -->
344
+ </entity >
345
+ </provider >
346
+
347
+ <provider name =" all_users" >
348
+ <chain >
349
+ <provider >backend_users</provider >
350
+ <provider >legacy_users</provider >
351
+ <provider >users</provider >
352
+ </chain >
353
+ </provider >
354
+ </config >
355
+ </srv : container >
356
+
357
+ .. code-block :: php
358
+
359
+ // config/packages/security.php
360
+ use App\Entity\User;
361
+ use Symfony\Config\SecurityConfig;
362
+
363
+ return static function (SecurityConfig $security): void {
364
+ // ...
365
+
366
+ $backendProvider = $security->provider('backend_users')
367
+ ->ldap()
368
+ // ...
369
+ ;
370
+
371
+ $legacyProvider = $security->provider('legacy_users')
372
+ ->entity()
373
+ // ...
374
+ ;
375
+
376
+ $userProvider = $security->provider('users')
377
+ ->entity()
378
+ // ...
379
+ ;
380
+
381
+ $allProviders = $security->provider('all_users')->chain()
382
+ ->providers([$backendProvider, $legacyProvider, $userProvider])
383
+ ;
384
+ };
270
385
271
386
.. _security-custom-user-provider :
272
387
@@ -362,14 +477,52 @@ Most of the work is already done! Read the comments in the code and update the
362
477
TODO sections to finish the user provider. When you're done, tell Symfony about
363
478
the user provider by adding it in ``security.yaml ``:
364
479
365
- .. code-block :: yaml
480
+ .. configuration-block ::
481
+
482
+ .. code-block :: yaml
483
+
484
+
10000
# config/packages/security.yaml
485
+ security :
486
+ providers :
487
+ # the name of your user provider can be anything
488
+ your_custom_user_provider :
489
+ id : App\Security\UserProvider
490
+
491
+ .. code-block :: xml
492
+
493
+ <!-- config/packages/security.xml -->
494
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
495
+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
496
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
497
+ xmlns : srv =" http://symfony.com/schema/dic/services"
498
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
499
+ https://symfony.com/schema/dic/services/services-1.0.xsd
500
+ http://symfony.com/schema/dic/security
501
+ https://symfony.com/schema/dic/security/security-1.0.xsd" >
502
+
503
+ <config >
504
+ <!-- ... -->
505
+
506
+ <provider name =" your_custom_user_provider" id =" App\Security\UserProvider" >
507
+ <!-- ... -->
508
+ </provider >
509
+ </config >
510
+ </srv : container >
511
+
512
+ .. code-block :: php
513
+
514
+ // config/packages/security.php
515
+ use App\Security\UserProvider;
516
+ use Symfony\Config\SecurityConfig;
517
+
518
+ return static function (SecurityConfig $security): void {
519
+ // ...
366
520
367
- # config/packages/security.yaml
368
- security :
369
- providers :
370
- # the name of your user provider can be anything
371
- your_custom_user_provider :
372
- id : App\Security\UserProvider
521
+ $customProvider = $security->provider('your_custom_user_provider')
522
+ ->id(UserProvider::class)
523
+ // ...
524
+ ;
525
+ };
373
526
374
527
Lastly, update the ``config/packages/security.yaml `` file to set the
375
528
``provider `` key to ``your_custom_user_provider `` in all the firewalls which
0 commit comments