|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\components; |
|
4
|
|
|
|
|
5
|
|
|
use app; |
|
6
|
|
|
use app\modules\user\models\UserService; |
|
7
|
|
|
use yii\base\ErrorException; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* AuthClientHelper is a helper class for serving login through social networks and retrieving needed information by api |
|
11
|
|
|
* @package app\components |
|
12
|
|
|
*/ |
|
13
|
|
|
class AuthClientHelper |
|
14
|
|
|
{ |
|
15
|
|
|
public static $ServiceIdMapping = [ |
|
16
|
|
|
'app\modules\user\authclients\GitHub' => 'id', |
|
17
|
|
|
'yii\authclient\clients\YandexOpenId' => 'id', |
|
18
|
|
|
'yii\authclient\clients\Twitter' => 'id', |
|
19
|
|
|
'app\modules\user\authclients\Facebook' => 'id', |
|
20
|
|
|
'app\modules\user\authclients\VKontakte' => 'uid', |
|
21
|
|
|
'yii\authclient\clients\YandexOAuth' => 'id', |
|
22
|
|
|
'yii\authclient\clients\GoogleOAuth' => 'id', |
|
23
|
|
|
'app\modules\user\authclients\PayPal' => 'user_id' |
|
24
|
|
|
]; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Finds service record for current logged client and returns corresponding user. |
|
28
|
|
|
* @param \yii\authclient\BaseClient $client AuthClient instance with social authenticated details(ie. user attributes) |
|
29
|
|
|
* @throws ErrorException |
|
30
|
|
|
* @return app\modules\user\models\User|null |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function findUserByService(\yii\authclient\BaseClient $client) |
|
33
|
|
|
{ |
|
34
|
|
|
$serviceType = $client->className(); |
|
|
|
|
|
|
35
|
|
|
if (isset(static::$ServiceIdMapping[$client->className()])) { |
|
|
|
|
|
|
36
|
|
|
$id_attribute = static::$ServiceIdMapping[$client->className()]; |
|
|
|
|
|
|
37
|
|
|
$attributes = $client->getUserAttributes(); |
|
38
|
|
|
$serviceId = null; |
|
|
|
|
|
|
39
|
|
|
if (isset($attributes[$id_attribute])) { |
|
40
|
|
|
$serviceId = $attributes[$id_attribute]; |
|
41
|
|
|
} else { |
|
42
|
|
|
throw new ErrorException("No user identified supplied by social service."); |
|
43
|
|
|
} |
|
44
|
|
|
/** @var \app\modules\user\models\UserService $service */ |
|
45
|
|
|
$service = UserService::find() |
|
46
|
|
|
->where([ |
|
47
|
|
|
'service_type' => $serviceType, |
|
48
|
|
|
'service_id' => $serviceId, |
|
49
|
|
|
]) |
|
50
|
|
|
->with('user') |
|
51
|
|
|
->one(); |
|
52
|
|
|
|
|
53
|
|
|
if ($service === null) { |
|
54
|
|
|
return null; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $service->user; |
|
58
|
|
|
} else { |
|
59
|
|
|
throw new ErrorException("Unidentified social service used."); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Retrieves additional profile information which can be needed for first-login(registration) |
|
66
|
|
|
* and which was not provided by first api call. |
|
67
|
|
|
* Returns merged user attributes |
|
68
|
|
|
* @param \yii\authclient\BaseClient $client |
|
69
|
|
|
* @return \yii\authclient\BaseClient Client with merged attributes |
|
70
|
|
|
*/ |
|
71
|
|
|
public static function retrieveAdditionalData(\yii\authclient\BaseClient $client) |
|
72
|
|
|
{ |
|
73
|
|
|
$attributes = $client->getUserAttributes(); |
|
74
|
|
|
|
|
75
|
|
|
switch ($client->className()) { |
|
|
|
|
|
|
76
|
|
|
case 'app\modules\user\authclients\GitHub': |
|
77
|
|
|
try { |
|
78
|
|
|
/** @var \app\modules\user\authclients\GitHub $client */ |
|
79
|
|
|
$emails = $client->api('user/emails'); |
|
80
|
|
|
|
|
81
|
|
|
foreach ($emails as $email) { |
|
82
|
|
|
if ($email['primary'] === true) { |
|
83
|
|
|
$attributes['email'] = $email['email']; |
|
84
|
|
|
break; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} catch (\yii\authclient\InvalidResponseException $e) { |
|
89
|
|
|
// no email :-( |
|
90
|
|
|
} |
|
91
|
|
|
break; |
|
92
|
|
|
default: |
|
93
|
|
|
break; |
|
94
|
|
|
} |
|
95
|
|
|
$client->setUserAttributes($attributes); |
|
96
|
|
|
return $client; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Converts service attributes to app\modules\user\models\User model attributes |
|
102
|
|
|
* @param \yii\authclient\BaseClient $client |
|
103
|
|
|
* @return array Array of attributes by model type which we can apply by $model->setAttributes() |
|
104
|
|
|
*/ |
|
105
|
|
|
public static function mapUserAttributesWithService(\yii\authclient\BaseClient $client) |
|
106
|
|
|
{ |
|
107
|
|
|
$mappings = [ |
|
108
|
|
|
'service' => [ |
|
109
|
|
|
// id of user in service |
|
110
|
|
|
'service_id' => static::$ServiceIdMapping, |
|
111
|
|
|
], |
|
112
|
|
|
'user' => [ |
|
113
|
|
|
'username' => [ |
|
114
|
|
|
'app\modules\user\authclients\GitHub' => 'login', |
|
115
|
|
|
'yii\authclient\clients\Twitter' => 'screen_name', |
|
116
|
|
|
'app\modules\user\authclients\VKontakte' => 'nickname', |
|
117
|
|
|
'yii\authclient\clients\YandexOAuth' => 'login', |
|
118
|
|
|
], |
|
119
|
|
|
'email' => [ |
|
120
|
|
|
'app\modules\user\authclients\GitHub' => 'email', |
|
121
|
|
|
'yii\authclient\clients\YandexOpenId' => 'email', |
|
122
|
|
|
'app\modules\user\authclients\Facebook' => 'email', |
|
123
|
|
|
'yii\authclient\clients\YandexOAuth' => 'default_email', |
|
124
|
|
|
], |
|
125
|
|
|
'first_name' => [ |
|
126
|
|
|
'app\modules\user\authclients\Facebook' => 'first_name', |
|
127
|
|
|
'app\modules\user\authclients\VKontakte' => 'first_name', |
|
128
|
|
|
'yii\authclient\clients\YandexOAuth' => 'first_name', |
|
129
|
|
|
], |
|
130
|
|
|
'last_name' => [ |
|
131
|
|
|
'app\modules\user\authclients\Facebook' => 'last_name', |
|
132
|
|
|
'app\modules\user\authclients\VKontakte' => 'last_name', |
|
133
|
|
|
'yii\authclient\clients\YandexOAuth' => 'last_name', |
|
134
|
|
|
], |
|
135
|
|
|
'avatar_url' => [ |
|
136
|
|
|
'app\modules\user\authclients\GitHub' => 'avatar_url', |
|
137
|
|
|
'yii\authclient\clients\Twitter' => 'profile_image_url', |
|
138
|
|
|
'app\modules\user\authclients\VKontakte' => 'photo', |
|
139
|
|
|
], |
|
140
|
|
|
'company' => [ |
|
141
|
|
|
'app\modules\user\authclients\GitHub' => 'company', |
|
142
|
|
|
], |
|
143
|
|
|
'url' => [ |
|
144
|
|
|
'app\modules\user\authclients\GitHub' => 'html_url', |
|
145
|
|
|
], |
|
146
|
|
|
'location' => [ |
|
147
|
|
|
'app\modules\user\authclients\GitHub' => 'location', |
|
148
|
|
|
], |
|
149
|
|
|
], |
|
150
|
|
|
]; |
|
151
|
|
|
|
|
152
|
|
|
$class_name = $client->className(); |
|
|
|
|
|
|
153
|
|
|
$attributes = $client->getUserAttributes(); |
|
154
|
|
|
$result = []; |
|
155
|
|
|
foreach ($mappings as $model_type => $mappings_by_attribute) { |
|
156
|
|
|
$result [$model_type] = []; |
|
157
|
|
|
|
|
158
|
|
|
foreach ($mappings_by_attribute as $attribute => $maps) { |
|
159
|
|
|
if (isset($maps[$class_name])) { |
|
160
|
|
|
$key_in_attributes = $maps[$class_name]; |
|
161
|
|
|
$value = null; |
|
|
|
|
|
|
162
|
|
|
if (is_array($key_in_attributes)) { |
|
163
|
|
|
$value = []; |
|
164
|
|
|
foreach ($key_in_attributes as $key) { |
|
165
|
|
|
if (isset($attributes[$key])) { |
|
166
|
|
|
$value[] = $attributes[$key]; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
if (count($value) > 0) { |
|
170
|
|
|
$value = implode(' ', $value); |
|
171
|
|
|
} else { |
|
172
|
|
|
$value = null; |
|
173
|
|
|
} |
|
174
|
|
|
} else { |
|
175
|
|
|
$value = isset($attributes[$key_in_attributes]) ? $attributes[$key_in_attributes] : null; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
if ($value !== null) { |
|
179
|
|
|
$result[$model_type][$attribute] = $value; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
return $result; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
} |
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.