0

我们正在尝试按照这里描述的方式一起使用 FOSUserBundle 和 HWIOAuthBundle https://gist.github.com/danvbe/4476697

唯一的区别是我们使用的是 mongodb。触发此错误:

用户提供者“wf\UserBundle\Document\FOSUBUserProvider”必须实现“Symfony\Component\Security\Core\User\UserProviderInterface”

FOSUBUserProvider 是从这个示例中挑选出来的(src 在下面)。不应该 HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider 实现 Symfony\Component\Security\Core\User\UserProviderInterface 吗?

<?php

namespace wf\UserBundle\Document;

use Symfony\Component\Security\Core\User\UserInterface; 
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseFOSUBUserProvider;

class FOSUBUserProvider extends BaseFOSUBUserProvider
{

    /**
     * {@inheritDoc}
     */
    public function connect(UserInterface $user, UserResponseInterface $response)
    {
        $property = $this->getProperty($response);
        $username = $response->getUsername();

        //on connect - get the access token and the user ID
        $service = $response->getResourceOwner()->getName();

        $setter = 'set'.ucfirst($service);
        $setter_id = $setter.'Id';
        $setter_token = $setter.'AccessToken';

        //we "disconnect" previously connected users
        if (null !== $previousUser = $this->userManager->findUserBy(array($property => $username))) {
            $previousUser->$setter_id(null);
            $previousUser->$setter_token(null);
            $this->userManager->updateUser($previousUser);
        }

        //we connect current user
        $user->$setter_id($username);
        $user->$setter_token($response->getAccessToken());

        $this->userManager->updateUser($user);
    }

    /**
     * {@inheritdoc}
     */
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
    {
        $username = $response->getUsername();
        $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
        //when the user is registrating
        if (null === $user) {
            $service = $response->getResourceOwner()->getName();
            $setter = 'set'.ucfirst($service);
            $setter_id = $setter.'Id';
            $setter_token = $setter.'AccessToken';
            // create new user here
            $user = $this->userManager->createUser();
            $user->$setter_id($username);
            $user->$setter_token($response->getAccessToken());
            //I have set all requested data with the user's username
            //modify here with relevant data
            $user->setUsername($username);
            $user->setEmail($username);
            $user->setPassword($username);
            $user->setEnabled(true);
            $this->userManager->updateUser($user);
            return $user;
        }

        //if user exists - go with the HWIOAuth way
        $user = parent::loadUserByOAuthUserResponse($response);

        $serviceName = $response->getResourceOwner()->getName();
        $setter = 'set' . ucfirst($serviceName) . 'AccessToken';

        //update access token
        $user->$setter($response->getAccessToken());

        return $user;
    }

}
4

2 回答 2

0

FOSUBUserProvider implementsAccountConnectorInterface并且 OAuthAwareUserProviderInterface 它们都是接口,因此它们不可能实现UserProviderInterface.

检查文档hwioauthbundle我发现那是 OAuthUserProvider实现 UserProviderInterface.

https://github.com/hwi/HWIOAuthBundle/blob/master/Security/Core/User/OAuthUserProvider.php

于 2013-10-24T18:59:48.173 回答
0

使用

providers:
    fos_userbundle:
          id: fos_user.user_manager

在“security.yml”而不是(自定义)

providers: 
   wf_hwi_provider:
         id: wf_user.fos_user_provider

可以

于 2013-10-24T20:36:47.023 回答