FOSUBUserProvider必须实现UserProviderInterface

时间:2013-10-24 16:40:05

标签: mongodb symfony fosuserbundle hwioauthbundle

我们正在尝试将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;
    }

}

2 个答案:

答案 0 :(得分:0)

FOSUBUserProvider实现了AccountConnectorInterfaceOAuthAwareUserProviderInterface两个接口,因此无法实现UserProviderInterface

hwioauthbundle中检查docs我发现OAuthUserProvider是实施UserProviderInterface的类。

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

答案 1 :(得分:0)

使用

providers:
    fos_userbundle:
          id: fos_user.user_manager

在'security.yml'而不是(custom)

providers: 
   wf_hwi_provider:
         id: wf_user.fos_user_provider

是吗