HWIOAuthBundle自定义服务提供程序参数

时间:2013-03-15 17:45:31

标签: symfony service oauth fosuserbundle hwioauthbundle

我正在尝试设置HWIOAuthBundle以使用FOSUserBundle

在创建扩展FOSUBUserProvider的我自己的用户提供程序时,我执行了以下操作:

namespace Naroga\Reader\CommonBundle\Service\Security;

use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;

class NarogaUserProvider extends FOSUBUserProvider {

    public function loadUserByOAuthUserResponse(UserResponseInterface $response) {

        [...]

    }

}

我的services.yml如下:

naroga.reader.common.security.user_provider:
    class: Naroga\Reader\CommonBundle\Service\Security\NarogaUserProvider
    arguments: [ @fos_user.user_manager ]

每当我运行程序时,都会出现以下错误:

Argument 2 passed to HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider::__construct() must be of the type array, none given, called in

这很有道理,因为FOSUBUserProvider::__construct的签名是public function __construct(UserManagerInterface $userManager, array $properties)

我不知道将什么定义为我的服务的第二个参数,因此它可以覆盖FOSUBUserProvider。我一直在谷歌搜索它,而我发现的只有同样问题的人,没有答案。

我永远感激温柔的灵魂,告诉我第二个参数必须符合FOSUBUserProvider的签名。

谢谢。

2 个答案:

答案 0 :(得分:1)

第二个参数用于映射。 据我了解,这是您的实体中对应OAuth服务和字段名称的名称。

例如,您可以按如下方式发送它们:

naroga.reader.common.security.user_provider:
    class: Naroga\Reader\CommonBundle\Service\Security\NarogaUserProvider
    arguments: [ @fos_user.user_manager, { facebook: facebookId, twitter: twitterId } ]

答案 1 :(得分:1)

如果你想传递一些额外的参数,让我们说使用调度程序激活一个事件,这是我的情况,只是覆盖了构造函数。

use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;

FOSUBUserProvider extends BaseClass
{
    /**
     * @var EventDispatcherInterface
     */
    private $eventDispatcher;

    /**
     * Constructor.
     *
     * @param UserManagerInterface $userManager FOSUB user provider.
     * @param array $properties Property mapping.
     * @param EventDispatcherInterface $eventDispatcher
     */
    public function __construct(
        UserManagerInterface $userManager,
        array $properties,
        EventDispatcherInterface $eventDispatcher)
    {
        $this->userManager     = $userManager;
        $this->properties      = array_merge($this->properties, $properties);
        $this->accessor        = PropertyAccess::createPropertyAccessor();
        $this->eventDispatcher = $eventDispatcher;
    }

服务定义:

tb_user_provider:
    class: "%tb_user_provider.class%"
    arguments: [@fos_user.user_manager, { facebook: facebook_id }, @event_dispatcher]