Skip to content

Commit 2870bba

Browse files
update readme
1 parent 1f73f1e commit 2870bba

1 file changed

Lines changed: 235 additions & 0 deletions

File tree

controllers/AuthController.php

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
<?php
2+
/**
3+
* @author Semenov Alexander <[email protected]>
4+
* @link http://skeeks.com/
5+
* @copyright 2010 SkeekS (СкикС)
6+
* @date 16.04.2016
7+
*/
8+
namespace skeeks\cms\authclient\controllers;
9+
10+
/**
11+
* Class AuthController
12+
* @package skeeks\cms\modules\admin\controllers
13+
*/
14+
class AuthController extends Controller
15+
{
16+
/**
17+
* @inheritdoc
18+
*/
19+
public function actions()
20+
{
21+
return [
22+
23+
'client' => [
24+
'class' => 'yii\authclient\AuthAction',
25+
'successCallback' => [$this, 'onAuthSuccess'],
26+
'successUrl' => Url::to(['/cms/user/profile']),
27+
'cancelUrl' => Url::to(['/cms/user/profile']),
28+
],
29+
];
30+
}
31+
32+
33+
/**
34+
* @param BaseOAuth $client
35+
* @throws \yii\db\Exception
36+
*/
37+
public function onAuthSuccess($client)
38+
{
39+
\Yii::info('start auth client: ' . $client->id, 'authClient');
40+
41+
$attributes = $client->getUserAttributes();
42+
43+
/* @var $userAuthClient UserAuthClient */
44+
$userAuthClient = UserAuthClient::find()->where([
45+
'provider' => $client->id,
46+
'provider_identifier' => ArrayHelper::getValue($attributes, 'id'),
47+
])->one();
48+
49+
if (\Yii::$app->user->isGuest)
50+
{
51+
if ($userAuthClient)
52+
{
53+
// Все просто идет авторизация
54+
$userAuthClient->provider_data = $attributes;
55+
$userAuthClient->save();
56+
57+
$user = $userAuthClient->user;
58+
\Yii::$app->user->login($user);
59+
60+
} else
61+
{
62+
// Регистрация
63+
64+
/**
65+
* @var $user User
66+
*/
67+
$user = null;
68+
//Если соц сеть вернула нам email то на него можно опираться.
69+
if ($emailFromAuthClient = ArrayHelper::getValue($attributes, 'email'))
70+
{
71+
//Нашли email
72+
$userEmailModel = CmsUserEmail::find()->where(['value' => $emailFromAuthClient])
73+
//->andWhere(['approved' => Cms::BOOL_Y])
74+
->one();
75+
if ($userEmailModel)
76+
{
77+
if ($userEmailModel->user)
78+
{
79+
$user = $userEmailModel->user;
80+
}
81+
}
82+
}
83+
84+
if (!$user)
85+
{
86+
$userClassName = \Yii::$app->user->identityClass;
87+
$user = new $userClassName();
88+
$user->populate();
89+
90+
if (!$user->save())
91+
{
92+
\Yii::error("Не удалось создать пользователя: " . serialize($user->getErrors()), 'authClient');
93+
return false;
94+
}
95+
96+
97+
//Тут можно обновить данные пользователя.
98+
if ($login = ArrayHelper::getValue($attributes, 'screen_name'))
99+
{
100+
$user->username = $login;
101+
if (!$user->save())
102+
{
103+
\Yii::error("Не удалось обновить данные пользователя: " . serialize($user->getErrors()), 'authClient');
104+
}
105+
}
106+
107+
108+
//Тут можно обновить данные пользователя.
109+
if ($login = ArrayHelper::getValue($attributes, 'login'))
110+
{
111+
$user->username = $login;
112+
if (!$user->save())
113+
{
114+
\Yii::error("Не удалось обновить данные пользователя: " . serialize($user->getErrors()), 'authClient');
115+
}
116+
}
117+
118+
119+
if ($email = ArrayHelper::getValue($attributes, 'email'))
120+
{
121+
$user->email = $email;
122+
if (!$user->save())
123+
{
124+
\Yii::error("Не удалось обновить данные пользователя: " . serialize($user->getErrors()), 'authClient');
125+
}
126+
}
127+
128+
if ($name = ArrayHelper::getValue($attributes, 'name'))
129+
{
130+
$user->name = $name;
131+
if (!$user->save())
132+
{
133+
\Yii::error("Не удалось обновить данные пользователя: " . serialize($user->getErrors()), 'authClient');
134+
}
135+
}
136+
137+
$firstName = ArrayHelper::getValue($attributes, 'first_name');
138+
$lastName = ArrayHelper::getValue($attributes, 'last_name');
139+
140+
if ($firstName || $lastName)
141+
{
142+
$user->name = $lastName . " " . $firstName;
143+
if (!$user->save())
144+
{
145+
\Yii::error("Не удалось обновить данные пользователя: " . serialize($user->getErrors()), 'authClient');
146+
}
147+
}
148+
}
149+
150+
151+
//$transaction = $user->getDb()->beginTransaction();
152+
153+
$auth = new UserAuthClient([
154+
'user_id' => $user->id,
155+
'provider' => $client->id,
156+
'provider_identifier' => (string)$attributes['id'],
157+
'provider_data' => $attributes,
158+
]);
159+
if ($auth->save())
160+
{
161+
//$transaction->commit();
162+
Yii::$app->user->login($user);
163+
164+
if (!$user->image)
165+
{
166+
try
167+
{
168+
if ($photoUrl = ArrayHelper::getValue($attributes, 'photo'))
169+
{
170+
$file = \Yii::$app->storage->upload($photoUrl, [
171+
'name' => $user->name
172+
]);
173+
174+
$user->link('image', $file);
175+
}
176+
} catch(\Exception $e)
177+
{
178+
179+
}
180+
181+
}
182+
183+
if (!$user->image)
184+
{
185+
try
186+
{
187+
if ($photoUrl = ArrayHelper::getValue($attributes, 'avatar_url'))
188+
{
189+
$file = \Yii::$app->storage->upload($photoUrl, [
190+
'name' => $user->name
191+
]);
192+
193+
$user->link('image', $file);
194+
}
195+
} catch(\Exception $e)
196+
{
197+
198+
}
199+
200+
201+
}
202+
203+
} else
204+
{
205+
\Yii::error("Не удалось создать социальный профиль: " . serialize($auth->getErrors()), 'authClient');
206+
}
207+
}
208+
} else
209+
{ // user already logged in
210+
if (!$userAuthClient)
211+
{ // add auth provider
212+
213+
$userAuthClient = new UserAuthClient([
214+
'user_id' => \Yii::$app->user->identity->id,
215+
'provider' => $client->id,
216+
'provider_identifier' => (string) $attributes['id'],
217+
'provider_data' => $attributes,
218+
]);
219+
220+
221+
if (!$userAuthClient->save())
222+
{
223+
print_r($userAuthClient->getErrors());
224+
die('no');
225+
}
226+
} else
227+
{
228+
$userAuthClient->provider_data = $attributes;
229+
$userAuthClient->save();
230+
}
231+
}
232+
233+
}
234+
235+
}

0 commit comments

Comments
 (0)