1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* YAWIK |
4
|
|
|
* |
5
|
|
|
* @filesource |
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
7
|
|
|
* @license MIT |
8
|
|
|
* @author Carsten Bleek <[email protected]> |
9
|
|
|
* @author Mathias Gelhausen <[email protected]> |
10
|
|
|
* @author Miroslav Fedeleš <[email protected]> |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
/** Auth adapter */ |
14
|
|
|
namespace Auth\Adapter; |
15
|
|
|
|
16
|
|
|
use Hybrid_Auth; |
17
|
|
|
use Zend\Authentication\Result; |
18
|
|
|
use Zend\Authentication\Adapter\AdapterInterface; |
19
|
|
|
use Doctrine\MongoDB\GridFSFile; |
20
|
|
|
use Auth\Entity\UserImage; |
21
|
|
|
use Auth\Controller\Plugin\SocialProfiles as SocialProfilePlugin; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This class allows to authenticate with HybridAuth |
25
|
|
|
* |
26
|
|
|
* HybridAuth adapter for \Zend\Authentication |
27
|
|
|
* |
28
|
|
|
* Class HybridAuth |
29
|
|
|
* @package Auth\Adapter |
30
|
|
|
*/ |
31
|
|
|
class HybridAuth implements AdapterInterface |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* HybridAuth instance. |
35
|
|
|
* |
36
|
|
|
* @var Hybrid_Auth |
37
|
|
|
*/ |
38
|
|
|
protected $_hybridAuth; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* User mapper. |
42
|
|
|
* |
43
|
|
|
* @var \Auth\Repository\User |
44
|
|
|
*/ |
45
|
|
|
protected $repository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* HybridAuth provider identifier |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $_provider; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Social profile plugin |
56
|
|
|
* |
57
|
|
|
* @var SocialProfilePlugin |
58
|
|
|
*/ |
59
|
|
|
protected $socialProfilePlugin; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Sets the provider identifier used by HybridAuth. |
63
|
|
|
* |
64
|
|
|
* @param string $provider |
65
|
|
|
* @return HybridAuth |
66
|
|
|
*/ |
67
|
|
|
public function setProvider($provider) |
68
|
|
|
{ |
69
|
|
|
$this->_provider = $provider; |
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets the provider identifier used by HybridAuth. |
75
|
|
|
* |
76
|
|
|
* @return string|null |
77
|
|
|
*/ |
78
|
|
|
public function getProvider() |
79
|
|
|
{ |
80
|
|
|
return $this->_provider; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* {@inheritdoc} |
85
|
|
|
* |
86
|
|
|
* |
87
|
|
|
* @see \Zend\Authentication\Adapter\AdapterInterface::authenticate() |
88
|
|
|
*/ |
89
|
|
|
public function authenticate() |
90
|
|
|
{ |
91
|
|
|
$hybridAuth = $this->getHybridAuth(); |
92
|
|
|
/* @var $adapter \Hybrid_Provider_Model */ |
93
|
|
|
$adapter = $hybridAuth->authenticate($this->_provider); |
94
|
|
|
|
95
|
|
|
$userProfile = $adapter->getUserProfile(); |
96
|
|
|
$email = isset($userProfile->emailVerified) && !empty($userProfile->emailVerified) |
97
|
|
|
? $userProfile->emailVerified |
98
|
|
|
: $userProfile->email; |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
$forceSave = false; |
102
|
|
|
$user = $this->getRepository()->findByProfileIdentifier($userProfile->identifier, $this->_provider, ['allowDeactivated' => true]); |
103
|
|
|
|
104
|
|
|
if (!$user) { |
105
|
|
|
$forceSave = true; |
106
|
|
|
$user = $this->getRepository()->create(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
|
110
|
|
|
$currentInfo = $user->getProfile($this->_provider); |
111
|
|
|
$socialData = []; |
112
|
|
|
|
113
|
|
|
try { |
114
|
|
|
$socialProfile = $this->socialProfilePlugin->fetch($this->_provider); |
115
|
|
|
|
116
|
|
|
if (false !== $socialProfile) { |
117
|
|
|
$socialData = $socialProfile->getData(); |
118
|
|
|
} |
119
|
|
|
} catch (\InvalidArgumentException $e) {} |
|
|
|
|
120
|
|
|
|
121
|
|
|
$newInfo = [ |
122
|
|
|
'auth' => (array) $userProfile, |
123
|
|
|
'data' => $socialData, |
124
|
|
|
]; |
125
|
|
|
|
126
|
|
|
if ($forceSave || $currentInfo != $newInfo) { |
127
|
|
|
$dm = $this->getRepository()->getDocumentManager(); |
128
|
|
|
$userInfo = $user->getInfo(); |
129
|
|
|
|
130
|
|
|
if ('' == $userInfo->getEmail()) { |
131
|
|
|
$userInfo->setEmail($email); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$userInfo->setFirstName($userProfile->firstName); |
135
|
|
|
$userInfo->setLastName($userProfile->lastName); |
136
|
|
|
$userInfo->setBirthDay($userProfile->birthDay); |
137
|
|
|
$userInfo->setBirthMonth($userProfile->birthMonth); |
138
|
|
|
$userInfo->setBirthYear($userProfile->birthYear); |
139
|
|
|
$userInfo->setPostalCode($userProfile->zip); |
140
|
|
|
$userInfo->setCity($userProfile->city); |
141
|
|
|
$userInfo->setStreet($userProfile->address); |
142
|
|
|
$userInfo->setPhone($userProfile->phone); |
143
|
|
|
$userInfo->setGender($userProfile->gender); |
144
|
|
|
|
145
|
|
|
// $user->setLogin($email); // this may cause duplicate key exception |
146
|
|
|
$user->addProfile($this->_provider, $newInfo); |
147
|
|
|
|
148
|
|
|
$dm->persist($user); |
149
|
|
|
// make sure all ids are generated and user exists in database. |
150
|
|
|
$dm->flush(); |
151
|
|
|
|
152
|
|
|
/* |
153
|
|
|
* This must be after flush because a newly created user has no id! |
154
|
|
|
*/ |
155
|
|
|
if ($forceSave || (!$userInfo->getImage() && $userProfile->photoURL)) { |
156
|
|
|
// get user image |
157
|
|
|
if ('' != $userProfile->photoURL) { |
158
|
|
|
$client = new \Zend\Http\Client($userProfile->photoURL, array('sslverifypeer' => false)); |
159
|
|
|
$response = $client->send(); |
160
|
|
|
$file = new GridFSFile(); |
161
|
|
|
$file->setBytes($response->getBody()); |
162
|
|
|
|
163
|
|
|
$userImage = new UserImage(); |
164
|
|
|
$userImage->setName($userProfile->lastName.$userProfile->firstName); |
165
|
|
|
$userImage->setType($response->getHeaders()->get('Content-Type')->getFieldValue()); |
166
|
|
|
$userImage->setUser($user); |
167
|
|
|
$userImage->setFile($file); |
168
|
|
|
$userInfo->setImage($userImage); |
169
|
|
|
$dm->persist($userImage); |
170
|
|
|
//$this->getRepository()->store($user->info); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
// We have to flush again |
174
|
|
|
$dm->flush(); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
|
179
|
|
|
return new Result(Result::SUCCESS, $user->getId(), array('firstLogin' => $forceSave, 'user' => $user)); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Get the Hybrid_Auth object |
184
|
|
|
* |
185
|
|
|
* @return Hybrid_Auth |
186
|
|
|
*/ |
187
|
|
|
public function getHybridAuth() |
188
|
|
|
{ |
189
|
|
|
return $this->_hybridAuth; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Set the Hybrid_Auth object |
194
|
|
|
* |
195
|
|
|
* @param Hybrid_Auth $hybridAuth |
196
|
|
|
* @return HybridAuth |
197
|
|
|
*/ |
198
|
1 |
|
public function setHybridAuth(Hybrid_Auth $hybridAuth) |
199
|
|
|
{ |
200
|
1 |
|
$this->_hybridAuth = $hybridAuth; |
201
|
|
|
|
202
|
1 |
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Sets the user repository |
207
|
|
|
* |
208
|
|
|
* @param $repository |
209
|
|
|
* |
210
|
|
|
* @return $this |
211
|
|
|
*/ |
212
|
1 |
|
public function setRepository($repository) |
213
|
|
|
{ |
214
|
1 |
|
$this->repository = $repository; |
215
|
|
|
|
216
|
1 |
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Gets the user repository |
221
|
|
|
* |
222
|
|
|
* @return \Auth\Repository\User |
223
|
|
|
*/ |
224
|
|
|
public function getRepository() |
225
|
|
|
{ |
226
|
|
|
return $this->repository; |
227
|
|
|
} |
228
|
|
|
/** |
229
|
|
|
* @param SocialProfilePlugin |
230
|
|
|
* @return HybridAuth |
231
|
|
|
*/ |
232
|
1 |
|
public function setSocialProfilePlugin(SocialProfilePlugin $socialProfilePlugin) |
233
|
|
|
{ |
234
|
1 |
|
$this->socialProfilePlugin = $socialProfilePlugin; |
235
|
|
|
|
236
|
1 |
|
return $this; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|