0

FosUserBundle を 2 つのユーザー インターフェイスで使用しようとしています。

フロントエンド : / バックエンド : /admin/

そこで、LoginHandler を使用します。

services.xml に FosUserBundle を拡張するバンドル (CulturalStore/UserBundle) があります。

parameters:
    cultural_store_user.component.authentication.handler.logout_success_handler.class: CulturalStore\UserBundle\Component\Authentication\Handler\LogoutSuccessHandler
    cultural_store_user.component.authentication.handler.login_success_handler.class: CulturalStore\UserBundle\Component\Authentication\Handler\LoginSuccessHandler

services:
    cultural_store_user.component.authentication.handler.login_success_handler:
        class:  %cultural_store_user.component.authentication.handler.login_success_handler.class%
        arguments:  [@service_container, @router, @security.context]
        tags:
            - { name: 'monolog.logger', channel: 'security' } 
    cultural_store_user.component.authentication.handler.logout_success_handler:
        class:  %cultural_store_user.component.authentication.handler.logout_success_handler.class%
        arguments:  [@service_container, @router]
        tags:
            - { name: 'monolog.logger', channel: 'security' }

LoginSuccessHandler には次のものがあります。

<?php

namespace CulturalStore\UserBundle\Component\Authentication\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_SUPER_ADMIN'))
        {
            $response = new RedirectResponse($this->router->generate('category_index'));            
        }
        elseif ($this->security->isGranted('ROLE_ADMIN'))
        {
            $response = new RedirectResponse($this->router->generate('category_index'));
        } 
        elseif ($this->security->isGranted('ROLE_USER'))
        {
            // redirect the user to where they were before the login process begun.
            $referer_url = $request->headers->get('referer');

            $response = new RedirectResponse($referer_url);
        }

        return $response;
    }

}

そして LogoutSuccesHandler で:

<?php

namespace CulturalStore\UserBundle\Component\Authentication\Handler;

use Symfony\Component\Security\Http\Logout\LogoutSuccessHandlerInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Bundle\FrameworkBundle\Routing\Router;

class LogoutSuccessHandler implements LogoutSuccessHandlerInterface
{

    protected $router;

    public function __construct(Router $router)
    {
        $this->router = $router;
    }

    public function onLogoutSuccess(Request $request)
    {
        // redirect the user to where they were before the login process begun.
        $referer_url = $request->headers->get('referer');

        $response = new RedirectResponse($referer_url);     
        return $response;
    }

}

最後に、グローバル security.yml には次のものがあります。

firewalls:
        main:
            pattern:        ^/
            anonymous:      true
            provider:       main
            form_login:
                login_path: fos_user_security_login
                check_path: fos_user_security_check
                use_referer : true
                success_handler: cultural_store_user.component.authentication.handler.login_success_handler
                failure_handler: cultural_store_user.component.authentication.handler.login_success_handler
            logout:
                path:       fos_user_security_logout
                target:     /
                success_handler: cultural_store_user.component.authentication.handler.logout_success_handler
            remember_me:
                key:        %secret%

しかし、ウェブサイトにアクセスすると、次のエラーが発生します。

FatalErrorException: Error: Class 'CulturalStore\UserBundle\Component\Authentication\Handler\LoginSuccessHandler' not found in /Users/Juju/Sites/workspace/CS/app/cache/dev/appDevDebugProjectContainer.php line 428

appDevDebugProjectContainer の 428 行目に、次のようなものがあります。

/**
     * Gets the 'cultural_store_user.component.authentication.handler.login_success_handler' service.
     *
     * This service is shared.
     * This method always returns the same instance of the service.
     *
     * @return CulturalStore\UserBundle\Component\Authentication\Handler\LoginSuccessHandler A CulturalStore\UserBundle\Component\Authentication\Handler\LoginSuccessHandler instance.
     */
    protected function getCulturalStoreUser_Component_Authentication_Handler_LoginSuccessHandlerService()
    {
        return $this->services['cultural_store_user.component.authentication.handler.login_success_handler'] = new \CulturalStore\UserBundle\Component\Authentication\Handler\LoginSuccessHandler($this, $this->get('router'), $this->get('security.context'));
    }
4

1 に答える 1