/**
  * @param $client
  *
  * TODO
  */
 public function onAuthSuccess(ClientInterface $client)
 {
     $attributes = $client->getUserAttributes();
     /* @var $auth UserAuth */
     $auth = UserAuth::find()->where(['source' => $client->getId(), 'source_id' => $attributes['id']])->one();
     if (Yii::$app->user->isGuest) {
         if ($auth) {
             $user = $auth->user;
             Yii::$app->user->login($user);
         } else {
             if (isset($attributes['email']) && User::find()->where(['email' => $attributes['email']])->exists()) {
                 Yii::$app->getSession()->setFlash('error', [Yii::t('app', "User with the same email as in {client} account already exists but isn't linked to it. Login using email first to link it.", ['client' => $client->getTitle()])]);
             } else {
                 if ($client->signIn()) {
                     $this->redirect('/profile');
                 }
             }
         }
     } else {
         if (!$auth) {
             $auth = new UserAuth(['user_id' => Yii::$app->user->id, 'source' => $client->getId(), 'source_id' => $attributes['id']]);
             $auth->save();
         }
     }
 }
Пример #2
0
 /**
  * @param \yii\authclient\ClientInterface $Client
  * @throws \yii\base\NotSupportedException
  */
 public function authSuccessCallback(\yii\authclient\ClientInterface $Client)
 {
     $AuthResponse = new \resources\User\Auth\Response();
     $AuthResponse->client = $Client->getId();
     $attributes = $Client->getUserAttributes();
     $AuthResponse->response = Json::encode($attributes);
     $UserQuery = \resources\User::find();
     switch ($Client->getId()) {
         case 'facebook':
             $UserQuery->byFacebookId($attributes['id']);
             break;
         case 'github':
             $UserQuery->byGithubId($attributes['id']);
             break;
         case 'google':
             $UserQuery->byGoogleId($attributes['id']);
             break;
         case 'linkedin':
             $UserQuery->byLinkedinId($attributes['id']);
             break;
         case 'live':
             $UserQuery->byLiveId($attributes['id']);
             break;
         case 'twitter':
             $UserQuery->byTwitterId($attributes['id']);
             break;
         case 'vkontakte':
             $UserQuery->byVkontakteId($attributes['id']);
             break;
         case 'yandex':
             $UserQuery->byYandexId($attributes['id']);
             break;
     }
     /** @var \resources\User $User */
     $User = $UserQuery->one();
     if ($User instanceof \resources\User) {
         $AuthResponse->result = Json::encode($User->id);
     } else {
         $User = new \resources\User();
         $User->appendClientAttributes($Client);
         if ($User->save()) {
             $User->createSocialLink($Client);
             $AuthResponse->result = Json::encode($User->id);
             AuthManager()->assign(RbacFactory::Role(\frontend\Permissions::ROLE_USER), $User->id);
         } else {
             $AuthResponse->result = Json::encode($User->getErrors());
         }
     }
     $AuthResponse->save();
     if ($User instanceof \resources\User && !$User->isNewRecord) {
         $User->save();
         User()->login($User, 86400);
     }
 }
Пример #3
0
 /**
  * Composes client auth URL.
  * @param ClientInterface $provider external auth client instance.
  * @return string auth URL.
  */
 public function createClientUrl($provider)
 {
     $this->autoRender = false;
     $url = $this->getBaseAuthUrl();
     $url[$this->clientIdGetParamName] = $provider->getId();
     return Url::to($url);
 }
