您可以擴展HWIOauthBundle的FOSUBUserProvider類並實現所需的邏輯。實施例(相關HWIOauthBundle 0.4.x):
的src /的appbundle /安全/核心/用戶/ FOSUBUserProvider.php
<?php
namespace AppBundle\Security\Core\User;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\Exception\AccountNotLinkedException;
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;
use AppBundle\Entity\User;
use Symfony\Component\Security\Core\User\UserInterface;
use AppBundle\Event\UserEvent;
use AppBundle\Doctrine\UserManager;
class FOSUBUserProvider extends BaseClass
{
/**
* {@inheritDoc}
*/
public function connect(UserInterface $user, UserResponseInterface $response)
{
$id = $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([$this->getProperty($response) => $id])) {
$previousUser->$setterId(null);
$previousUser->$setterToken(null);
$this->userManager->updateUser($previousUser);
}
//connect current user
$user->$setterId($id);
$user->$setterToken($response->getAccessToken());
$this->userManager->updateUser($user);
}
/**
* {@inheritdoc}
*/
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
$id = $response->getUsername();
$email = $response->getEmail();
if (empty($email)) {
throw new AccountNotLinkedException("Email is empty");
}
/** @var User $user */
$user = $this->userManager->findUserBy([$this->getProperty($response) => $id]);
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($id);
$user->$setterToken($response->getAccessToken());
$user->setUsername($id)
->setFirstName($response->getFirstName())
->setLastName($response->getLastName() ?: $response->getRealName())
->setPassword('123456');
$user->setEmail($email);
$user->setEnabled(true);
$this->userManager->updateUser($user);
return $user;
} else {
$user->$setterId($id);
$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;
}
}
services.yml
services:
app.my_user_provider:
class: AppBundle\Security\Core\User\FOSUBUserProvider
arguments: ['@fos_user.user_manager', { facebook: 'facebookId' }]
config.yml
hwi_oauth:
connect:
account_connector: app.my_user_provider
security.yml
oauth:
resource_owners:
facebook: "/login/check-facebook"
login_path: /login
failure_path: /login
oauth_user_provider:
service: app.my_user_provider
[Symfony \ Component \ DependencyInjection \ Exception \ ParameterNotFoundException] 服務「app.my_user_provider」對不存在的參數「facebook_id」具有依賴性。 – Kiddo
facebook_id是Facebook令牌字段名稱的參數。如果您要使用我的示例代碼,請在參數部分的parameters.yml或config.yml中定義它。例如。 facebook_id:facebookId –
我已經改變了我的代碼,並補充說: '服務: app.my_user_provider: 類:HWI \包\ OAuthBundle \安全\核心\用戶\ FOSUBUserProvider 參數:[ '@user_manager',{Facebook的:' %facebook_id%'}]' – Kiddo