Skip to content

Commit 1907875

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

File tree

2 files changed

+76
-31
lines changed

2 files changed

+76
-31
lines changed

security.rst

Lines changed: 59 additions & 14 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
@@ -86,7 +87,7 @@ configuration looks like this:
8687
// app/config/security.php
8788
$container->loadFromExtension('security', [
8889
'providers' => [
89-
'in_memory' => [
90+
'users_in_memory' => [
9091
'memory' => null,
9192
],
9293
],
@@ -97,6 +98,7 @@ configuration looks like this:
9798
],
9899
'main' => [
99100
'anonymous' => null,
101+
'provider' => 'users_in_memory'
100102
],
101103
],
102104
]);
@@ -315,7 +317,7 @@ provider, but it's better to think of it as an "in configuration" provider:
315317
# app/config/security.yml
316318
security:
317319
providers:
318-
in_memory:
320+
users_in_memory:
319321
memory:
320322
users:
321323
ryan:
@@ -352,7 +354,7 @@ provider, but it's better to think of it as an "in configuration" provider:
352354
// app/config/security.php
353355
$container->loadFromExtension('security', [
354356
'providers' => [
355-
'in_memory' => [
357+
'users_in_memory' => [
356358
'memory' => [
357359
'users' => [
358360
'ryan' => [
@@ -371,9 +373,9 @@ provider, but it's better to think of it as an "in configuration" provider:
371373
]);
372374
373375
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
376+
probably only need one. If you *do* have multiple, you have to configure which
375377
*one* provider to use for your firewall under its ``provider`` key (e.g.
376-
``provider: in_memory``).
378+
``provider: users_in_memory``).
377379

378380
.. seealso::
379381

@@ -421,20 +423,22 @@ To fix this, add an ``encoders`` key:
421423
.. code-block:: php
422424
423425
// app/config/security.php
426+
use Symfony\Component\Security\Core\User\User;
427+
424428
$container->loadFromExtension('security', [
425429
// ...
426430
427431
'encoders' => [
428-
'Symfony\Component\Security\Core\User\User' => 'plaintext',
432+
User::class => 'plaintext',
429433
],
430434
// ...
431435
]);
432436
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>`
437+
User providers load user information and put it into a :class:`Symfony\Component\Security\Core\User\UserInterface`
438+
implementation. If you :doc:`load users from the database </security/entity_provider>`
435439
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.
440+
use your own custom User class. But when you use the "in memory" provider type,
441+
it gives you a :class:`Symfony\Component\Security\Core\User\User` object.
438442

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

456+
.. tip::
457+
458+
If you have many providers and want to define the same encoder for all of
459+
them, you can configure as follow:
460+
461+
.. configuration-block::
462+
463+
.. code-block:: yaml
464+
465+
# app/config/security.yml
466+
security:
467+
# ...
468+
469+
encoders:
470+
Symfony\Component\Security\Core\User\UserInterface: bcrypt
471+
472+
# would be equivalent to:
473+
# App\Entity\User: bcrypt
474+
# Symfony\Component\Security\Core\User\User: bcrypt
475+
# ...
476+
477+
.. code-block:: php
478+
479+
// app/config/security.php
480+
use Symfony\Component\Security\Core\User\UserInterface;
481+
482+
$container->loadFromExtension('security', [
483+
// ...
484+
485+
'encoders' => [
486+
UserInterface::class => 'bcrypt',
487+
488+
// would be equivalent to:
489+
// App\Entity\User::class => 'bcrypt',
490+
// Symfony\Component\Security\Core\User\User::class => 'bcrypt',
491+
],
492+
// ...
493+
]);
494+
452495
Loading Users from the Database
453496
...............................
454497

@@ -502,11 +545,13 @@ is ``bcrypt``:
502545
.. code-block:: php
503546
504547
// app/config/security.php
548+
use Symfony\Component\Security\Core\User\User;
549+
505550
$container->loadFromExtension('security', [
506551
// ...
507552
508553
'encoders' => [
509-
'Symfony\Component\Security\Core\User\User' => [
554+
User::class => [
510555
'algorithm' => 'bcrypt',
511556
'cost' => 12,
512557
]
@@ -532,7 +577,7 @@ It will give you something like this:
532577
# ...
533578
534579
providers:
535-
in_memory:
580+
users_in_memory:
536581
memory:
537582
users:
538583
ryan:
@@ -571,7 +616,7 @@ It will give you something like this:
571616
// ...
572617
573618
'providers' => [
574-
'in_memory' => [
619+
'users_in_memory' => [
575620
'memory' => [
576621
'users' => [
577622
'ryan' => [

security/multiple_user_providers.rst

Lines changed: 17 additions & 17 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
@@ -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
@@ -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)