繁体   English   中英

php / codeigniter基于组成员身份重定向用户

[英]php / codeigniter redirect user based on group membership

我有一个来自基于CodeIgniter的PHP应用程序的登录php文件。 基本上,我想对此进行编辑,以便如果用户是“推销员”组的成员,则将他们重定向到另一个页面。 这仅适用于该组,其他人将照常重定向,不会受到影响。

假设我需要在某个部分添加一个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);
    }
}

这种情况将捕获已登录用户的组,如果他们是salesman ,则将其重定向to/whatever/page

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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