Пример #4
0
 /**
  * @param \yii\authclient\ClientInterface $Client
  * @return static
  */
 public static function createLog(\yii\authclient\ClientInterface $Client)
 {
     $AuthResponse = new static();
     $AuthResponse->client = $Client->getId();
     $attributes = $Client->getUserAttributes();
     $AuthResponse->response = Json::encode($attributes);
     return $AuthResponse;
 }
 /**
  * @param ClientInterface $client
  */
 public function onAuthSuccess($client)
 {
     $attributes = $client->getUserAttributes();
     $email = ArrayHelper::getValue($attributes, 'email');
     /** @var Auth $auth */
     $auth = Auth::find()->where(['source' => $client->getId(), 'source_id' => $attributes['id']])->one();
     if (Yii::$app->user->isGuest) {
         if ($auth) {
             // login
             $user = $auth->user;
             Yii::$app->user->login($user, 3600 * 24 * 30);
         } else {
             // signup
             if (User::find()->where(['email' => $email])->exists()) {
                 Yii::$app->getSession()->setFlash('error', [Yii::t('app', "User with the same email as in {client} account already exists but isn't linked to it. Login using email first to link it.", ['client' => $client->getTitle()])]);
             } else {
                 $password = Yii::$app->security->generateRandomString(6);
                 $user = new User(['username' => $attributes['login'], 'email' => $email, 'password' => $password]);
                 $user->generateAuthKey();
                 $user->generatePasswordResetToken();
                 $transaction = $user->getDb()->beginTransaction();
                 if ($user->save()) {
                     $auth = new Auth(['user_id' => $user->id, 'source' => $client->getId(), 'source_id' => (string) $attributes['id']]);
                     if ($auth->save()) {
                         $transaction->commit();
                         Yii::$app->user->login($user, 3600 * 24 * 30);
                     } else {
                         print_r($auth->getErrors());
                         die;
                     }
                 } else {
                     print_r($user->getErrors());
                     die;
                 }
             }
         }
     } else {
         // user already logged in
         if (!$auth) {
             // add auth provider
             $auth = new Auth(['user_id' => Yii::$app->user->id, 'source' => $client->getId(), 'source_id' => $attributes['id']]);
             $auth->save();
         }
     }
 }
Пример #6
0
 /**
  * Finds an account by client.
  * @param  ClientInterface $client
  * @return AccountQuery
  */
 public function byClient(ClientInterface $client)
 {
     //xiaoma update
     //qq and sina 's attr has no id
     $client_type = $client->getId();
     switch ($client_type) {
         case 'qq':
             $client_id = $client->getUserAttributes()['openid'];
             break;
         case 'sina':
             $client_id = $client->getUserAttributes()['uid'];
             break;
         default:
             $client_id = $client->getUserAttributes()['id'];
             break;
     }
     return $this->andWhere(['provider' => $client->getId(), 'client_id' => $client_id]);
 }
Пример #7
0
 /**
  * Меняем ссылки на добавление и удаление ключей
  * @param ClientInterface $client external auth client instance.
  * @param string $text link text, if not set - default value will be generated.
  * @param array $htmlOptions link HTML options.
  * @throws InvalidConfigException on wrong configuration.
  */
 public function clientLink($client, $text = null, array $htmlOptions = [])
 {
     echo Html::beginTag('div', ['class' => 'col-xs-4']);
     $exists = UserOauthKey::findOne(['user_id' => Yii::$app->user->id, 'provider_id' => UserOauthKey::getAvailableClients()[$client->getId()]]);
     if ($exists) {
         $button = Html::a('<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> <span class="hidden-xs">' . Yii::t('user', 'Удалить') . '</span>', Url::toRoute(['auth/unbind', 'id' => $client->getId()]), ['class' => 'btn btn-danger btn-sm', 'onclick' => '$(this).off("click"); return true;']);
     } else {
         $viewOptions = $client->getViewOptions();
         if (isset($viewOptions['popupWidth'])) {
             $htmlOptions['data-popup-width'] = $viewOptions['popupWidth'];
         }
         if (isset($viewOptions['popupHeight'])) {
             $htmlOptions['data-popup-height'] = $viewOptions['popupHeight'];
         }
         $htmlOptions['class'] = 'btn btn-success btn-sm';
         $button = Html::a('<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> <span class="hidden-xs">' . Yii::t('user', 'Добавить') . '</span>', $this->createClientUrl($client), $htmlOptions);
     }
     echo Html::tag('span', $button, ['class' => 'auth-icon ' . $client->getName(), 'style' => 'padding-left: 40px; margin-bottom: 10px;']);
     echo Html::endTag('div');
 }
