使用表单登录和HWIOAuthBundle登录

时间:2015-05-26 16:02:02

标签: symfony hwioauthbundle

我尝试通过HWIOAuthBundle完成授权,但我遇到了一些问题。当我第一次通过注册表注册用户然后在注销后,我尝试通过HWIOAuthBundle登录我有错误:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'userl@gmail.com' for key 'UNIQ_957A6479A0D96FBF'

我遵循了post

我替换了

$user = $this->userManager->findUserBy(array($this->getProperty($response) => mb_strtolower($response->getEmail())));

$user = $this->userManager->findUserByEmail($response->getEmail());

但是,当我再次尝试通过Facebook登录时,该页面会重定向我的注册表格

我的自定义权限:

命名空间My \ UserBundle \ Security \ Core \ User;

use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class FOSUBUserProvider extends BaseClass
{

/**
 * {@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)
{


  //  return new Response(var_dump($response->getRealName()) );
   // $check = $this->getDoctrine()->getRepository('MyUserBundle:User')->findOneByEmail_canonical($response->getEmail());
    $username = $response->getUsername();

    $user = $this->userManager->findUserByEmail(array($this->getProperty($response) => mb_strtolower($response->getEmail())));
    //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($response->getRealName());
        $user->setEmail($response->getEmail());
        $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;
}

} 

如果存在具有相同电子邮件的用户,则需要将facebok_id和token添加到数据库中,然后登录。 如何解决这个问题?

1 个答案:

答案 0 :(得分:4)

请尝试使用此解决方案:

<?php

namespace My\UserBundle\Security\Core\User;

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

class FOSUBUserProvider extends BaseClass
{
    /**
     * {@inheritDoc}
     */
    public function connect(UserInterface $user, UserResponseInterface $response)
    {
        $username = $response->getUsername();

        $service = $response->getResourceOwner()->getName();
        $setter = 'set' . ucfirst($service);
        $setterId = $setter . 'Id';
        $setterToken = $setter . 'AccessToken';

        //disconnect previously connected user
        if (null !== $previousUser = $this->userManager->findUserBy(array($this->getProperty($response) => $username))) {
            $previousUser->$setterId(null);
            $previousUser->$setterToken(null);
            $this->userManager->updateUser($previousUser);
        }

        //connect current user
        $user->$setterId($username);
        $user->$setterToken($response->getAccessToken());

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

    /**
     * {@inheritdoc}
     */
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
    {
        $username = $response->getUsername();
        $email = $response->getEmail();

        $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
        if (null === $user) {
            $user = $this->userManager->findUserByEmail($email);

            $service = $response->getResourceOwner()->getName();
            $setter = 'set' . ucfirst($service);
            $setterId = $setter . 'Id';
            $setterToken = $setter . 'AccessToken';

            if (null === $user) {
                $user = $this->userManager->createUser();
                $user->$setterId($username);
                $user->$setterToken($response->getAccessToken());

                $user->setUsername($username);
                $user->setEmail($email);
                $user->setPassword($username);
                $user->setEnabled(true);
                $this->userManager->updateUser($user);

                return $user;
            } else {
                $user->$setterId($username);
                $user->$setterToken($response->getAccessToken());

                $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;
    }
}