SwitchUserSubscriber.php 1.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
<?php
namespace App\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Http\Event\SwitchUserEvent;
use Symfony\Component\Security\Http\SecurityEvents;

class SwitchUserSubscriber implements EventSubscriberInterface
{
    private $router;

    /**
     * SwitchUserSubscriber constructor.
     *
     * @param RouterInterface $router
     */
    public function __construct(RouterInterface $router)
    {
        $this->router = $router;
    }

    public function onSwitchUser(SwitchUserEvent $event)
    {
        // $request = $event->getRequest();

        // if ($request->hasSession() && $request->getSession()->has('_groupId')) {
        //     $group = $request->getSession()->get('_groupId');
        //     if ($group->getRoles()->contains('ROLE_SUPER_ADMIN')) {
        //         return new RedirectResponse($this->router->generate('sonata_admin_dashboard'));
        //     }
        // }

        // return new RedirectResponse($this->router->generate('index'));

        // if ($request->hasSession() && ($session = $request->getSession)) {
        //     $session->set(
        //         '_locale',
        //         // assuming your User has some getLocale() method
        //         $event->getTargetUser()->getLocale()
        //     );
        // }
    }

    public static function getSubscribedEvents()
    {
        return [
            // constant for security.switch_user
            SecurityEvents::SWITCH_USER => 'onSwitchUser',
        ];
    }
}