Exemplo n.º 1
0
 /**
  * Index method
  */
 public function actionLogin()
 {
     $objLoginModel = LoginModel::model();
     try {
         //检查是否是post请求
         $boolCheckLogin = false;
         if (Nbt::app()->request->isPostRequest) {
             // 绑定数据
             $aryUserInfo['uname'] = isset($_POST['uname']) ? htmlspecialchars(trim($_POST['uname'])) : '';
             $aryUserInfo['pwd'] = isset($_POST['pwd']) ? CString::encodeMachinePassword(trim($_POST['pwd'])) : '';
             $strIsRemember = isset($_POST['remember']) ? htmlspecialchars(trim($_POST['remember'])) : 'no';
             // 检查登录
             $boolCheckLogin = LoginModel::model()->checkLogin($aryUserInfo);
             if ($boolCheckLogin === false) {
                 throw new CModelException(CUtil::i18n('controllers,login_index_pwdWrong'));
             }
             //根据是否登入成功,再判断是否将用户名和密码写入cookie
             if ($boolCheckLogin === true && $strIsRemember === 'yes') {
                 $aryUserInfo['pwd'] = CString::encode($aryUserInfo['pwd'], UKEY);
                 //设置cookie,时间为一年
                 setcookie($this->_cookeName, base64_encode(json_encode($aryUserInfo)), time() + 365 * 24 * 3600);
             }
         } else {
             if (!empty($_COOKIE[$this->_cookeName]) && $boolCheckLogin === false) {
                 $aryUserInfo = json_decode(base64_decode($_COOKIE[$this->_cookeName]), 1);
                 $aryUserInfo['pwd'] = CString::decode($aryUserInfo['pwd'], UKEY);
                 if (($boolCheckLogin = $objLoginModel->checkLogin($aryUserInfo)) === false) {
                     throw new CModelException(CUtil::i18n('controllers,login_index_pwdWrong'));
                 }
             }
         }
         //contains/判断是否登入成功
         if ($boolCheckLogin === true) {
             Nbt::app()->session->set('userInfo', $aryUserInfo);
             UtilMsg::saveTipToSession(CUtil::i18n('controllers,login_index_success'));
             $this->redirect(array('index/index'));
         }
     } catch (CModelException $e) {
         UtilMsg::saveErrorTipToSession($e->getMessage());
     }
     $this->layout = 'login';
     $this->seoTitle = CUtil::i18n('controllers,login_index_seoTitle');
     //根据key判断文件是否存在,如果不存在则)创建一个默认账户
     $objLoginModel->createDefaultUserInfo();
     $this->render('login', array('aryData' => $aryUserInfo));
 }
Exemplo n.º 2
0
 /**
  * 创建默认用户
  * 
  * @author zhangyi
  * @date 2014-5-30
  */
 public function createDefaultUserInfo()
 {
     //判断是否存在用户信息文件来确定是否创建用户文件
     $redis = $this->getRedis();
     $aryUserInfo = $redis->readByKey($this->_fileName);
     if (empty($aryUserInfo)) {
         return $redis->writeByKey($this->_fileName, json_encode(array('uname' => UUSERNAME, 'pwd' => CString::encodeMachinePassword(UPASSWORD))));
     } else {
         return false;
     }
 }