我正在编写管理员检查以确保登录管理区域的电子邮件是:
我到目前为止编写了以下代码:
控制器:
$this->form_validation->set_rules('userName','userName', 'required|trim|max_length[99]|callback_admin_check|xss_clean');
function _admin_check($adminUsername, $adminEmail)
{
if($this->users_model->admin_check($adminUsername) || $this->users_model->admin_check($adminEmail))
{
$this->form_validation->set_message('admin_check', 'Sorry you have an %s error!');
return FALSE;
}else{
return TRUE;
}
我在这里感到困惑的是,如果userGroup == admin
或帐户是userActive == yes
,我希望它接受管理员用户名或电子邮件地址,但我不确定如何构建模型或要发送到模型的数据。
更新 - >乔:
乔,
只是几个问题:
return false
?控制器:
function _admin_check($adminUsername = null, $adminEmail = null)
{
$adminUser = $this->user_model->admin_check($adminUsername,$adminEmail);
//if the UN || PW are not correct
if(! $adminUser)
{
$this->session->set_flashdata('login_error', TRUE); // Does not bother adding incorrect data into the session for the Admin Login
$this->form_validation->set_message('admin_check', 'Sorry you have a %s error!');
return FALSE;
}else{
//Set the session data
$this->session->set_userdata('logged_in', TRUE);
$this->session->set_userdata('userId',$adminUser->id);
$this->session->set_userdata('userFirstName',$adminUser->userFirstName);
$this->session->set_userdata('userLastName',$adminUser->userLastName);
$this->session->set_userdata('userEmail',$adminUser->userEmail);
$this->session->set_userdata('userGroup',$adminUser->userGroup);
$this->session->set_userdata('userActive',$adminUser->userActive);
return TRUE;
}
型号:
function admin_check($adminUsername, $adminEmail)
{
if(is_null($adminUsername && is_null($adminEmail)))
{
return FALSE;
}
if(is_null($adminUsername))
{
$login_field = 'userEmail';
$login_name = '$adminEmail';
}else{
$login_field = 'userName';
$login_name = '$adminUsername'
}
$this->db->select($login_field,$login_name);
$this->db->from('users');
$this->db->where('userName', $adminUsername , 'userEmail' , $adminEmail );
$query = $this->db-get();
if($query->num_rows() > 0)
{
return TRUE;
}else{
return FALSE;
}
}
答案 0 :(得分:3)
您可以创建模型的方法以接收2个参数,即用户名和电子邮件。
function admin_check($username = NULL, $email = NULL)
现在,如果您想使用用户名进行检查,只需调用admin_check($username)
之类的函数即可。但是,如果您想通过电子邮件进行检查,可以将NULL
作为第一个参数传递,将电子邮件作为第二个参数传递。
然后在函数内部创建一个if
语句来检查第一个参数是否为NULL
。如果是,请使用电子邮件地址进行检查。否则,请使用用户名进行检查。
if ($username === NULL)
{
// use the email address to do the authentication
}
else
{
// use $username instead
}
答案 1 :(得分:2)
将两个参数的默认值设置为null:function _admin_check($adminUsername = null, $adminEmail = null)
然后在模型中方法的顶部添加:
function admin_check($adminUsername = null, $adminEmail = null)
{
if (is_null($adminUsername) && is_null($adminEmail))
{
return false;
}
if (is_null($adminUsername))
{
$login_field = 'email'; // field in the table
$login_name = $adminEmail; // value of the field
}
else
{
$login_field = 'username';
$login_name = $adminUsername;
}
// Your SQL here, using $login_field and $login_name
要使用用户名进行呼叫,请按以下方式拨打电话:
$this->users_model->admin_check($adminUsername);
$this->users_model->admin_check($adminUsername, null); // or this
对于电子邮件
$this->users_model->admin_check(null, $adminEmail);
将其称为一个:
$this->users_model->admin_check($adminUsername, $adminEmail);