I am writing an admin check to make sure that the email that is logging into the admin area is:
- An admin user
- Is active
I have so far written the following code:
Controller:
$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;
}
What I am confused about here is that I would like it to accept both the admin username or email address if the userGroup == admin or if the account is userActive == yes but I am unsure how I would construct the model to or what data to send through to the model.
Update -> Joe:
Joe,
Just a couple of questions:
- Do I still set my validation error messages where it
return false? - Is there anyway that I could check that the account is an admin account and is active?
- Is the Model OK?
Controller:
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;
}
Model:
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;
}
}
Set the defaults for both parameters to null:
function _admin_check($adminUsername = null, $adminEmail = null)Then at the top of the method in the model, add this:
To call with the username, call like this:
And for the email
To call it in one: