[eluser]Xeoncross[/eluser]
It would be nice if DX Auth supported URI redirects so that if we call deny_access() then after they login we can (optionally) send them back to the page they were trying to access.
First we need to be able to encode the return_to url
Code:
//Alows me to pass complex values though the URL
function base64_url_encode($string=null){
return strtr(base64_encode($string), '+/=', '-_~');
}
//Alows me to recive complex values though the URL
function base64_url_decode($string=null) {
return base64_decode(strtr($string, '-_~','+/='));
}
Then we need to allow the deny_access() function to have a new "return_to" param.
Code:
function deny_access($uri = 'deny', $return_to=null)
{
$this->ci->load->helper('url');
if ($uri == 'login')
{
if($return_to) {
$return_to = base64_url_encode($return_to);
}
redirect($this->ci->config->item('DX_login_uri'). $return_to, 'location');
}
else if ($uri == 'banned')
{
redirect($this->ci->config->item('DX_banned_uri'), 'location');
}
else
{
redirect($this->ci->config->item('DX_deny_uri'), 'location');
}
exit;
}
Now the only other thing to edit is the Auth controller on login() and logout() functions so that they can deal with the redirect to send the user back to wherever they were.
Code:
function login($return_to = NULL)
{
if ( ! $this->dx_auth->is_logged_in())
{
$val = $this->form_validation;
// Set form validation rules
$val->set_rules('username', 'Username', 'trim|required|xss_clean');
$val->set_rules('password', 'Password', 'trim|required|xss_clean');
$val->set_rules('remember', 'Remember me', 'integer');
// Set captcha rules if login attempts exceed max attempts in config
if ($this->dx_auth->is_max_login_attempts_exceeded())
{
$val->set_rules('captcha', 'Confirmation Code', 'trim|required|xss_clean|callback_captcha_check');
}
if ($val->run() AND $this->dx_auth->login($val->set_value('username'), $val->set_value('password'), $val->set_value('remember')))
{
//-------------------Added for redirect ---------------------//
//If they were trying to access another page - then send them back now!
if($return_to) {
$return_to = base64_url_decode($return_to);
}
//-------------------Added for redirect ---------------------//
// Redirect (default to homepage)
redirect($return_to, 'location');
}
else
{
// Check if the user is failed logged in because user is banned user or not
if ($this->dx_auth->is_banned())
{
// Redirect to banned uri
$this->dx_auth->deny_access('banned');
}
else
{
// Default is we don't show captcha until max login attempts eceeded
$data['show_captcha'] = FALSE;
// Show captcha if login attempts exceed max attempts in config
if ($this->dx_auth->is_max_login_attempts_exceeded())
{
// Create catpcha
$this->dx_auth->captcha();
// Set view data to show captcha on view file
$data['show_captcha'] = TRUE;
}
// Load login page view
$this->load->view($this->dx_auth->login_view, $data);
}
}
}
else
{
//-------------------Added for redirect ---------------------//
//If they were trying to access another page - then send them back now!
if($return_to) {
redirect(base64_url_decode($return_to));
}
//-------------------Added for redirect ---------------------//
$data['auth_message'] = 'You are already logged in.';
$this->load->view($this->dx_auth->logged_in_view, $data);
}
}
function logout($return_to = NULL)
{
$this->dx_auth->logout();
//-------------------Added for redirect ---------------------//
//If they were trying to access another page - then send them back now!
if($return_to) {
redirect(base64_url_decode($return_to));
}
//-------------------Added for redirect ---------------------//
$data['auth_message'] = 'You have been logged out.';
$this->load->view($this->dx_auth->logout_view, $data);
}
Being able to send people back to a page after they login saves the user from having to go re-find that page each time they login. Definitely something that should be included in this awesome system.
Now with just a couple small lines added to DX Auth we can return users to wherever they where before they tried to login.
Code:
//Must be logged in!
if ( ! $this->dx_auth->is_logged_in()) {
// Redirect to login page (return to the upload/index page)
$this->dx_auth->deny_access('login', 'upload/index/');
}