这是我的模型类
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)。当这个登录方法完成时,我已经获得了具有权限的角色。我设置了具有关联权限的私有属性..到目前为止,所有事情似乎都可以 我的概率当用户被授权时,我试图验证该用户是否具有该类的权限,因此我调用函数 $这 - > sessions_model-> hasPermTo(); 但是这次我的私有属性是空数组()..它有... 我不知道问题是什么?
答案 0 :(得分:0)
您的私有变量为permission
,但您设置为permissions
(请注意结尾处的S)。