0

这是我的模型课

class Sessions_model extends CI_Model
{
    private $permission = array()
    public function __construct()
    {
        parent::__construct();
    }

    public function is_login()
    {
        if($this->session->userdata('logged_in')) return TRUE;
        else return false;
    }
    public function login($username,$password)
    {
        $this->db->where('username',$username);
        $this->db->where('password',$password);
        $query = $this->db->get('users');
        if($query->num_rows()==1){
            $user = $query->row();
            $this->set_authnication($user->id);
            return TRUE;
        }
    }
    public function logout()
    {
        $this->session->sess_destroy();
    }

    public function set_authnication($user_id)
    {
        $this->load->helper('date');
        $query = $this->db->where('id',$user_id)->get('users');
        $user = $query->row();
        $auth_data = array(
            'user_id' => $user_id,
            'logged_in' => 1,
            'name' => $user->name,
            'username'=> $user->username,
            'email' => $user->email,
            'role' => $this->initRole($user_id),
            'lastVisitDate'=> now()
        );
        $this->session->set_userdata($auth_data);
    }
    public function initRole($id)
    {
        $role_perm = array();
        $role = $this->db->where('id',$id)->get('roles')->row();        
        return $role_perm= array(
            $role->description => $this->getPerm($role->id)
        );

    }
    public function getPerm($id)
    {       
        $perms = $this->db->where('role_id',$id)->get('permissions')->result();
        foreach ($perms as $perm) {
            $this->permissions[$perm->permission] = true;
        }
        return $this->permissions;  
    }
    public function hasPermTo()
    {
        // $this->getPerm('1');
        // return (isset($permissions[$perm]))? 'true' : 'false';
        // print_r($this->session->userdata('role'));
        print_r($this->permissions);
    }

}

当我设置登录用户名和密码并调用 set_authnication($user_id) 时。当此登录方法完成时,我获得了具有权限的角色。我将我的私有属性设置为具有关联权限..到目前为止,一切似乎都还不错。已通过身份验证,我尝试验证该用户是否有权访问该类,因此我调用函数 $this->sessions_model->hasPermTo(); 但是这次我的私有属性是空数组()..它有第n个......我不知道问题是什么?

4

1 回答 1

0

您的私有变量是permission,但您是设置permissions(注意末尾的 S)。

于 2013-04-30T05:36:45.747 回答