Пример #8
0
 /**
  * @param \yii\authclient\ClientInterface $Client
  * @throws \yii\base\NotSupportedException
  */
 public function appendClientAttributes(\yii\authclient\ClientInterface $Client)
 {
     /** @var \cookyii\modules\Account\resources\Account\Model $self */
     $self = $this;
     $attributes = $Client->getUserAttributes();
     switch ($Client->getId()) {
         default:
             $attributes = null;
             break;
         case 'facebook':
             $attributes = $this->appendFacebookAttributes($attributes);
             break;
         case 'instagram':
             $attributes = $this->appendInstagramAttributes($attributes);
             break;
         case 'github':
             $attributes = $this->appendGithubAttributes($attributes);
             break;
         case 'google':
             $attributes = $this->appendGoogleAttributes($attributes);
             break;
         case 'linkedin':
             $attributes = $this->appendLinkedinAttributes($attributes);
             break;
         case 'live':
             $attributes = $this->appendLiveAttributes($attributes);
             break;
         case 'twitter':
             $attributes = $this->appendTwitterAttributes($attributes);
             break;
         case 'vkontakte':
             $attributes = $this->appendVkontakteAttributes($attributes);
             break;
         case 'yandex':
             $attributes = $this->appendYandexAttributes($attributes);
             break;
         case 'odnoklassniki':
             $attributes = $this->appendOdnoklassnikiAttributes($attributes);
             break;
     }
     if (!empty($attributes)) {
         foreach ($attributes as $key => $value) {
             $attr = $self->getAttribute($key);
             if ($self->hasAttribute($key) && empty($attr)) {
                 $self->setAttribute($key, $value);
             }
         }
     }
 }
 /**
  * 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'];
     if (null === ($account = $this->module->manager->findAccount($provider, $clientId))) {
         $account = $this->module->manager->createAccount(['provider' => $provider, 'client_id' => $clientId, 'data' => json_encode($attributes)]);
         $account->save(false);
     }
     if (null === ($user = $account->user)) {
         $this->action->successUrl = Url::to(['/user/registration/connect', 'account_id' => $account->id]);
     } else {
         \Yii::$app->user->login($user, $this->module->rememberFor);
     }
 }
Пример #10
0
 public function authenticate(ClientInterface $client)
 {
     $attributes = $client->getUserAttributes();
     $provider = $client->getId();
     $clientId = $attributes['id'];
     $model = SocialAccount::find()->where(['provider' => $provider, 'client_id' => $clientId])->one();
     if ($model === NULL) {
         $model->save(FALSE);
     }
     if (NULL === ($user = $model->getUser())) {
         $this->action->successUrl = Url::to(['/user/registration/connect', 'account_id' => $model->id]);
     } else {
         Yii::$app->user->login($user, UserModule::$rememberMeDuration);
     }
 }
Пример #11
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
     }
 }
Пример #12
0
 /**
  * Invoked after a successful authentication with a client.
  *
  * @param ClientInterface $client client instance.
  * @return \yii\web\Response
  */
 public function clientLogin(ClientInterface $client)
 {
     $attributes = $client->getUserAttributes();
     $name = $client->getId();
     $dataContract = $this->module->getDataContract();
     $provider = $dataContract->findProvider(['name' => $name, 'clientId' => $attributes['id']]);
     if ($provider === null) {
         $provider = $dataContract->createProvider(['attributes' => ['name' => $name, 'clientId' => $attributes['id'], 'data' => $attributes]]);
         if (!$provider->save(false)) {
             $this->fatalError();
         }
     }
     if ($provider->account !== null) {
         Yii::$app->user->login($provider->account, Module::getParam(Module::PARAM_LOGIN_EXPIRE_TIME));
         return $this->goHome();
     } else {
         return $this->redirect([Module::URL_ROUTE_CONNECT, 'providerId' => $provider->id]);
     }
 }
