-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOAuthUserProvider.php
54 lines (45 loc) · 1.9 KB
/
OAuthUserProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace Owl\UserBundle\Security\User\Provider;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\User\UserChecker;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Class OAuthUserProvider
* @package Owl\UserBundle\Security\User\Provider
*/
class OAuthUserProvider extends BaseClass
{
/**
* {@inheritdoc}
*/
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
$userId = $response->getUsername();
$user = $this->userManager->findUserBy(array($this->getProperty($response) => $userId));
$email = $response->getEmail();
$username = $response->getNickname() ?: $response->getRealName();
if (null === $user) {
$user = $this->userManager->findUserByUsernameAndEmail($username, $email);
if (null === $user || !$user instanceof UserInterface) {
$user = $this->userManager->createUser();
$username = str_replace(' ', '', $username);
$user->setUsername($username);
$user->setEmail($email);
$user->setPassword('');
$user->setEnabled(true);
$user->setOAuthService($response->getResourceOwner()->getName());
$user->setOAuthId($userId);
$user->setOAuthAccessToken($response->getAccessToken());
$this->userManager->updateUser($user);
} else {
throw new AuthenticationException('Username or email has been already used.');
}
} else {
$checker = new UserChecker();
$checker->checkPreAuth($user);
}
return $user;
}
}