Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 0c8d7c0

Browse filesBrowse files
committed
Many changes thanks for GREAT feedback from various people
1 parent 066794f commit 0c8d7c0
Copy full SHA for 0c8d7c0

14 files changed

+128
-104
lines changed

‎controller/error_pages.rst

Copy file name to clipboardExpand all lines: controller/error_pages.rst
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ logic to determine the template filename:
6060
a generic template for the given format (like ``error.json.twig`` or
6161
``error.xml.twig``);
6262

63-
#. If none of the previous template exist, fall back to the generic HTML template
63+
#. If none of the previous templates exist, fall back to the generic HTML template
6464
(``error.html.twig``).
6565

6666
.. _overriding-or-adding-templates:
@@ -69,7 +69,7 @@ To override these templates, rely on the standard Symfony method for
6969
:ref:`overriding templates that live inside a bundle <override-templates>` and
7070
put them in the ``templates/bundles/TwigBundle/Exception/`` directory.
7171

72-
A typical project that returns HTML and JSON pages, might look like this:
72+
A typical project that returns HTML and JSON pages might look like this:
7373

7474
.. code-block:: text
7575
@@ -126,7 +126,7 @@ Security & 404 Pages
126126
--------------------
127127

128128
Due to the order of how routing and security are loaded, security information will
129-
*not* be available on your 404 pages. This means that it will appear as if you're
129+
*not* be available on your 404 pages. This means that it will appear as if your
130130
user is logged out on the 404 page (it will work while testing, but not on production).
131131

132132
.. _testing-error-pages:

‎doctrine/registration_form.rst

Copy file name to clipboardExpand all lines: doctrine/registration_form.rst
+3-5Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ Your ``User`` entity will probably at least have the following fields:
2323

2424
``username``
2525
This will be used for logging in, unless you instead want your user to
26-
:ref:`login via email <registration-form-via-email>` (in that case, this
26+
:ref:`log in via email <registration-form-via-email>` (in that case, this
2727
field is unnecessary).
2828

2929
``email``
3030
A nice piece of information to collect. You can also allow users to
31-
:ref:`login via email <registration-form-via-email>`.
31+
:ref:`log in via email <registration-form-via-email>`.
3232

3333
``password``
3434
The encoded password.
@@ -418,9 +418,7 @@ us to add validation, even though there is no ``termsAccepted`` property on ``Us
418418
Manually Authenticating after Success
419419
-------------------------------------
420420

421-
If you're using Guard authentication, you can :ref:`automatically authenticate<guard-manual-auth>`
421+
If you're using Guard authentication, you can :ref:`automatically authenticate <guard-manual-auth>`
422422
after registration is successful.
423423

424-
.. _`CVE-2013-5750`: https://symfony.com/blog/cve-2013-5750-security-issue-in-fosuserbundle-login-form
425-
.. _`FOSUserBundle`: https://github.com/FriendsOfSymfony/FOSUserBundle
426424
.. _`bcrypt`: https://en.wikipedia.org/wiki/Bcrypt

‎security.rst

Copy file name to clipboardExpand all lines: security.rst
+58-38Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Security
1010
Do you prefer video tutorials? Check out the `Symfony Security screencast series`_.
1111

1212
Symfony's security system is incredibly powerful, but it can also be confusing
13-
to set up. But don't worry! In this article, you'll learn how to set up your app's
13+
to set up. Don't worry! In this article, you'll learn how to set up your app's
1414
security system step-by-step:
1515

1616
#. :ref:`Installing security support <security-installation>`;
@@ -41,12 +41,12 @@ install the security feature before using it:
4141
.. _initial-security-yaml-setup-authentication:
4242
.. _create-user-class:
4343

44-
2) Create your User Class
45-
-------------------------
44+
2a) Create your User Class
45+
--------------------------
4646

