|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: device |
|
5
|
|
|
* Date: 19.02.16 |
|
6
|
|
|
* Time: 17:37 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace AppBundle\OAuthProvider; |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
use AppBundle\Entity\User; |
|
13
|
|
|
use Doctrine\ORM\EntityManager; |
|
14
|
|
|
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface; |
|
15
|
|
|
use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthAwareUserProviderInterface; |
|
16
|
|
|
use Symfony\Component\Security\Core\Exception\UnsupportedUserException; |
|
17
|
|
|
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; |
|
18
|
|
|
use Symfony\Component\Security\Core\User\UserInterface; |
|
19
|
|
|
use Symfony\Component\Security\Core\User\UserProviderInterface; |
|
20
|
|
|
|
|
21
|
|
|
class LoginUserProvider implements UserProviderInterface, OAuthAwareUserProviderInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var EntityManager $em */ |
|
24
|
|
|
protected $em; |
|
25
|
|
|
|
|
26
|
27 |
|
public function __construct(EntityManager $em) |
|
|
|
|
|
|
27
|
|
|
{ |
|
28
|
27 |
|
$this->em = $em; |
|
29
|
27 |
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Loads the user by a given UserResponseInterface object. |
|
33
|
|
|
* |
|
34
|
|
|
* @param UserResponseInterface $response |
|
35
|
|
|
* |
|
36
|
|
|
* @return UserInterface |
|
37
|
|
|
* |
|
38
|
|
|
* @throws UsernameNotFoundException if the user is not found |
|
39
|
|
|
*/ |
|
40
|
|
|
public function loadUserByOAuthUserResponse(UserResponseInterface $response) |
|
41
|
|
|
{ |
|
42
|
|
|
$em = $this->em; |
|
43
|
|
|
$type = $response->getResourceOwner()->getName(); |
|
44
|
|
|
|
|
45
|
|
|
$user = $em->getRepository('AppBundle:User')->findOneBy(['email' => $response->getEmail()]); |
|
46
|
|
|
if ($user === null) { |
|
47
|
|
|
$user = new User(); |
|
48
|
|
|
$user->setEmail($response->getEmail()) |
|
49
|
|
|
->setFirstName($response->getFirstName() ?: $response->getUsername()) |
|
50
|
|
|
->setLastName($response->getLastName() ?: $response->getRealname()) |
|
51
|
|
|
->setIsActive(true); |
|
52
|
|
|
$em->persist($user); |
|
53
|
|
|
$em->flush(); |
|
54
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
if ($type === 'facebook') { |
|
57
|
|
|
$user->setFacebookToken($response->getAccessToken()) |
|
58
|
|
|
->setFacebookId($response->getUsername()); |
|
59
|
|
|
} |
|
60
|
|
|
if ($type === 'google') { |
|
61
|
|
|
$user->setGoogleToken($response->getAccessToken()) |
|
62
|
|
|
->setGoogleId($response->getUsername()); |
|
63
|
|
|
} |
|
64
|
|
|
$em->flush(); |
|
65
|
|
|
|
|
66
|
|
|
return $user; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Loads the user for the given username. |
|
71
|
|
|
* |
|
72
|
|
|
* This method must throw UsernameNotFoundException if the user is not |
|
73
|
|
|
* found. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $username The username |
|
76
|
|
|
* |
|
77
|
|
|
* @return UserInterface |
|
78
|
|
|
* |
|
79
|
|
|
* @see UsernameNotFoundException |
|
80
|
|
|
* |
|
81
|
|
|
* @throws UsernameNotFoundException if the user is not found |
|
82
|
|
|
*/ |
|
83
|
|
|
public function loadUserByUsername($username) |
|
84
|
|
|
{ |
|
85
|
|
|
$user = $this->em->getRepository('AppBundle:User')->findOneBy(['email' => $username]); |
|
86
|
|
|
if (!$user) { |
|
87
|
|
|
throw new UsernameNotFoundException(sprintf("User '%s' not found.", $username)); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
return $user; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Refreshes the user for the account interface. |
|
95
|
|
|
* |
|
96
|
|
|
* It is up to the implementation to decide if the user data should be |
|
97
|
|
|
* totally reloaded (e.g. from the database), or if the UserInterface |
|
98
|
|
|
* object can just be merged into some internal array of users / identity |
|
99
|
|
|
* map. |
|
100
|
|
|
* |
|
101
|
|
|
* @param UserInterface $user |
|
102
|
|
|
* |
|
103
|
|
|
* @return UserInterface |
|
104
|
|
|
* |
|
105
|
|
|
* @throws UnsupportedUserException if the account is not supported |
|
106
|
|
|
*/ |
|
107
|
|
|
public function refreshUser(UserInterface $user) |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->loadUserByUsername($user->getUsername()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Whether this provider supports the given user class. |
|
114
|
|
|
* |
|
115
|
|
|
* @param string $class |
|
116
|
|
|
* |
|
117
|
|
|
* @return bool |
|
118
|
|
|
*/ |
|
119
|
|
|
public function supportsClass($class) |
|
120
|
|
|
{ |
|
121
|
|
|
return $class instanceof User; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Connects the response the the user object. |
|
126
|
|
|
* |
|
127
|
|
|
* @param UserInterface $user The user object |
|
128
|
|
|
* @param UserResponseInterface $response The oauth response |
|
129
|
|
|
*/ |
|
130
|
|
|
public function connect(UserInterface $user, UserResponseInterface $response) |
|
|
|
|
|
|
131
|
|
|
{ |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
} |
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.