alpha dash dot validation for LDAP

This commit is contained in:
Claude 2013-11-07 21:29:29 +01:00
parent 08b54f7ad9
commit 5678e09c44

View File

@ -1,4 +1,18 @@
<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); <?php
/**
* Class and Function List:
* Function list:
* - __construct()
* - index()
* - login()
* - logout()
* - alpha_dash_dot()
* Classes list:
* - Auth extends CI_Controller
*/
if (!defined('BASEPATH')) exit('No direct script access allowed');
/* /*
* This file is part of Auth_Ldap. * This file is part of Auth_Ldap.
@ -16,7 +30,6 @@
along with Auth_Ldap. If not, see <http://www.gnu.org/licenses/>. along with Auth_Ldap. If not, see <http://www.gnu.org/licenses/>.
* *
*/ */
/** /**
* @author Greg Wojtak <gwojtak@techrockdo.com> * @author Greg Wojtak <gwojtak@techrockdo.com>
* @copyright Copyright © 2010,2011 by Greg Wojtak <gwojtak@techrockdo.com> * @copyright Copyright © 2010,2011 by Greg Wojtak <gwojtak@techrockdo.com>
@ -24,10 +37,13 @@
* @subpackage auth demo * @subpackage auth demo
* @license GNU Lesser General Public License * @license GNU Lesser General Public License
*/ */
class Auth extends CI_Controller {
function __construct() {
parent::__construct();
class Auth extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('form'); $this->load->helper('form');
$this->load->library('Form_validation'); $this->load->library('Form_validation');
$this->load->library('auth_ldap'); $this->load->library('auth_ldap');
@ -35,51 +51,76 @@ class Auth extends CI_Controller {
$this->load->library('table'); $this->load->library('table');
} }
function index() { function index()
{
$this->db_session->keep_flashdata('tried_to'); $this->db_session->keep_flashdata('tried_to');
$this->login(); $this->login();
} }
function login($errorMsg = NULL){ function login($errorMsg = NULL)
{
$this->db_session->keep_flashdata('tried_to'); $this->db_session->keep_flashdata('tried_to');
if(!$this->auth_ldap->is_authenticated()) {
if (!$this->auth_ldap->is_authenticated())
{
// Set up rules for form validation // Set up rules for form validation
$rules = $this->form_validation; $rules = $this->form_validation;
$rules->set_rules('username', 'Username', 'required|alpha_dash'); $rules->set_rules('username', 'Username', 'required|callback_alpha_dash_dot');
$rules->set_rules('password', 'Password', 'required'); $rules->set_rules('password', 'Password', 'required');
// Do the login... // Do the login...
if($rules->run() && $this->auth_ldap->login(
$rules->set_value('username'), if ($rules->run() && $this->auth_ldap->login($rules->set_value('username') , $rules->set_value('password')))
$rules->set_value('password'))) { {
// Login WIN! // Login WIN!
if($this->db_session->flashdata('tried_to')) {
if ($this->db_session->flashdata('tried_to'))
{
redirect($this->db_session->flashdata('tried_to')); redirect($this->db_session->flashdata('tried_to'));
}else { }
else
{
redirect('/'); redirect('/');
} }
}else { }
else
{
// Login FAIL // Login FAIL
$this->db_session->set_flashdata('login_error', 'Incorrect username or password.'); $this->db_session->set_flashdata('login_error', 'Incorrect username or password.');
$this->load->view('auth/login_form'); $this->load->view('auth/login_form');
} }
}else { }
else
{
// Already logged in... // Already logged in...
redirect('/'); redirect('/');
} }
} }
function logout() { function logout()
if($this->db_session->userdata('logged_in')) { {
if ($this->db_session->userdata('logged_in'))
{
$data['name'] = $this->db_session->userdata('cn'); $data['name'] = $this->db_session->userdata('cn');
$data['username'] = $this->db_session->userdata('username'); $data['username'] = $this->db_session->userdata('username');
$data['logged_in'] = TRUE; $data['logged_in'] = TRUE;
$this->auth_ldap->logout(); $this->auth_ldap->logout();
} else { }
else
{
$data['logged_in'] = FALSE; $data['logged_in'] = FALSE;
} }
redirect('/'); redirect('/');
} }
public
function alpha_dash_dot($str)
{
return (!preg_match("/^([-a-z0-9_-\.])+$/i", $str)) ? FALSE : TRUE;
}
} }
?> ?>