[eluser]sandwormusmc[/eluser]
Ok, this is pretty complex, but I think it's a flexible and powerful solution:
Calling the MyAuth Class from the main controller:
Code:
/*
Originally coded by Charlie Dumont for Detroit Public Schools - Summer 2007
Modified and completed for Detroit Public Schools by Matt Moldvan - Fall 2007
*/
$this->params = array(
'AppName' => 'UAMS',
'AuthMethod' => 'LDAPAuth',
'RolesBased' => FALSE, // SET TO TRUE TO CHECK ROLES
'RolesType' => 'LocalRoles'
);
$this->load->library('MyAuth', $this->params);
MyAuth Class (allows for different "AuthTypes" which dictates which custom auth library it will call, we are using LDAP at our site):
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/*
Originally coded by Charlie Dumont for Detroit Public Schools - Summer 2007
Modified and completed for Detroit Public Schools by Matt Moldvan - Fall 2007
*/
class MyAuth {
public $CI;
public $isRoles;
public $authFailure = '';
protected $authType;
protected $params;
protected $callingFunction;
private $roles;
private $userArray;
private $username;
private $password;
public function __construct($params) {
$this->CI=&get;_instance();
$this->params = $params;
// print_r($this->CI=& get_instance());
/*echo '<pre>';
print_r($this->CI);
echo '</pre>';*/
$authMethod = $params["AuthMethod"];
$this->CI->load->library($authMethod);
// this kludginess is because CI insists that libraries be all lowercase when called
$authMethod = strtolower ($authMethod);
$this->authType =& $this->CI->$authMethod;
// if Roles based we load the roles class and set a static variable
if ($params['RolesBased']) {
$this->isRoles = TRUE;
$this->_loadRoles();
}
$this->username=$this->CI->input->post("username");
$this->password=$this->CI->input->post("password");
}
public function check($path='') {
// Find the calling function for roles based validation
$backtrace = debug_backtrace();
$this->callingFunction = $backtrace[1]['function'];
$username = $this->CI->input->post("username");
$params=array('username'=>$username);
$this->_setUserArray($params);
// Check session, cookie, or post variables
if ($this->_isValidSession() || $this->_isValidLogin()) {
$username = $this->CI->session->userdata('username');
if ($this->isRoles) {
// echo "<br />checking roles";
$this->CI->roles->checkRoleAgainstAction($this->callingFunction, $this->CI->session->userdata('role'));
}
$params=array(
'username'=>$username,
'prevAction'=>'login',
'action'=>'default',
'loggedIn'=>TRUE
);
$this->_setUserArray($params);
return TRUE;
}
// echo "this is the calling function: " . $backtrace[1]['function'] . " and this is the URI Path:$path";
// if not available or valid offer login screen
$data = array(
'actionPath' =>$path,
'error' =>$this->authFailure,
'header' =>$this->CI->load->view($this->params['AppName'].'_header','',true),
'footer' =>$this->CI->load->view($this->params['AppName'].'_footer','',true)
);
$this->CI->load->vars($data);
$this->CI->load->view('login.php',$data);
$string = $this->CI->output->get_output();
$this->CI->output->set_output($string);
// $this->CI->load->view($this->params['AppName'] . '_footer');
exit($string);
}
public function logout() {
$newdata = array(
'username' => 0,
'action' => 'default',
'loggedIn' => FALSE
);
$this->_setUserArray($newdata);
$this->CI->load->view('logout');
}
public function get_username() {
return $this->CI->session->userdata('username');
}
public function get_role() {
return $this->CI->session->userdata('role');
}
private function _loggedIn() {
return $this->CI->session->userdata('loggedIn');
}
private function _isValidSession() {
if($this->CI->session->userdata('loggedIn')) {
return TRUE;
}
else {
return FALSE;
}
}
private function _isValidLogin() {
$username = $this->CI->input->post("username");
$password = $this->CI->input->post("password");
if (strlen($username) < 1) {
return FALSE;
}
if ($this->authType->verifyLogin($username, $password) ) {
return TRUE;
}
else {
$this->authFailure='There was a problem with your LDAP credentials.';
return FALSE;
}
}
private function _setUserArray($params) {
if ($this->isRoles) {
$role = $this->CI->roles->getUserRole($username);
$params['role']=$role;
}
$this->CI->session->set_userdata($params);
}
private function _loadRoles() {
$this->CI->load->library("Roles", $this->params);
}
}
?>
Running out of allowed length for this post ...