public function onAuthSuccess($client) { $source = $client->getId(); $userAttributes = $client->getUserAttributes(); if (!isset($this->module->attributeParsers[$source])) { throw \yii\base\InvalidConfigException("There are no settings for '{$source}' in the AuthModule::attributeParsers."); } $attributes = $this->module->attributeParsers[$source]($userAttributes); Yii::$app->session->set(AuthModule::PARAMS_SESSION_ID, $attributes); /* @var $auth Auth */ $auth = Auth::find()->where(['source' => $attributes['source'], 'source_id' => $attributes['source_id']])->one(); if (Yii::$app->user->isGuest) { if ($auth) { // login $user = $auth->user; Yii::$app->user->login($user); } else { // signup if (isset($attributes['email']) && $attributes['email'] && User::find()->where(['email' => $attributes['email']])->exists()) { Yii::$app->getSession()->setFlash('error', [Yii::t('yee/auth', "User with the same email as in {client} account already exists but isn't linked to it. Login using email first to link it.", ['client' => $client->getTitle()])]); Yii::$app->getResponse()->redirect(['auth/default/login']); } else { return $this->createUser($attributes); } } } else { // user already logged in if (!$auth) { // add auth provider $auth = new Auth(['user_id' => Yii::$app->user->id, 'source' => $attributes['source'], 'source_id' => $attributes['source_id']]); $auth->save(); } } }
/** * @return \yii\db\ActiveQuery */ public static function unlinkClient($client) { $userId = Yii::$app->user->id; $client = Auth::find()->where(['user_id' => $userId, 'source' => $client])->one(); return $client ? $client->delete() : false; }