Пример #1
0
 /**
  * Finds user by [[username]]
  *
  * @return User|null
  */
 public function getUser()
 {
     if ($this->_user === false) {
         $this->_user = User::find()->published()->andWhere(['or', ['username' => $this->username], ['email' => $this->username]])->one();
     }
     return $this->_user;
 }
 public function down()
 {
     if ($user = User::find()->where(['username' => 'admin'])->one()) {
         /** @var $user User */
         $user->delete();
         echo 'User "' . $user->username . '" deleted.';
     }
 }
 /**
  * @param $client \yii\authclient\BaseClient
  * @throws \yii\db\Exception
  */
 public function onAuthSuccess($client)
 {
     $attributes = $client->getUserAttributes();
     /* @var $auth UserAuthClient */
     $auth = UserAuthClient::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', "Пользователь с такой электронной почтой как в {client} уже существует, но с ним не связан. Для начала войдите на сайт использую электронную почту, для того, что бы связать её.", ['client' => $client->getTitle()])]);
             } else {
                 $password = Yii::$app->security->generateRandomString(6);
                 $user = new User(['username' => $attributes['login'], 'email' => $attributes['email'], 'password' => $password]);
                 $user->generateAuthKey();
                 $user->generatePasswordResetToken();
                 $transaction = $user->getDb()->beginTransaction();
                 if ($user->save()) {
                     $auth = new UserAuthClient(['user_id' => $user->id, 'source' => $client->getId(), 'source_id' => (string) $attributes['id']]);
                     if ($auth->save()) {
                         $transaction->commit();
                         Yii::$app->user->login($user);
                     } else {
                         print_r($auth->getErrors());
                     }
                 } else {
                     print_r($user->getErrors());
                 }
             }
         }
     } else {
         // Пользователь уже зарегистрирован
         if (!$auth) {
             // добавляем внешний сервис аутентификации
             $auth = new UserAuthClient(['user_id' => Yii::$app->user->id, 'source' => $client->getId(), 'source_id' => $attributes['id']]);
             $auth->save();
         }
     }
 }
Пример #4
0
 /**
  * Creates data provider instance with search query applied
  * @param array $params
  * @param bool $trashed
  * @return ActiveDataProvider
  */
 public function search($params, $trashed = false)
 {
     $query = User::find();
     if ($trashed) {
         $query->trashed();
     } else {
         $query->published();
     }
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['updated_at' => SORT_DESC]]]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'status' => $this->status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'deleted_at' => $this->deleted_at, 'last_visit_at' => $this->last_visit_at, 'login_ip' => $this->login_ip]);
     $query->andFilterWhere(['like', 'username', $this->username])->andFilterWhere(['like', 'email', $this->email])->andFilterWhere(['like', 'password_hash', $this->password_hash])->andFilterWhere(['like', 'password_reset_token', $this->password_reset_token])->andFilterWhere(['like', 'auth_key', $this->auth_key]);
     if (count($this->getRoles())) {
         /** @var \yii\rbac\DbManager $auth */
         $auth = Yii::$app->authManager;
         $query->leftJoin($auth->assignmentTable . ' roles', 'roles.user_id=id');
         $query->andWhere(['roles.item_name' => $this->getRoles()]);
     }
     return $dataProvider;
 }
 /**
  * @return \yii\web\Response
  * @throws ForbiddenHttpException
  * @throws \Exception
  */
 public function actionBulkRestore()
 {
     $data = Yii::$app->request->getBodyParam('data', []);
     $models = User::find()->trashed()->andWhere(['id' => $data])->all();
     foreach ($models as $model) {
         $model->untrash();
     }
     return $this->redirect(ArrayHelper::getValue(Yii::$app->request, 'referrer', ['index']));
 }