From f40d11e362d4d3287c3ed35ec1f92a82bf66402b Mon Sep 17 00:00:00 2001 From: Yarik Date: Wed, 7 Jun 2017 17:06:35 +0300 Subject: [PATCH] Social auth + view history --- migrations/m170607_100409_customer_add_social_id_column.php | 16 ++++++++++++++++ migrations/m170607_102520_social_customer_modification.php | 16 ++++++++++++++++ models/Customer.php | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 133 insertions(+), 7 deletions(-) create mode 100644 migrations/m170607_100409_customer_add_social_id_column.php create mode 100644 migrations/m170607_102520_social_customer_modification.php diff --git a/migrations/m170607_100409_customer_add_social_id_column.php b/migrations/m170607_100409_customer_add_social_id_column.php new file mode 100644 index 0000000..eeefe5e --- /dev/null +++ b/migrations/m170607_100409_customer_add_social_id_column.php @@ -0,0 +1,16 @@ +addColumn('customer', 'social_id', $this->string()); + } + + public function safeDown() + { + $this->dropColumn('customer', 'social_id'); + } + } diff --git a/migrations/m170607_102520_social_customer_modification.php b/migrations/m170607_102520_social_customer_modification.php new file mode 100644 index 0000000..0cdef53 --- /dev/null +++ b/migrations/m170607_102520_social_customer_modification.php @@ -0,0 +1,16 @@ +alterColumn('customer', 'email', 'DROP NOT NULL'); + } + + public function safeDown() + { + $this->alterColumn('customer', 'email', 'SET NOT NULL'); + } + } diff --git a/models/Customer.php b/models/Customer.php index 9a5e64c..58fa68f 100755 --- a/models/Customer.php +++ b/models/Customer.php @@ -4,6 +4,8 @@ use artbox\catalog\models\Product; use artbox\catalog\models\Variant; + use artbox\core\helpers\UnificatorHelper; + use nodge\eauth\ErrorException; use Yii; use yii\base\NotSupportedException; use yii\behaviors\TimestampBehavior; @@ -31,6 +33,7 @@ * @property string $address * @property array $wishlist * @property Order[] $orders + * @property string $social_id */ class Customer extends ActiveRecord implements IdentityInterface { @@ -39,9 +42,18 @@ const GENDER_MALE = 1; const GENDER_FEMALE = 2; + const SCENARIO_SOCIAL = 'social'; + protected $wishlist = null; /** + * @var array EAuth attributes + */ + public $profile; + + public static $users; + + /** * @return array|\yii\db\ActiveRecord[] */ public function getWishlist() @@ -100,7 +112,6 @@ [ [ 'username', - 'email', ], 'required', ], @@ -152,6 +163,13 @@ self::GENDER_FEMALE, ], ], + [ + [ 'email,' ], + 'required', + 'except' => [ + self::SCENARIO_SOCIAL, + ], + ], ]; } @@ -221,12 +239,21 @@ */ public static function findIdentity($id) { - return static::findOne( - [ - 'id' => $id, - 'status' => self::STATUS_ACTIVE, - ] - ); + if (\Yii::$app->getSession() + ->has('user-' . $id) + ) { + return new self( + \Yii::$app->getSession() + ->get('user-' . $id) + ); + } else { + return static::findOne( + [ + 'id' => $id, + 'status' => self::STATUS_ACTIVE, + ] + ); + } } /** @@ -362,4 +389,71 @@ { $this->password_reset_token = null; } + + /** + * @param \nodge\eauth\ServiceBase $service + * + * @return \artbox\order\models\Customer + * @throws \nodge\eauth\ErrorException + */ + public static function findByEAuth($service) + { + if (!$service->getIsAuthenticated()) { + throw new ErrorException('EAuth user should be authenticated before creating identity.'); + } + $id = $service->getServiceName() . '-' . $service->getId(); + $attributes = [ + 'id' => $id, + 'username' => $service->getAttribute('name') ? : $service->getAttribute('username'), + 'authKey' => md5($id), + 'profile' => $service->getAttributes(), + ]; + $attributes[ 'profile' ][ 'service' ] = $service->getServiceName(); + $identity = self::find() + ->where( + [ + 'social_id' => $id, + ] + ) + ->one(); + if (!$identity) { + $identityAttrs = $attributes; + unset($identityAttrs[ 'id' ]); + $identityAttrs[ 'social_id' ] = $id; + $identity = self::register($identityAttrs); + } + \Yii::$app->getSession() + ->set('user-' . $id, $attributes); + return $identity; + } + + public function setAuthKey($key) + { + $this->auth_key = $key; + } + + /** + * @param array $attributes + * + * @return \artbox\order\models\Customer + */ + protected function register(array $attributes) + { + $identity = new self($attributes); + $identity->scenario = self::SCENARIO_SOCIAL; + $identity->username = UnificatorHelper::unify( + $identity->username, + function ($name) { + if (Customer::findByUsername($name)) { + return false; + } else { + return true; + } + } + ); + $identity->setPassword($attributes[ 'social_id' ]); + $identity->save(); + return $identity; + } + } -- libgit2 0.21.4