I have a question about user roles or privileges in Codeigniter, I have a page where I can change the role of each user if they are administrator or limited user, what I cannot do is load their profile when logging in if it is administrator with more privileges and if you are a limited user with less, here I have my controllers Controller Login (Function to start session)
// Verificar usuario en base de datos
public function iniciar_sesion(){
$this->form_validation->set_rules('nom_usuario', 'nom_usuario', 'trim|required|xss_clean');
$this->form_validation->set_rules('pass_usuario', 'pass_usuario', 'trim|required|xss_clean|callback_check_database');
$nom_usuario = $this->input->post('nom_usuario');
$pass_usuario = $this->input->post('pass_usuario');
$result = $this->Login_model->logearse($nom_usuario, $pass_usuario);
if($result){
$sess_array = array();
foreach($result as $row){
$sess_array = array(
'nom_usuario' => $row->nom_usuario,
'pass_usuario' => $row->pass_usuario,
'tipo_usuario' => $row->tipo_usuario
);
$this->session->set_userdata('logged_in', $sess_array);
}
}else{
echo "error";
}
}
controller model
function logearse($nom_usuario, $pass_usuario){
$this -> db -> select('nom_usuario, pass_usuario');
$this -> db -> from('usuarios');
$this -> db -> where('nom_usuario', $nom_usuario);
$this -> db -> where('pass_usuario', $pass_usuario);
$this -> db -> limit(1);
$query = $this -> db -> get();
if($query -> num_rows() == 1)
{
return $query->result();
}
else
{
return false;
}
}
The controller of the panel or the page to show if you are logged in
//Cargar la vista Panel
public function index()
{
if($this->session->userdata('logged_in'))
{
$session_data = $this->session->userdata('logged_in');
$this->load->view('header.php');
$this->load->view('panel');
$this->load->view('footer.php');
}
else
{
//Si no ha iniciado sesion redirecionar a login
redirect('login');
}
}
That in the controller of the panel an if could go to redirect to its due profile but I don't know how to insert it, any idea? Thank you
When I work with the sessions of the different types of users I program it in the controller in the following way. The code is commented so that you can be guided, I hope and it will serve you.