Пример #13
0
 public function authenticate(ClientInterface $client)
 {
     $attributes = $client->getUserAttributes();
     $provider = $client->getId();
     $clientId = $attributes['id'];
     $account = $this->finder->findAccountByProviderAndClientId($provider, $clientId);
     if ($account === null) {
         $account = \Yii::createObject(['class' => Account::className(), 'provider' => $provider, 'client_id' => $clientId, 'data' => json_encode($attributes)]);
         $account->save(false);
     }
     if (null === ($user = $account->user)) {
         if ($provider == 'kd') {
             $this->action->successUrl = Url::to(['/user/registration/connect', 'account_id' => $account->id, 'provider' => $provider, 'username' => $attributes['username'], 'email' => $attributes['email']]);
         } else {
             $this->action->successUrl = Url::to(['/user/registration/connect', 'account_id' => $account->id]);
         }
     } else {
         \Yii::$app->user->login($user, $this->module->rememberFor);
     }
 }
Пример #14
0
 /**
  * @param \yii\authclient\ClientInterface $Client
  * @throws \yii\base\NotSupportedException
  */
 public function appendClientAttributes(\yii\authclient\ClientInterface $Client)
 {
     $attributes = $Client->getUserAttributes();
     switch ($Client->getId()) {
         default:
             $attributes = null;
             break;
         case 'facebook':
             $attributes = $this->aggregateFacebookAttributes($attributes);
             break;
         case 'github':
             $attributes = $this->aggregateGithubAttributes($attributes);
             break;
         case 'google':
             $attributes = $this->aggregateGoogleAttributes($attributes);
             break;
         case 'linkedin':
             $attributes = $this->aggregateLinkedinAttributes($attributes);
             break;
         case 'live':
             $attributes = $this->aggregateLiveAttributes($attributes);
             break;
         case 'twitter':
             $attributes = $this->aggregateTwitterAttributes($attributes);
             break;
         case 'vkontakte':
             $attributes = $this->aggregateVkontakteAttributes($attributes);
             break;
         case 'yandex':
             $attributes = $this->aggregateYandexAttributes($attributes);
             break;
     }
     if (!empty($attributes)) {
         $this->setAttributes($attributes);
     }
 }
Пример #15
0
 /**
  * Finds an account by client.
  * @param ClientInterface $client
  * @return models\Account|null
  */
 public function findAccountByClient(ClientInterface $client)
 {
     return $this->accountQuery->where(['provider' => $client->getId(), 'client_id' => $client->getUserAttributes()['id']])->one();
 }
Пример #16
0
 /**
  * Connects social account to user.
  * @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' => Account::className(), 'provider' => $provider, 'client_id' => $clientId, 'data' => json_encode($attributes), 'user_id' => \Yii::$app->user->id]);
         $account->save(false);
         \Yii::$app->session->setFlash('success', \Yii::t('user', 'Your account has been connected'));
     } else {
         if (null == $account->user) {
             $account->user_id = \Yii::$app->user->id;
             $account->save(false);
         } else {
             \Yii::$app->session->setFlash('error', \Yii::t('user', 'This account has already been connected to another user'));
         }
     }
     $this->action->successUrl = Url::to(['/user/settings/networks']);
 }
 /**
  * Connects social account to user.
  *
  * @param  ClientInterface $client
  * @return \yii\web\Response
  */
 public function connect(ClientInterface $client)
 {
     $attributes = $client->getUserAttributes();
     $provider = $client->getId();
     $clientId = $attributes['id'];
     if (null === ($account = $this->module->manager->findAccount($provider, $clientId))) {
         $account = $this->module->manager->createAccount(['provider' => $provider, 'client_id' => $clientId, 'properties' => json_encode($attributes), 'user_id' => \Yii::$app->user->id]);
         $account->save(false);
         \Yii::$app->session->setFlash('account_connected', \Yii::t('user', 'Account has successfully been connected'));
     } else {
         \Yii::$app->session->setFlash('account_not_connected', \Yii::t('user', 'This account has already been connected to another user'));
     }
     return $this->redirect(['networks']);
 }
