/lenasys/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/trunk
36.3.1 by Daniel Hermansson
Added login functionality
1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3
	class Login extends CI_Controller {
4
		/*
5
		 *	Constructor
6
		 */
7
		function __construct() {
8
			parent::__construct();
9
			//Load required library
10
			$this->load->model('user', '', TRUE);
11
		}
12
	
13
		
14
		/*
15
		 *	This function runs when the user navigates directly to the login controller
16
		 */
17
		public function index() {
18
			//If user is already logged in
19
			if($this->user->isLoggedIn()) {
20
				//User already logged in
42.1.2 by Albin Larsson
Fixed bad URL formatting
21
				redirect(base_url().'home', 'refresh');
36.3.1 by Daniel Hermansson
Added login functionality
22
			} else {
23
				//Display the login form
24
				$this->drawLoginForm('');
25
			}
26
		}
27
		
28
		
29
		/*
30
		 *	This function validate the user input from login form using the library "form_validation".
31
		 *  NOTICE: This does NOT mean that it validates it against the database.
32
		 */
33
		public function validate() {
34
			//Load required library
35
			$this->load->library('form_validation');
36
			
37
			//Sets validation rules
38
			$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
39
			$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean');
40
			
41
			//Run validation
42
			if($this->form_validation->run() == FALSE) {
43
				//Field validation failed. Display login form (with error message).
44
				$this->drawLoginForm(validation_errors());
45
			} else {
46
				$username = $this->input->post('username');
47
				$password = $this->input->post('password');
48
				
49
				//Try to login
50
				if ($this->user->login($username, $password)) {
42.1.2 by Albin Larsson
Fixed bad URL formatting
51
					redirect(base_url().'home', 'refresh');
64.1.1 by b11johgu
ExamplesController:
52
36.3.1 by Daniel Hermansson
Added login functionality
53
				} else {
54
					$this->drawLoginForm('Access denied!');
55
				}
56
			}	
57
		}
58
		
59
	
60
		/*
61
		 *	This function draws the login form.
62
		 */
63
		private function drawLoginForm($errors) {
64
			//Load required library
65
			$this->load->helper(array('form'));
64.1.1 by b11johgu
ExamplesController:
66
67
			$headTagData = array(
67.1.6 by a11emmjo
Replaced bannermenu/topmenu/header... on login-page.
68
				'cssFiles' => array('bannermenu', 'login_view'),
64.1.1 by b11johgu
ExamplesController:
69
				'jsFiles' => array('header', 'userControls')
70
			);
71
			$this->load->view('headTag', array('headTagData' => $headTagData));	
72
			$userInfo = array(
73
				'userType' => $this->user->getUserType(),
74
				'userName' => $this->user->getUserName()
75
			);
67.1.6 by a11emmjo
Replaced bannermenu/topmenu/header... on login-page.
76
			$this->load->view('bannermenu', $userInfo);
64.1.1 by b11johgu
ExamplesController:
77
			
77 by Gustav Hatvigsson
Fixed the loginview
78
			$this->load->view('login_view');
64.1.1 by b11johgu
ExamplesController:
79
36.3.1 by Daniel Hermansson
Added login functionality
80
		}
81
	}
77 by Gustav Hatvigsson
Fixed the loginview
82
?>