4747
No matter *how* you will authenticate (e.g. login form or API tokens) or *where*
48-
your user data will be stored (database, SSO), the next step is always the same:
49-
create a "User" class. The easiest way is to use `MakerBundle`_.
48+
your user data will be stored (database, single sign-on), the next step is always the same:
49+
create a "User" class. The easiest way is to use the `MakerBundle`_.
5050

5151
Let's assume that you want to store your user data in the database with Doctrine:
5252

@@ -122,7 +122,8 @@ command will pre-configure this for you:
122122
# ...
123123
124124
encoders:
125-
Symfony\Component\Security\Core\User\User:
125+
# use your user class name here
126+
App\Entity\User:
126127
# bcrypt or argon21 are recommended
127128
# argon21 is more secure, but requires PHP 7.2 or the Sodium extension
128129
algorithm: bcrypt
@@ -141,7 +142,7 @@ command will pre-configure this for you:
141142
<config>
142143
<!-- ... -->
143144
144-
<encoder class="Symfony\Component\Security\Core\User\User"
145+
<encoder class="App\Entity\User"
145146
algorithm="bcrypt"
146147
cost="12" />
147148
@@ -156,7 +157,7 @@ command will pre-configure this for you:
156157
// ...
157158
158159
'encoders' => array(
159-
'Symfony\Component\Security\Core\User\User' => array(
160+
'App\Entity\User' => array(
160161
'algorithm' => 'bcrypt',
161162
'cost' => 12,
162163
)
@@ -210,7 +211,7 @@ Use this service to encode the passwords:
210211
}
211212
}
212213
213-
Of, you can manually encode a password by running:
214+
You can manually encode a password by running:
214215

215216
.. code-block:: terminal
216217
@@ -220,8 +221,8 @@ Of, you can manually encode a password by running:
220221
.. _security-firewalls:
221222
.. _firewalls-authentication:
222223

223-
3) Authentication & Firewalls
224-
-----------------------------
224+
3a) Authentication & Firewalls
225+
------------------------------
225226

226227
The security system is configured in ``config/packages/security.yaml``. The *most*
227228
important section is ``firewalls``:
@@ -290,7 +291,8 @@ Nope, thanks to the ``anonymous`` key, this firewall *is* accessible anonymously
290291

291292
In fact, if you go to the homepage right now, you *will* have access and you'll see
292293
that you're "authenticated" as ``anon.``. Don't be fooled by the "Yes" next to
293-
Authenticated, you're just an anonymous user:
294+
Authenticated. The firewall verified that it does not know your identity, and so,
295+
you are anonymous:
294296

295297
.. image:: /_images/security/anonymous_wdt.png
296298
:align: center
@@ -299,7 +301,7 @@ You'll learn later how to deny access to certain URLs or controllers.
299301

300302
.. note::
301303

302-
If you do not see toolbar, install the :doc:`profiler </profiler>` with:
304+
If you do not see the toolbar, install the :doc:`profiler </profiler>` with:
303305

304306
.. code-block:: terminal
305307
@@ -348,7 +350,7 @@ For the most detailed description of authenticators and how they work, see
348350
4) Denying Access, Roles and other Authorization
349351
------------------------------------------------
350352

351-
Users can now login to your app using your login form. Great! Now, you need to learn
353+
Users can now log in to your app using your login form. Great! Now, you need to learn
352354
how to deny access and work with the User object. This is called **authorization**,
353355
and its job is to decide if a user can access some resource (a URL, a model object,
354356
a method call, ...).
@@ -364,7 +366,7 @@ Roles
364366
~~~~~
365367

366368
When a user logs in, Symfony calls the ``getRoles()`` method on your ``User``
367-
object to determime which roles this user has. In the ``User`` class that we
369+
object to determine which roles this user has. In the ``User`` class that we
368370
generated earlier, the roles are an array that's stored in the database, and
369371
every user is *always* given at least one role: ``ROLE_USER``::
370372

@@ -388,16 +390,14 @@ every user is *always* given at least one role: ``ROLE_USER``::
388390
This is a nice default, but you can do *whatever* you want to determine which roles
389391
a user should have. Here are a few guidelines:
390392

