我有一个限制访问我网站上某些地方的功能。在此代码中,userGroup必须是“用户”才能让人员访问站点上的链接。但是,我最近添加了userGroup'vip',但是他们无法访问链接,因为方法中传递的参数只是'user'。我不确定我要做什么才能让它与'user'和'vip'一起使用....但这是我的代码:
此处调用方法$this->access->restrict('user');
:
class MY_User extends MY_Controller {
function MY_User() {
parent::MY_Controller();
// Protect the user area
$this->access->restrict('user');
// Don't want to cache this content
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0", false);
$this->output->set_header("Pragma: no-cache");
}
}
并传递给restrict()函数:
function restrict($userGroup = FALSE)
{
if($userGroup == 'logged_out')
{
if ($this->CI->user->id > 0)
{
redirect('user');
}
}
else if($this->CI->user->id < 0)
{
$this->CI->session->set_flashdata('referrer', $this->CI->uri->uri_string());
redirect();
} else if($this->CI->user->userGroup != 'admin'){
if ($userGroup AND $this->CI->user->userGroup !== $userGroup)
{
$this->CI->session->set_flashdata('referrer', $this->CI->uri->uri_string());
redirect();
}
}
}
我意识到我必须在
中创建一个新参数 function restrict($userGroup = FALSE)
和
$this->access->restrict('user');
但我不确定在那之后该怎么做
答案 0 :(得分:0)
我假设对redirect
的调用意味着你希望函数在那里结束,所以我已经返回重定向(除非由于某些其他原因调用redirect导致函数终止,然后删除return
)
function restrict($userGroup = FALSE)
{
if( $this->CI->user->id < 0)
{
$this->CI->session->set_flashdata('referrer', $this->CI->uri->uri_string());
return redirect();
}
switch( $userGroup)
{
case 'logged_out':
if( $this->CI->user->id > 0)
{
return redirect( 'user');
}
break;
case 'vip':
case 'user':
if( !( $this->CI->user->userGroup == $userGroup))
{
$this->CI->session->set_flashdata('referrer', $this->CI->uri->uri_string());
return redirect();
}
break;
case 'admin':
// Something - Maybe don't do anything?
break;
default:
// Something ?
break;
}
}
答案 1 :(得分:0)
我找到了解决方案!
我改变了:
else if($this->CI->user->userGroup != 'admin'){
if ($userGroup AND $this->CI->user->userGroup !== $userGroup)
{
$this->CI->session->set_flashdata('referrer', $this->CI->uri->uri_string());
redirect();
}
}
为:
else if($this->CI->user->userGroup != 'admin'){
if($this->CI->user->userGroup != 'vip'){
if ($userGroup AND $this->CI->user->userGroup !== $userGroup)
{
$this->CI->session->set_flashdata('referrer', $this->CI->uri->uri_string());
redirect();
}
}
}
第二个有效的解决方案(谢谢你昵称:
function restrict($userGroup = FALSE)
{
if( $this->CI->user->id < 0)
{
$this->CI->session->set_flashdata('referrer', $this->CI->uri->uri_string());
return redirect();
}
if($userGroup == 'logged_out')
{
if ($this->CI->user->id > 0)
{
redirect('user');
}
}
switch($this->CI->user->userGroup)
{
case 'vip':
break;
case 'user':
break;
case 'admin':
break;
default:
$this->CI->session->set_flashdata('referrer', $this->CI->uri->uri_string());
return redirect();
break;
}
}