Base.class.php 2.08 KB
<?php
/**
 * 默认控制器
 */
class Controller_Admin_Base extends Controller_Abstract
{

    /**
     * 当前登录的用户信息
     *
     * @var array
     */
    protected $_admin = array() ;
    
    /**
     * 管理账户列表
     *
     * @var unknown_type
     */
    private $_admins = array
    (
        //array('username' => '用户名', 'password' => '密码'),
        array('username' => 'yohoadmin',  'password' => '9646abcdef'),
   // 	  array('username' => 'test',  'password' => '123456')
    );
    
    /**
     * 检测权限
     *
     */
    protected function beforeExecute()
    {
        //获取session
        $session = Util_Utils_SafeCookie::get('_admin');
        $session = unserialize($session);
        //非默认控制器,则校验权限
        if ($this->_request->controller_name != 'default'  && !$session)
        {
            //跳转至登录页
            header('Location: ' . url('default/index'));
            exit();
        }
        $this->_admin = $session ;
        $this->_view['_ctx'] = $this->_request ;
    }
    
    /**
     * 处理登录
     * @param string $username 
     * @param string $password
     *
     */
    protected function login($username, $password)
    {
        if ($username == '' || $password =='')
        {
            return false ;
        }
        $exitUser = array() ;
        
        //查找用户
        foreach ($this->_admins as $k => $val)
        {
            if ($val['username'] == $username)
            {
                $exitUser = $this->_admins[$k];
                $exitUser['id'] = $k ;
            }
        }
        if (empty($exitUser)) 
        {
        	return false ;
        }
        
        //校验密码
        if ($password == $exitUser['password']) 
        {
            $exitUser = serialize($exitUser);
            //通过
            Util_Utils_SafeCookie::set('_admin', $exitUser);
        	return true ;
        }
        return false ;
    }
    
    /**
     * 登出
     *
     */
    protected function logout()
    {
        Util_Utils_SafeCookie::clear('_admin'); 
    }
}