Skip to content

Commit d7c9085

Browse files
committed
Merge branch '2.6' into 2.7
* 2.6: Changed PhpStormOpener to PhpStormProtocol [#5402] Being explicit what this applies to (it should not apply to things like >=) [Contributing] [Conventions] Added entry for Yoda conditions Added the "payload" option back Show annotations first Reordered the code blocks to show Annotations, YAML, XML and PHP Fixed the issues reported by @xabbuh Finished the documentation of the new data comparison validators Added information about the new date handling in the comparison constraints and Range Document security.switch_user event Added some more docs about the remember me feature Fixed a minor grammar issue Fixed a minor grammar issue Added support for standard Forwarded header Added support for standard Forwarded header Fixed issues reported by @xabbuh Remove the Propel book chapter and explain why we do that
2 parents 9f62c19 + 6d3d892 commit d7c9085

File tree

11 files changed

+1116
-569
lines changed

11 files changed

+1116
-569
lines changed

book/propel.rst

Lines changed: 13 additions & 526 deletions
Large diffs are not rendered by default.

contributing/code/standards.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ Structure
102102

103103
* Always use `identical comparison`_ unless you need type juggling;
104104

105+
* Use `Yoda conditions`_ when checking a variable against an expression to avoid
106+
an accidental assignment inside the condition statement (this applies to ``==``,
107+
``!=``, ``===``, and ``!==``);
108+
105109
* Add a comma after each array item in a multi-line array, even after the
106110
last one;
107111

@@ -189,3 +193,4 @@ License
189193
.. _`PSR-2`: http://www.php-fig.org/psr/psr-2/
190194
.. _`PSR-4`: http://www.php-fig.org/psr/psr-4/
191195
.. _`identical comparison`: https://php.net/manual/en/language.operators.comparison.php
196+
.. _`Yoda conditions`: https://en.wikipedia.org/wiki/Yoda_conditions

cookbook/request/load_balancer_reverse_proxy.rst

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ an AWS Elastic Load Balancer) or a reverse proxy (e.g. Varnish for
77

88
For the most part, this doesn't cause any problems with Symfony. But, when
99
a request passes through a proxy, certain request information is sent using
10-
special ``X-Forwarded-*`` headers. For example, instead of reading the ``REMOTE_ADDR``
11-
header (which will now be the IP address of your reverse proxy), the user's
12-
true IP will be stored in an ``X-Forwarded-For`` header.
10+
either the standard ``Forwarded`` header or non-standard special ``X-Forwarded-*``
11+
headers. For example, instead of reading the ``REMOTE_ADDR`` header (which
12+
will now be the IP address of your reverse proxy), the user's true IP will be
13+
stored in a standard ``Forwarded: for="..."`` header or a non standard
14+
``X-Forwarded-For`` header.
15+
16+
.. versionadded:: 2.7
17+
``Forwarded`` header support was introduced in Symfony 2.7.
1318

1419
If you don't configure Symfony to look for these headers, you'll get incorrect
1520
information about the client's IP address, whether or not the client is connecting
@@ -57,9 +62,9 @@ the IP address ``192.0.0.1`` or matches the range of IP addresses that use
5762
the CIDR notation ``10.0.0.0/8``. For more details, see the
5863
:ref:`framework.trusted_proxies <reference-framework-trusted-proxies>` option.
5964

60-
That's it! Symfony will now look for the correct ``X-Forwarded-*`` headers
61-
to get information like the client's IP address, host, port and whether or
62-
not the request is using HTTPS.
65+
That's it! Symfony will now look for the correct headers to get information
66+
like the client's IP address, host, port and whether the request is
67+
using HTTPS.
6368

6469
But what if the IP of my Reverse Proxy Changes Constantly!
6570
----------------------------------------------------------
@@ -93,9 +98,14 @@ other information.
9398
My Reverse Proxy Uses Non-Standard (not X-Forwarded) Headers
9499
------------------------------------------------------------
95100

96-
Most reverse proxies store information on specific ``X-Forwarded-*`` headers.
97-
But if your reverse proxy uses non-standard header names, you can configure
101+
Although `RFC 7239`_ recently defined a standard ``Forwarded`` header to disclose
102+
all proxy information, most reverse proxies store information in non-standard
103+
``X-Forwarded-*`` headers.
104+
105+
But if your reverse proxy uses other non-standard header names, you can configure
98106
these (see ":doc:`/components/http_foundation/trusting_proxies`").
107+
99108
The code for doing this will need to live in your front controller (e.g. ``web/app.php``).
100109

101110
.. _`security groups`: http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/using-elb-security-groups.html
111+
.. _`RFC 7239`: http://tools.ietf.org/html/rfc7239

cookbook/security/impersonating_user.rst

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,63 @@ setting:
152152
),
153153
),
154154
));
155+
156+
Events
157+
------
158+
159+
The firewall dispatches the ``security.switch_user`` event right after the impersonation
160+
is completed. The :class:`Symfony\\Component\\Security\\Http\\Event\\SwitchUserEvent` is
161+
passed to the listener, and you can use this to get the user that you are now impersonating.
162+
163+
The cookbook article about
164+
:doc:`Making the Locale "Sticky" during a User's Session </cookbook/session/locale_sticky_session>`
165+
does not update the locale when you impersonate a user. The following code sample will show
166+
how to change the sticky locale:
167+
168+
.. configuration-block::
169+
170+
.. code-block:: yaml
171+
172+
# app/config/services.yml
173+
services:
174+
app.switch_user_listener:
175+
class: AppBundle\EventListener\SwitchUserListener
176+
tags:
177+
- { name: kernel.event_listener, event: security.switch_user, method: onSwitchUser }
178+
179+
.. code-block:: xml
180+
181+
<!-- app/config/services.xml -->
182+
<service id="app.switch_user_listener" class="AppBundle\EventListener\SwitchUserListener">
183+
<tag name="kernel.event_listener" event="security.switch_user" method="onSwitchUser" />
184+
</service>
185+
186+
.. code-block:: php
187+
188+
// app/config/services.php
189+
$container
190+
->register('app.switch_user_listener', 'AppBundle\EventListener\SwitchUserListener')
191+
->addTag('kernel.event_listener', array('event' => 'security.switch_user', 'method' => 'onSwitchUser'))
192+
;
193+
194+
.. caution::
195+
196+
The listener implementation assumes your ``User`` entity has a ``getLocale()`` method.
197+
198+
.. code-block:: php
199+
200+
// src/AppBundle/EventListener/SwitchUserListener.pnp
201+
namespace AppBundle\EventListener;
202+
203+
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
204+
205+
class SwitchUserListener
206+
{
207+
public function onSwitchUser(SwitchUserEvent $event)
208+
{
209+
$event->getRequest()->getSession()->set(
210+
'_locale',
211+
$event->getTargetUser()->getLocale()
212+
);
213+
}
214+
}

