1

fos ユーザー バンドルと他のバンドルを拡張するバンドルがあります。
ユーザーが認証されたら、ロール管理者または単純なユーザーに応じて、ユーザーをさまざまなビューにリダイレクトしたいと考えています。
私の問題は、リダイレクトを行う場所からログインのコントローラーが見つからないことです。

ロールはUser、データベースに由来するエンティティの属性です。

4

2 に答える 2

10

AuthenticationSuccessHandler インターフェイスを実装するLoginSuccessHandlerを追加する必要があります。

onAuthenticationSuccess()その後、次のようにメソッド内でリダイレクト ロジックを設定できます。

namespace XXX\YourBundler\Handler;

use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Router;

class LoginSuccessHandler implements AuthenticationSuccessHandlerInterface
{   
    protected $router;
    protected $security;

    public function __construct(Router $router, SecurityContext $security)
    {
        $this->router   = $router;
        $this->security = $security;
    }
    public function onAuthenticationSuccess(Request $request, TokenInterface $token)
    {   
        if ($this->security->isGranted('ROLE_XXXX_1'))
        {
            $response = new RedirectResponse($this->router->generate('route_1'));           
        }
        elseif ($this->security->isGranted('ROLE_XXXX_2'))
        {
            $response = new RedirectResponse($this->router->generate('route_2'));
        }
        // ...
    } 
}

ハンドラーもサービスとして登録する必要があります。

parameters:
     security.authentication.success_handler.class: XXX\YourBundler\Handler\AuthenticationSuccessHandler

services:
    security.authentication.customized_success_handler:
        class: %security.authentication.success_handler.class%
        public: false
        arguments:  [@router, @security.context]

次に、次の行をファイアウォール セキュリティ構成に追加します。

  success_handler: security.authentication.customized_success_handler
于 2013-04-15T16:54:45.987 に答える
0

次のように renderLogin 関数を上書きできます。

class SecurityController extends BaseController
{
/**
 * Renders the login template with the given parameters. Overwrite this function in
 * an extended controller to provide additional data for the login template.
 *
 * @param array $data
 *
 * @return \Symfony\Component\HttpFoundation\Response
 */
protected function renderLogin(array $data)
{
    $securityContext = $this->get('security.context');
    $router = $this->container->get('router');
//        if ($this->get('security.context')->getToken()->getUser() instanceof \FOS\UserBundle\Propel\User) {
//            $url = $this->container->get('router')->generate('dashboard');
//            return new RedirectResponse($url);
//        }
    if ($securityContext->isGranted('ROLE_ADMIN')) {
        return new RedirectResponse($router->generate('dashboard'), 307);
    }

    if ($securityContext->isGranted('ROLE_USER')) {
        return new RedirectResponse($router->generate('front_page_home'), 307);
    }

    $requestAttributes = $this->container->get('request')->attributes;
    if ($requestAttributes->get('_route') == 'admin_fos_user_security_login') {
        $template = sprintf('FOSUserBundle:Security:login.html.twig');
        $data['admin'] = true;
    } else {
        $template = sprintf('FOSUserBundle:Security:login.html.twig');
        $data['admin'] = false;
    }

    return $this->container->get('templating')->renderResponse($template, $data);
}


}
于 2015-10-24T12:35:12.117 に答える