/** * Displays page where user can create new account that will be connected to social account. * * @param string $code * * @return string * @throws NotFoundHttpException */ public function actionConnect($code) { $account = Account::find()->byCode($code)->one(); if ($account === null || $account->getIsConnected()) { throw new NotFoundHttpException(); } /** @var User $user */ $user = Yii::createObject(['class' => User::className(), 'scenario' => 'connect', 'username' => $account->username, 'email' => $account->email]); $event = $this->getConnectEvent($account, $user); $this->trigger(self::EVENT_BEFORE_CONNECT, $event); if ($user->load(Yii::$app->request->post()) && $user->create()) { $account->connect($user); $this->trigger(self::EVENT_AFTER_CONNECT, $event); Yii::$app->user->login($user, $this->module->rememberFor); return $this->goBack(); } return $this->render('connect', ['model' => $user, 'account' => $account]); }
/** * Tries to find account, otherwise creates new account. * * @param BaseClientInterface $client * * @return Account * @throws \yii\base\InvalidConfigException */ protected static function fetchAccount(BaseClientInterface $client) { $account = self::find()->byClient($client)->one(); if (null === $account) { /** @var Account $account */ $account = Yii::createObject(['class' => Account::className(), 'provider' => $client->getId(), 'clientId' => $client->getUserAttributes()['id'], 'data' => Json::encode($client->getUserAttributes())]); $account->save(false); } return $account; }
/** * Tries to connect social account to user. * * @param ClientInterface $client */ public function connect(ClientInterface $client) { /** @var Account $account */ $account = Yii::createObject(Account::className()); $event = $this->getAuthEvent($account, $client); $this->trigger(self::EVENT_BEFORE_CONNECT, $event); $account->connectWithUser($client); $this->trigger(self::EVENT_AFTER_CONNECT, $event); $this->action->successUrl = Url::to(['/users/settings/networks']); }
/** * Disconnects a network account from user. * * @param int $id * * @return \yii\web\Response * @throws \yii\web\NotFoundHttpException * @throws \yii\web\ForbiddenHttpException */ public function actionDisconnect($id) { $account = Account::find()->byId($id)->one(); if ($account === null) { throw new NotFoundHttpException(); } if ($account->userId != Yii::$app->user->id) { throw new ForbiddenHttpException(); } $event = $this->getConnectEvent($account, $account->user); $this->trigger(self::EVENT_BEFORE_DISCONNECT, $event); $account->delete(); $this->trigger(self::EVENT_AFTER_DISCONNECT, $event); return $this->redirect(['networks']); }