コード例 #1
0
ファイル: User.php プロジェクト: ut8ia/radioDoc
 /**
  * @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);
 }