Symfony句柄不起作用

时间:2016-08-24 15:43:46

标签: symfony oauth-2.0 registration

所以我按照本教程强制不完整的用户注册,如果他们来自Oauth并且没有在他们的Facebook / Twitter帐户中发送电子邮件。

http://alexfu.it/2012/11/09/force-incomplete-users-to-complete-registration/

在最后一步中有一个句柄功能,它似乎永远不会触发。还有其他东西丢失吗?

namespace FYP\UserBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Security\Http\Authorization\AccessDeniedHandlerInterface;

class UserIncompleteListener implements AccessDeniedHandlerInterface
{
    protected $security;
    protected $router;

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

    public function handle(Request $request, AccessDeniedException $accessDeniedException)
    {   
        if(!is_null($this->security->getToken()->getUser()->getRoles()) &&  $this->security->getToken()->getUser()->getRoles() == array('ROLE_INCOMPLETE_USER')) {
            return new RedirectResponse($this->router->generate('register_complete'));
        }
    }
}

配置:

fyp_user.access_denied.handler:
        class:           FYP\UserBundle\Listener\UserIncompleteListener
        arguments:
            security:    @security.context
            router:      @router

安全性:

 firewalls:
        main:
            access_denied_handler: fyp_user.access_denied.handler

继承我的防火墙:

firewalls:
    main:
        access_denied_handler: fyp_user.access_denied.handler
        pattern: ^/
        anonymous: ~
        oauth:
            failure_path: /login
            login_path:  /login
            check_path: /connect_check
            provider: fos_userbundle
            remember_me:  true
            always_use_default_target_path: false
            default_target_path: /login
            resource_owners:
                facebook: "/external-login/check-facebook"
                paypal: "/external-login/check-paypal"
                amazon: "/external-login/check-amazon"
            oauth_user_provider:
                service: app.provider.oauth
            remember_me:
                key:      %secret%  # pararmeter
                lifetime: 31536000  # 365 days in seconds
                path:     /
                domain:   ~
                always_remember_me: true
        form_login:
            login_path:  /login
            check_path:  /login_check
            success_handler: authentication_handler
            failure_handler: authentication_handler
            csrf_provider: form.csrf_provider
            remember_me:  true
        logout:       true
        anonymous:    true
        switch_user: { role: ROLE_ALLOWED_TO_SWITCH, parameter: _new_user }
        remember_me:
            key:      %secret%  # pararmeter
            lifetime: 31536000  # 365 days in seconds
            path:     /
            domain:   ~
            always_remember_me: true

谢谢!

1 个答案:

答案 0 :(得分:0)

您缺少服务定义中的kernel.event_listener标记。出于某种原因,博客文章作者也没有添加它。

您的服务定义实际上应如下所示:

fyp_user.access_denied.handler:
    class:           FYP\UserBundle\Listener\UserIncompleteListener
    arguments:
        security:    @security.context
        router:      @router
    tags:
        - { name: kernel.event_listener, event: kernel.exception, method: handle }

有关详细信息,请查看events and listenersthis SO answer上的Symfony文档。