Index.php 3.43 KB
<?php

class IndexController extends QLib_Actions_Login
{
    public function indexAction()
    {
        $this->_sessionNamespace('adminx_profile')->unsetAll();
        $this->_assign('refer', $this->_get('refer'));
        Adminx_Models_Profile_Client::getRoleByProfile(1);
    }

    public function loginAction()
    {
        $this->disableView();
        
        $username = trim($this->_post('username'));
        $inputPassword = trim($this->_post('password'));
        $refer = trim($this->_post('refer'));
        if (empty($username) || empty($inputPassword)) {
            $this->helpLocation('账号和密码不能为空.', '/');
        }
        $profile = Adminx_Models_Profile_Client::getProfileByUsername($username);     
        if (empty($profile)) {
            $this->helpLocation('账号不存在或者被禁用.', '/');
        }
        if ((int)$profile['status'] === 0) {
            $this->helpLocation('账号已关闭.', '/');
        }
        $loginExpires = QLibConfigs_GlobalConfig::$login_expires_day * 86400;
        if ((int)$profile['login_time'] + $loginExpires < time()) {
            $this->helpLocation('你的账号已经 ' . QLibConfigs_GlobalConfig::$login_expires_day . ' 天未登录,账号被冻结,请联系管理员.', '/');
        }
        if ($profile['is_lock'] == 'Y') {
            $this->helpLocation('账号已锁定请联系管理员.', '/');
        }
        if ((int)$profile['expires'] < time()) {
            $this->helpLocation('账号已过期请联系管理员.', '/');
        }
        #############################################
        $cache = Q_Cache::factory('Memcached')->setPrefix('adminx' . '.' . 'profile');
        #############################################
        $logKey = 'adminx_login_' . $profile['pid'];
        $password = $profile['password'];
        $authPass = Q_Utils_AuthCode::authPassword(QLibConfigs_GlobalConfig::confusionPassword($inputPassword), $password);
        if (empty($authPass)) {
            $cache->increment($logKey) == false ? $cache->set($logKey, 1) : true;
            if ($cache->get($logKey) > 3) {
                Adminx_Models_Profile_Client::updateProfileLockByPid($profile['pid']);
            }
            $this->helpLocation('密码错误.', '/');
           
        }
        ################################### BEGIN SET COOKIE ###############################################
        $profile['signin_time'] = time();
        $profile['token'] = uniqid();
        Adminx_Models_Profile_Client::updateLoginTime($profile['pid']);
        $cookieDataKey = Q_Utils_AuthCode::encode(json_encode($profile), QLibConfigs_GlobalConfig::$cookie_key);
        Q_Core_Cookie::setCookie(QLibConfigs_GlobalConfig::$cookie_name, QLibConfigs_GlobalConfig::$domain, $cookieDataKey, 0, '/', true, false);
        ################################### END SET COOKIE #################################################
        ################################### BEGIN SET SESSION ##############################################
        $this->_sessionNamespace('adminx_profile')->unsetAll();
        $this->_sessionNamespace('adminx_profile')->__set(QLibConfigs_GlobalConfig::$session_name, $profile);
        $this->_sessionNamespace('adminx_profile')->__set(QLibConfigs_GlobalConfig::$token_name, $profile['token']);
        ################################### END SET SESSION ################################################
        $this->helpGo(empty($refer) ? '/console.html' : $refer);
    }
}