Esempio n. 1
0
 public function actionAuthHistory($user_id)
 {
     $model = new UsersAuthLogs('search');
     $model->unsetAttributes();
     if (isset($_GET['UsersAuthLogs'])) {
         $model->setAttributes($_GET['UsersAuthLogs']);
     }
     $dataProvider = $model->search();
     $dataProvider->criteria->mergeWith(array('condition' => 'user_id = :user_id', 'params' => array('user_id' => $user_id)));
     $this->render('//users/auth-history', array('user' => Users::model()->findByPk($user_id), 'model' => $model, 'dataProvider' => $dataProvider));
 }
Esempio n. 2
0
 public function authenticate()
 {
     $userIp = userIp();
     $this->_user = Users::model()->with('profile')->find('login = :login AND role = :role', array('login' => $this->username, 'role' => Users::ROLE_ADMIN));
     if ($this->_user === NULL) {
         $this->errorCode = self::ERROR_USERNAME_INVALID;
     } elseif (Users::validatePassword($this->password, $this->_user->password) === FALSE) {
         $this->errorCode = self::ERROR_PASSWORD_INVALID;
         // Сохраняю неудачную попытку входа
         UsersAuthLogs::model()->addErrorAuth($this->_user->getPrimaryKey());
     } elseif ($this->_user->activated == Users::STATUS_INACTIVATED) {
         $this->errorCode = self::ERROR_STATUS_INACTIVE;
     } elseif ($this->_user->role == Users::ROLE_BANNED) {
         $this->errorCode = self::ERROR_STATUS_BANNED;
     } elseif ($this->_user->profile->protected_ip && !in_array($userIp, $this->_user->profile->protected_ip)) {
         $this->errorCode = self::ERROR_STATUS_IP_NO_ACCESS;
     } else {
         $this->_id = $this->_user->getPrimaryKey();
         $this->_user->auth_hash = Users::generateAuthHash();
         $this->setState('auth_hash', $this->_user->auth_hash);
         $this->_user->save(FALSE, array('auth_hash', 'updated_at'));
         // Запись в лог
         UsersAuthLogs::model()->addSuccessAuth($this->_user->getPrimaryKey());
         $this->errorCode = self::ERROR_NONE;
     }
     return !$this->errorCode;
 }
 public function actionIndex()
 {
     $dependency = new CDbCacheDependency('SELECT MAX(UNIX_TIMESTAMP(created_at)) FROM {{users_auth_logs}} WHERE user_id = :user_id');
     $dependency->params = array('user_id' => user()->getId());
     $model = UsersAuthLogs::model()->cache(3600 * 24, $dependency, 2);
     $dataProvider = new CActiveDataProvider($model, array('criteria' => array('condition' => 'user_id = :user_id', 'params' => array('user_id' => user()->getId()), 'order' => 't.created_at DESC'), 'pagination' => array('pageSize' => (int) config('cabinet.auth_logs_limit'), 'pageVar' => 'page')));
     $this->render('//cabinet/auth-history', array('dataProvider' => $dataProvider));
 }
Esempio n. 4
0
 public function authenticate()
 {
     $userIp = userIp();
     $this->_user = Users::model()->with('profile')->find('login = :login AND ls_id = :ls_id', array('login' => $this->username, 'ls_id' => $this->_ls_id));
     if ($this->_user === NULL) {
         $this->errorCode = self::ERROR_USERNAME_INVALID;
     } elseif (!$this->_user->isActivated()) {
         $this->errorCode = self::ERROR_STATUS_INACTIVE;
     } elseif ($this->_user->isBanned()) {
         $this->errorCode = self::ERROR_STATUS_BANNED;
     } elseif ($this->_user->profile->protected_ip && !in_array($userIp, $this->_user->profile->protected_ip)) {
         $this->errorCode = self::ERROR_STATUS_IP_NO_ACCESS;
     } else {
         $this->_id = $this->_user->getPrimaryKey();
         $this->_user->auth_hash = Users::generateAuthHash();
         $this->setState('auth_hash', $this->_user->auth_hash);
         $this->setState('gs_id', $this->_gs_id);
         $this->setState('ls_id', $this->_user->getLsId());
         UsersAuthLogs::model()->addSuccessAuth($this->_id);
         $this->_user->save(FALSE, array('auth_hash', 'updated_at'));
         $this->errorCode = self::ERROR_NONE;
     }
     return !$this->errorCode;
 }
Esempio n. 5
0
 /**
  * Проверка логина на сервере
  *
  * @param $attr
  */
 public function loginExists($attr)
 {
     if (!$this->hasErrors($attr)) {
         $siteAccountUserId = NULL;
         try {
             $found = FALSE;
             $login = $this->getLogin();
             $lsId = $this->getLsId();
             $l2 = l2('ls', $lsId)->connect();
             $command = $l2->getDb()->createCommand();
             $command->where('login = :login', array('login' => $login));
             $command->from('accounts');
             $account = $command->queryRow();
             // Ищю аккаунт на сайте
             $siteAccount = db()->createCommand("SELECT user_id FROM {{users}} WHERE login = :login LIMIT 1")->queryRow(TRUE, array('login' => $login));
             if (isset($siteAccount['user_id'])) {
                 $siteAccountUserId = $siteAccount['user_id'];
             }
             // Аккаунт на сервере найден
             if ($account) {
                 if ($account['password'] == $l2->passwordEncrypt($this->getPassword())) {
                     // Аккаунта на сайте нет, создаю его так как на сервере он уже есть
                     if (!$siteAccount) {
                         $email = NULL;
                         $columnNames = $l2->getDb()->getSchema()->getTable('accounts')->getColumnNames();
                         if (is_array($columnNames)) {
                             foreach ($columnNames as $column) {
                                 if (strpos($column, 'mail') !== FALSE && isset($account[$column])) {
                                     $email = $account[$column];
                                 }
                             }
                         }
                         // Создаю аккаунт на сайте
                         $userModel = new Users();
                         $userModel->password = NULL;
                         $userModel->login = $login;
                         $userModel->email = $email;
                         $userModel->activated = Users::STATUS_ACTIVATED;
                         $userModel->role = Users::ROLE_DEFAULT;
                         $userModel->ls_id = $lsId;
                         $userModel->save(FALSE);
                         $siteAccountUserId = $userModel->getPrimaryKey();
                     }
                     $found = TRUE;
                 }
             }
             // Аккаунт не найден
             if (!$found) {
                 if ($siteAccountUserId) {
                     UsersAuthLogs::model()->addErrorAuth($siteAccountUserId);
                 }
                 $this->incrementBadAttempt();
                 $this->addError($attr, Yii::t('main', 'Неправильный Логин или Пароль.'));
             }
         } catch (Exception $e) {
             $this->addError($attr, Yii::t('main', 'Произошла ошибка! Поробуйте повторить позже.'));
         }
     }
 }