Преглед изворни кода

Refactor the impersonating option

Thomas Rabaix пре 13 година
родитељ
комит
c20e9e839d

+ 8 - 0
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 ...

+ 32 - 0
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')));
+    }
+}

+ 1 - 0
DependencyInjection/Configuration.php

@@ -39,6 +39,7 @@ class Configuration implements ConfigurationInterface
                         ->scalarNode('user_group')->defaultValue('fos_user_user_group')->end()
                     ->end()
                 ->end()
+                ->scalarNode('impersonating_route')->defaultValue('homepage')->end()
                 ->arrayNode('google_authenticator')
                     ->addDefaultsIfNotSet()
                     ->children()

+ 3 - 0
DependencyInjection/SonataUserExtension.php

@@ -42,6 +42,7 @@ class SonataUserExtension extends Extension
         $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 @@ class SonataUserExtension extends Extension
             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);

+ 12 - 0
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>

+ 4 - 3
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:

+ 0 - 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

+ 1 - 1
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>

+ 1 - 1
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 %}

+ 9 - 0
SonataUserBundle.php

@@ -12,6 +12,7 @@ namespace Sonata\UserBundle;
 
 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 @@ class SonataUserBundle extends Bundle
     {
         return $this->parent;
     }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function build(ContainerBuilder $container)
+    {
+        $container->addCompilerPass(new GlobalVariablesCompilerPass());
+    }
 }

+ 40 - 0
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');
+    }
+}