简体   繁体   中英

php / codeigniter redirect user based on group membership

I have this login php file from a CodeIgniter based PHP app. Basically I want to edit this so that if the user is member of "salesman" group then redirect them to another page. This will just be for this group others will redirect as normal and wont be effected.

Presume I need to add in an If at some part.

class Auth extends MX_Controller {

function __construct()
{
    parent::__construct();
    $this->load->library('ion_auth');
    $this->load->library('session');
    $this->load->library('form_validation');
    $this->load->helper('url');
    // Load MongoDB library instead of native db driver if required
    $this->config->item('use_mongodb', 'ion_auth') ?
        $this->load->library('mongo_db') :
        $this->load->database();

}

//redirect if needed, otherwise display the user list
function index()
{
    if (!$this->ion_auth->logged_in())
    {
        redirect('module=auth&view=login');
    } else {
        redirect('module=home');
    }
}

function users() {
    $groups = array('admin', 'purchaser', 'salesman', 'viewer');
    if ($this->ion_auth->in_group($groups))
    {
        $this->session->set_flashdata('message', $this->lang->line("access_denied"));
        $data['message'] = (validation_errors() ? validation_errors() : $this->session->flashdata('message'));
        redirect('module=home', 'refresh');
    }

    $this->user_check();

    $data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
    $data['success_message'] = $this->session->flashdata('success_message');

        //list the users
        $data['users'] = $this->ion_auth->users()->result();
        foreach ($data['users'] as $k => $user)
        {
            $data['users'][$k]->groups = $this->ion_auth->get_users_groups($user->id)->result();
        }

        $meta['page_title'] = 'Users';
        $this->load->view('commons/header', $meta);

        $this->load->view('index', $data);

        $this->load->view('commons/footer');
}
//log the user in
function login()
{
    $data['title'] = "Login";

    //validate form input
    $this->form_validation->set_rules('identity', 'Identity', 'required');
    $this->form_validation->set_rules('password', 'Password', 'required');

    if ($this->form_validation->run() == true)
    { //check to see if the user is logging in
        //check for "remember me"
        $remember = (bool) $this->input->post('remember');

        if ($this->ion_auth->login($this->input->post('identity'), $this->input->post('password'), $remember))
        { //if the login is successful
            //redirect them back to the home page
            $this->session->set_flashdata('success_message', $this->ion_auth->messages());
            redirect('module=home', 'refresh');
        }
        else
        { //if the login was un-successful
            //redirect them back to the login page
            $this->session->set_flashdata('message', $this->ion_auth->errors());
            redirect('module=auth&view=login', 'refresh'); //use redirects instead of loading views for compatibility with MY_Controller libraries
        }
    }
    else
    {  //the user is not logging in so display the login page
        //set the flash data error message if there is one
        $data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
        $data['success_message'] = $this->session->flashdata('success_message');
        $data['identity'] = array('name' => 'identity',
            'id' => 'identity',
            'type' => 'text',
            'value' => $this->form_validation->set_value('identity'),
        );
        $data['password'] = array('name' => 'password',
            'id' => 'password',
            'type' => 'password',
        );

        $this->load->view('auth/login', $data);
    }
}

This condition will catch the logged in user's group and, if they are salesman , redirect them to to/whatever/page .

   if ($this->ion_auth->in_group(array('salesman')))
     redirect('to/whatever/page');
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM