Inheritance: extends yii\db\ActiveRecord
示例#1
0
 /**
  * Logs the user in if this social account has been already used. Otherwise shows registration form.
  * @param  ClientInterface $client
  * @return \yii\web\Response
  */
 public function authenticate(ClientInterface $client)
 {
     $attributes = $client->getUserAttributes();
     $provider = $client->getId();
     $clientId = $attributes['id'];
     $account = UserAccount::find()->where(['provider' => $provider, 'client_id' => $clientId])->one();
     if ($account === null) {
         $account = \Yii::createObject(['class' => UserAccount::className(), 'provider' => $provider, 'client_id' => $clientId, 'data' => json_encode($attributes), 'created_at' => time()]);
         $account->save(false);
     }
     if (null === ($user = $account->user)) {
         $this->action->successUrl = Url::to(['/site/connect', 'account_id' => $account->id]);
     } else {
         \Yii::$app->user->login($user, 1209600);
         // two weeks
     }
 }
 /**
  * 绑定第三方账号
  * @param  ClientInterface $client
  * @return \yii\web\Response
  */
 public function connect(ClientInterface $client)
 {
     $attributes = $client->getUserAttributes();
     $provider = $client->getId();
     $clientId = $attributes['id'];
     $account = $this->finder->findAccountByProviderAndClientId($provider, $clientId);
     if ($account === null) {
         $account = Yii::createObject(['class' => UserAccount::className(), 'provider' => $provider, 'client_id' => $clientId, 'data' => json_encode($attributes), 'user_id' => Yii::$app->user->id, 'created_at' => time()]);
         $account->save(false);
         Yii::$app->session->setFlash('success', '账号绑定成功');
     } else {
         Yii::$app->session->setFlash('error', '绑定失败,此账号已经绑定过了');
     }
     $this->action->successUrl = Url::to(['/user/setting/networks']);
 }
示例#3
0
文件: User.php 项目: tqsq2005/getyii
 /**
  * @return Account[] Connected accounts ($provider => $account)
  */
 public function getAccounts()
 {
     $connected = [];
     $accounts = $this->hasMany(UserAccount::className(), ['user_id' => 'id'])->all();
     // @var Account $account
     foreach ($accounts as $account) {
         $connected[$account->provider] = $account;
     }
     return $connected;
 }
示例#4
0
 /**
  * Displays page where user can create new account that will be connected to social account.
  * @param  integer $account_id
  * @return string
  * @throws NotFoundHttpException
  */
 public function actionConnect($account_id)
 {
     $account = UserAccount::find()->where(['id' => $account_id])->one();
     if ($account === null || $account->getIsConnected()) {
         throw new NotFoundHttpException();
     }
     $accountData = \yii\helpers\Json::decode($account->data);
     $model = new SignupForm();
     $model->username = $accountData['login'];
     $model->email = empty($accountData['email']) ? '' : $accountData['email'];
     $this->performAjaxValidation($model);
     if ($model->load(Yii::$app->request->post())) {
         if ($user = $model->signup()) {
             $account->user_id = $user->id;
             $account->save(false);
             if (Yii::$app->getUser()->login($user, 1209600)) {
                 return $this->goHome();
             }
         }
     }
     return $this->render('signup', ['model' => $model]);
 }