![](/img/trans.png)
[英]Laravel 5 rydurham/sentinal: post-login, redirect based on group membership
[英]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.