Skip to content

Commit

Permalink
Refactor the impersonating option
Browse files Browse the repository at this point in the history
  • Loading branch information
rande committed Jul 21, 2012
1 parent 4d5b5e5 commit c20e9e8
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 8 deletions.
8 changes: 8 additions & 0 deletions 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 ...
Expand Down
32 changes: 32 additions & 0 deletions 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 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Expand Up @@ -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()
Expand Down
3 changes: 3 additions & 0 deletions DependencyInjection/SonataUserExtension.php
Expand Up @@ -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');
Expand All @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions 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>
7 changes: 4 additions & 3 deletions Resources/doc/reference/advanced_configuration.rst
Expand Up @@ -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:
Expand Down
3 changes: 0 additions & 3 deletions Resources/doc/reference/installation.rst
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/Admin/Core/user_block.html.twig
Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/Admin/Field/impersonating.html.twig
Expand Up @@ -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 %}
Expand Down
9 changes: 9 additions & 0 deletions SonataUserBundle.php
Expand Up @@ -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
{
Expand All @@ -32,4 +33,12 @@ public function getParent()
{
return $this->parent;
}

/**
* {@inheritdoc}
*/
public function build(ContainerBuilder $container)
{
$container->addCompilerPass(new GlobalVariablesCompilerPass());
}
}
40 changes: 40 additions & 0 deletions 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.