Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor the impersonate option: add a parameters option

  • Loading branch information...
commit 3a4aff6924ceaf723fe4f0196fb548b46ddc9045 1 parent e4c16f3
Thomas rande authored
11 DependencyInjection/Configuration.php
View
@@ -39,7 +39,16 @@ public function getConfigTreeBuilder()
->scalarNode('user_group')->defaultValue('fos_user_user_group')->end()
->end()
->end()
- ->scalarNode('impersonating_route')->defaultValue(false)->end()
+ ->scalarNode('impersonating_route')->end()
+ ->arrayNode('impersonating')
+ ->children()
+ ->scalarNode('route')->defaultValue(false)->end()
+ ->arrayNode('parameters')
+ ->useAttributeAsKey('id')
+ ->prototype('scalar')->end()
+ ->end()
+ ->end()
+ ->end()
->arrayNode('google_authenticator')
->addDefaultsIfNotSet()
->children()
33 DependencyInjection/SonataUserExtension.php
View
@@ -37,6 +37,7 @@ public function load(array $configs, ContainerBuilder $container)
$processor = new Processor();
$configuration = new Configuration();
$config = $processor->processConfiguration($configuration, $configs);
+ $config = $this->fixImpersonating($config);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load(sprintf('admin_%s.xml', $config['manager_type']));
@@ -63,7 +64,7 @@ public function load(array $configs, ContainerBuilder $container)
array('SonataUserBundle:Form:form_admin_fields.html.twig')
));
- $container->setParameter('sonata.user.impersonating_route', $config['impersonating_route']);
+ $container->setParameter('sonata.user.impersonating', $config['impersonating']);
$this->configureGoogleAuthenticator($config, $container);
$this->configureShortcut($container);
@@ -71,6 +72,36 @@ public function load(array $configs, ContainerBuilder $container)
}
/**
+ * @param array $config
+ *
+ * @return array
+ * @throws \RuntimeException
+ */
+ public function fixImpersonating(array $config)
+ {
+ if (isset($config['impersonating']) && isset($config['impersonating_route'])) {
+ throw new \RuntimeException('you can\'t have `impersonating` and `impersonating_route` keys defined at the same time');
+ }
+
+ if (isset($config['impersonating_route'])) {
+ $config['impersonating'] = array(
+ 'route' => $config['impersonating_route'],
+ 'parameters' => array()
+ );
+ }
+
+ if (!isset($config['impersonating']['parameters'])) {
+ $config['impersonating']['parameters'] = array();
+ }
+
+ if (!isset($config['impersonating']['route'])) {
+ $config['impersonating'] = false;
+ }
+
+ return $config;
+ }
+
+ /**
* @param array $config
* @param ContainerBuilder $container
*
6 Resources/doc/reference/advanced_configuration.rst
View
@@ -22,7 +22,11 @@ Full configuration options:
sonata_user:
security_acl: false
- impersonating_route: homepage # or any route you want to use
+
+ impersonating:
+ route: page_slug
+ parameters: { path: / }
+
class: # Entity Classes
user: Application\Sonata\UserBundle\Entity\User
group: Application\Sonata\UserBundle\Entity\Group
23 Resources/doc/reference/two_step_validation.rst
View
@@ -1,40 +1,29 @@
Two Step Validation (with Google Authenticator)
===============================================
-The SonataUserBundle provides an optional layer of security by including a
+The SonataUserBundle provides an optional layer of security by including a
support for a Two Step Validation process.
-When the option is enabled, the login process is done with the following
+When the option is enabled, the login process is done with the following
workflow :
* the user enters the login and password
* if the user get the correct credentials, then
* a code validation form is diplayed
-* at this point the user must enter a time based code provided by the Google
+* at this point the user must enter a time based code provided by the Google
Authenticator application
* the code is valid only once per minute
-So if your login and password are compromised then the hacker must also hold
+So if your login and password are compromised then the hacker must also hold
your phone!
Installation
------------
-Add the following lines to the file ``deps``::
+Add the following line to the ``composer.json`` file::
- [GoogleAuthenticator]
- git=git://github.com/rande/GoogleAuthenticator.php.git
- target=/google-authenticator
-
-Update the autoload.php file::
-
- // app/autoload.php
- $loader->registerNamespaces(array(
- // ...
- 'Google' => __DIR__.'/../vendor/google-authenticator/lib'
- // ...
- ));
+ "sonata-project/google-authenticator": "dev-master"
Edit the configuration file
4 Resources/views/Admin/Core/user_block.html.twig
View
@@ -2,8 +2,8 @@
{% if app.user %}
{{ app.user }}
- {% if is_granted('ROLE_PREVIOUS_ADMIN') and sonata_user.getImpersonatingRoute %}
- <a href="{{ url(sonata_user.getImpersonatingRoute, {'_switch_user': '_exit'}) }}">(exit)</a>
+ {% if is_granted('ROLE_PREVIOUS_ADMIN') and sonata_user.impersonating %}
+ <a href="{{ url(sonata_user.impersonating.route, sonata_user.impersonating.parameters| merge({'_switch_user': '_exit'})) }}">(exit)</a>
{% endif %}
- <a href="{{ url('sonata_user_admin_security_logout') }}">{{ 'user_block_logout'|trans({}, 'SonataUserBundle') }}</a>
4 Resources/views/Admin/Field/impersonating.html.twig
View
@@ -12,8 +12,8 @@ file that was distributed with this source code.
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
- {% if object.username != app.user.username and sonata_user.getImpersonatingRoute %}
- <a href="{{ url(sonata_user.getImpersonatingRoute, {'_switch_user': object.username}) }}" title="{{ 'switch_user'|trans({}, 'SonataUserBundle')}}">
+ {% if object.username != app.user.username and sonata_user.impersonating %}
+ <a href="{{ url(sonata_user.impersonating.route, sonata_user.impersonating.parameters | merge({'_switch_user': object.username})) }}" title="{{ 'switch_user'|trans({}, 'SonataUserBundle')}}">
<img src="{{ asset('bundles/sonataadmin/famfamfam/group_go.png') }}" alt="{{ 'switch_user'|trans({}, 'SonataUserBundle')}}" />
</a>
{% else %}
6 Twig/GlobalVariables.php
View
@@ -23,7 +23,7 @@ class GlobalVariables
protected $container;
/**
- * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
+ * @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
@@ -33,8 +33,8 @@ public function __construct(ContainerInterface $container)
/**
* @return string
*/
- public function getImpersonatingRoute()
+ public function getImpersonating()
{
- return $this->container->getParameter('sonata.user.impersonating_route');
+ return $this->container->getParameter('sonata.user.impersonating');
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.