@@ -5,32 +5,31 @@ Authorization
5
5
=============
6
6
7
7
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
9
9
be returned. The authentication listener should set this token directly
10
10
in the :class: `Symfony\\ Component\\ Security\\ Core\\ SecurityContextInterface `
11
11
using its :method: `Symfony\\ Component\\ Security\\ Core\\ SecurityContextInterface::setToken `
12
12
method.
13
13
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 `.
18
18
19
19
An authorization decision will always be based on a few things:
20
20
21
- The current token
21
+ * The current token
22
22
For instance, the token's :method: `Symfony\\ Component\\ Security\\ Core\\ Authentication\\ Token\\ TokenInterface::getRoles `
23
23
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
26
26
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
31
30
an article or a comment object.
32
31
33
- Access decision manager
32
+ Access Decision Manager
34
33
-----------------------
35
34
36
35
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
39
38
the votes (either positive, negative or neutral) it has received. It
40
39
recognizes several strategies:
41
40
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;
44
43
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;
47
46
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;
50
49
51
50
.. code-block :: php
52
51
@@ -79,23 +78,28 @@ of :class:`Symfony\\Component\\Security\\Core\\Authorization\\Voter\\VoterInterf
79
78
which means they have to implement a few methods which allows the decision
80
79
manager to use them:
81
80
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
89
90
of the class constants of :class: `Symfony\\ Component\\ Security\\ Core\\ Authorization\\ Voter\\ VoterInterface `,
90
91
i.e. ``VoterInterface::ACCESS_GRANTED ``, ``VoterInterface::ACCESS_DENIED ``
91
- or ``VoterInterface::ACCESS_ABSTAIN ``.
92
+ or ``VoterInterface::ACCESS_ABSTAIN ``;
92
93
93
94
The security component contains some standard voters which cover many use
94
95
cases:
95
96
97
+ AuthenticatedVoter
98
+ ~~~~~~~~~~~~~~~~~~
99
+
96
100
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
99
103
level of authentication, i.e. is the user fully authenticated, or only based
100
104
on a "remember-me" cookie, or even authenticated anonymously?
101
105
@@ -118,30 +122,32 @@ on a "remember-me" cookie, or even authenticated anonymously?
118
122
119
123
$vote = $authenticatedVoter->vote($token, $object, array('IS_AUTHENTICATED_FULLY');
120
124
125
+ RoleVoter
126
+ ~~~~~~~~~
127
+
121
128
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
124
131
roles returned by the token's :method: `Symfony\\ Component\\ Security\\ Core\\ Authentication\\ Token\\ TokenInterface::getRoles `
125
- method.
126
-
127
- .. code-block :: php
132
+ method::
128
133
129
134
use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter;
130
135
131
136
$roleVoter = new RoleVoter('ROLE_');
132
137
133
138
$roleVoter->vote($token, $object, 'ROLE_ADMIN');
134
139
140
+ RoleHierarchyVoter
141
+ ~~~~~~~~~~~~~~~~~~
142
+
135
143
The :class: `Symfony\\ Component\\ Security\\ Core\\ Authorization\\ Voter\\ RoleHierarchyVoter `
136
144
extends :class: `Symfony\\ Component\\ Security\\ Core\\ Authorization\\ Voter\\ RoleVoter `
137
145
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::
145
151
146
152
use Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter;
147
153
use Symfony\Component\Security\Core\Role\RoleHierarchy;
@@ -179,26 +185,25 @@ first constructor argument::
179
185
.. note ::
180
186
181
187
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
183
189
automatically converted from strings to these simple ``Role `` objects.
184
190
185
191
Using the decision manager
186
192
--------------------------
187
193
188
- The access listener
194
+ The Access Listener
189
195
~~~~~~~~~~~~~~~~~~~
190
196
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 `,
194
201
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 `).
196
203
197
204
It uses an access map (which should be an instance of :class: `Symfony\\ Component\\ Security\\ Http\\ AccessMapInterface `)
198
205
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::
202
207
203
208
use Symfony\Component\Security\Http\AccessMap;
204
209
use Symfony\Component\HttpFoundation\RequestMatcher;
@@ -219,12 +224,10 @@ Security context
219
224
~~~~~~~~~~~~~~~~
220
225
221
226
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 `
223
228
method of the :class: `Symfony\\ Component\\ Security\\ Core\\ SecurityContext `.
224
229
A call to this method will directly delegate the question to the access
225
- decision manager.
226
-
227
- .. code-block :: php
230
+ decision manager::
228
231
229
232
use Symfony\Component\Security\SecurityContext;
230
233
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
0 commit comments