Compare View
Commits (2)
-
авторизация пользователей
Showing
4 changed files
Show diff stats
1 | +<?php | ||
2 | + | ||
3 | +namespace common\models; | ||
4 | + | ||
5 | +use Yii; | ||
6 | + | ||
7 | +/** | ||
8 | + * This is the model class for table "social". | ||
9 | + * | ||
10 | + * @property integer $social_id | ||
11 | + * @property string $social_name | ||
12 | + * @property integer $social_user_id | ||
13 | + * @property integer $user_id | ||
14 | + */ | ||
15 | +class Social extends \yii\db\ActiveRecord | ||
16 | +{ | ||
17 | + /** | ||
18 | + * @inheritdoc | ||
19 | + */ | ||
20 | + public static function tableName() | ||
21 | + { | ||
22 | + return 'social'; | ||
23 | + } | ||
24 | + | ||
25 | + /** | ||
26 | + * @inheritdoc | ||
27 | + */ | ||
28 | + public function rules() | ||
29 | + { | ||
30 | + return [ | ||
31 | + [['social_user_id', 'user_id'], 'integer'], | ||
32 | + [['social_name'], 'string', 'max' => 255] | ||
33 | + ]; | ||
34 | + } | ||
35 | + | ||
36 | + /** | ||
37 | + * @inheritdoc | ||
38 | + */ | ||
39 | + public function attributeLabels() | ||
40 | + { | ||
41 | + return [ | ||
42 | + 'social_id' => Yii::t('app', 'Social ID'), | ||
43 | + 'social_name' => Yii::t('app', 'Social Name'), | ||
44 | + 'social_user_id' => Yii::t('app', 'Social User ID'), | ||
45 | + 'user_id' => Yii::t('app', 'User ID'), | ||
46 | + ]; | ||
47 | + } | ||
48 | +} |
No preview for this file type
No preview for this file type
frontend/controllers/SiteController.php
@@ -20,6 +20,8 @@ use common\models\User; | @@ -20,6 +20,8 @@ use common\models\User; | ||
20 | use yii\helpers\VarDumper; | 20 | use yii\helpers\VarDumper; |
21 | use common\models\Page; | 21 | use common\models\Page; |
22 | use frontend\models\Option; | 22 | use frontend\models\Option; |
23 | +use common\models\Social; | ||
24 | + | ||
23 | 25 | ||
24 | /** | 26 | /** |
25 | * Site controller | 27 | * Site controller |
@@ -101,8 +103,13 @@ class SiteController extends Controller | @@ -101,8 +103,13 @@ class SiteController extends Controller | ||
101 | */ | 103 | */ |
102 | public function actionLogin() | 104 | public function actionLogin() |
103 | { | 105 | { |
106 | + | ||
107 | + // creat new model table Social and new model User | ||
108 | + $social = new Social(); | ||
109 | + $user = new User(); | ||
104 | 110 | ||
105 | $serviceName = Yii::$app->getRequest()->getQueryParam('service'); | 111 | $serviceName = Yii::$app->getRequest()->getQueryParam('service'); |
112 | + | ||
106 | if (isset($serviceName)) { | 113 | if (isset($serviceName)) { |
107 | /** @var $eauth \nodge\eauth\ServiceBase */ | 114 | /** @var $eauth \nodge\eauth\ServiceBase */ |
108 | $eauth = Yii::$app->get('eauth')->getIdentity($serviceName); | 115 | $eauth = Yii::$app->get('eauth')->getIdentity($serviceName); |
@@ -111,11 +118,24 @@ class SiteController extends Controller | @@ -111,11 +118,24 @@ class SiteController extends Controller | ||
111 | 118 | ||
112 | try { | 119 | try { |
113 | if ($eauth->authenticate()) { | 120 | if ($eauth->authenticate()) { |
114 | -// var_dump($eauth->getIsAuthenticated(), $eauth->getAttributes()); exit; | ||
115 | - | ||
116 | $identity = User::findByEAuth($eauth); | 121 | $identity = User::findByEAuth($eauth); |
117 | Yii::$app->getUser()->login($identity); | 122 | Yii::$app->getUser()->login($identity); |
118 | 123 | ||
124 | + //Save date get social network in database | ||
125 | + if (! $social::find()->where(['social_user_id' => $identity[profile][id], 'social_name' => $identity[profile][service]])->exists()) { | ||
126 | + $name = explode(' ',$identity[profile][name]); | ||
127 | + $user->firstname = $name[0]; | ||
128 | + $user->lastname = $name[1]; | ||
129 | + $user->id_system_date = date("d.m.y.H:i:s"); | ||
130 | + $user->save(); | ||
131 | + $social->social_name = $identity[profile][service]; | ||
132 | + $social->social_user_id = $identity[profile][id]; | ||
133 | + $social->user_id = $user->id; | ||
134 | + $social->validate(); | ||
135 | + $social->errors; | ||
136 | + $social->save(); | ||
137 | + } | ||
138 | + | ||
119 | // special redirect with closing popup window | 139 | // special redirect with closing popup window |
120 | $eauth->redirect(); | 140 | $eauth->redirect(); |
121 | } | 141 | } |
@@ -170,7 +190,6 @@ class SiteController extends Controller | @@ -170,7 +190,6 @@ class SiteController extends Controller | ||
170 | */ | 190 | */ |
171 | public function actionContact() | 191 | public function actionContact() |
172 | { | 192 | { |
173 | - | ||
174 | //Yii::$app->user->logout(); | 193 | //Yii::$app->user->logout(); |
175 | $identity = Yii::$app->getUser()->getIdentity(); | 194 | $identity = Yii::$app->getUser()->getIdentity(); |
176 | var_dump($identity[profile]); | 195 | var_dump($identity[profile]); |