-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Moved "Impersonating a User" to a cookbook entry #3331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
weaverryan
merged 3 commits into
symfony:2.3
from
romaricdrigon:feat-impersonating-user
Dec 26, 2013
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
.. index:: | ||
single: Security; Impersonating User | ||
|
||
How to impersonate a User | ||
========================= | ||
|
||
Sometimes, it's useful to be able to switch from one user to another without | ||
having to log out and log in 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: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/security.yml | ||
security: | ||
firewalls: | ||
main: | ||
# ... | ||
switch_user: true | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/security.xml --> | ||
<config> | ||
<firewall> | ||
<!-- ... --> | ||
<switch-user /> | ||
</firewall> | ||
</config> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <!-- app/config/security.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<srv:container xmlns="http://symfony.com/schema/dic/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<config>
<firewall>
<!-- ... -->
<switch-user />
</firewall>
</config>
</srv:container> |
||
|
||
.. code-block:: php | ||
|
||
// app/config/security.php | ||
$container->loadFromExtension('security', array( | ||
'firewalls' => array( | ||
'main'=> array( | ||
// ... | ||
'switch_user' => true | ||
), | ||
), | ||
)); | ||
|
||
To switch to another user, just add a query string with the ``_switch_user`` | ||
parameter and the username as the value to the current URL: | ||
|
||
.. code-block:: text | ||
|
||
http://example.com/somewhere?_switch_user=thomas | ||
|
||
To switch back to the original user, use the special ``_exit`` username: | ||
|
||
.. code-block:: text | ||
|
||
http://example.com/somewhere?_switch_user=_exit | ||
|
||
During impersonation, the user is provided with a special role called | ||
``ROLE_PREVIOUS_ADMIN``. In a template, for instance, this role can be used | ||
to show a link to exit impersonation: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: html+jinja | ||
|
||
{% if is_granted('ROLE_PREVIOUS_ADMIN') %} | ||
<a href="{{ path('homepage', {'_switch_user': '_exit'}) }}">Exit impersonation</a> | ||
{% endif %} | ||
|
||
.. code-block:: html+php | ||
|
||
<?php if ($view['security']->isGranted('ROLE_PREVIOUS_ADMIN')): ?> | ||
<a | ||
href="<?php echo $view['router']->generate('homepage', array( | ||
'_switch_user' => '_exit', | ||
) ?>" | ||
> | ||
Exit impersonation | ||
</a> | ||
<?php endif; ?> | ||
|
||
Of course, this feature needs to be made available to a small group of users. | ||
By default, access is restricted to users having the ``ROLE_ALLOWED_TO_SWITCH`` | ||
role. The name of this role can be modified via the ``role`` setting. For | ||
extra security, you can also change the query parameter name via the ``parameter`` | ||
setting: | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# app/config/security.yml | ||
security: | ||
firewalls: | ||
main: | ||
# ... | ||
switch_user: { role: ROLE_ADMIN, parameter: _want_to_be_this_user } | ||
|
||
.. code-block:: xml | ||
|
||
<!-- app/config/security.xml --> | ||
<config> | ||
<firewall> | ||
<!-- ... --> | ||
<switch-user role="ROLE_ADMIN" parameter="_want_to_be_this_user" /> | ||
</firewall> | ||
</config> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <!-- app/config/security.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<srv:container xmlns="http://symfony.com/schema/dic/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:srv="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services
http://symfony.com/schema/dic/services/services-1.0.xsd">
<config>
<firewall>
<!-- ... -->
<switch-user role="ROLE_ADMIN" parameter="_want_to_be_this_user" />
</firewall>
</config>
</srv:container> |
||
|
||
.. code-block:: php | ||
|
||
// app/config/security.php | ||
$container->loadFromExtension('security', array( | ||
'firewalls' => array( | ||
'main'=> array( | ||
// ... | ||
'switch_user' => array( | ||
'role' => 'ROLE_ADMIN', | ||
'parameter' => '_want_to_be_this_user', | ||
), | ||
), | ||
), | ||
)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ Security | |
|
||
entity_provider | ||
remember_me | ||
impersonating_user | ||
voters | ||
acl | ||
acl_advanced | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
uppercase
impersonate
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed :)