我正在尝试在HWIOAuthBundle中实现方法public function loadUserByOAuthUserResponse(UserResponseInterface $response)
。当用户登录他的facebook帐户时,我注意到getNickname()
返回始终为null,即使是为该用户指定的也是如此。
这是正常的吗?
public function loadUserByOAuthUserResponse(UserResponseInterface $response) {
$username = $response->getUsername();
$nickname= $response->getNickname(); // it returns always null
//$user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
$user = $this->userManager->findUserBy(array("email" => $response->getEmail()));
$attr = $response->getResponse();
//when the user is registrating
if (null === $user) {
// init some setter functions
$service = $response->getResourceOwner()->getName();
$setter = 'set' . ucfirst($service);
$setter_id = $setter . 'Id';
$setter_token = $setter . 'AccessToken';
// create new user here indepently of which service we are using!
$user = $this->userManager->createUser();
$user->$setter_id($username);
$user->$setter_token($response->getAccessToken());
$user->setUsername($response->getUsername());
//$user->setUsernameCanonical($response->getNickname());
$user->setEmail($response->getEmail());
$user->setEmailCanonical($response->getEmail());
switch ($service) {
case 'facebook':
if (isset($attr['gender'])) {
if ($attr['gender'] == 'male')
$user->setGender(true);
else
$user->setGender(false);
}
if (isset($attr['first_name'])) {
$user->setName($attr['first_name']);
}
if (isset($attr['last_name'])) {
$user->setFamilyname($attr['last_name']);
}
if (isset($attr['location'])) {
if (isset($attr['location']['name'])) {
if ($attr['location']['name']) {
$user->setLocation($attr['location']['name']);
}
}
}
if (isset($attr['hometown'])) {
if (isset($attr['hometown']['name'])) {
if ($attr['hometown']['name']) {
$user->setHometown($attr['hometown']['name']);
}
}
}
if (isset($attr['birthday'])) {
$user->setBirthday(new \DateTime($attr['birthday']));
}
if ($nickname) {
$user->setPassword("aa" . $nickname);
}else $user->setPassword($attr['name']);
break;
case 'google':
// not ready yet!
break;
}
$user->setAccept(true);
$user->setEnabled(true);
// update the user here
$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;
}
答案 0 :(得分:2)