/** * @param \nodge\eauth\ServiceBase $service * @return User * @throws ErrorException */ public static function findByEAuth($service) { if (!$service->getIsAuthenticated()) { throw new ErrorException('EAuth user should be authenticated before creating identity.'); } $service_id = $service->getServiceName() . '-' . $service->getId(); // find user auth $user_auth = AuthRecords::find()->where(["id" => $service_id])->one(); if (!isset($user_auth->user_id)) { $attributes = $service->getAttributes(); $nameFromService = isset($attributes['name']) ? $attributes['name'] : null; // add user $model = new User(); $model->username = $nameFromService ? $nameFromService : $service_id; $model->auth_key = md5($service_id); $model->password_hash = "asd" . rand(0, 1045693); // $model->email = ""; $model->save(); $user_id = $model->id; // add auth $new_auth = new AuthRecords(); $new_auth->id = $service_id; $new_auth->user_id = $user_id; $new_auth->attributes = serialize($service->getAttributes()); $new_auth->save(); } else { $user_id = $user_auth->user_id; } return user::findIdentity($user_id); }
/** * @param \nodge\eauth\ServiceBase $service * @return User * @throws ErrorException */ public static function findByEAuth($service) { if (!$service->getIsAuthenticated()) { throw new ErrorException('EAuth user should be authenticated before creating identity.'); } $service_id = $service->getServiceName() . '-' . $service->getId(); // find user auth $user_auth = AuthRecords::find()->where(["id" => $service_id])->one(); // make new auth record and create user if (!isset($user_auth->user_id)) { $attributes = $service->getAttributes(); $nameFromService = isset($attributes['name']) ? $attributes['name'] : null; // add user $model = new User(); $model->username = $nameFromService ? $nameFromService : $service_id; $model->auth_key = md5($service_id); // set default pass for direct account access $password = $model->generatePass(); $model->password_hash = Yii::$app->security->generatePasswordHash($password); $model->save(); $user_id = $model->id; // add auth $new_auth = new AuthRecords(); $new_auth->id = $service_id; $new_auth->user_id = $user_id; $new_auth->attributes = serialize($service->getAttributes()); $new_auth->save(); } else { $user_id = $user_auth->user_id; } return user::findIdentity($user_id); }
/** * @param \nodge\eauth\ServiceBase $service * @return User * @throws 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, 'steamid' => $service->getAttribute('steamid'), 'username' => $service->getAttribute('username'), 'profile_url' => $service->getAttribute('profile_url'), 'avatar' => $service->getAttribute('avatar'), 'avatar_md' => $service->getAttribute('avatar_md'), 'avatar_lg' => $service->getAttribute('avatar_lg')]; Yii::$app->getSession()->set('user-' . $id, $attributes); return new self($attributes); }
/** * @param \nodge\eauth\ServiceBase $service * @return User * @throws 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 = array('id' => $id, 'username' => $service->getAttribute('name'), 'authKey' => md5($id), 'profile' => $service->getAttributes()); $attributes['profile']['service'] = $service->getServiceName(); Yii::$app->getSession()->set('user-' . $id, $attributes); return new self($attributes); }
/** * @param \nodge\eauth\ServiceBase $service * @return User * @throws ErrorException */ public static function findByEAuth($service) { if (!$service->getIsAuthenticated()) { throw new ErrorException('EAuth user should be authenticated before creating identity.'); } $user = self::find()->where(['eauth_service' => $service->getServiceName(), 'eauth_id' => $service->getId()])->one(); if ($user == null) { // print_r($service->attributes); // exit; $username = $service->getAttribute('username'); $username = $username ? $username : "******" . $service->getId(); $user = new User(['username' => $username, 'password' => Yii::$app->security->generateRandomString(6), 'status' => self::STATUS_ACTIVE, 'name' => $service->getAttribute('name') ? $service->getAttribute('name') : $service->getAttribute('username'), 'email' => $service->getAttribute('email'), 'eauth_service' => (string) $service->getServiceName(), 'eauth_id' => (string) $service->getId()]); $user->generateAuthKey(); $user->save(); } return $user; }
/** * @param \nodge\eauth\ServiceBase $service * @return User * @throws ErrorException */ public static function findByEAuth($service) { if (!$service->getIsAuthenticated()) { throw new ErrorException('Авторизован.'); } /** * @var $social UserSocial * @var $socialOth UserSocial */ $email = $service->getAttribute('email'); $social = UserSocial::findOne(['service' => $service->getServiceName(), 'service_id' => "{$service->getId()}"]); if ($social) { return $social->user; } $socialOth = $email ? UserSocial::findOne(['email' => $email]) : false; $user = User::findOne(['email' => $email]); if (!$user) { if (!$socialOth) { $user = new static(['username' => $service->getAttribute('name'), 'email' => $email, 'auth_key' => Yii::$app->security->generateRandomString()]); $user->insert(false); } else { $user = $socialOth->user; } } $social = new UserSocial(['service' => $service->getServiceName(), 'service_id' => $service->getId(), 'user_id' => $user->id, 'email' => $user->email]); $social->insert(false); return $user; }
/** * @param \nodge\eauth\ServiceBase $service * @return User * @throws ErrorException */ public static function findByEAuth($service) { if (!$service->getIsAuthenticated()) { throw new ErrorException('EAuth user should be authenticated before creating identity.'); } $user = static::find()->where('service = :service AND service_id = :ID', [':service' => $service->getServiceName(), ':ID' => $service->getAttribute('id')])->one(); if (!$user) { $photo = $service->getServiceName() === 'vkontakte' ? $service->getAttribute('photo_big') : "https://graph.facebook.com/{$service->getAttribute('id')}/picture?type=large"; $user = new static(['username' => $service->getAttribute('name'), 'service' => $service->getServiceName(), 'service_id' => $service->getAttribute('id'), 'img' => $photo]); $user->save(); } $id = $service->getServiceName() . '-' . $service->getId(); $attributes = array_merge($user->getAttributes(['id', 'username', 'img', 'rating']), ['id' => $id, 'auth_key' => md5($id), 'cid' => $user->id]); Yii::$app->getSession()->set('user-' . $id, $attributes); return new self($attributes); }
/** * Finds user by \nodge\eauth\ServiceBase eauth service. * * @param \nodge\eauth\ServiceBase $service * * @return object ServiceUser * * @throws ErrorException */ public static function findByEAuth($service) { if (!$service->getIsAuthenticated()) { throw new ErrorException('EAuth user should be authenticated before creating identity.'); } $identity = static::findIdentity(['service_name' => $service->getServiceName(), 'service_user_id' => $service->getId()]); if ($identity instanceof \yii\web\IdentityInterface) { $identity->username = $service->getAttribute('username'); $identity->access_token = $service->getAttribute('access_token'); $identity->avatar_url = $service->getAttribute('avatar_url'); $identity->save(); } return $identity; }