Skip to content

Commit 54fa3be

Browse files
committed
[symfony#1604] Proofreading and tweaking the new security component docs
1 parent fc7b8a6 commit 54fa3be

File tree

4 files changed

+105
-88
lines changed

4 files changed

+105
-88
lines changed

components/security/authentication.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.. index::
2-
single: Security, Authentication Manager
2+
single: Security, Authentication
33

44
Authentication
55
==============
@@ -9,7 +9,7 @@ firewall map is able to extract the user's credentials from the current
99
:class:`Symfony\\Component\\HttpFoundation\\Request` object, it should create
1010
a token, containing these credentials. The next thing the listener should
1111
do is ask the authentication manager to validate the given token, and return
12-
an authenticated token when the supplied credentials were found to be valid.
12+
an *authenticated* token if the supplied credentials were found to be valid.
1313
The listener should then store the authenticated token in the security context::
1414

1515
use Symfony\Component\Security\Http\Firewall\ListenerInterface;
@@ -63,7 +63,7 @@ The listener should then store the authenticated token in the security context::
6363
A token can be of any class, as long as it implements
6464
:class:`Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface`.
6565

66-
The authentication manager
66+
The Authentication Manager
6767
--------------------------
6868

6969
The default authentication manager is an instance of
@@ -101,11 +101,12 @@ Each provider (since it implements
101101
has a method :method:`Symfony\\Component\\Security\\Core\\Authentication\\Provider\\AuthenticationProviderInterface::supports`
102102
by which the ``AuthenticationProviderManager``
103103
can determine if it supports the given token. If this is the case, the
104-
manager then calls the provider's method :class:`Symfony\\Component\\Security\\Core\\Authentication\\Provider\\AuthenticationProviderInterface::authenticate`. This method
105-
should return an authenticated token or throw an :class:`Symfony\\Component\\Security\\Core\\Exception\\AuthenticationException`
104+
manager then calls the provider's method :class:`Symfony\\Component\\Security\\Core\\Authentication\\Provider\\AuthenticationProviderInterface::authenticate`.
105+
This method should return an authenticated token or throw an
106+
:class:`Symfony\\Component\\Security\\Core\\Exception\\AuthenticationException`
106107
(or any other exception extending it).
107108

108-
Authenticating users by their username and password
109+
Authenticating Users by their Username and Password
109110
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110111

111112
An authentication provider will attempt to authenticate a user based on
@@ -159,7 +160,7 @@ password was valid::
159160
It is also possible to let multiple user providers try to find the user's
160161
data, using the :class:`Symfony\\Component\\Security\\Core\\User\\ChainUserProvider`.
161162

162-
The password encoder factory
163+
The Password encoder Factory
163164
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
164165

165166
The :class:`Symfony\\Component\\Security\\Core\\Authentication\\Provider\\DaoAuthenticationProvider`
@@ -186,7 +187,7 @@ Each encoder should implement :class:`Symfony\\Component\\Security\\Core\\Encode
186187
or be an array with a ``class`` and an ``arguments`` key, which allows the
187188
encoder factory to construct the encoder only when it is needed.
188189

189-
Password encoders
190+
Password Encoders
190191
~~~~~~~~~~~~~~~~~
191192

192193
When the :method:`Symfony\\Component\\Security\\Core\\Encoder\\EncoderFactory::getEncoder`

components/security/authorization.rst

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,31 @@ Authorization
55
=============
66

77
When any of the authentication providers (see :ref:`authentication_providers`)
8-
has verified the still unauthenticated token, an authenticated token will
8+
has verified the still-unauthenticated token, an authenticated token will
99
be returned. The authentication listener should set this token directly
1010
in the :class:`Symfony\\Component\\Security\\Core\\SecurityContextInterface`
1111
using its :method:`Symfony\\Component\\Security\\Core\\SecurityContextInterface::setToken`
1212
method.
1313

14-
From then on, the user is authenticated, i.e. means identified.
15-
Now, other parts of the application can use the token to decide whether
16-
or not the user may request a certain URI, or modify a certain object.
17-
This decision will be made by an instance of :class:`Symfony\\Component\\Security\\Core\\Authorization\\AccessDecisionManagerInterface`.
14+
From then on, the user is authenticated, i.e. identified. Now, other parts
15+
of the application can use the token to decide whether or not the user may
16+
request a certain URI, or modify a certain object. This decision will be made
17+
by an instance of :class:`Symfony\\Component\\Security\\Core\\Authorization\\AccessDecisionManagerInterface`.
1818

1919
An authorization decision will always be based on a few things:
2020

21-
The current token
21+
* The current token
2222
For instance, the token's :method:`Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface::getRoles`
2323
method may be used to retrieve the roles of the current user (e.g.
24-
"ROLE_SUPER_ADMIN"), or a decision may be based on the class of the token.
25-
A set of attributes
24+
``ROLE_SUPER_ADMIN``), or a decision may be based on the class of the token.
25+
* A set of attributes
2626
Each attribute stands for a certain right the user should have, e.g.
27-
"ROLE_ADMIN" to make sure the user is an administrator.
28-
An object (optional)
29-
Any object on which to decide, e.g. the current :class:`Symfony\\Component\\HttpFoundation\\Request`
30-
object, or an object for which access control needs to be checked, like
27+
``ROLE_ADMIN`` to make sure the user is an administrator.
28+
* An object (optional)
29+
Any object on which for which access control needs to be checked, like
3130
an article or a comment object.
3231

33-
Access decision manager
32+
Access Decision Manager
3433
-----------------------
3534

3635
Since deciding whether or not a user is authorized to perform a certain
@@ -39,14 +38,14 @@ itself depends on multiple voters, and makes a final verdict based on all
3938
the votes (either positive, negative or neutral) it has received. It
4039
recognizes several strategies:
4140

42-
``affirmative`` (default)
43-
Grant access as soon as any voter returns an affirmative response
41+
* ``affirmative`` (default)
42+
grant access as soon as any voter returns an affirmative response;
4443

45-
``consensus``
46-
Grant access if there are more voters granting access then there are denying
44+
* ``consensus``
45+
grant access if there are more voters granting access than there are denying;
4746

48-
``unanimous``
49-
Only grant access if none of the voters has denied access
47+
* ``unanimous``
48+
only grant access if none of the voters has denied access;
5049

5150
.. code-block:: php
5251
@@ -79,23 +78,28 @@ of :class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\VoterInterf
7978
which means they have to implement a few methods which allows the decision
8079
manager to use them:
8180

82-
``supportsAttribute($attribute)``
83-
Will be used to check if the voter knows how to handle the given attribute.
84-
``supportsClass($class)``
85-
Will be used to check if the voter is able to grant or deny access for
86-
an object of the given class.
87-
``vote(TokenInterface $token, $object, array $attributes)``
88-
This method will do the actual voting and return a value equal to one
81+
* ``supportsAttribute($attribute)``
82+
will be used to check if the voter knows how to handle the given attribute;
83+
84+
* ``supportsClass($class)``
85+
will be used to check if the voter is able to grant or deny access for
86+
an object of the given class;
87+
88+
* ``vote(TokenInterface $token, $object, array $attributes)``
89+
this method will do the actual voting and return a value equal to one
8990
of the class constants of :class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\VoterInterface`,
9091
i.e. ``VoterInterface::ACCESS_GRANTED``, ``VoterInterface::ACCESS_DENIED``
91-
or ``VoterInterface::ACCESS_ABSTAIN``.
92+
or ``VoterInterface::ACCESS_ABSTAIN``;
9293

9394
The security component contains some standard voters which cover many use
9495
cases:
9596

97+
AuthenticatedVoter
98+
~~~~~~~~~~~~~~~~~~
99+
96100
The :class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\AuthenticatedVoter`
97-
voter supports the attributes "IS_AUTHENTICATED_FULLY", "IS_AUTHENTICATED_REMEMBERED",
98-
and "IS_AUTHENTICATED_ANONYMOUSLY" and grants access based on the current
101+
voter supports the attributes ``IS_AUTHENTICATED_FULLY``, ``IS_AUTHENTICATED_REMEMBERED``,
102+
and ``IS_AUTHENTICATED_ANONYMOUSLY`` and grants access based on the current
99103
level of authentication, i.e. is the user fully authenticated, or only based
100104
on a "remember-me" cookie, or even authenticated anonymously?
101105

@@ -118,30 +122,32 @@ on a "remember-me" cookie, or even authenticated anonymously?
118122
119123
$vote = $authenticatedVoter->vote($token, $object, array('IS_AUTHENTICATED_FULLY');
120124
125+
RoleVoter
126+
~~~~~~~~~
127+
121128
The :class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\RoleVoter`
122-
supports attributes starting with "ROLE_" and grants access to the user
123-
when the required "ROLE_*" attributes can all be found in the array of
129+
supports attributes starting with ``ROLE_`` and grants access to the user
130+
when the required ``ROLE_*`` attributes can all be found in the array of
124131
roles returned by the token's :method:`Symfony\\Component\\Security\\Core\\Authentication\\Token\\TokenInterface::getRoles`
125-
method.
126-
127-
.. code-block:: php
132+
method::
128133

129134
use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter;
130135

131136
$roleVoter = new RoleVoter('ROLE_');
132137

133138
$roleVoter->vote($token, $object, 'ROLE_ADMIN');
134139

140+
RoleHierarchyVoter
141+
~~~~~~~~~~~~~~~~~~
142+
135143
The :class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\RoleHierarchyVoter`
136144
extends :class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\RoleVoter`
137145
and provides some additional functionality: it knows how to handle a
138-
hierarchy of roles. For instance, a "ROLE_SUPER_ADMIN" role may have subroles
139-
"ROLE_ADMIN" and "ROLE_USER", so that when a certain object requires the
140-
user to have the "ROLE_ADMIN" role, it grants access to users who in fact
141-
have the "ROLE_ADMIN" role, but also to users having the "ROLE_SUPER_ADMIN"
142-
role.
143-
144-
.. code-block:: php
146+
hierarchy of roles. For instance, a ``ROLE_SUPER_ADMIN`` role may have subroles
147+
``ROLE_ADMIN`` and ``ROLE_USER``, so that when a certain object requires the
148+
user to have the ``ROLE_ADMIN`` role, it grants access to users who in fact
149+
have the ``ROLE_ADMIN`` role, but also to users having the ``ROLE_SUPER_ADMIN``
150+
role::
145151

146152
use Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter;
147153
use Symfony\Component\Security\Core\Role\RoleHierarchy;
@@ -179,26 +185,25 @@ first constructor argument::
179185
.. note::
180186

181187
Most authentication tokens extend from :class:`Symfony\\Component\\Security\\Core\\Authentication\\Token\\AbstractToken`,
182-
which means that the roles given to its constructor, will be
188+
which means that the roles given to its constructor will be
183189
automatically converted from strings to these simple ``Role`` objects.
184190

185191
Using the decision manager
186192
--------------------------
187193

188-
The access listener
194+
The Access Listener
189195
~~~~~~~~~~~~~~~~~~~
190196

191-
Normally, the access decision manager will already be asked to decide whether
192-
or not the current user is entitled to make the current request. This is done
193-
by the :class:`Symfony\\Component\\Security\\Http\\Firewall\\AccessListener`,
197+
The access decision manager can be used at any point in a request to decide whether
198+
or not the current user is entitled to access a given resource. One optional,
199+
but useful, method for restricting access based on a URL pattern is the
200+
:class:`Symfony\\Component\\Security\\Http\\Firewall\\AccessListener`,
194201
which is one of the firewall listeners (see :ref:`firewall_listeners`) that
195-
will be triggered for each request matching the firewall map (see :ref:`firewall`).
202+
is triggered for each request matching the firewall map (see :ref:`firewall`).
196203

197204
It uses an access map (which should be an instance of :class:`Symfony\\Component\\Security\\Http\\AccessMapInterface`)
198205
which contains request matchers and a corresponding set of attributes that
199-
are required for the current user to get access to the application.
200-
201-
.. code-block:: php
206+
are required for the current user to get access to the application::
202207

203208
use Symfony\Component\Security\Http\AccessMap;
204209
use Symfony\Component\HttpFoundation\RequestMatcher;
@@ -219,12 +224,10 @@ Security context
219224
~~~~~~~~~~~~~~~~
220225

221226
The access decision manager is also available to other parts of the application
222-
by means of the :method:`Symfony\\Component\\Security\\Core\\SecurityContext::isGranted`
227+
via the :method:`Symfony\\Component\\Security\\Core\\SecurityContext::isGranted`
223228
method of the :class:`Symfony\\Component\\Security\\Core\\SecurityContext`.
224229
A call to this method will directly delegate the question to the access
225-
decision manager.
226-
227-
.. code-block:: php
230+
decision manager::
228231

229232
use Symfony\Component\Security\SecurityContext;
230233
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

components/security/firewall.rst

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
.. index::
22
single: Security, Firewall
33

4-
The Security Context
5-
====================
4+
The Firewall and Security Context
5+
=================================
66

77
Central to the Security Component is the security context, which is an instance
88
of :class:`Symfony\\Component\\Security\\Core\\SecurityContextInterface`. When all
99
steps in the process of authenticating the user have been taken successfully,
10-
the security context may be asked if the authenticated user has access
11-
to a certain action or resource of the application.
12-
13-
.. code-block:: php
10+
you can ask the security context if the authenticated user has access to a
11+
certain action or resource of the application::
1412

1513
use Symfony\Component\Security\SecurityContext;
1614
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
@@ -25,18 +23,16 @@ to a certain action or resource of the application.
2523

2624
.. _firewall:
2725

28-
A firewall for HTTP requests
29-
============================
26+
A Firewall for HTTP Requests
27+
----------------------------
3028

3129
Authenticating a user is done by the firewall. An application may have
3230
multiple secured areas, so the firewall is configured using a map of these
3331
secured areas. For each of these areas, the map contains a request matcher
3432
and a collection of listeners. The request matcher gives the firewall the
3533
ability to find out if the current request points to a secured area.
3634
The listeners are then asked if the current request can be used to authenticate
37-
the user.
38-
39-
.. code-block:: php
35+
the user::
4036

4137
use Symfony\Component\Security\Http\FirewallMap;
4238
use Symfony\Component\HttpFoundation\RequestMatcher;
@@ -53,10 +49,8 @@ the user.
5349

5450
$map->add($requestMatcher, $listeners, $exceptionListener);
5551

56-
The firewall map will be given to the firewall as it's first argument, together
57-
with the event dispatcher that is used by the :class:`Symfony\\Component\\HttpKernel\\HttpKernel`.
58-
59-
.. code-block:: php
52+
The firewall map will be given to the firewall as its first argument, together
53+
with the event dispatcher that is used by the :class:`Symfony\\Component\\HttpKernel\\HttpKernel`::
6054

6155
use Symfony\Component\Security\Http\Firewall;
6256
use Symfony\Component\HttpKernel\KernelEvents;
@@ -76,33 +70,33 @@ further than allowed.
7670
.. _firewall_listeners:
7771

7872
Firewall listeners
79-
------------------
73+
~~~~~~~~~~~~~~~~~~
8074

8175
When the firewall gets notified of the ``kernel.request`` event, it asks
8276
the firewall map if the request matches one of the secured areas. The first
83-
secured area that matches the request, will return a set of corresponding
77+
secured area that matches the request will return a set of corresponding
8478
firewall listeners (which each implement :class:`Symfony\\Component\\Security\\Http\\Firewall\\ListenerInterface`).
8579
These listeners will all be asked to handle the current request. This basically
8680
means: find out if the current request contains any information by which
8781
the user might be authenticated (for instance the Basic HTTP authentication
88-
listener checks if the request has a header called "PHP_AUTH_USER").
82+
listener checks if the request has a header called ``PHP_AUTH_USER``).
8983

9084
Exception listener
91-
------------------
85+
~~~~~~~~~~~~~~~~~~
9286

9387
If any of the listeners throws an :class:`Symfony\\Component\\Security\\Core\\Exception\\AuthenticationException`,
9488
the exception listener that was provided when adding secured areas to the
9589
firewall map will jump in.
9690

9791
The exception listener determines what happens next, based on the arguments
9892
it received when it was created. It may start the authentication procedure,
99-
maybe ask the user to supply his credentials again (when he has only been
93+
perhaps ask the user to supply his credentials again (when he has only been
10094
authenticated based on a "remember-me" cookie), or transform the exception
10195
into an :class:`Symfony\\Component\\HttpKernel\\Exception\\AccessDeniedHttpException`,
10296
which will eventually result in an "HTTP/1.1 403: Access Denied" response.
10397

10498
Entry points
105-
------------
99+
~~~~~~~~~~~~
106100

107101
When the user is not authenticated at all (i.e. when the security context
108102
has no token yet), the firewall's entry point will be called to "start"
@@ -112,6 +106,26 @@ which has only one method: :method:`Symfony\\Component\\Security\\Http\\EntryPoi
112106
This method receives the current :class:`Symfony\\Component\\HttpFoundation\\Request`
113107
object and the exception by which the exception listener was triggered.
114108
The method should return a :class:`Symfony\\Component\\HttpFoundation\\Response`
115-
object, for instance the page containing the login form, or in the case
116-
of Basic HTTP authentication a response with a "WWW-Authenticate" header,
117-
which will prompt the user to supply his username and password.
109+
object. This could be, for instance, the page containing the login form or,
110+
in the case of Basic HTTP authentication, a response with a ``WWW-Authenticate``
111+
header, which will prompt the user to supply his username and password.
112+
113+
Flow: Firewall, Authentication, Authorization
114+
---------------------------------------------
115+
116+
Hopefully you can now see a little bit about how the "flow" of the security
117+
context works:
118+
119+
#. the Firewall is registered as a listener on the reques;
120+
#. at the beginning of the request, the Firewall checks the firewall map
121+
to see if any firewall should be active for this URL;
122+
#. If a firewall is found on the map for this URL, its listeners are notified
123+
#. each listener checks to see if the current request contains any authentication
124+
information - a listener may (a) authenticate a user, (b) throw an
125+
``AuthenticationException``, or (c) do nothing (because there is no
126+
authentication information on the request);
127+
#. Once a user is authentication, you'll use :doc:`/components/security/authorization`
128+
to deny access to certain resources.
129+
130+
Read the next sections to find out more about :doc:`/components/security/authentication`
131+
and :doc:`/components/security/authorization`.

components/security/introduction.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ Installation
2020
You can install the component in many different ways:
2121

2222
* Use the official Git repository (https://github.com/symfony/Security);
23-
* Install it via PEAR ( `pear.symfony.com/Security`);
24-
* Install it via Composer (`symfony/security` on Packagist).
23+
* :doc:`Install it via Composer</components/using_components>` (``symfony/security`` on `Packagist`_).
2524

2625
Sections
2726
--------

0 commit comments

Comments
 (0)