Skip to content

Commit b2f1f7c

Browse files
committed
[Security] renamed memory provider
1 parent 10747a8 commit b2f1f7c

File tree

2 files changed

+122
-43
lines changed

2 files changed

+122
-43
lines changed

security.rst

Lines changed: 98 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ configuration looks like this:
4545
# app/config/security.yml
4646
security:
4747
providers:
48-
in_memory:
48+
users_in_memory:
4949
memory: ~
5050
5151
firewalls:
@@ -55,6 +55,7 @@ configuration looks like this:
5555
5656
main:
5757
anonymous: ~
58+
provider: users_in_memory
5859
5960
.. code-block:: xml
6061
@@ -67,7 +68,7 @@ configuration looks like this:
6768
https://symfony.com/schema/dic/services/services-1.0.xsd">
6869
6970
<config>
70-
<provider name="in_memory">
71+
<provider name="users_in_memory">
7172
<memory/>
7273
</provider>
7374
@@ -77,6 +78,7 @@ configuration looks like this:
7778
7879
<firewall name="main">
7980
<anonymous/>
81+
<provider>users_in_memory</provider>
8082
</firewall>
8183
</config>
8284
</srv:container>
@@ -86,7 +88,7 @@ configuration looks like this:
8688
// app/config/security.php
8789
$container->loadFromExtension('security', [
8890
'providers' => [
89-
'in_memory' => [
91+
'users_in_memory' => [
9092
'memory' => null,
9193
],
9294
],
@@ -97,6 +99,7 @@ configuration looks like this:
9799
],
98100
'main' => [
99101
'anonymous' => null,
102+
'provider' => 'users_in_memory'
100103
],
101104
],
102105
]);
@@ -315,7 +318,7 @@ provider, but it's better to think of it as an "in configuration" provider:
315318
# app/config/security.yml
316319
security:
317320
providers:
318-
in_memory:
321+
users_in_memory:
319322
memory:
320323
users:
321324
ryan:
@@ -324,7 +327,11 @@ provider, but it's better to think of it as an "in configuration" provider:
324327
admin:
325328
password: kitten
326329
roles: 'ROLE_ADMIN'
327-
# ...
330+
331+
firewalls:
332+
main:
333+
provider: users_in_memory
334+
# ...
328335
329336
.. code-block:: xml
330337
@@ -337,13 +344,16 @@ provider, but it's better to think of it as an "in configuration" provider:
337344
https://symfony.com/schema/dic/services/services-1.0.xsd">
338345
339346
<config>
340-
<provider name="in_memory">
347+
<provider name="users_in_memory">
341348
<memory>
342349
<user name="ryan" password="ryanpass" roles="ROLE_USER"/>
343350
<user name="admin" password="kitten" roles="ROLE_ADMIN"/>
344351
</memory>
345352
</provider>
346-
<!-- ... -->
353+
<firewall name="main">
354+
<provider>users_in_memory</provider>
355+
<!-- ... -->
356+
</firewall>
347357
</config>
348358
</srv:container>
349359
@@ -352,7 +362,7 @@ provider, but it's better to think of it as an "in configuration" provider:
352362
// app/config/security.php
353363
$container->loadFromExtension('security', [
354364
'providers' => [
355-
'in_memory' => [
365+
'users_in_memory' => [
356366
'memory' => [
357367
'users' => [
358368
'ryan' => [
@@ -367,13 +377,17 @@ provider, but it's better to think of it as an "in configuration" provider:
367377
],
368378
],
369379
],
370-
// ...
380+
'firewalls' => [
381+
'main' => [
382+
'provider' => 'users_in_memory',
383+
],
384+
],
371385
]);
372386
373387
Like with ``firewalls``, you can have multiple ``providers``, but you'll
374-
probably only need one. If you *do* have multiple, you can configure which
388+
probably only need one. If you *do* have multiple, you have to configure which
375389
*one* provider to use for your firewall under its ``provider`` key (e.g.
376-
``provider: in_memory``).
390+
``provider: users_in_memory``).
377391

378392
.. seealso::
379393

@@ -421,20 +435,22 @@ To fix this, add an ``encoders`` key:
421435
.. code-block:: php
422436
423437
// app/config/security.php
438+
use Symfony\Component\Security\Core\User\User;
439+
424440
$container->loadFromExtension('security', [
425441
// ...
426442
427443
'encoders' => [
428-
'Symfony\Component\Security\Core\User\User' => 'plaintext',
444+
User::class => 'plaintext',
429445
],
430446
// ...
431447
]);
432448
433-
User providers load user information and put it into a ``User`` object. If
434-
you :doc:`load users from the database </security/entity_provider>`
449+
User providers load user information and put it into a :class:`Symfony\\Component\\Security\\Core\\User\\UserInterface`
450+
implementation. If you :doc:`load users from the database </security/entity_provider>`
435451
or :doc:`some other source </security/custom_provider>`, you'll
436-
use your own custom User class. But when you use the "in memory" provider,
437-
it gives you a ``Symfony\Component\Security\Core\User\User`` object.
452+
use your own custom User class. But when you use the "in memory" provider type,
453+
it gives you a :class:`Symfony\\Component\\Security\\Core\\User\\User` object.
438454

439455
Whatever your User class is, you need to tell Symfony what algorithm was
440456
used to encode the passwords. In this case, the passwords are just plaintext,
@@ -449,6 +465,67 @@ you who you are and what roles you have:
449465
Because this URL requires ``ROLE_ADMIN``, if you had logged in as ``ryan``,
450466
this would deny you access. More on that later (:ref:`security-authorization-access-control`).
451467

468+
.. tip::
469+
470+
If you have many providers and want to define the same encoder for all of
471+
them, you can configure as follow:
472+
473+
.. configuration-block::
474+
475+
.. code-block:: yaml
476+
477+
# app/config/security.yml
478+
security:
479+
encoders:
480+
Symfony\Component\Security\Core\User\UserInterface: bcrypt
481+
482+
# would be equivalent to:
483+
AppBundle\Entity\User: bcrypt
484+
Symfony\Component\Security\Core\User\User: bcrypt
485+
# and any other type you may add in the future
486+
# ...
487+
488+
.. code-block:: xml
489+
490+
<!-- app/config/security.xml -->
491+
<?xml version="1.0" encoding="UTF-8"?>
492+
<srv:container xmlns="http://symfony.com/schema/dic/security"
493+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
494+
xmlns:srv="http://symfony.com/schema/dic/services"
495+
xsi:schemaLocation="http://symfony.com/schema/dic/services
496+
https://symfony.com/schema/dic/services/services-1.0.xsd">
497+
498+
<config>
499+
<encoder class="Symfony\Component\Security\Core\User\UserInterface"
500+
algorithm="bcrypt"/>
501+
<!-- would be equivalent to: -->
502+
<encoder class="AppBundle\Entity\User"
503+
algorithm="bcrypt"/>
504+
<encoder class="Symfony\Component\Security\Core\User\User"
505+
algorithm="bcrypt"/>
506+
<!-- and any other type you may add in the future -->
507+
508+
<!-- ... -->
509+
</config>
510+
</srv:container>
511+
512+
.. code-block:: php
513+
514+
// app/config/security.php
515+
use Symfony\Component\Security\Core\User\UserInterface;
516+
517+
$container->loadFromExtension('security', [
518+
'encoders' => [
519+
UserInterface::class => 'bcrypt',
520+
521+
// would be equivalent to:
522+
AppBundle\Entity\User::class => 'bcrypt',
523+
Symfony\Component\Security\Core\User\User::class => 'bcrypt',
524+
// and any other type you may add in the future
525+
],
526+
// ...
527+
]);
528+
452529
Loading Users from the Database
453530
...............................
454531

@@ -502,11 +579,13 @@ is ``bcrypt``:
502579
.. code-block:: php
503580
504581
// app/config/security.php
582+
use Symfony\Component\Security\Core\User\User;
583+
505584
$container->loadFromExtension('security', [
506585
// ...
507586
508587
'encoders' => [
509-
'Symfony\Component\Security\Core\User\User' => [
588+
User::class => [
510589
'algorithm' => 'bcrypt',
511590
'cost' => 12,
512591
]
@@ -532,7 +611,7 @@ It will give you something like this:
532611
# ...
533612
534613
providers:
535-
in_memory:
614+
users_in_memory:
536615
memory:
537616
users:
538617
ryan:
@@ -571,7 +650,7 @@ It will give you something like this:
571650
// ...
572651
573652
'providers' => [
574-
'in_memory' => [
653+
'users_in_memory' => [
575654
'memory' => [
576655
'users' => [
577656
'ryan' => [

security/multiple_user_providers.rst

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ a new provider that chains the two together:
2020
# app/config/security.yml
2121
security:
2222
providers:
23-
chain_provider:
23+
users:
2424
chain:
25-
providers: [in_memory, user_db]
26-
in_memory:
25+
providers: [users_in_memory, users_in_db]
26+
users_in_memory:
2727
memory:
2828
users:
2929
foo: { password: test }
30-
user_db:
30+
users_in_db:
3131
entity: { class: AppBundle\Entity\User, property: username }
3232
3333
.. code-block:: xml
@@ -41,20 +41,20 @@ a new provider that chains the two together:
4141
https://symfony.com/schema/dic/services/services-1.0.xsd">
4242
4343
<config>
44-
<provider name="chain_provider">
44+
<provider name="users">
4545
<chain>
46-
<provider>in_memory</provider>
47-
<provider>user_db</provider>
46+
<provider>users_in_memory</provider>
47+
<provider>users_in_db</provider>
4848
</chain>
4949
</provider>
5050
51-
<provider name="in_memory">
51+
<provider name="users_in_memory">
5252
<memory>
5353
<user name="foo" password="test"/>
5454
</memory>
5555
</provider>
5656
57-
<provider name="user_db">
57+
<provider name="users_in_db">
5858
<entity class="AppBundle\Entity\User" property="username"/>
5959
</provider>
6060
</config>
@@ -67,19 +67,19 @@ a new provider that chains the two together:
6767
6868
$container->loadFromExtension('security', [
6969
'providers' => [
70-
'chain_provider' => [
70+
'users' => [
7171
'chain' => [
7272
'providers' => ['in_memory', 'user_db'],
7373
],
7474
],
75-
'in_memory' => [
75+
'users_in_memory' => [
7676
'memory' => [
7777
'users' => [
7878
'foo' => ['password' => 'test'],
7979
],
8080
],
8181
],
82-
'user_db' => [
82+
'users_in_db' => [
8383
'entity' => [
8484
'class' => User::class,
8585
'property' => 'username',
@@ -88,14 +88,14 @@ a new provider that chains the two together:
8888
],
8989
]);
9090
91-
Now, all firewalls that explicitly define ``chain_provider`` as their user
92-
provider will, in turn, try to load the user from both the ``in_memory`` and
93-
``user_db`` providers.
91+
Now, all firewalls that explicitly define ``users`` as their user
92+
provider will, in turn, try to load the user from both the ``users_in_memory`` then
93+
``users_in_db`` providers.
9494

9595
.. deprecated:: 3.4
9696

9797
In previous Symfony versions, firewalls that didn't define their user provider
98-
explicitly, used the first existing provider (``chain_provider`` in this
98+
explicitly, used the first existing provider (``users`` in this
9999
example). However, auto-selecting the first user provider has been deprecated
100100
in Symfony 3.4 and will throw an exception in 4.0. Always define the provider
101101
used by the firewall when there are multiple providers.
@@ -114,10 +114,10 @@ the first provider is always used:
114114
secured_area:
115115
# ...
116116
pattern: ^/
117-
provider: user_db
117+
provider: users_in_db
118118
http_basic:
119119
realm: 'Secured Demo Area'
120-
provider: in_memory
120+
provider: users_in_memory
121121
form_login: ~
122122
123123
.. code-block:: xml
@@ -131,9 +131,9 @@ the first provider is always used:
131131
https://symfony.com/schema/dic/services/services-1.0.xsd">
132132
133133
<config>
134-
<firewall name="secured_area" pattern="^/" provider="user_db">
134+
<firewall name="secured_area" pattern="^/" provider="users_in_db">
135135
<!-- ... -->
136-
<http-basic realm="Secured Demo Area" provider="in_memory"/>
136+
<http-basic realm="Secured Demo Area" provider="users_in_memory"/>
137137
<form-login/>
138138
</firewall>
139139
</config>
@@ -147,20 +147,20 @@ the first provider is always used:
147147
'secured_area' => [
148148
// ...
149149
'pattern' => '^/',
150-
'provider' => 'user_db',
150+
'provider' => 'users_in_db',
151151
'http_basic' => [
152152
// ...
153153
'realm' => 'Secured Demo Area',
154-
'provider' => 'in_memory',
154+
'provider' => 'users_in_memory',
155155
],
156156
'form_login' => [],
157157
],
158158
],
159159
]);
160160
161161
In this example, if a user tries to log in via HTTP authentication, the authentication
162-
system will use the ``in_memory`` user provider. But if the user tries to
163-
log in via the form login, the ``user_db`` provider will be used (since it's
162+
system will use the ``users_in_memory`` user provider. But if the user tries to
163+
log in via the form login, the ``users_in_db`` provider will be used (since it's
164164
the default for the firewall as a whole).
165165

166166
If you need to check that the user being returned by your provider is a allowed

0 commit comments

Comments
 (0)