Commit f40d11e362d4d3287c3ed35ec1f92a82bf66402b
1 parent
622d0396
Social auth + view history
Showing
3 changed files
with
133 additions
and
7 deletions
Show diff stats
migrations/m170607_100409_customer_add_social_id_column.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | + use yii\db\Migration; | ||
4 | + | ||
5 | + class m170607_100409_customer_add_social_id_column extends Migration | ||
6 | + { | ||
7 | + public function safeUp() | ||
8 | + { | ||
9 | + $this->addColumn('customer', 'social_id', $this->string()); | ||
10 | + } | ||
11 | + | ||
12 | + public function safeDown() | ||
13 | + { | ||
14 | + $this->dropColumn('customer', 'social_id'); | ||
15 | + } | ||
16 | + } |
migrations/m170607_102520_social_customer_modification.php
0 → 100644
1 | +<?php | ||
2 | + | ||
3 | + use yii\db\Migration; | ||
4 | + | ||
5 | + class m170607_102520_social_customer_modification extends Migration | ||
6 | + { | ||
7 | + public function safeUp() | ||
8 | + { | ||
9 | + $this->alterColumn('customer', 'email', 'DROP NOT NULL'); | ||
10 | + } | ||
11 | + | ||
12 | + public function safeDown() | ||
13 | + { | ||
14 | + $this->alterColumn('customer', 'email', 'SET NOT NULL'); | ||
15 | + } | ||
16 | + } |
models/Customer.php
@@ -4,6 +4,8 @@ | @@ -4,6 +4,8 @@ | ||
4 | 4 | ||
5 | use artbox\catalog\models\Product; | 5 | use artbox\catalog\models\Product; |
6 | use artbox\catalog\models\Variant; | 6 | use artbox\catalog\models\Variant; |
7 | + use artbox\core\helpers\UnificatorHelper; | ||
8 | + use nodge\eauth\ErrorException; | ||
7 | use Yii; | 9 | use Yii; |
8 | use yii\base\NotSupportedException; | 10 | use yii\base\NotSupportedException; |
9 | use yii\behaviors\TimestampBehavior; | 11 | use yii\behaviors\TimestampBehavior; |
@@ -31,6 +33,7 @@ | @@ -31,6 +33,7 @@ | ||
31 | * @property string $address | 33 | * @property string $address |
32 | * @property array $wishlist | 34 | * @property array $wishlist |
33 | * @property Order[] $orders | 35 | * @property Order[] $orders |
36 | + * @property string $social_id | ||
34 | */ | 37 | */ |
35 | class Customer extends ActiveRecord implements IdentityInterface | 38 | class Customer extends ActiveRecord implements IdentityInterface |
36 | { | 39 | { |
@@ -39,9 +42,18 @@ | @@ -39,9 +42,18 @@ | ||
39 | const GENDER_MALE = 1; | 42 | const GENDER_MALE = 1; |
40 | const GENDER_FEMALE = 2; | 43 | const GENDER_FEMALE = 2; |
41 | 44 | ||
45 | + const SCENARIO_SOCIAL = 'social'; | ||
46 | + | ||
42 | protected $wishlist = null; | 47 | protected $wishlist = null; |
43 | 48 | ||
44 | /** | 49 | /** |
50 | + * @var array EAuth attributes | ||
51 | + */ | ||
52 | + public $profile; | ||
53 | + | ||
54 | + public static $users; | ||
55 | + | ||
56 | + /** | ||
45 | * @return array|\yii\db\ActiveRecord[] | 57 | * @return array|\yii\db\ActiveRecord[] |
46 | */ | 58 | */ |
47 | public function getWishlist() | 59 | public function getWishlist() |
@@ -100,7 +112,6 @@ | @@ -100,7 +112,6 @@ | ||
100 | [ | 112 | [ |
101 | [ | 113 | [ |
102 | 'username', | 114 | 'username', |
103 | - 'email', | ||
104 | ], | 115 | ], |
105 | 'required', | 116 | 'required', |
106 | ], | 117 | ], |
@@ -152,6 +163,13 @@ | @@ -152,6 +163,13 @@ | ||
152 | self::GENDER_FEMALE, | 163 | self::GENDER_FEMALE, |
153 | ], | 164 | ], |
154 | ], | 165 | ], |
166 | + [ | ||
167 | + [ 'email,' ], | ||
168 | + 'required', | ||
169 | + 'except' => [ | ||
170 | + self::SCENARIO_SOCIAL, | ||
171 | + ], | ||
172 | + ], | ||
155 | ]; | 173 | ]; |
156 | } | 174 | } |
157 | 175 | ||
@@ -221,12 +239,21 @@ | @@ -221,12 +239,21 @@ | ||
221 | */ | 239 | */ |
222 | public static function findIdentity($id) | 240 | public static function findIdentity($id) |
223 | { | 241 | { |
224 | - return static::findOne( | ||
225 | - [ | ||
226 | - 'id' => $id, | ||
227 | - 'status' => self::STATUS_ACTIVE, | ||
228 | - ] | ||
229 | - ); | 242 | + if (\Yii::$app->getSession() |
243 | + ->has('user-' . $id) | ||
244 | + ) { | ||
245 | + return new self( | ||
246 | + \Yii::$app->getSession() | ||
247 | + ->get('user-' . $id) | ||
248 | + ); | ||
249 | + } else { | ||
250 | + return static::findOne( | ||
251 | + [ | ||
252 | + 'id' => $id, | ||
253 | + 'status' => self::STATUS_ACTIVE, | ||
254 | + ] | ||
255 | + ); | ||
256 | + } | ||
230 | } | 257 | } |
231 | 258 | ||
232 | /** | 259 | /** |
@@ -362,4 +389,71 @@ | @@ -362,4 +389,71 @@ | ||
362 | { | 389 | { |
363 | $this->password_reset_token = null; | 390 | $this->password_reset_token = null; |
364 | } | 391 | } |
392 | + | ||
393 | + /** | ||
394 | + * @param \nodge\eauth\ServiceBase $service | ||
395 | + * | ||
396 | + * @return \artbox\order\models\Customer | ||
397 | + * @throws \nodge\eauth\ErrorException | ||
398 | + */ | ||
399 | + public static function findByEAuth($service) | ||
400 | + { | ||
401 | + if (!$service->getIsAuthenticated()) { | ||
402 | + throw new ErrorException('EAuth user should be authenticated before creating identity.'); | ||
403 | + } | ||
404 | + $id = $service->getServiceName() . '-' . $service->getId(); | ||
405 | + $attributes = [ | ||
406 | + 'id' => $id, | ||
407 | + 'username' => $service->getAttribute('name') ? : $service->getAttribute('username'), | ||
408 | + 'authKey' => md5($id), | ||
409 | + 'profile' => $service->getAttributes(), | ||
410 | + ]; | ||
411 | + $attributes[ 'profile' ][ 'service' ] = $service->getServiceName(); | ||
412 | + $identity = self::find() | ||
413 | + ->where( | ||
414 | + [ | ||
415 | + 'social_id' => $id, | ||
416 | + ] | ||
417 | + ) | ||
418 | + ->one(); | ||
419 | + if (!$identity) { | ||
420 | + $identityAttrs = $attributes; | ||
421 | + unset($identityAttrs[ 'id' ]); | ||
422 | + $identityAttrs[ 'social_id' ] = $id; | ||
423 | + $identity = self::register($identityAttrs); | ||
424 | + } | ||
425 | + \Yii::$app->getSession() | ||
426 | + ->set('user-' . $id, $attributes); | ||
427 | + return $identity; | ||
428 | + } | ||
429 | + | ||
430 | + public function setAuthKey($key) | ||
431 | + { | ||
432 | + $this->auth_key = $key; | ||
433 | + } | ||
434 | + | ||
435 | + /** | ||
436 | + * @param array $attributes | ||
437 | + * | ||
438 | + * @return \artbox\order\models\Customer | ||
439 | + */ | ||
440 | + protected function register(array $attributes) | ||
441 | + { | ||
442 | + $identity = new self($attributes); | ||
443 | + $identity->scenario = self::SCENARIO_SOCIAL; | ||
444 | + $identity->username = UnificatorHelper::unify( | ||
445 | + $identity->username, | ||
446 | + function ($name) { | ||
447 | + if (Customer::findByUsername($name)) { | ||
448 | + return false; | ||
449 | + } else { | ||
450 | + return true; | ||
451 | + } | ||
452 | + } | ||
453 | + ); | ||
454 | + $identity->setPassword($attributes[ 'social_id' ]); | ||
455 | + $identity->save(); | ||
456 | + return $identity; | ||
457 | + } | ||
458 | + | ||
365 | } | 459 | } |