@@ -77,24 +77,15 @@ the user provider uses :doc:`Doctrine </doctrine>` to retrieve them.
7777 use App\Entity\User;
7878 use Symfony\Config\SecurityConfig;
7979
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 {
9681 // ...
97- ]);
82+
83+ $security->provider('app_user_provider')
84+ ->entity()
85+ ->class(User::class)
86+ ->property('email')
87+ ;
88+ };
9889
9990 .. _authenticating-someone-with-a-custom-entity-provider :
10091
@@ -174,18 +165,16 @@ To finish this, remove the ``property`` key from the user provider in
174165
175166 // config/packages/security.php
176167 use App\Entity\User;
168+ use Symfony\Config\SecurityConfig;
177169
178- $container->loadFromExtension('security', [
179- 'providers' => [
180- 'users' => [
181- 'entity' => [
182- 'class' => User::class,
183- ],
184- ],
185- ],
186-
170+ return static function (SecurityConfig $security): void {
187171 // ...
188- ]);
172+
173+ $security->provider('app_user_provider')
174+ ->entity()
175+ ->class(User::class)
176+ ;
177+ };
189178
190179 Now, whenever Symfony uses the user provider, the ``loadUserByIdentifier() ``
191180method on your ``UserRepository `` will be called.
@@ -206,18 +195,67 @@ including their passwords. Make sure the passwords are hashed properly. See
206195After setting up hashing, you can configure all the user information in
207196``security.yaml ``:
208197
209- .. code -block :: yaml
198+ .. configuration -block ::
210199
211- # config/packages/security.yaml
212- security :
213- providers :
214- backend_users :
215- memory :
216- users :
217- john_admin : { password: '$2y$13$jxGxc ... IuqDju', roles: ['ROLE_ADMIN'] }
218- jane_admin : { password: '$2y$13$PFi1I ... rGwXCZ', roles: ['ROLE_ADMIN', 'ROLE_SUPER_ADMIN'] }
200+ .. code-block :: yaml
219201
220- # ...
202+ # config/packages/security.yaml
203+ security :
204+ providers :
205+ backend_users :
206+ memory :
207+ users :
208+ john_admin : { password: '$2y$13$jxGxc ... IuqDju', roles: ['ROLE_ADMIN'] }
209+ jane_admin : { password: '$2y$13$PFi1I ... rGwXCZ', roles: ['ROLE_ADMIN', 'ROLE_SUPER_ADMIN'] }
210+
211+ # ...
212+
213+ .. code-block :: xml
214+
215+ <!-- config/packages/security.xml -->
216+ <?xml version =" 1.0" encoding =" UTF-8" ?>
217+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
218+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
219+ xmlns : srv =" http://symfony.com/schema/dic/services"
220+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
221+ https://symfony.com/schema/dic/services/services-1.0.xsd
222+ http://symfony.com/schema/dic/security
223+ https://symfony.com/schema/dic/security/security-1.0.xsd" >
224+
225+ <config >
226+ <!-- ... -->
227+
228+ <provider name =" app_user_provider2" >
229+ <memory >
230+ <user identifier =" john_admin" password =" $2y$13$jxGxc ... IuqDju" roles =" ROLE_ADMIN" />
231+ <user identifier =" jane_admin" password =" $2y$13$PFi1I ... rGwXCZ" roles =" ROLE_ADMIN, ROLE_SUPER_ADMIN" />
232+ </memory >
233+ </provider >
234+ </config >
235+ </srv : container >
236+
237+ .. code-block :: php
238+
239+ // config/packages/security.php
240+ use App\Entity\User;
241+ use Symfony\Config\SecurityConfig;
242+
243+ return static function (SecurityConfig $security): void {
244+ // ...
245+
246+ $memoryProvider = $security->provider('app_user_provider')->memory();
247+ $memoryProvider
248+ ->user('john_admin')
249+ ->password('$2y$13$jxGxc ... IuqDju')
250+ ->roles(['ROLE_ADMIN'])
251+ ;
252+
253+ $memoryProvider
254+ ->user('jane_admin')
255+ ->password('$2y$13$PFi1I ... rGwXCZ')
256+ ->roles(['ROLE_ADMIN', 'ROLE_SUPER_ADMIN'])
257+ ;
258+ };
221259
222260 .. caution ::
223261
@@ -235,27 +273,99 @@ providers are configured is important because Symfony will look for users
235273starting from the first provider and will keep looking for in the other
236274providers until the user is found:
237275
238- .. code-block :: yaml
276+ .. configuration-block ::
277+
278+ .. code-block :: yaml
279+
280+ # config/packages/security.yaml
281+ security :
282+ # ...
283+ providers :
284+ backend_users :
285+ ldap :
286+ # ...
287+
288+ legacy_users :
289+ entity :
290+ # ...
239291
240- # config/packages/security.yaml
241- security :
242- # ...
243- providers :
244- backend_users :
245- ldap :
246- # ...
292+ users :
293+ entity :
294+ # ...
247295
248- legacy_users :
249- entity :
250- # ...
296+ all_users :
297+ chain :
298+ providers : ['legacy_users', 'users', 'backend_users']
251299
252- users :
253- entity :
254- # ...
300+ .. code-block :: xml
255301
256- all_users :
257- chain :
258- providers : ['legacy_users', 'users', 'backend_users']
302+ <!-- config/packages/security.xml -->
303+ <?xml version =" 1.0" encoding =" UTF-8" ?>
304+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
305+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
306+ xmlns : srv =" http://symfony.com/schema/dic/services"
307+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
308+ https://symfony.com/schema/dic/services/services-1.0.xsd
309+ http://symfony.com/schema/dic/security
310+ https://symfony.com/schema/dic/security/security-1.0.xsd" >
311+
312+ <config >
313+ <!-- ... -->
314+
315+ <provider name =" backend_users" >
316+ <ldap service =" ..." base-dn =" ..." />
317+ </provider >
318+
319+ <provider name =" legacy_users" >
320+ <entity >
321+ <!-- ... -->
322+ </entity >
323+ </provider >
324+
325+ <provider name =" users" >
326+ <entity >
327+ <!-- ... -->
328+ </entity >
329+ </provider >
330+
331+ <provider name =" all_users" >
332+ <chain >
333+ <provider >backend_users</provider >
334+ <provider >legacy_users</provider >
335+ <provider >users</provider >
336+ </chain >
337+ </provider >
338+ </config >
339+ </srv : container >
340+
341+ .. code-block :: php
342+
343+ // config/packages/security.php
344+ use App\Entity\User;
345+ use Symfony\Config\SecurityConfig;
346+
347+ return static function (SecurityConfig $security): void {
348+ // ...
349+
350+ $backendProvider = $security->provider('backend_users')
351+ ->ldap()
352+ // ...
353+ ;
354+
355+ $legacyProvider = $security->provider('legacy_users')
356+ ->entity()
357+ // ...
358+ ;
359+
360+ $userProvider = $security->provider('users')
361+ ->entity()
362+ // ...
363+ ;
364+
365+ $allProviders = $security->provider('all_users')->chain()
366+ ->providers([$backendProvider, $legacyProvider, $userProvider])
367+ ;
368+ };
259369
260370 .. _security-custom-user-provider :
261371
@@ -346,14 +456,52 @@ Most of the work is already done! Read the comments in the code and update the
346456TODO sections to finish the user provider. When you're done, tell Symfony about
347457the user provider by adding it in ``security.yaml ``:
348458
349- .. code-block :: yaml
459+ .. configuration-block ::
460+
461+ .. code-block :: yaml
462+
463+ # config/packages/security.yaml
464+ security :
465+ providers :
466+ # the name of your user provider can be anything
467+ your_custom_user_provider :
468+ id : App\Security\UserProvider
469+
470+ .. code-block :: xml
471+
472+ <!-- config/packages/security.xml -->
473+ <?xml version =" 1.0" encoding =" UTF-8" ?>
474+ <srv : container xmlns =" http://symfony.com/schema/dic/security"
475+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
476+ xmlns : srv =" http://symfony.com/schema/dic/services"
477+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
478+ https://symfony.com/schema/dic/services/services-1.0.xsd
479+ http://symfony.com/schema/dic/security
480+ https://symfony.com/schema/dic/security/security-1.0.xsd" >
481+
482+ <config >
483+ <!-- ... -->
484+
485+ <provider name =" your_custom_user_provider" id =" App\Security\UserProvider" >
486+ <!-- ... -->
487+ </provider >
488+ </config >
489+ </srv : container >
490+
491+ .. code-block :: php
492+
493+ // config/packages/security.php
494+ use App\Security\UserProvider;
495+ use Symfony\Config\SecurityConfig;
496+
497+ return static function (SecurityConfig $security): void {
498+ // ...
350499
351- # config/packages/security.yaml
352- security :
353- providers :
354- # the name of your user provider can be anything
355- your_custom_user_provider :
356- id : App\Security\UserProvider
500+ $customProvider = $security->provider('your_custom_user_provider')
501+ ->id(UserProvider::class)
502+ // ...
503+ ;
504+ };
357505
358506 Lastly, update the ``config/packages/security.yaml `` file to set the
359507``provider `` key to ``your_custom_user_provider `` in all the firewalls which
0 commit comments