391-
* Every role **must start with** ``ROLE_`` (otherwise, things won't as expected)
393+
* Every role **must start with** ``ROLE_`` (otherwise, things won't work as expected)
392394

393395
* Other than the above rule, a role is just a string and you can invent what you
394-
need (e.g. ``ROLE_PRODUCT_ADMIN``)
395-
396-
* Every User **must** have at least **one** role - a common convention is to give
397-
*every* user ``ROLE_USER``.
396+
need (e.g. ``ROLE_PRODUCT_ADMIN``).
398397

398+
You'll use these roles next to grant access to specific sections of your site.
399399
You can also use a :ref:`role hierarchy <security-role-hierarchy>` where having
400-
some roles automatically gives you other roles.
400+
some roles automatically give you other roles.
401401

402402
.. _security-role-authorization:
403403

@@ -531,7 +531,7 @@ Prepending the path with ``^`` means that only URLs *beginning* with the
531531
pattern are matched. For example, a path of simply ``/admin`` (without
532532
the ``^``) would match ``/admin/foo`` but would also match URLs like ``/foo/admin``.
533533

534-
Each ``access_control`` can also match on IP address, host name and HTTP methods.
534+
Each ``access_control`` can also match on IP address, hostname and HTTP methods.
535535
It can also be used to redirect a user to the ``https`` version of a URL pattern.
536536
See :doc:`/security/access_control`.
537537

@@ -550,15 +550,15 @@ You can easily deny access from inside a controller::
550550
$this->denyAccessUnlessGranted('ROLE_ADMIN');
551551

552552
// or add an optional message - seen by developers
553-
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
553+
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'User tried to access a page without having ROLE_ADMIN');
554554
}
555555

556556
That's it! If access is not granted, a special
557557
:class:`Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException`
558558
is thrown and no more code in your controller is executed. Then, one of two things
559559
will happen:
560560

561-
1) If the user isn't logged in yet, they will be asked to login (e.g. redirected
561+
1) If the user isn't logged in yet, they will be asked to log in (e.g. redirected
562562
to the login page).
563563

564564
2) If the user *is* logged in, but does *not* have the ``ROLE_ADMIN`` role, they'll
@@ -637,7 +637,7 @@ You can use ``IS_AUTHENTICATED_FULLY`` anywhere roles are used: like ``access_co
637637
or in Twig.
638638

639639
``IS_AUTHENTICATED_FULLY`` isn't a role, but it kind of acts like one, and every
640-
user that has logged in will have this. ACtually, there are 3 special attributes
640+
user that has logged in will have this. Actually, there are 3 special attributes
641641
like this:
642642

643643
* ``IS_AUTHENTICATED_REMEMBERED``: *All* logged in users have this, even
@@ -671,8 +671,8 @@ If you still prefer to use traditional ACLs, refer to the `Symfony ACL bundle`_.
671671

672672
.. _retrieving-the-user-object:
673673

674-
5) Fetching the User Object
675-
---------------------------
674+
5a) Fetching the User Object
675+
----------------------------
676676

677677
After authentication, the ``User`` object of the current user can be accessed
678678
via the ``getUser()`` shortcut::
@@ -683,14 +683,16 @@ via the ``getUser()`` shortcut::
683683
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
684684

685685
// returns your User object, or null if the user is not authenticated
686+
// use inline documentation to tell your editor your exact User class
687+
/** @var \App\Entity\User $user */
686688
$user = $this->getUser();
687689

688690
// Call whatever methods you've added to your User class
689691
// For example, if you added a getFirstName() method, you can use that.
690692
return new Response('Well hi there '.$user->getFirstName());
691693
}
692694

693-
5B) Fetching the User from a Service
695+
5b) Fetching the User from a Service
694696
------------------------------------
695697

696698
If you need to get the logged in user from a service, use the
@@ -707,6 +709,8 @@ If you need to get the logged in user from a service, use the
707709

