aboutsummaryrefslogtreecommitdiffstats
path: root/htdocs/application/controllers/Auth.php
blob: 017465ce252115fc456c8671414dfaebc8d0e4bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?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.

    Auth_Ldap is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Auth_Ldap is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Auth_Ldap.  If not, see <http://www.gnu.org/licenses/>.
 * 
*/
/**
 * @author      Greg Wojtak <gwojtak@techrockdo.com>
 * @copyright   Copyright © 2010,2011 by Greg Wojtak <gwojtak@techrockdo.com>
 * @package     Auth_Ldap
 * @subpackage  auth demo
 * @license     GNU Lesser General Public License
 */

class Auth extends CI_Controller
{
	
	function __construct() 
	{
		parent::__construct();
		$this->load->helper('form');
		$this->load->library('Form_validation');
		$this->load->library('auth_ldap');
		$this->load->helper('url');
		$this->load->library('table');
	}
	
	function index() 
	{
		$this->session->keep_flashdata('tried_to');
		$this->login();
	}
	
	function login($errorMsg = NULL) 
	{
		$this->session->keep_flashdata('tried_to');
		
		if (!$this->auth_ldap->is_authenticated()) 
		{

			// Set up rules for form validation
			$rules = $this->form_validation;
			$rules->set_rules('username', 'Username', 'required|callback_alpha_dash_dot');
			$rules->set_rules('password', 'Password', 'required');

			// Do the login...
			
			if ($rules->run() && $this->auth_ldap->login($rules->set_value('username') , $rules->set_value('password'))) 
			{

				// Login WIN!
				
				if ($this->session->flashdata('tried_to')) 
				{
					redirect($this->session->flashdata('tried_to'));
				}
				else
				{
					redirect('/');
				}
			}
			else
			{

				// Login FAIL
				$this->session->set_flashdata('login_error', 'Incorrect username or password.');
				$this->load->view('auth/login_form');
			}
		}
		else
		{

			// Already logged in...
			redirect('/');
		}
	}
	
	function logout() 
	{
		
		if ($this->session->userdata('logged_in')) 
		{
			$data['name'] = $this->session->userdata('cn');
			$data['username'] = $this->session->userdata('username');
			$data['logged_in'] = TRUE;
			$this->auth_ldap->logout();
		}
		else
		{
			$data['logged_in'] = FALSE;
		}
		redirect('/');
	}
	public 
	function alpha_dash_dot($str) 
	{
		return (!preg_match("/^([-a-z0-9_\-\.])+$/i", $str)) ? FALSE : TRUE;
	}
}
?>

© 2014-2024 Faster IT GmbH | imprint | privacy policy