Skip to content
Browse files

Refactor the impersonating option

  • Loading branch information...
1 parent 4d5b5e5 commit c20e9e839d627728b13a770a2ea358c001140cd3 @rande rande committed Jul 21, 2012
View
8 CHANGELOG.txt
@@ -1,6 +1,14 @@
CHANGELOG
=========
+### [BC BREAK] 2012-07-21
+
+* change impersonating definition, now the url is defined as a configuration, you don't need to create
+ a custom route anymore.
+
+ * remove ``sonata_user_impersonating`` from the routing file
+ * add ``impersonating_route`` into the ``sonata_user`` configuration section
+
### 2012-06-08
* Introduce new field for user: firstname, lastname, gender, etc ...
View
32 DependencyInjection/Compiler/GlobalVariablesCompilerPass.php
@@ -0,0 +1,32 @@
+<?php
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\UserBundle\DependencyInjection\Compiler;
+
+use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
+use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Symfony\Component\DependencyInjection\Reference;
+
+/**
+ * GlobalVariablesCompilerPass
+ *
+ * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ */
+class GlobalVariablesCompilerPass implements CompilerPassInterface
+{
+ /**
+ * {@inheritdoc}
+ */
+ function process(ContainerBuilder $container)
+ {
+ $container->getDefinition('twig')
+ ->addMethodCall('addGlobal', array('sonata_user', new Reference('sonata.user.twig.global')));
+ }
+}
View
1 DependencyInjection/Configuration.php
@@ -39,6 +39,7 @@ public function getConfigTreeBuilder()
->scalarNode('user_group')->defaultValue('fos_user_user_group')->end()
->end()
->end()
+ ->scalarNode('impersonating_route')->defaultValue('homepage')->end()
->arrayNode('google_authenticator')
->addDefaultsIfNotSet()
->children()
View
3 DependencyInjection/SonataUserExtension.php
@@ -42,6 +42,7 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load(sprintf('admin_%s.xml', $config['manager_type']));
$loader->load('form.xml');
$loader->load('google_authenticator.xml');
+ $loader->load('twig.xml');
if ($config['security_acl']) {
$loader->load('security_acl.xml');
@@ -58,6 +59,8 @@ public function load(array $configs, ContainerBuilder $container)
array('SonataUserBundle:Form:form_admin_fields.html.twig')
));
+ $container->setParameter('sonata.user.impersonating_route', $config['impersonating_route']);
+
$this->configureGoogleAuthenticator($config, $container);
$this->configureShortcut($container);
$this->configureProfile($config, $container);
View
12 Resources/config/twig.xml
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<container xmlns="http://symfony.com/schema/dic/services"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
+
+ <services>
+ <service id="sonata.user.twig.global" class="Sonata\UserBundle\Twig\GlobalVariables" >
+ <argument type="service" id="service_container" />
+ </service>
+ </services>
+</container>
View
7 Resources/doc/reference/advanced_configuration.rst
@@ -21,10 +21,11 @@ Full configuration options:
validation_groups: [Authentication] # Please note : this is not the default value
sonata_user:
- security_acl: false
+ security_acl: false
+ impersonating_route: homepage # or any route you want to use
class:
- user: Application\Sonata\UserBundle\Entity\User
- group: Application\Sonata\UserBundle\Entity\Group
+ user: Application\Sonata\UserBundle\Entity\User
+ group: Application\Sonata\UserBundle\Entity\Group
profile: # Profile Form (firstname, lastname, etc ...)
form:
View
3 Resources/doc/reference/installation.rst
@@ -127,9 +127,6 @@ Add the related security routing information
resource: '@SonataUserBundle/Resources/config/routing/admin_security.xml'
prefix: /admin
-You also need to define a ``sonata_user_impersonating`` route, used as a
-redirection after an user impersonating.
-
Then add a new custom firewall handlers for the admin
.. code-block:: yaml
View
2 Resources/views/Admin/Core/user_block.html.twig
@@ -3,7 +3,7 @@
{{ app.user }}
{% if is_granted('ROLE_PREVIOUS_ADMIN') %}
- <a href="{{ url('sonata_user_impersonating', {'_switch_user': '_exit'}) }}">(exit)</a>
+ <a href="{{ url(sonata_user.getImpersonatingRoute, {'_switch_user': '_exit'}) }}">(exit)</a>
{% endif %}
- <a href="{{ url('sonata_user_admin_security_logout') }}">{{ 'user_block_logout'|trans({}, 'SonataUserBundle') }}</a>
View
2 Resources/views/Admin/Field/impersonating.html.twig
@@ -13,7 +13,7 @@ file that was distributed with this source code.
{% block field %}
{% if object.username != app.user.username %}
- <a href="{{ url('sonata_user_impersonating', {'_switch_user': object.username}) }}" title="{{ 'switch_user'|trans({}, 'SonataUserBundle')}}">
+ <a href="{{ url(sonata_user.getImpersonatingRoute, {'_switch_user': object.username}) }}" title="{{ 'switch_user'|trans({}, 'SonataUserBundle')}}">
<img src="{{ asset('bundles/sonataadmin/famfamfam/group_go.png') }}" alt="{{ 'switch_user'|trans({}, 'SonataUserBundle')}}" />
</a>
{% endif %}
View
9 SonataUserBundle.php
@@ -12,6 +12,7 @@
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
+use Sonata\UserBundle\DependencyInjection\Compiler\GlobalVariablesCompilerPass;
class SonataUserBundle extends Bundle
{
@@ -32,4 +33,12 @@ public function getParent()
{
return $this->parent;
}
+
+ /**
+ * {@inheritdoc}
+ */
+ public function build(ContainerBuilder $container)
+ {
+ $container->addCompilerPass(new GlobalVariablesCompilerPass());
+ }
}
View
40 Twig/GlobalVariables.php
@@ -0,0 +1,40 @@
+<?php
+
+/*
+ * This file is part of the Sonata package.
+ *
+ * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Sonata\UserBundle\Twig;
+
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * GlobalVariables
+ *
+ * @author Thomas Rabaix <thomas.rabaix@sonata-project.org>
+ */
+class GlobalVariables
+{
+ protected $container;
+
+ /**
+ * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
+ */
+ public function __construct(ContainerInterface $container)
+ {
+ $this->container = $container;
+ }
+
+ /**
+ * @return string
+ */
+ public function getImpersonatingRoute()
+ {
+ return $this->container->getParameter('sonata.user.impersonating_route');
+ }
+}

0 comments on commit c20e9e8

Please sign in to comment.
Something went wrong with that request. Please try again.