708710
public function __construct(Security $security)
709711
{
712+
// Avoid calling getUser() in the constructor: auth may not
713+
// be complete yet. Instead, store the entire Security object.
710714
$this->security = $security;
711715
}
712716

@@ -734,7 +738,7 @@ key:
734738
Logging Out
735739
-----------
736740

737-
To add logout, activate the ``logout`` config parameter under your firewall:
741+
To enable logging out, activate the ``logout`` config parameter under your firewall:
738742

739743
.. configuration-block::
740744

@@ -808,11 +812,12 @@ Next, you'll need to create a route for this URL (but not a controller):
808812
class SecurityController extends AbstractController
809813
{
810814
/**
811-
* @Route("/login", name="app_logout")
815+
* @Route("/logout", name="app_logout")
812816
*/
813817
public function logout()
814818
{
815-
// controller should be blank - will never be executed
819+
// controller can be blank: it will never be executed!
820+
throw new \Exception('Don\'t forget to activate logout in security.yaml');
816821
}
817822
}
818823
@@ -905,9 +910,18 @@ Users with the ``ROLE_ADMIN`` role will also have the
905910
``ROLE_USER`` role. And users with ``ROLE_SUPER_ADMIN``, will automatically have
906911
``ROLE_ADMIN``, ``ROLE_ALLOWED_TO_SWITCH`` and ``ROLE_USER`` (inherited from ``ROLE_ADMIN``).
907912

913+
For role hierarchy to work, do not try to call ``$user->getRoles()`` manually::
914+
915+
// BAD - $user->getRoles() will not know about the role hierarchy
916+
$hasAccess = in_array('ROLE_ADMIN', $user->getRoles());
917+
918+
// GOOD - use of the normal security methods
919+
$hasAccess = $this->isGranted('ROLE_ADMIN');
920+
$this->denyAccessUnlessGranted('ROLE_ADMIN');
921+
908922
.. note::
909923

910-
The ``role_hierarchy`` values iare static - you can't, for example, store the
924+
The ``role_hierarchy`` values are static - you can't, for example, store the
911925
role hierarchy in a database. If you need that, create a custom
912926
:doc:`security voter </security/voters>` that looks for the user roles
913927
in the database.
@@ -923,7 +937,9 @@ Frequently Asked Questions
923937
**Can I have Multiple Firewalls?**
924938
Yes! But it's usually not necessary. Each firewall is like a separate security
925939
system. And so, unless you have *very* different authentication needs, one
926-
firewall usually works well.
940+
firewall usually works well. With :doc:`Guard authentication </security/guard_authentication>`,
941+
you can create various, diverse ways of allowing authentication (e.g. form login,
942+
API key authentication and LDAP) all under the same firewall.
927943

928944
**Can I Share Authentication Between Firewalls?**
929945
Yes, but only with some configuration. If you're using multiple firewalls and
@@ -942,11 +958,15 @@ Frequently Asked Questions
942958
**My Authentication Doesn't Seem to Work: No Errors, but I'm Never Logged In**
943959
Sometimes authentication may be successful, but after redirecting, you're
944960
logged out immediately due to a problem loading the ``User`` from the session.
945-
To see if this is the issue, temporarily enable :ref:`intercept_redirects`.
946-
Then, when you login, instead of being redirected, you'll be stopped. Check
947-
the web debug toolbar on that page to see if you're logged in. If you *are*,
948-
but are no longer logged in after redirecting, then there is a problem loading
949-
your User from the session. See :ref:`user_session_refresh`.
961+
To see if this is an issue, check your log file (``var/log/dev.log``) for
962+
the log message:
963+
964+
> Cannot refresh token because user has changed.
965+
966+
If you see this, there are two possible causes. First, there may be a problem
967+
loading your User from the session. See :ref:`user_session_refresh`. Second,
968+
if certain user information was changed in the database since the last page
969+
refresh, Symfony will purposely log out the user for security reasons.
950970

951971
Learn More
952972
----------

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.