Parcourir la source

user role redirect

feature/ticket
Charly il y a 3 ans
Parent
révision
85546f9dd9
2 fichiers modifiés avec 68 ajouts et 30 suppressions
  1. +40
    -16
      Authenticator/LoginFormAuthenticator.php
  2. +28
    -14
      DependencyInjection/Configuration.php

+ 40
- 16
Authenticator/LoginFormAuthenticator.php Voir le fichier

@@ -4,6 +4,7 @@ namespace Lc\SovBundle\Authenticator;

use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Doctrine\EntityManager;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\User\UserInterface as SfUserInterface;
@@ -30,35 +31,39 @@ class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements P
private $urlGenerator;
private $csrfTokenManager;
private $passwordEncoder;
protected $parameterBag;

public function __construct(
EntityManager $entityManager,
UrlGeneratorInterface $urlGenerator,
CsrfTokenManagerInterface $csrfTokenManager,
UserPasswordEncoderInterface $passwordEncoder
) {
EntityManager $entityManager,
UrlGeneratorInterface $urlGenerator,
CsrfTokenManagerInterface $csrfTokenManager,
UserPasswordEncoderInterface $passwordEncoder,
ParameterBagInterface $parameterBag
)
{
$this->entityManager = $entityManager;
$this->urlGenerator = $urlGenerator;
$this->csrfTokenManager = $csrfTokenManager;
$this->passwordEncoder = $passwordEncoder;
$this->parameterBag = $parameterBag;
}

public function supports(Request $request)
{
return self::LOGIN_ROUTE === $request->attributes->get('_route')
&& $request->isMethod('POST');
&& $request->isMethod('POST');
}

public function getCredentials(Request $request)
{
$credentials = [
'email' => $request->request->get('email'),
'password' => $request->request->get('password'),
'csrf_token' => $request->request->get('_csrf_token'),
'email' => $request->request->get('email'),
'password' => $request->request->get('password'),
'csrf_token' => $request->request->get('_csrf_token'),
];
$request->getSession()->set(
Security::LAST_USERNAME,
$credentials['email']
Security::LAST_USERNAME,
$credentials['email']
);

return $credentials;
@@ -72,7 +77,7 @@ class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements P
}

$user = $this->entityManager->getRepository(UserInterface::class)->findOneBy(
['email' => $credentials['email']]
['email' => $credentials['email']]
);

if (!$user) {
@@ -100,13 +105,32 @@ class LoginFormAuthenticator extends AbstractFormLoginAuthenticator implements P
{
$routeName = 'home';
$email = $request->request->get('email');
$user = $this->entityManager->getRepository(UserInterface::class)->findOneBy(['email' => $email]);
$loginRedirection = $this->parameterBag->get('lc_sov.login_redirection');
$useReferer = $loginRedirection['redirect_referer'];
$rolesRedirection = $loginRedirection['roles_redirection'];

if (isset($useReferer) && $useReferer == true) {
$url = $request->request->get('_target_path');

} else {
$user = $this->entityManager->getRepository(UserInterface::class)->findOneBy(['email' => $email]);

if ($user && ($user->hasRole('ROLE_ADMIN') || $user->hasRole('ROLE_SUPER_ADMIN'))) {
$routeName = 'admin_dashboard';
if (!empty($user)) {
$roles = $user->getRoles();

foreach ($rolesRedirection as $roleRedirect) {
if (array_search($roleRedirect['role'], $roles)) {
$routeName = $roleRedirect['redirect'];
}
}
}
}

return new RedirectResponse($this->urlGenerator->generate($routeName));
if (isset($url) && !empty($url)) {
return new RedirectResponse($url);
} else {
return new RedirectResponse($this->urlGenerator->generate($routeName));
}
}

protected function getLoginUrl()

+ 28
- 14
DependencyInjection/Configuration.php Voir le fichier

@@ -12,21 +12,35 @@ use Symfony\Component\Config\Definition\ConfigurationInterface;
*/
class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('lc_sov');
$rootNode = $treeBuilder->getRootNode();
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder('lc_sov');
$rootNode = $treeBuilder->getRootNode();

$rootNode
->children()
//->scalarNode('dashboard_default')->defaultValue('App\Controller\Admin\DashboardController')->end()
->scalarNode('homepage_route')->end()
->end();
$rootNode
->children()
//->scalarNode('dashboard_default')->defaultValue('App\Controller\Admin\DashboardController')->end()
->scalarNode('homepage_route')->end()
->arrayNode('login_redirection')
->children()
->scalarNode('redirect_referer')
->defaultValue(true)
->end()
->arrayNode('roles_redirection')
->arrayPrototype()
->children()
->scalarNode('role')->end()
->scalarNode('redirect')->end()
->end()
->end()
->end()
->end()
->end();


return $treeBuilder;
}
return $treeBuilder;
}
}

Chargement…
Annuler
Enregistrer