Пример #18
0
 /**
  * handle
  */
 public function handle()
 {
     $attributes = $this->client->getUserAttributes();
     // common
     $continue = false;
     $id = ArrayHelper::getValue($attributes, 'id');
     $fullname = '';
     $email = '';
     // google
     if ($this->client->getName() == 'google') {
         $fullname = ArrayHelper::getValue($attributes, 'displayName');
         $emails = ArrayHelper::getValue($attributes, 'emails');
         $email = $emails[0]['value'];
         $continue = true;
     }
     // facebook
     if ($this->client->getName() == 'facebook') {
         $fullname = ArrayHelper::getValue($attributes, 'name');
         $email = ArrayHelper::getValue($attributes, 'email');
         $continue = true;
     }
     if (!$continue) {
         //            Yii::$app->getSession()->setFlash('info', [
         //                Yii::t('app', 'Flickr'),
         //            ]);
         //Yii::$app->user->setReturnUrl(Yii::$app->request->referrer);
         return;
     }
     /* @var Auth $auth */
     $auth = Auth::find()->where(['source' => $this->client->getId(), 'source_id' => $id])->one();
     if (Yii::$app->user->isGuest) {
         if ($auth) {
             // login
             /* @var Account $user */
             $user = $auth->user;
             $this->updateUserInfo($user);
             Yii::$app->user->login($user, Setting::getValue('rememberMeDuration'));
         } else {
             // signup
             if ($email !== null && Account::find()->where(['email' => $email])->exists()) {
                 Yii::$app->getSession()->setFlash('error', [Yii::t('app', "User with the same email as in {client} account already exists but isn't linked to it. Login using email first to link it.", ['client' => $this->client->getTitle()])]);
             } else {
                 $password = Yii::$app->security->generateRandomString(6);
                 $user = new Account(['fullname' => $fullname, 'email' => $email, 'password' => $password]);
                 $user->generateAuthKey();
                 $user->generatePasswordResetToken();
                 $transaction = Account::getDb()->beginTransaction();
                 //file_put_contents('D:\log', json_encode($transaction));
                 if ($user->save()) {
                     $auth = new Auth(['user_id' => $user->id, 'source' => $this->client->getId(), 'source_id' => (string) $id]);
                     if ($auth->save()) {
                         $transaction->commit();
                         Yii::$app->user->login($user, Setting::getValue('rememberMeDuration'));
                     } else {
                         $transaction->rollBack();
                         Yii::$app->getSession()->setFlash('error', [Yii::t('app', 'Unable to save {client} account: {errors}', ['client' => $this->client->getTitle(), 'errors' => json_encode($auth->getErrors())])]);
                     }
                 } else {
                     $transaction->rollBack();
                     Yii::$app->getSession()->setFlash('error', [Yii::t('app', 'Unable to save user: {errors}', ['client' => $this->client->getTitle(), 'errors' => json_encode($user->getErrors())])]);
                 }
             }
         }
     } else {
         // user already logged in
         Yii::$app->user->setReturnUrl(Yii::$app->request->referrer);
         if (!$auth) {
             // add auth provider
             $auth = new Auth(['user_id' => Yii::$app->user->id, 'source' => $this->client->getId(), 'source_id' => (string) $attributes['id']]);
             if ($auth->save()) {
                 /** @var Account $user */
                 $user = $auth->user;
                 $this->updateUserInfo($user);
                 Yii::$app->getSession()->setFlash('success', [Yii::t('app', 'Linked {client} account.', ['client' => $this->client->getTitle()])]);
             } else {
                 Yii::$app->getSession()->setFlash('error', [Yii::t('app', 'Unable to link {client} account: {errors}', ['client' => $this->client->getTitle(), 'errors' => json_encode($auth->getErrors())])]);
             }
         } else {
             // there's existing auth
             Yii::$app->getSession()->setFlash('error', [Yii::t('app', 'Unable to link {client} account. There is another user using it.', ['client' => $this->client->getTitle()])]);
         }
     }
 }
