php / codeigniter根据组成员资格重定向用户

时间:2014-02-10 13:52:14

标签: php mysql codeigniter

我从基于CodeIgniter的PHP应用程序获得此登录php文件。基本上我想编辑它,以便如果用户是" salesman"的成员。然后,组将它们重定向到另一个页面。这只适用于这个群体,其他人会像往常一样重定向而不会受到影响。

假设我需要在某个部分添加一个If。

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);
    }
}

1 个答案:

答案 0 :(得分:0)

此条件将捕获已登录用户的组,如果他们是salesman,则会将其重定向到to/whatever/page

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