コード例 #1
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();
     }
     /**
      * xiaoma update;
      */
     $accountData = json_decode($account->data);
     switch ($account->provider) {
         case 'qq':
             $avatar = $accountData->figureurl;
             $nickname = $accountData->nickname;
             break;
         case 'sina':
             $avatar = $accountData->profile_image_url;
             $nickname = $accountData->name;
             break;
         case 'github':
             $avatar = $accountData->avatar_url;
             $nickname = $accountData->login;
             break;
         default:
             $avatar = $nickname = '';
     }
     /** @var User $user */
     $user = Yii::createObject(['class' => User::className(), 'scenario' => 'connect', 'username' => $account->username, 'email' => $account->email]);
     if ($user->load(Yii::$app->request->post()) && $user->create()) {
         $account->connect($user);
         Yii::$app->user->login($user, $this->module->rememberFor);
         return $this->goBack();
     }
     return $this->render('connect', ['model' => $user, 'account' => $account, 'avatar' => $avatar, 'nickname' => $nickname, 'provider' => $account->provider]);
 }
コード例 #2
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]);
     if ($user->load(Yii::$app->request->post()) && $user->create()) {
         $account->connect($user);
         Yii::$app->user->login($user, $this->module->rememberFor);
         return $this->goBack();
     }
     return $this->render('connect', ['model' => $user, 'account' => $account]);
 }
コード例 #3
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);
 }
コード例 #4
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();
     }
     $account->delete();
     return $this->redirect(['networks']);
 }
コード例 #5
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 ($account === null) {
         $account = Account::create($client);
     }
     if ($account->user instanceof User) {
         Yii::$app->user->login($account->user, $this->module->rememberFor);
         $this->action->successUrl = Yii::$app->getUser()->getReturnUrl();
     } else {
         $this->action->successUrl = $account->getConnectUrl();
     }
 }
コード例 #6
0
ファイル: SettingsController.php プロジェクト: 88c/yii2-user
 /**
  * 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']);
 }
コード例 #7
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 ($account === null) {
         $account = Account::create($client);
     }
     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();
     }
 }