cookbook/security/remember_me.rst

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,16 @@ the session lasts using a cookie with the ``remember_me`` firewall option:
1616
1717
# app/config/security.yml
1818
firewalls:
19-
main:
19+
default:
20+
# ...
2021
remember_me:
2122
key: "%secret%"
2223
lifetime: 604800 # 1 week in seconds
2324
path: /
25+
# by default, the feature is enabled by checking a
26+
# checkbox in the login form (see below), uncomment the
27+
# below lines to always enable it.
28+
#always_remember_me: true
2429
2530
.. code-block:: xml
2631
@@ -33,12 +38,16 @@ the session lasts using a cookie with the ``remember_me`` firewall option:
3338
http://symfony.com/schema/dic/services/services-1.0.xsd">
3439
3540
<config>
36-
<firewall>
37-
<!-- lifetime: 604800 seconds = 1 week -->
41+
<firewall name="default">
42+
<!-- ... -->
43+
44+
<!-- by default, the feature is enabled by checking a checkbox
45+
in the login form (see below), add always-remember-me="true"
46+
to always enable it. -->
3847
<remember-me
39-
key="%secret%"
40-
lifetime="604800"
41-
path="/"
48+
key = "%secret%"
49+
lifetime = "604800" <!-- 1 week in seconds -->
50+
path = "/"
4251
/>
4352
</firewall>
4453
</config>
@@ -49,11 +58,16 @@ the session lasts using a cookie with the ``remember_me`` firewall option:
4958
// app/config/security.php
5059
$container->loadFromExtension('security', array(
5160
'firewalls' => array(
52-
'main' => array(
61+
'default' => array(
62+
// ...
5363
'remember_me' => array(
5464
'key' => '%secret%',
5565
'lifetime' => 604800, // 1 week in seconds
5666
'path' => '/',
67+
// by default, the feature is enabled by checking a
68+
// checkbox in the login form (see below), uncomment
69+
// the below lines to always enable it.
70+
//'always_remember_me' => true,
5771
),
5872
),
5973
),
@@ -103,21 +117,30 @@ The ``remember_me`` firewall defines the following configuration options:
103117
"Remember Me" feature is always enabled, regardless of the desire of the
104118
end user.
105119

120+
``token_provider`` (default value: ``null``)
121+
Defines the service id of a token provider to use. By default, tokens are
122+
stored in a cookie. For example, you might want to store the token in a
123+
database, to not have a (hashed) version of the password in a cookie. The
124+
DoctrineBridge comes with a
125+
``Symfony\Bridge\Doctrine\Security\RememberMe\DoctrineTokenProvider`` that
126+
you can use.
127+
106128
Forcing the User to Opt-Out of the Remember Me Feature
107129
------------------------------------------------------
108130

109131
It's a good idea to provide the user with the option to use or not use the
110132
remember me functionality, as it will not always be appropriate. The usual
111133
way of doing this is to add a checkbox to the login form. By giving the checkbox
112-
the name ``_remember_me``, the cookie will automatically be set when the checkbox
113-
is checked and the user successfully logs in. So, your specific login form
114-
might ultimately look like this:
134+
the name ``_remember_me`` (or the name you configured using ``remember_me_parameter``),
135+
the cookie will automatically be set when the checkbox is checked and the user
136+
successfully logs in. So, your specific login form might ultimately look like
137+
this:
115138

116139
.. configuration-block::
117140

118141
.. code-block:: html+jinja
119142

120-
{# src/Acme/SecurityBundle/Resources/views/Security/login.html.twig #}
143+
{# app/Resources/views/security/login.html.twig #}
121144
{% if error %}
122145
<div>{{ error.message }}</div>
123146
{% endif %}
@@ -137,7 +160,7 @@ might ultimately look like this:
137160

138161
.. code-block:: html+php
139162

140-
<!-- src/Acme/SecurityBundle/Resources/views/Security/login.html.php -->
163+
<!-- app/Resources/views/security/login.html.php -->
141164
<?php if ($error): ?>
142165
<div><?php echo $error->getMessage() ?></div>
143166
<?php endif ?>
@@ -159,7 +182,7 @@ might ultimately look like this:
159182
The user will then automatically be logged in on subsequent visits while
160183
the cookie remains valid.
161184

162-
Forcing the User to Re-authenticate before Accessing certain Resources
185+
Forcing the User to Re-Authenticate before Accessing certain Resources
163186
----------------------------------------------------------------------
164187

165188
When the user returns to your site, they are authenticated automatically based

reference/configuration/framework.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ you use PHPstorm on the Mac OS platform, you will do something like:
262262
263263
.. tip::
264264

265-
If you're on a Windows PC, you can install the `PhpStormOpener`_ to
265+
If you're on a Windows PC, you can install the `PhpStormProtocol`_ to
266266
be able to use this.
267267

268268
Of course, since every developer uses a different IDE, it's better to set
@@ -1620,5 +1620,5 @@ Full Default Configuration
16201620
.. _`HTTP Host header attacks`: http://www.skeletonscribe.net/2013/05/practical-http-host-header-attacks.html
16211621
.. _`Security Advisory Blog post`: http://symfony.com/blog/security-releases-symfony-2-0-24-2-1-12-2-2-5-and-2-3-3-released#cve-2013-4752-request-gethost-poisoning
16221622
.. _`Doctrine Cache`: http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/caching.html
1623-
.. _`PhpStormOpener`: https://github.com/pinepain/PhpStormOpener
16241623
.. _`egulias/email-validator`: https://github.com/egulias/EmailValidator
1624+
.. _`PhpStormProtocol`: https://github.com/aik099/PhpStormProtocol

0 commit comments

Comments
 (0)