Пример #1
0
 /**
  * Tries to authenticate user via social network. If user has already used
  * this network's account, he will be logged in. Otherwise, it will try
  * to create new user account.
  *
  * @param ClientInterface $client
  */
 public function authenticate(ClientInterface $client)
 {
     $account = $this->finder->findAccount()->byClient($client)->one();
     if (!$this->module->enableRegistration && ($account === null || $account->user === null)) {
         Yii::$app->session->setFlash('danger', Yii::t('user', 'Registration on this website is disabled'));
         $this->action->successUrl = Url::to(['/user/security/login']);
         return;
     }
     if ($account === null) {
         /** @var Account $account */
         $accountObj = Yii::createObject(Account::className());
         $account = $accountObj::create($client);
     }
     $event = $this->getAuthEvent($account, $client);
     $this->trigger(self::EVENT_BEFORE_AUTHENTICATE, $event);
     if ($account->user instanceof User) {
         if ($account->user->isBlocked) {
             Yii::$app->session->setFlash('danger', Yii::t('user', 'Your account has been blocked.'));
             $this->action->successUrl = Url::to(['/user/security/login']);
         } else {
             Yii::$app->user->login($account->user, $this->module->rememberFor);
             $this->action->successUrl = Yii::$app->getUser()->getReturnUrl();
         }
     } else {
         $this->action->successUrl = $account->getConnectUrl();
     }
     $this->trigger(self::EVENT_AFTER_AUTHENTICATE, $event);
 }
Пример #2
0
 /**
  * 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 = $this->finder->findAccount()->byId($id)->one();
     if ($account === null) {
         throw new NotFoundHttpException();
     }
     if ($account->user_id != 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']);
 }
Пример #3
0
 /**
  * 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 = $this->finder->findAccount()->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]);
 }