HWIOAuthBundle监听器完成工作

时间:2016-01-14 13:41:02

标签: symfony hwioauthbundle

我对HWIOAuthBundle和Google身份验证有疑问,我无法完成OAuthProvider的工作。刷新数据之后,我想要返回实体对象,我在stackoverflow中的某个地方看到了示例。 但是,当我返回$ obj; 我抓到了错误:

捕获致命错误:传递给HWI \ Bundle \ OAuthBundle \ Security \ Core \ Authentication \ Token \ OAuthToken :: __ construct()的参数2必须是类型数组,给定整数,在/ var / www / integra /中调用第109行上的vendor / hwi / oauth-bundle / Security / Core / Authentication / Provider / OAuthProvider.php并定义了

构建此类:

public function __construct($accessToken, array $roles = array())
{
    parent::__construct($roles);

    $this->setRawToken($accessToken);

    parent::setAuthenticated(count($roles) > 0);
}

然后我回来了:

  return new JsonResponse(['accessToken' => $user->getToken(), 'Roles' => $user->getRoles()]);  // I catch error what it loadUserByOAuthUserResponse() must return a UserInterface

类OAuthProvider扩展OAuthUserProvider     {

    protected $container, $em;

    public function __construct(\Doctrine\ORM\EntityManager $em, $container)
    {
        $this->container = $container;
        $this->em = $em;
    }


public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
    $email = $response->getEmail();

    if ($email === null) {
        throw new NotFoundHttpException("User email adress not found", 404);
    }

    $name = $response->getFirstName();
    $surname = $response->getLastName();
    $photo = $response->getProfilePicture();



    $repository = $this->em->getRepository('IdamasBillingBundle:User');
    $user = $repository->searchByNameSurnameEmail($email);

    if($user){

        $login = new User();
        $login->setEmail($email);

        $session = $this->container->get('session');
        $session->set('login', $login);

        return $user;

    } else {

        $user = new User();
        $user->setEmail($email);
        $user->setName($name);
        $user->setSurname($surname);
        $user->setPosition('Just user');
        $user->setRoles(1);
        $user->setPhoto($photo);

        $this->em->persist($user);
        $this->em->flush();

        $session = $this->container->get('session');
        $session->set('login', $user); 

//        return $user;f
        return new JsonResponse(['accessToken' => $user->getToken(), 'Roles' => $user->getRoles()]);  
    }

    //return new RedirectResponse("/billing");
}

}

我该怎么做,重定向到完整的登录页面?

1 个答案:

答案 0 :(得分:0)

User对象应该具有roles属性,并且它必须是数组:

class User {

    protected $roles = [];

    public function getRoles() {
        return $this->roles;
    }

    public function addRole($role) {
        $this->roles[] = $role;
    }

}