我正在写一个管理检查,以确保登录到管理区域的电子邮件是:
- 管理员用户
- 处于活动状态
到目前为止,我已经写了以下代码:
控制器:
$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
,我希望它同时接受管理员用户名或电子邮件地址,但我不确定如何构建模型,也不确定要向模型发送什么数据。
更新->Joe:
Joe,
只有几个问题:
- 我是否仍将验证错误消息设置为
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;
}
}
您可以创建模型的方法来接收两个参数,即用户名和电子邮件。
function admin_check($username = NULL, $email = NULL)
现在,如果您想使用username进行检查,只需调用类似admin_check($username)
的函数即可。但如果你想通过电子邮件进行检查,你可以将NULL
作为第一个参数,将电子邮件作为第二个参数。
然后在函数内部,创建一个if
语句来检查第一个参数是否为NULL
。如果是,请使用电子邮件地址进行检查。否则,请使用用户名进行检查。
if ($username === NULL)
{
// use the email address to do the authentication
}
else
{
// use $username instead
}
将两个参数的默认值设置为空: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);