How to create validation of users with access levels in codeigniter?

1

I have this model and control that verifies and validates registered users in the tb_users table of the database. I need a script that checks whether the user is 0 or 1 in the nv_level column. If the user is equal to 0 it directs to redirect('usuario/home', 'refresh'); if it is equal to 1 directs to redirect('admin/home', 'refresh');

Below is the code Model and Controller .

- > Model

   class Login_model extends CI_Model {

    function ValidaLogin() {
        $this->db->where('hl_email', $this->input->post('email'));
        $this->db->where('pw_password', md5($this->input->post('senha')));
        $query = $this->db->get('tb_user');
        if ($query->num_rows == 1) {
            return true;
        }
    }
}

- > controller

public function valida() {

        $this->load->model('login_model'); //Carrega o model
        $query = $this->login_model->ValidaLogin(); //Chama a função da Model que checa o usuário no BD

        if ($query) { //Se o Usuário e senha existir no mesmo registro...
            $data = array(
                'login' => $this->input->post('email'),
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
            redirect('usuario/home', 'refresh');
        } else { // incorreto username ou password
            $info['msg'] = "Informações incorretas";
            $this->load->view('header_html');
            $this->load->view('header_view');
            $this->load->view('login/login_view', $info);
            $this->load->view('footer_view');
            $this->load->view('footer_html');
        }
    }
    
asked by anonymous 30.01.2014 / 13:05

2 answers

1

The first option you have is to create this "system" of privilege in the arm. How?

In your user table or related third party, create a column called role or function and assign a value to that column.

Then, breaking with if , we can distinguish the role of the user and determine something for him. For example:

if ($user->role == 1) {
    // é administrador
} else {
   // não é administrador
}

Or, if you want, you can use a ready library, such as Ion Auth .

Unfortunately, CI does not have a native role provider .

    
30.01.2014 / 13:10
0

What you need is an ACL - ACESS CONTROL LIST , there is a very good ready called Ion Auth .

Visit Ion Auth Docs , and view its documentation, download and configure it in your project.

    
18.02.2014 / 23:36