|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DoS\UserBundle\OAuth; |
|
4
|
|
|
|
|
5
|
|
|
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface; |
|
6
|
|
|
use DoS\UserBundle\Model\UserOAuthInterface; |
|
7
|
|
|
use DoS\UserBundle\Model\UserInterface as DoSUserInterface; |
|
8
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
9
|
|
|
use Sylius\Bundle\UserBundle\OAuth\UserProvider as SyliusUserProvider; |
|
10
|
|
|
use Sylius\Component\User\Model\CustomerInterface; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Loading and ad-hoc creation of a user by an OAuth sign-in provider account. |
|
14
|
|
|
*/ |
|
15
|
|
|
class UserProvider extends SyliusUserProvider |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @param UserResponseInterface|ResourceResponse $response |
|
19
|
|
|
* |
|
20
|
|
|
* @return UserInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected function createUserByOAuthUserResponse(UserResponseInterface $response) |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var DoSUserInterface $user */ |
|
25
|
|
|
$user = $this->userFactory->createNew(); |
|
26
|
|
|
/** @var CustomerInterface $customer */ |
|
27
|
|
|
$customer = $this->customerFactory->createNew(); |
|
28
|
|
|
|
|
29
|
|
|
// set default values taken from OAuth sign-in provider account |
|
30
|
|
|
// todo: check security configuration provide by `fos....username_email` |
|
31
|
|
|
if (null === $response->getEmail()) { |
|
32
|
|
|
throw new AccountNoEmailException(); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
// set default values taken from OAuth sign-in provider account |
|
36
|
|
|
if (null !== $email = $response->getEmail()) { |
|
37
|
|
|
$customer->setEmail($email); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
if (!$user->getUsername()) { |
|
41
|
|
|
$user->setUsername($response->getEmail() ?: $response->getNickname()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// set random password to prevent issue with not nullable field & potential security hole |
|
45
|
|
|
$user->setPlainPassword(substr(sha1($response->getAccessToken()), 0, 10)); |
|
46
|
|
|
|
|
47
|
|
|
$user->setDisplayName($response->getNickname()); |
|
48
|
|
|
$user->setLocale($response->getLocale()); |
|
|
|
|
|
|
49
|
|
|
$user->setCustomer($customer); |
|
50
|
|
|
$user->confirmed(); |
|
51
|
|
|
|
|
52
|
|
|
$customer->setFirstName($response->getFirstName()); |
|
53
|
|
|
$customer->setLastName($response->getLastName()); |
|
54
|
|
|
$customer->setGender($response->getGender() ?: CustomerInterface::UNKNOWN_GENDER); |
|
|
|
|
|
|
55
|
|
|
$customer->setBirthday($response->getBirthday()); |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
return $this->updateUserByOAuthUserResponse($user, $response); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* {@inheritdoc} |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function updateUserByOAuthUserResponse(UserInterface $user, UserResponseInterface $response) |
|
64
|
|
|
{ |
|
65
|
|
|
/** @var UserOAuthInterface $oauth */ |
|
66
|
|
|
$oauth = $this->oauthFactory->createNew(); |
|
67
|
|
|
$oauth->setIdentifier($response->getUsername()); |
|
68
|
|
|
$oauth->setProvider($response->getResourceOwner()->getName()); |
|
69
|
|
|
$oauth->setAccessToken($response->getAccessToken()); |
|
70
|
|
|
$oauth->setProfilePicture($response->getProfilePicture()); |
|
71
|
|
|
|
|
72
|
|
|
/* @var DoSUserInterface $user */ |
|
73
|
|
|
$user->addOAuthAccount($oauth); |
|
74
|
|
|
|
|
75
|
|
|
$this->userManager->persist($user); |
|
76
|
|
|
$this->userManager->flush(); |
|
77
|
|
|
|
|
78
|
|
|
return $user; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: