-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathsecurity.html.twig
409 lines (319 loc) · 11.9 KB
/
security.html.twig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<p>Security is a two-step process whose goal is to prevent a user from accessing a resource that he/she should not have access to.</p>
<p><strong>Authentication</strong></p>
<ul>
<li>Login form</li>
<li>HTTP Authentication</li>
<li>HTTP digest</li>
<li>X.509 certs</li>
<li>Custom auth methods</li>
</ul>
<p><strong>Authorization</strong></p>
<ul>
<li>Access control for URL</li>
<li>Secure object and methods</li>
<li>Access control lists (ACLs)</li>
</ul>
<h3>Basic Example: HTTP Authentication</h3>
{% raw %}<pre><code># app/config/security.yml
security:
firewalls:
secured_area:
pattern: ^/
anonymous: ~
http_basic:
realm: "Secured Demo Area"
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
providers:
in_memory:
memory:
users:
ryan: { password: ryanpass, roles: 'ROLE_USER' }
admin: { password: kitten, roles: 'ROLE_ADMIN' }
encoders:
Symfony\Component\Security\Core\User\User: plaintext
</code></pre>{% endraw %}
<h3>Using a Traditional Login Form</h3>
{% raw %}<pre><code># app/config/security.yml
security:
firewalls:
secured_area:
pattern: ^/
anonymous: ~
form_login:
login_path: /login
check_path: /login_check
</code></pre>{% endraw %}
<p>If you don't need to customize your login_path or check_path values (the values used here are the default values), you can shorten your configuration:</p>
{% raw %}<pre><code>form_login: ~
</code></pre>{% endraw %}
<p>Now we need to create the login routes:</p>
{% raw %}<pre><code># app/config/routing.yml
login:
pattern: /login
defaults: { _controller: AcmeSecurityBundle:Security:login }
login_check:
pattern: /login_check
</code></pre>{% endraw %}
<p>Next, create the controller that will display the login form:</p>
{% raw %}
<pre><code>// src/Acme/SecurityBundle/Controller/SecurityController.php;
namespace Acme\SecurityBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Security\Core\SecurityContext;
class SecurityController extends Controller
{
public function loginAction()
{
$request = $this->getRequest();
$session = $request->getSession();
// get the login error if there is one
if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
$error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
} else {
$error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
$session->remove(SecurityContext::AUTHENTICATION_ERROR);
}
return $this->render('AcmeSecurityBundle:Security:login.html.twig', array(
// last username entered by the user
'last_username' => $session->get(SecurityContext::LAST_USERNAME),
'error' => $error,
));
}
}
</code></pre>{% endraw %}
<b>Upate Since 2.6</b>
{% raw %}
<pre><code>
// Symfony 2.6
public function loginAction()
{
$helper = $this->get('security.authentication_utils');
return $this->render('AcmeSecurityBundle:Security:login.html.twig', array(
'last_username' => $helper->getLastUsername(),
'error' => $helper->getLastAuthenticationError(),
));
}
</code></pre>{% endraw %}
<p>Finally, create the corresponding template:</p>
<pre><code>{% filter escape %}{% raw %}{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
{% if error %}
<div>{{ error.message }}</div>
{% endif %}
<form action="{{ path('login_check') }}" method="post">
<label for="username">Username:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
<label for="password">Password:</label>
<input type="password" id="password" name="_password" />
{#If you want to control the URL the user is redirected to on success (more details below) #}
<input type="hidden" name="_target_path" value="/account" />
<button type="submit">login</button>
</form>{% endraw %}
{% endfilter %}
</code></pre>
<h3>Securing Specific URL Patterns</h3>
<p>The most basic way to secure part of your application is to secure an entire URL pattern. You've seen this already in the first example of this chapter, where anything matching the regular expression pattern ^/admin requires the ROLE_ADMIN role.</p>
{% raw %}<pre><code># app/config/security.yml
security:
# ...
access_control:
- { path: ^/admin/users, roles: ROLE_SUPER_ADMIN }
- { path: ^/admin, roles: ROLE_ADMIN }
</code></pre>{% endraw %}
<h3>Securing by IP</h3>
<p>Here is an example of how you might secure this route from outside access:</p>
{% raw %}<pre><code># app/config/security.yml
security:
# ...
access_control:
- { path: ^/_internal, roles: IS_AUTHENTICATED_ANONYMOUSLY, ip: 127.0.0.1 }
</code></pre>{% endraw %}
<h3>Securing by Channel</h3>
{% raw %}<pre><code># app/config/security.yml
security:
# ...
access_control:
- { path: ^/cart/checkout, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }
</code></pre>{% endraw %}
<h3>Securing a Controller</h3>
{% raw %}<pre><code>/ ...
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
public function helloAction($name)
{
if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) {
throw new AccessDeniedException();
}
//Since 2.6
if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
throw new AccessDeniedException();
}
}
</code></pre>{% endraw %}
<p>You can also choose to install and use the optional JMSSecurityExtraBundle, which can secure your controller using annotations:</p>
{% raw %}<pre><code>// ...
use JMS\SecurityExtraBundle\Annotation\Secure;
/**
* @Secure(roles="ROLE_ADMIN")
*/
public function helloAction($name)
{
// ...
}
</code></pre>{% endraw %}
<h3>Users</h3>
<p><strong>User Providers</strong></p>
{% raw %}
<pre><code># app/config/security.yml
security:
# ...
providers:
default_provider:
memory:
users:
ryan: { password: ryanpass, roles: 'ROLE_USER' }
admin: { password: kitten, roles: 'ROLE_ADMIN' }
</code></pre>{% endraw %}
<p><strong>Loading Users from the Database</strong></p>
<p>Next, configure an entity user provider, and point it to your User class:</p>
{% raw %}<pre><code># app/config/security.yml
security:
providers:
main:
entity: { class: Acme\UserBundle\Entity\User, property: username }
</code></pre>{% endraw %}
<h3>Encoding the User's Password</h3>
{% raw %}
<pre><code># app/config/security.yml
security:
# ...
providers:
in_memory:
memory:
users:
ryan: { password: bb87a29949f3a1ee0559f8a57357487151281386, roles: 'ROLE_USER' }
admin: { password: 74913f5cd5f61ec0bcfdb775414c2fb3d161b620, roles: 'ROLE_ADMIN' }
encoders:
Symfony\Component\Security\Core\User\User:
algorithm: sha1
iterations: 1
encode_as_base64: false
Acme\UserBundle\Entity\User: sha512 //user comes from database
</code></pre>{% endraw %}
<p>To encode the password you can use some online functions such as
<a href="http://www.functions-online.com/sha1.html">functions-online.com</a></p>
<h3>Determine the hasing password in a controller</h3>
{% raw %}<pre><code>$factory = $this->get('security.encoder_factory');
$user = new Acme\UserBundle\Entity\User();
$encoder = $factory->getEncoder($user);
$password = $encoder->encodePassword('ryanpass', $user->getSalt());
$user->setPassword($password);
//Since 2.6
$user = new Acme\UserBundle\Entity\User();
$encoder = $this->container->get('security.password_encoder');
$password = $encoder->encodePassword($user, $plainTextPassword);
</code></pre>{% endraw %}
<h3>Retrieving the User Object</h3>
<p>After authentication, the User object of the current user can be accessed via the security.context service.</p>
{% raw %}<pre><code>public function indexAction()
{
$user = $this->get('security.context')->getToken()->getUser();
//Since 2.6
$user = $this->get('security.token_storage')->getToken()->getUser();
//or
$user = $this->getUser();
}
</code></pre>{% endraw %}
<p>You can also retrieve current user in a twig template by:</p>
{% raw %}<pre><code><p>Username: {{ app.user.username }}</p>
</code></pre>{% endraw %}
<h3>Using Multiple User Providers</h3>
{% raw %}<pre><code># app/config/security.yml
security:
providers:
chain_provider:
chain:
providers: [in_memory, user_db]
in_memory:
memory:
users:
foo: { password: test }
user_db:
entity: { class: Acme\UserBundle\Entity\User, property: username }
</code></pre>{% endraw %}
<p>You can also configure the firewall or individual authentication mechanisms to use a specific provider. Again, unless a provider is specified explicitly, the first provider is always used:</p>
{% raw %}<pre><code># app/config/security.yml
security:
firewalls:
secured_area:
# ...
provider: user_db
http_basic:
realm: "Secured Demo Area"
provider: in_memory
form_login: ~
</code></pre>{% endraw %}
<p>For more information about user provider and firewall configuration, see the
<a href="http://symfony.com/doc/current/reference/configuration/security.html">Security Configuration Reference</a>.
</p>
<h3>Roles</h3>
{% raw %}<pre><code># app/config/security.yml
security:
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
</code></pre>{% endraw %}
<h3>Logging Out</h3>
{% raw %}<pre><code># app/config/security.yml
security:
firewalls:
secured_area:
# ...
logout:
path: /logout
target: /
</code></pre>{% endraw %}
<p>and define the route:</p>
{% raw %}<pre><code># app/config/routing.yml
logout:
pattern: /logout
</code></pre>{% endraw %}
<h3>Access Control in Templates</h3>
<p><strong>Twig</strong></p>
{% raw %}<pre><code>{% if is_granted('ROLE_ADMIN') %}
<a href="...">Delete</a>
{% endif %}
</code></pre>{% endraw %}
<h3>Access Control in Controllers</h3>
{% raw %}
<pre><code>public function indexAction()
{
// show different content to admin users
if ($this->get('security.context')->isGranted('ROLE_ADMIN')) {
// Load admin content here
}
// load other regular content here
}
</code></pre>{% endraw %}
<h3>Switching users</h3>
<p>Sometimes, it's useful to be able to switch from one user to another without having to logout and login again (for instance when you are debugging or trying to understand a bug a user sees that you can't reproduce). This can be easily done by activating the switch_user firewall listener:</p>
{% raw %}<pre><code># app/config/security.yml
security:
firewalls:
main:
# ...
switch_user: true
</code></pre>{% endraw %}
<p>Switch user by:</p>
{% raw %}<pre><code>http://example.com/somewhere?_switch_user=thomas
</code></pre>{% endraw %}
<p>and back to normal user:</p>
{% raw %}<pre><code>http://example.com/somewhere?_switch_user=_exit
</code></pre>{% endraw %}
<p>Secure this behaviour:</p>
{% raw %}<pre><code># app/config/security.yml
security:
firewalls:
main:
# ...
switch_user: { role: ROLE_ADMIN, parameter: _want_to_be_this_user }
</code></pre>{% endraw %}