|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @link https://dukt.net/social/ |
|
4
|
|
|
* @copyright Copyright (c) Dukt |
|
5
|
|
|
* @license https://github.com/dukt/social/blob/v2/LICENSE.md |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace dukt\social\elements; |
|
9
|
|
|
|
|
10
|
|
|
use Craft; |
|
11
|
|
|
use craft\base\Element; |
|
12
|
|
|
use craft\elements\db\ElementQueryInterface; |
|
13
|
|
|
use craft\elements\User; |
|
14
|
|
|
use craft\helpers\Html; |
|
15
|
|
|
use dukt\social\elements\db\LoginAccountQuery; |
|
16
|
|
|
use dukt\social\Plugin; |
|
17
|
|
|
use dukt\social\models\Token; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* LoginAccount represents a login account element. |
|
22
|
|
|
* |
|
23
|
|
|
* @property int $userId |
|
24
|
|
|
* @property string $providerHandle |
|
25
|
|
|
* @property string $socialUid |
|
26
|
|
|
* |
|
27
|
|
|
* @author Dukt <[email protected]> |
|
28
|
|
|
* @since 2.0 |
|
29
|
|
|
*/ |
|
30
|
|
|
class LoginAccount extends Element |
|
31
|
|
|
{ |
|
32
|
|
|
// Static |
|
33
|
|
|
// ========================================================================= |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @inheritdoc |
|
37
|
|
|
* |
|
38
|
|
|
* @return LoginAccountQuery The newly created [[LoginAccountQuery]] instance. |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function find(): ElementQueryInterface |
|
41
|
|
|
{ |
|
42
|
|
|
return new LoginAccountQuery(static::class); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @inheritdoc |
|
47
|
|
|
*/ |
|
48
|
|
|
protected static function defineSources(string $context = null): array |
|
49
|
|
|
{ |
|
50
|
|
|
$sources = [ |
|
51
|
|
|
[ |
|
52
|
|
|
'key' => '*', |
|
53
|
|
|
'label' => Craft::t('social', 'All login accounts'), |
|
54
|
|
|
'criteria' => [], |
|
55
|
|
|
'hasThumbs' => false |
|
56
|
|
|
] |
|
57
|
|
|
]; |
|
58
|
|
|
|
|
59
|
|
|
$loginProviders = Plugin::getInstance()->getLoginProviders()->getLoginProviders(); |
|
60
|
|
|
|
|
61
|
|
|
if ($loginProviders) { |
|
|
|
|
|
|
62
|
|
|
$sources[] = ['heading' => Craft::t('social', 'Login Providers')]; |
|
63
|
|
|
|
|
64
|
|
|
foreach ($loginProviders as $loginProvider) { |
|
65
|
|
|
$providerHandle = $loginProvider->getHandle(); |
|
66
|
|
|
$key = 'group:' . $providerHandle; |
|
67
|
|
|
|
|
68
|
|
|
$sources[] = [ |
|
69
|
|
|
'key' => $key, |
|
70
|
|
|
'label' => Craft::t('social', $loginProvider->getName()), |
|
71
|
|
|
'criteria' => ['providerHandle' => $providerHandle], |
|
72
|
|
|
'hasThumbs' => false |
|
73
|
|
|
]; |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $sources; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @inheritdoc |
|
82
|
|
|
*/ |
|
83
|
|
|
protected static function defineSearchableAttributes(): array |
|
84
|
|
|
{ |
|
85
|
|
|
return ['socialUid', 'username', 'firstName', 'lastName', 'fullName', 'email', 'loginProvider']; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @inheritdoc |
|
90
|
|
|
*/ |
|
91
|
|
|
protected static function defineSortOptions(): array |
|
92
|
|
|
{ |
|
93
|
|
|
$attributes = []; |
|
94
|
|
|
$attributes['socialUid'] = Craft::t('social', 'Social User ID'); |
|
95
|
|
|
$attributes['username'] = Craft::t('social', 'Username'); |
|
96
|
|
|
$attributes['email'] = Craft::t('social', 'Email'); |
|
97
|
|
|
$attributes['providerHandle'] = Craft::t('social', 'Login Provider'); |
|
98
|
|
|
$attributes['elements.dateCreated'] = Craft::t('social', 'Date Created'); |
|
99
|
|
|
$attributes['elements.dateUpdated'] = Craft::t('social', 'Date Updated'); |
|
100
|
|
|
$attributes['lastLoginDate'] = Craft::t('social', 'Last Login'); |
|
101
|
|
|
|
|
102
|
|
|
return $attributes; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @inheritdoc |
|
107
|
|
|
*/ |
|
108
|
|
|
protected static function defineTableAttributes(): array |
|
109
|
|
|
{ |
|
110
|
|
|
$attributes = []; |
|
111
|
|
|
$attributes['socialUid'] = ['label' => Craft::t('social', 'Social User ID')]; |
|
112
|
|
|
$attributes['username'] = ['label' => Craft::t('social', 'Username')]; |
|
113
|
|
|
$attributes['fullName'] = ['label' => Craft::t('social', 'Full Name')]; |
|
114
|
|
|
$attributes['email'] = ['label' => Craft::t('social', 'Email')]; |
|
115
|
|
|
$attributes['loginProvider'] = ['label' => Craft::t('social', 'Login Provider')]; |
|
116
|
|
|
$attributes['dateCreated'] = ['label' => Craft::t('social', 'Date Created')]; |
|
117
|
|
|
$attributes['dateUpdated'] = ['label' => Craft::t('social', 'Date Updated')]; |
|
118
|
|
|
$attributes['lastLoginDate'] = ['label' => Craft::t('social', 'Last Login')]; |
|
119
|
|
|
|
|
120
|
|
|
return $attributes; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @inheritdoc |
|
125
|
|
|
*/ |
|
126
|
|
|
protected static function defineDefaultTableAttributes(string $source): array |
|
127
|
|
|
{ |
|
128
|
|
|
return ['username', 'fullName', 'email', 'loginProvider', 'dateCreated', 'lastLoginDate']; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
// Properties |
|
132
|
|
|
// ========================================================================= |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @var |
|
136
|
|
|
*/ |
|
137
|
|
|
public $userId; |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @var |
|
141
|
|
|
*/ |
|
142
|
|
|
public $providerHandle; |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @var |
|
146
|
|
|
*/ |
|
147
|
|
|
public $socialUid; |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @var |
|
151
|
|
|
*/ |
|
152
|
|
|
private $_user; |
|
153
|
|
|
|
|
154
|
|
|
// Public Methods |
|
155
|
|
|
// ========================================================================= |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @inheritdoc |
|
159
|
|
|
*/ |
|
160
|
|
|
/** @noinspection PhpInconsistentReturnPointsInspection */ |
|
161
|
|
|
public function __toString() |
|
162
|
|
|
{ |
|
163
|
|
|
return (string)$this->socialUid; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Authenticate. |
|
168
|
|
|
* |
|
169
|
|
|
* @param Token $token |
|
170
|
|
|
* |
|
171
|
|
|
* @return bool |
|
172
|
|
|
* @throws \yii\base\InvalidConfigException |
|
173
|
|
|
*/ |
|
174
|
|
|
public function authenticate(Token $token): bool |
|
175
|
|
|
{ |
|
176
|
|
|
$socialLoginProvider = Plugin::getInstance()->getLoginProviders()->getLoginProvider($token->providerHandle); |
|
|
|
|
|
|
177
|
|
|
|
|
178
|
|
|
$profile = $socialLoginProvider->getProfile($token); |
|
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
$userFieldMapping = $socialLoginProvider->getUserFieldMapping(); |
|
|
|
|
|
|
181
|
|
|
$socialUid = Craft::$app->getView()->renderString($userFieldMapping['id'], ['profile' => $profile]); |
|
182
|
|
|
|
|
183
|
|
|
return $this->socialUid === $socialUid; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Get login provider. |
|
188
|
|
|
* |
|
189
|
|
|
* @return \dukt\social\base\LoginProvider|null |
|
190
|
|
|
* @throws \yii\base\InvalidConfigException |
|
191
|
|
|
*/ |
|
192
|
|
|
public function getLoginProvider() |
|
193
|
|
|
{ |
|
194
|
|
|
if ($this->providerHandle) { |
|
195
|
|
|
return Plugin::getInstance()->getLoginProviders()->getLoginProvider($this->providerHandle); |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Get user. |
|
201
|
|
|
* |
|
202
|
|
|
* @return User|null |
|
203
|
|
|
*/ |
|
204
|
|
|
public function getUser() |
|
205
|
|
|
{ |
|
206
|
|
|
if ($this->_user === null && $this->userId) { |
|
207
|
|
|
$this->_user = Craft::$app->users->getUserById($this->userId); |
|
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
return $this->_user; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* Gets the user's username. |
|
215
|
|
|
* |
|
216
|
|
|
* @return string|null |
|
217
|
|
|
*/ |
|
218
|
|
|
public function getUsername() |
|
219
|
|
|
{ |
|
220
|
|
|
$user = $this->getUser(); |
|
221
|
|
|
|
|
222
|
|
|
if ($user !== null) { |
|
223
|
|
|
return $user->username; |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Gets the user's first name. |
|
229
|
|
|
* |
|
230
|
|
|
* @return string|null |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getFirstName() |
|
233
|
|
|
{ |
|
234
|
|
|
$user = $this->getUser(); |
|
235
|
|
|
|
|
236
|
|
|
if ($user !== null) { |
|
237
|
|
|
return $user->firstName; |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* Gets the user's last name. |
|
243
|
|
|
* |
|
244
|
|
|
* @return string|null |
|
245
|
|
|
*/ |
|
246
|
|
|
public function getLastName() |
|
247
|
|
|
{ |
|
248
|
|
|
$user = $this->getUser(); |
|
249
|
|
|
|
|
250
|
|
|
if ($user !== null) { |
|
251
|
|
|
return $user->lastName; |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
/** |
|
256
|
|
|
* Gets the user's full name. |
|
257
|
|
|
* |
|
258
|
|
|
* @return string|null |
|
259
|
|
|
*/ |
|
260
|
|
|
public function getFullName() |
|
261
|
|
|
{ |
|
262
|
|
|
$user = $this->getUser(); |
|
263
|
|
|
|
|
264
|
|
|
if ($user !== null) { |
|
265
|
|
|
return $user->getFullName(); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Gets the user's email. |
|
271
|
|
|
* |
|
272
|
|
|
* @return string|null |
|
273
|
|
|
*/ |
|
274
|
|
|
public function getEmail() |
|
275
|
|
|
{ |
|
276
|
|
|
$user = $this->getUser(); |
|
277
|
|
|
|
|
278
|
|
|
if ($user !== null) { |
|
279
|
|
|
return $user->email; |
|
280
|
|
|
} |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
// Indexes, etc. |
|
284
|
|
|
// ------------------------------------------------------------------------- |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @inheritdoc |
|
288
|
|
|
*/ |
|
289
|
|
|
protected function tableAttributeHtml(string $attribute): string |
|
290
|
|
|
{ |
|
291
|
|
|
switch ($attribute) { |
|
292
|
|
|
case 'username': |
|
293
|
|
|
return $this->_usernameTableAttributeHtml(); |
|
294
|
|
|
case 'email': |
|
295
|
|
|
return $this->_emailTableAttributeHtml(); |
|
296
|
|
|
case 'lastLoginDate': |
|
297
|
|
|
return $this->_lastLoginDateTableAttributeHtml(); |
|
298
|
|
|
case 'loginProvider': |
|
299
|
|
|
return $this->_loginProviderTableAttributeHtml(); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
return parent::tableAttributeHtml($attribute); |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
// Events |
|
306
|
|
|
// ------------------------------------------------------------------------- |
|
307
|
|
|
|
|
308
|
|
|
/** |
|
309
|
|
|
* @inheritdoc |
|
310
|
|
|
*/ |
|
311
|
|
|
public function afterSave(bool $isNew) |
|
312
|
|
|
{ |
|
313
|
|
|
if ($isNew) { |
|
314
|
|
|
Craft::$app->db->createCommand() |
|
315
|
|
|
->insert('{{%social_login_accounts}}', [ |
|
316
|
|
|
'id' => $this->id, |
|
317
|
|
|
'userId' => $this->userId, |
|
318
|
|
|
'providerHandle' => $this->providerHandle, |
|
319
|
|
|
'socialUid' => $this->socialUid, |
|
320
|
|
|
]) |
|
321
|
|
|
->execute(); |
|
322
|
|
|
} else { |
|
323
|
|
|
Craft::$app->db->createCommand() |
|
324
|
|
|
->update('{{%social_login_accounts}}', [ |
|
325
|
|
|
'userId' => $this->userId, |
|
326
|
|
|
'providerHandle' => $this->providerHandle, |
|
327
|
|
|
'socialUid' => $this->socialUid, |
|
328
|
|
|
], ['id' => $this->id]) |
|
329
|
|
|
->execute(); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
parent::afterSave($isNew); |
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
|
|
336
|
|
|
// Private Methods |
|
337
|
|
|
// ------------------------------------------------------------------------- |
|
338
|
|
|
|
|
339
|
|
|
/** |
|
340
|
|
|
* @return string |
|
341
|
|
|
* @throws \Twig_Error_Loader |
|
342
|
|
|
* @throws \yii\base\Exception |
|
343
|
|
|
*/ |
|
344
|
|
|
private function _usernameTableAttributeHtml() |
|
345
|
|
|
{ |
|
346
|
|
|
$user = $this->getUser(); |
|
347
|
|
|
|
|
348
|
|
|
return $user ? Craft::$app->getView()->renderTemplate('_elements/element', ['element' => $user]) : ''; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* @return string |
|
353
|
|
|
*/ |
|
354
|
|
|
private function _emailTableAttributeHtml() |
|
355
|
|
|
{ |
|
356
|
|
|
$user = $this->getUser(); |
|
357
|
|
|
|
|
358
|
|
|
return $user ? Html::encodeParams('<a href="mailto:{email}">{email}</a>', ['email' => $user->email]) : ''; |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* @return string |
|
363
|
|
|
* @throws \yii\base\InvalidConfigException |
|
364
|
|
|
*/ |
|
365
|
|
|
private function _lastLoginDateTableAttributeHtml() |
|
366
|
|
|
{ |
|
367
|
|
|
$user = $this->getUser(); |
|
368
|
|
|
|
|
369
|
|
|
return Craft::$app->getFormatter()->asTime($user->lastLoginDate, 'short'); |
|
370
|
|
|
} |
|
371
|
|
|
|
|
372
|
|
|
/** |
|
373
|
|
|
* @return string |
|
374
|
|
|
* @throws \Twig_Error_Loader |
|
375
|
|
|
* @throws \yii\base\Exception |
|
376
|
|
|
* @throws \yii\base\InvalidConfigException |
|
377
|
|
|
*/ |
|
378
|
|
|
private function _loginProviderTableAttributeHtml() |
|
379
|
|
|
{ |
|
380
|
|
|
$loginProvider = $this->getLoginProvider(); |
|
381
|
|
|
|
|
382
|
|
|
return $loginProvider ? Craft::$app->getView()->renderTemplate('social/loginaccounts/_element', ['loginProvider' => $loginProvider]) : ''; |
|
|
|
|
|
|
383
|
|
|
} |
|
384
|
|
|
} |
|
385
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.