1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\UserBundle\Provider; |
4
|
|
|
|
5
|
|
|
use Cocur\Slugify\Slugify; |
6
|
|
|
use FOS\UserBundle\Model\UserInterface as FOSUserInterface; |
7
|
|
|
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface; |
8
|
|
|
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider; |
9
|
|
|
use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthAwareUserProviderInterface; |
10
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
11
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
12
|
|
|
use Alpixel\Bundle\UserBundle\Event\UserEvent; |
13
|
|
|
use FOS\UserBundle\Doctrine\UserManager; |
14
|
|
|
use FOS\UserBundle\Model\UserManagerInterface; |
15
|
|
|
use Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher; |
16
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
17
|
|
|
use Symfony\Component\HttpFoundation\RequestStack; |
18
|
|
|
|
19
|
|
|
class OAuthUserProvider extends FOSUBUserProvider implements OAuthAwareUserProviderInterface |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
protected $dispatcher; |
23
|
|
|
protected $request; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Constructor. |
27
|
|
|
* |
28
|
|
|
* @param UserManagerInterface $userManager FOSUB user provider. |
29
|
|
|
* @param array $properties Property mapping. |
30
|
|
|
*/ |
31
|
|
|
public function __construct(UserManagerInterface $userManager, array $properties, EventDispatcherInterface $dispatcher) |
32
|
|
|
{ |
33
|
|
|
parent::__construct($userManager, $properties); |
34
|
|
|
$this->dispatcher = $dispatcher; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function setRequest(RequestStack $request_stack) |
38
|
|
|
{ |
39
|
|
|
$this->request = $request_stack->getCurrentRequest(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritDoc} |
44
|
|
|
*/ |
45
|
|
|
public function loadUserByOAuthUserResponse(UserResponseInterface $response) |
46
|
|
|
{ |
47
|
|
|
try { |
48
|
|
|
return parent::loadUserByOAuthUserResponse($response); |
49
|
|
|
} catch (UsernameNotFoundException $e) { |
50
|
|
|
$email = $response->getEmail(); |
51
|
|
|
|
52
|
|
|
if (!empty($email)) { |
53
|
|
|
if (null === $user = $this->userManager->findUserByEmail($email)) { |
54
|
|
|
return $this->createUserByOAuthUserResponse($response); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->updateUserByOAuthUserResponse($user, $response); |
58
|
|
|
} |
59
|
|
|
throw new UsernameNotFoundException(); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
|
|
public function connect(UserInterface $user, UserResponseInterface $response) |
66
|
|
|
{ |
67
|
|
|
$providerName = $response->getResourceOwner()->getName(); |
68
|
|
|
$uniqueId = $response->getUsername(); |
69
|
|
|
$user->addOAuthAccount($providerName, $uniqueId); |
|
|
|
|
70
|
|
|
$this->userManager->updateUser($user); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Ad-hoc creation of user. |
75
|
|
|
* |
76
|
|
|
* @param UserResponseInterface $response |
77
|
|
|
* |
78
|
|
|
* @return User |
79
|
|
|
*/ |
80
|
|
|
protected function createUserByOAuthUserResponse(UserResponseInterface $response) |
81
|
|
|
{ |
82
|
|
|
$user = $this->userManager->createUser(); |
83
|
|
|
$this->updateUserByOAuthUserResponse($user, $response); |
84
|
|
|
|
85
|
|
|
$data = $response->getResponse(); |
86
|
|
|
|
87
|
|
|
$slugify = new Slugify(); |
88
|
|
|
$nickname = $slugify->slugify($data['first_name']); |
89
|
|
|
|
90
|
|
|
// set default values taken from OAuth sign-in provider account |
91
|
|
|
//$user->setBirthdate(new \DateTime($data['birthday'])); |
|
|
|
|
92
|
|
|
$user->setFirstname($data['first_name']); |
93
|
|
|
$user->setLastname($data['last_name']); |
94
|
|
|
|
95
|
|
|
if (null !== $email = $response->getEmail()) { |
96
|
|
|
$user->setEmail($email); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$originalNickname = $nickname; |
100
|
|
|
while (null !== $this->userManager->findUserByUsername($nickname)) { |
101
|
|
|
$nickname = $originalNickname.rand(0, 5000); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$user->setUsername($nickname); |
105
|
|
|
$user->setEnabled(true); |
106
|
|
|
|
107
|
|
|
$event = new UserEvent($user, $this->request); |
|
|
|
|
108
|
|
|
$this->dispatcher->dispatch("user.registration.done", $event); |
109
|
|
|
|
110
|
|
|
return $user; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Attach OAuth sign-in provider account to existing user. |
115
|
|
|
* |
116
|
|
|
* @param FOSUserInterface $user |
117
|
|
|
* @param UserResponseInterface $response |
118
|
|
|
* |
119
|
|
|
* @return FOSUserInterface |
120
|
|
|
*/ |
121
|
|
|
protected function updateUserByOAuthUserResponse(FOSUserInterface $user, UserResponseInterface $response) |
122
|
|
|
{ |
123
|
|
|
$providerName = $response->getResourceOwner()->getName(); |
124
|
|
|
$providerNameSetter = 'set'.ucfirst($providerName).'Id'; |
125
|
|
|
$user->$providerNameSetter($response->getUsername()); |
126
|
|
|
if (!$user->getPassword()) { |
127
|
|
|
// generate unique token |
128
|
|
|
$secret = md5(uniqid(rand(), true)); |
129
|
|
|
$user->setPassword($secret); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $user; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.