Пример #19
0
 /**
  * Connects social account to user.
  *
  * @param  ClientInterface $client
  * @return \yii\web\Response
  */
 public function connect(ClientInterface $client)
 {
     $attributes = $client->getUserAttributes();
     $provider = $client->getId();
     $clientId = $attributes['id'];
     if (null === ($account = $this->module->manager->findAccount($provider, $clientId))) {
         $account = $this->module->manager->createAccount(['provider' => $provider, 'client_id' => $clientId, 'data' => json_encode($attributes), 'user_id' => \Yii::$app->user->id]);
         $account->save(false);
         \Yii::$app->session->setFlash('success', \Yii::t('user', 'Account has been successfully connected'));
     } else {
         \Yii::$app->session->setFlash('error', \Yii::t('user', 'This account has already been connected to another user'));
     }
     $this->action->successUrl = Url::to(['/user/settings/networks']);
 }
Пример #20
0
 public function getRemoteUser(ClientInterface $client)
 {
     $class = $this->remoteUserClass;
     return $class::findOrCreate($client->getId(), $client->getUserAttributes()['id']);
 }
Пример #21
0
 public static function create(BaseClientInterface $client)
 {
     /** @var Account $account */
     $account = Yii::createObject(['class' => static::className(), 'provider' => $client->getId(), 'client_id' => $client->getUserAttributes()['id'], 'data' => json_encode($client->getUserAttributes())]);
     if ($client instanceof ClientInterface) {
         $account->setAttributes(['username' => $client->getUsername(), 'email' => $client->getEmail()], false);
     }
     if (($user = static::fetchUser($account)) instanceof User) {
         $account->user_id = $user->id;
     }
     $account->save(false);
     return $account;
 }
Пример #22
0
 public function setCurrentAuthClient(ClientInterface $authClient)
 {
     Yii::$app->session->set('currentAuthClientId', $authClient->getId());
 }
Пример #23
0
 /**
  * Creates auth instance by from client.
  *
  * @param ClientInterface $client
  * @return static
  */
 public static function getInstance(ClientInterface $client)
 {
     return Yii::createObject(['class' => static::className(), 'provider' => $client->getId(), 'provider_id' => $client->getUserAttributes()['id'], 'provider_attributes' => json_encode($client->getUserAttributes())]);
 }
Пример #24
0
 /**
  * Checks if provider already connected to user.
  *
  * @param ClientInterface $provider
  * @return bool
  */
 public function isConnected(ClientInterface $provider)
 {
     return $this->accounts != null && isset($this->accounts[$provider->getId()]);
 }
Пример #25
0
 /**
  * Returns all users which are using an given authclient
  * 
  * @param ClientInterface $authClient
  * @return \yii\db\ActiveQuery
  */
 public static function getUsersByAuthClient(ClientInterface $authClient)
 {
     $query = User::find();
     if ($authClient instanceof interfaces\PrimaryClient) {
         $query->where(['auth_mode' => $authClient->getId()]);
     } else {
         $query->where(['user_auth.source' => $authClient->getId()]);
     }
     return $query;
 }
