[eluser]Unknown[/eluser]
I wanted to create a hook to ensure the login/rights management part of the site, so i can call it before any page that needs it and display the login information without a redirect to keep the user on the same page.
1. I`m using CI since yesterday and i would like to know your opinion about it. I know i don`t have any guarantees about future-compatibility but for now it is self-contained and i can use it on any page without modifying the actual page, which is great.
* the part between /** test **/ and /** /test **/ is for testing the login with the addresses: site/welcome/index/login and site/welcome/index/logout
* i have done just the basic stuff as a proof-of-concept
2. If i want to create an object CurrentUser and access it globally, from whatever module/view it might need it, where should i put it? I am thinking about using a singleton, but if there is a simpler or cleaner way i would like to us it...
Thank you for your time...
in config/hooks.php
Code:
<?php
$hook['pre_controller'][] = array(
'class' =>'Auth_hook',
'function' => 'check',
'filename' => 'auth_hook.php',
'filepath' => 'hooks',
'params' => array('','index')
);
?>
in hooks/auth_hook.php
Code:
<?php
class Auth_hook extends Controller {
var $is_guest = 0;
var $is_logged_in = 1;
var $userstate=-1;
function auth_hook ()
{
parent::Controller();
$this->load->library('session');
/* if you auth from db, you'll need this
$this->load->database();
*/
$ustatus=$this->session->userdata('status');
if(strlen($ustatus)==0)
{
$ustatus=$this->is_guest;
$this->session->set_userdata('status',$this->is_guest);
$this->userstate = $this->is_guest;
} elseif ( $ustatus == $this->is_guest) {
$this->userstate=$this->is_guest;
} elseif ( $ustatus == $this->is_logged_in) {
$this->userstate=$this->is_logged_in;
}
unset($ustatus);
/** test **/
if($this->uri->segment(3)=='login')
{
$this->userstate=$this->is_logged_in;
$this->session->set_userdata('status',$this->is_logged_in);
}
if($this->uri->segment(3)=='logout')
{
$this->userstate=$this->is_guest;
$this->session->set_userdata('status',$this->is_guest);
}
/** /test **/
if($this->userstate==$this->is_guest && $this->uri->router->class != 'login' )
{
/** not needed to work, but kept for future reference.
$this->uri->router->uri_string = '/login';
$this->uri->router->segments[1] = '/login';
$this->uri->router->rsegments[1]= 'login';
$this->uri->router->class ='login';
$this->uri->router->method ='index';
**/
$GLOBALS['class']='login';
/** the login controller needs manual loading */
include_once($GLOBALS['system_folder'].'/'.$GLOBALS['application_folder'].'/controllers/login.php');
}
}
/**
* Check authentification for a controller and function
*
* @param array $arr
* @param strng $arr[0] - controller name
* @param strng $arr[1] - function name
*/
function check($arr)
{
if(!isset($arr)) { $arr=array('',''); }
if(!is_array($arr)) { $arr=array($arr); }
if(!isset($arr[0])) { $arr[0]=''; }
if(!isset($arr[0])) { $arr[1]=''; }
echo 'Auth m:[',$arr[0],'] w/ a:[',$arr[1],']<hr>';
}
}
?>
controllers/welcome.php
Code:
<?php
$hook['pre_controller'][] = array(
'class' =>'Auth_hook',
'function' => 'check',
'filename' => 'auth_hook.php',
'filepath' => 'hooks',
'params' => array('','index')
);
?>