@@ -7,8 +7,9 @@ Authorization
7
7
When any of the authentication providers (see:ref: `authentication_providers `)
8
8
has verified the still unauthenticated token, an authenticated token will
9
9
be returned. The authentication listener should set this token directly
10
- in the:class: `Symfony\\ Component\\ Security\\ Core\\ SecurityContext ` using its
11
- ``setToken() `` method.
10
+ in the:class: `Symfony\\ Component\\ Security\\ Core\\ SecurityContextInterface `
11
+ using its:method: `Symfony\\ Component\\ Security\\ Core\\ SecurityContextInterface::setToken `
12
+ method.
12
13
13
14
From then on, the user is authenticated, i.e. means identified.
14
15
Now, other parts of the application can use the token to decide whether
@@ -18,19 +19,21 @@ This decision will be made by an instance of :class:`Symfony\\Component\\Securit
18
19
An authorization decision will always be based on a few things:
19
20
20
21
The current token
21
- The token`s ``getRoles() `` method will be used to retrieve the roles
22
- of the current user (e.g. "ROLE_SUPER_ADMIN")
22
+ For instance, the token's:method: `Symfony\\ Component\\ Security\\ Core\\ Authentication\\ Token\\ TokenInterface::getRoles `
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.
23
25
A set of attributes
24
26
Each attribute stands for a certain right the user should have, e.g.
25
27
"ROLE_ADMIN" to make sure the user is an administrator.
26
28
An object (optional)
27
29
Any object on which to decide, e.g. the current:class: `Symfony\\ Component\\ HttpFoundation\\ Request `
28
- object.
30
+ object, or an object for which access control needs to be checked, like
31
+ an article or a comment object.
29
32
30
33
Access decision manager
31
34
-----------------------
32
35
33
- Sincechoosing whether or not a user is authorized to perform a certain
36
+ Sincedeciding whether or not a user is authorized to perform a certain
34
37
action can be a complicated process, the standard:class: `Symfony\\ Component\\ Security\\ Core\\ Authorization\\ AccessDecisionManager `
35
38
itself depends on multiple voters, and makes a final verdict based on all
36
39
the votes (either positive, negative or neutral) it has received. It
@@ -45,7 +48,7 @@ recognizes several strategies:
45
48
``unanimous ``
46
49
Only grant access if none of the voters has denied access
47
50
48
- ::
51
+ .. code-block :: php
49
52
50
53
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
51
54
@@ -61,8 +64,12 @@ recognizes several strategies:
61
64
// whether or not to grant access when there is no majority (applies only to the "consensus" strategy)
62
65
$allowIfEqualGrantedDeniedDecisions = ...;
63
66
64
- $accessDecisionManager = new AccessDecisionManager($voters, $strategy,
65
- $allowIfAllAbstainDecisions, $allowIfEqualGrantedDeniedDecisions);
67
+ $accessDecisionManager = new AccessDecisionManager(
68
+ $voters,
69
+ $strategy,
70
+ $allowIfAllAbstainDecisions,
71
+ $allowIfEqualGrantedDeniedDecisions
72
+ );
66
73
67
74
Voters
68
75
------
@@ -92,7 +99,7 @@ and "IS_AUTHENTICATED_ANONYMOUSLY" and grants access based on the current
92
99
level of authentication, i.e. is the user fully authenticated, or only based
93
100
on a "remember-me" cookie, or even authenticated anonymously?
94
101
95
- ::
102
+ .. code-block :: php
96
103
97
104
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
98
105
@@ -104,19 +111,20 @@ on a "remember-me" cookie, or even authenticated anonymously?
104
111
$authenticatedVoter = new AuthenticatedVoter($trustResolver);
105
112
106
113
// instance of Symfony\Component\Security\Core\Authentication\Token\TokenInterface
107
- $token = ...
114
+ $token = ...;
108
115
109
116
// any object
110
- $object = ...
117
+ $object = ...;
111
118
112
119
$vote = $authenticatedVoter->vote($token, $object, array('IS_AUTHENTICATED_FULLY');
113
120
114
121
The:class: `Symfony\\ Component\\ Security\\ Core\\ Authorization\\ Voter\\ RoleVoter `
115
122
supports attributes starting with "ROLE _" and grants access to the user
116
123
when the required "ROLE_*" attributes can all be found in the array of
117
- roles returned by the token's ``getRoles() `` method.
124
+ roles returned by the token's:method: `Symfony\\ Component\\ Security\\ Core\\ Authentication\\ Token\\ TokenInterface::getRoles `
125
+ method.
118
126
119
- ::
127
+ .. code-block :: php
120
128
121
129
use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter;
122
130
@@ -133,7 +141,7 @@ user to have the "ROLE_ADMIN" role, it grants access to users who in fact
133
141
have the "ROLE_ADMIN" role, but also to users having the "ROLE_SUPER_ADMIN"
134
142
role.
135
143
136
- ::
144
+ .. code-block :: php
137
145
138
146
use Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter;
139
147
use Symfony\Component\Security\Core\Role\RoleHierarchy;
@@ -156,11 +164,10 @@ Roles
156
164
157
165
Roles are objects that give expression to a certain right the user has.
158
166
The only requirement is that they implement:class: `Symfony\\ Component\\ Security\\ Core\\ Role\\ RoleInterface `,
159
- which means they should also have a ``getRole() `` method that returns a
160
- string representation of the role itself. The default:class: `Symfony\\ Component\\ Security\\ Core\\ Role\\ Role `
161
- simply returns its first constructor argument:
162
-
163
- ::
167
+ which means they should also have a:method: `Symfony\\ Component\\ Security\\ Core\\ Role\\ Role\\ RoleInterface::getRole `
168
+ method that returns a string representation of the role itself. The default
169
+ :class: `Symfony\\ Component\\ Security\\ Core\\ Role\\ Role ` simply returns its
170
+ first constructor argument::
164
171
165
172
use Symfony\Component\Security\Core\Role\Role;
166
173
@@ -191,7 +198,7 @@ It uses an access map (which should be an instance of :class:`Symfony\\Component
191
198
which contains request matchers and a corresponding set of attributes that
192
199
are required for the current user to get access to the application.
193
200
194
- ::
201
+ .. code-block :: php
195
202
196
203
use Symfony\Component\Security\Http\AccessMap;
197
204
use Symfony\Component\HttpFoundation\RequestMatcher;
@@ -201,23 +208,31 @@ are required for the current user to get access to the application.
201
208
$requestMatcher = new RequestMatcher('^/admin');
202
209
$accessMap->add($requestMatcher, array('ROLE_ADMIN'));
203
210
204
- $accessListener = new AccessListener($securityContext, $accessDecisionManager,
205
- $accessMap, $authenticationManager);
211
+ $accessListener = new AccessListener(
212
+ $securityContext,
213
+ $accessDecisionManager,
214
+ $accessMap,
215
+ $authenticationManager
216
+ );
206
217
207
218
Security context
208
219
~~~~~~~~~~~~~~~~
209
220
210
221
The access decision manager is also available to other parts of the application
211
- by means of the ``isGranted($attribute) `` method of the:class: `Symfony\\ Component\\ Security\\ Core\\ SecurityContext `.
222
+ by means of the:method: `Symfony\\ Component\\ Security\\ Core\\ SecurityContext::isGranted `
223
+ method of the:class: `Symfony\\ Component\\ Security\\ Core\\ SecurityContext `.
212
224
A call to this method will directly delegate the question to the access
213
225
decision manager.
214
226
215
- ::
227
+ .. code-block :: php
216
228
217
229
use Symfony\Component\Security\SecurityContext;
218
230
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
219
231
220
- $securityContext = new SecurityContext();
232
+ $securityContext = new SecurityContext(
233
+ $authenticationManager,
234
+ $accessDecisionManager
235
+ );
221
236
222
237
if (!$securityContext->isGranted('ROLE_ADMIN')) {
223
238
throw new AccessDeniedException();