Пример #26
0
 /**
  * @param \yii\authclient\ClientInterface $Client
  * @throws \yii\web\ForbiddenHttpException
  */
 public function authSuccessCallback(\yii\authclient\ClientInterface $Client)
 {
     $AuthResponse = new \cookyii\modules\Account\resources\Account\Auth\Response();
     $AuthResponse->client = $Client->getId();
     $attributes = $Client->getUserAttributes();
     $AuthResponse->response = Json::encode($attributes);
     /** @var \cookyii\modules\Account\resources\Account $AccountModel */
     $AccountModel = \Yii::createObject(\cookyii\modules\Account\resources\Account::className());
     $AccountQuery = $AccountModel::find();
     switch ($Client->getId()) {
         case 'facebook':
             $AccountQuery->byFacebookId($attributes['id']);
             break;
         case 'github':
             $AccountQuery->byGithubId($attributes['id']);
             break;
         case 'google':
             $AccountQuery->byGoogleId($attributes['id']);
             break;
         case 'linkedin':
             $AccountQuery->byLinkedinId($attributes['id']);
             break;
         case 'live':
             $AccountQuery->byLiveId($attributes['id']);
             break;
         case 'twitter':
             $AccountQuery->byTwitterId($attributes['id']);
             break;
         case 'vkontakte':
             $AccountQuery->byVkontakteId($attributes['id']);
             break;
         case 'yandex':
             $AccountQuery->byYandexId($attributes['id']);
             break;
     }
     $Account = $AccountQuery->one();
     if ($Account instanceof \cookyii\modules\Account\resources\Account) {
         if (true !== ($reason = $Account->isAvailable())) {
             switch ($reason) {
                 default:
                 case true:
                     break;
                 case 'not-activated':
                     $Account->addError('activated', \Yii::t('account', 'Account is not activated.'));
                     break;
                 case 'deleted':
                     $Account->addError('deleted', \Yii::t('account', 'Account removed.'));
                     break;
             }
             $AuthResponse->result = Json::encode($Account->getErrors());
         } else {
             $AuthResponse->result = Json::encode($Account->id);
         }
     } else {
         $Account = $AccountModel;
         $Account->appendClientAttributes($Client);
         if (!empty($Account->email)) {
             $SearchAccount = $AccountModel::find()->byEmail($Account->email)->one();
             if (!empty($SearchAccount)) {
                 $Account = $SearchAccount;
                 $Account->appendClientAttributes($Client);
             }
         } else {
             Session()->set('OAuthResponseClient', $Client);
             Response()->redirect(['/account/sign/fill'])->send();
             exit;
         }
         if ($Account->save()) {
             $Account->createSocialLink($Client);
             $AuthResponse->result = Json::encode($Account->id);
             if (!$Account->can(\common\Roles::USER)) {
                 AuthManager()->assign(RbacFactory::Role(\common\Roles::USER), $Account->id);
             }
         } else {
             $AuthResponse->result = Json::encode($Account->getErrors());
         }
     }
     $AuthResponse->save();
     if ($Account instanceof \cookyii\modules\Account\resources\Account && !$Account->isNewRecord && !$Account->hasErrors()) {
         $Account->save();
         User()->login($Account, 86400);
     } else {
         $errors = $Account->getFirstErrors();
         if (isset($errors['activated'])) {
             throw new \yii\web\ForbiddenHttpException($errors['activated']);
         }
         if (isset($errors['deleted'])) {
             throw new \yii\web\ForbiddenHttpException($errors['deleted']);
         }
     }
 }
Пример #27
0
 /**
  * 绑定第三方账号
  * @param  ClientInterface $client
  * @return \yii\web\Response
  */
 public function connect(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), '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']);
 }
Пример #28
0
 /**
  * Finds an account by client.
  * @param  ClientInterface $client
  * @return AccountQuery
  */
 public function byClient(ClientInterface $client)
 {
     return $this->andWhere(['provider' => $client->getId(), 'unionid' => $client->getUserAttributes()['unionid']]);
 }
Пример #29
0
 public static function create(BaseClientInterface $client)
 {
     //xiaoma update
     //qq and sina 's attr has no id
     $client_type = $client->getId();
     switch ($client_type) {
         case 'qq':
             $client_id = $client->getUserAttributes()['openid'];
             $data = json_encode($client->getUserInfo());
             break;
         case 'sina':
             $client_id = $client->getUserAttributes()['uid'];
             $data = json_encode($client->getUserInfo());
             break;
         default:
             $client_id = $client->getUserAttributes()['id'];
             $data = json_encode($client->getUserAttributes());
             break;
     }
     /** @var Account $account */
     $account = Yii::createObject(['class' => static::className(), 'provider' => $client->getId(), 'client_id' => $client_id, 'data' => $data]);
     if ($client instanceof ClientInterface) {
         $account->setAttributes(['username' => $client->getUsername(), 'email' => $client->getEmail()], false);
     }
     if (($user = static::fetchUser($account)) instanceof User) {
         $account->user_id = $user->id;
     }
     $account->save(false);
     return $account;
 }