@@ -3,50 +3,53 @@ How to add "Remember Me" Login Functionality
3
3
4
4
Once a user is authenticated, their credentials are typically stored in the
5
5
session. This means that when the session ends they will be logged out and
6
- have to provide their login details again next time they wish to access the
7
- application. You can allow users to choose to stay logged in for longer than
8
- the session lasts using a cookie with the ``remember_me `` firewall option.
9
- The firewall needs to have a secret key configured, which is used to encrypt
10
- the cookie's content. It also has several options with default values which
6
+ have to provide their login details again next time they wish to access the
7
+ application. You can allow users to choose to stay logged in for longer than
8
+ the session lasts using a cookie with the ``remember_me `` firewall option.
9
+ The firewall needs to have a secret key configured, which is used to encrypt
10
+ the cookie's content. It also has several options with default values which
11
11
are shown here:
12
12
13
13
.. configuration-block ::
14
14
15
15
.. code-block :: yaml
16
16
17
17
# app/config/security.yml
18
+
18
19
firewalls :
19
20
main :
20
21
remember_me :
21
- key : aSecretKey
22
+ key : %secret%
22
23
lifetime : 3600
23
24
path : /
24
25
domain : ~ # Defaults to the current domain from $_SERVER
25
26
26
27
.. code-block :: xml
27
28
28
29
<!-- app/config/security.xml -->
30
+
29
31
<config >
30
32
<firewall >
31
33
<remember-me
32
- key = " aSecretKey "
33
- lifetime = " 3600"
34
- path = " /"
35
- domain = " " <!-- Defaults to the current domain from $_SERVER -->
34
+ key = " %secret% "
35
+ lifetime = " 3600"
36
+ path = " /"
37
+ domain = " " <!-- Defaults to the current domain from $_SERVER -->
36
38
/>
37
39
</firewall >
38
40
</config >
39
41
40
42
.. code-block :: php
41
43
42
44
// app/config/security.php
45
+
43
46
$container->loadFromExtension('security', array(
44
47
'firewalls' => array(
45
48
'main' => array('remember_me' => array(
46
- 'key' => 'aSecretKey ',
47
- 'lifetime' => 3600,
48
- 'path' => '/',
49
- 'domain' => '', // Defaults to the current domain from $_SERVER
49
+ 'key' => '%secret% ',
50
+ 'lifetime' => 3600,
51
+ 'path' => '/',
52
+ 'domain' => '', // Defaults to the current domain from $_SERVER
50
53
)),
51
54
),
52
55
));
@@ -89,7 +92,7 @@ might ultimately look like this:
89
92
90
93
<form action="<?php echo $view['router']->generate('login_check') ?>" method="post">
91
94
<label for="username">Username:</label>
92
- <input type="text" id="username"
95
+ <input type="text" id="username"
93
96
name="_username" value="<?php echo $last_username ?>" />
94
97
95
98
<label for="password">Password:</label>
@@ -117,12 +120,12 @@ before accessing certain resources. For example, you might allow a "remember me"
117
120
user to see basic account information, but then require them to actually
118
121
re-authenticate before modifying that information.
119
122
120
- The security component provides an easy way to do this. In addition to roles
123
+ The security component provides an easy way to do this. In addition to roles
121
124
explicitly assigned to them, users are automatically given one of the following
122
125
roles depending on how they are authenticated:
123
126
124
- * ``IS_AUTHENTICATED_ANONYMOUSLY `` - automatically assigned to a user who is
125
- in a firewall protected part of the site but who has not actually logged in.
127
+ * ``IS_AUTHENTICATED_ANONYMOUSLY `` - automatically assigned to a user who is
128
+ in a firewall protected part of the site but who has not actually logged in.
126
129
This is only possible if anonymous access has been allowed.
127
130
128
131
* ``IS_AUTHENTICATED_REMEMBERED `` - automatically assigned to a user who
@@ -140,14 +143,14 @@ You can use these to control access beyond the explicitly assigned roles.
140
143
role, then you also have the other two roles. In other words, these roles
141
144
represent three levels of increasing "strength" of authentication.
142
145
143
- You can use these additional roles for finer grained control over access to
144
- parts of a site. For example, you may want you user to be able to view their
145
- account at ``/account `` when authenticated by cookie but to have to provide
146
+ You can use these additional roles for finer grained control over access to
147
+ parts of a site. For example, you may want you user to be able to view their
148
+ account at ``/account `` when authenticated by cookie but to have to provide
146
149
their login details to be able to edit the account details. You can do this
147
150
by securing specific controller actions using these roles. The edit action
148
- in the controller could be secured using the service context.
151
+ in the controller could be secured using the service context.
149
152
150
- In the following example, the action is only allowed if the user has the
153
+ In the following example, the action is only allowed if the user has the
151
154
``IS_AUTHENTICATED_FULLY `` role.
152
155
153 156
.. code-block :: php
@@ -186,10 +189,10 @@ which can secure your controller using annotations:
186
189
If you also had an access control in your security configuration that
187
190
required the user to have a ``ROLE_USER `` role in order to access any
188
191
of the account area, then you'd have the following situation:
189
-
192
+
190
193
* If a non-authenticated (or anonymously authenticated user) tries to
191
194
access the account area, the user will be asked to authenticate.
192
-
195
+
193
196
* Once the user has entered his username and password, assuming the
194
197
user receives the ``ROLE_USER `` role per your configuration, the user
195
198
will have the ``IS_AUTHENTICATED_FULLY `` role and be able to access
0 commit comments