controller = $controller; $this->client = $client; $this->socialNetworkAccountQuery = $socialNetworkAccountQuery; } public function run() { $account = $this->getSocialNetworkAccount(); $event = $this->make(SocialNetworkAuthEvent::class, [$account, $this->client]); $this->controller->trigger(SocialNetworkAuthEvent::EVENT_BEFORE_CONNECT, $event); if ($account && $account->user === null) { /** @var User $user */ $user = Yii::$app->user->identity; $account->link('user', $user); Yii::$app->session->setFlash('success', Yii::t('user', 'Your account has been connected')); $this->controller->trigger(SocialNetworkAuthEvent::EVENT_AFTER_CONNECT, $event); return true; } else { Yii::$app->session->setFlash( 'danger', Yii::t('user', 'This account has already been connected to another user') ); } return false; } protected function getSocialNetworkAccount() { $account = $this->socialNetworkAccountQuery->whereClient($this->client)->one(); if (null === $account) { $data = $this->client->getUserAttributes(); $account = $this->make( SocialNetworkAccount::class, [ 'provider' => $this->client->getId(), 'client_id' => $data['id'], 'data' => json_encode($data), ] ); if ($account->save(false)) { return $account; } } return false; } }