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 |
||
10 |
//Load required library
|
|
11 |
$this->load->model('user', '', TRUE); |
|
12 |
}
|
|
13 |
||
14 |
||
15 |
/*
|
|
16 |
* This function runs when the user navigates directly to the login controller
|
|
17 |
*/
|
|
18 |
public function index() { |
|
19 |
//If user is already logged in
|
|
20 |
if($this->user->isLoggedIn()) { |
|
21 |
//User already logged in
|
|
|
42.1.2
by Albin Larsson
Fixed bad URL formatting |
22 |
redirect(base_url().'home', 'refresh'); |
|
36.3.1
by Daniel Hermansson
Added login functionality |
23 |
} else { |
24 |
//Display the login form
|
|
25 |
$this->drawLoginForm(''); |
|
26 |
}
|
|
27 |
}
|
|
28 |
||
29 |
||
30 |
/*
|
|
31 |
* This function validate the user input from login form using the library "form_validation".
|
|
32 |
* NOTICE: This does NOT mean that it validates it against the database.
|
|
33 |
*/
|
|
34 |
public function validate() { |
|
35 |
//Load required library
|
|
36 |
$this->load->library('form_validation'); |
|
37 |
||
38 |
//Sets validation rules
|
|
39 |
$this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean'); |
|
40 |
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean'); |
|
41 |
||
42 |
//Run validation
|
|
43 |
if($this->form_validation->run() == FALSE) { |
|
44 |
//Field validation failed. Display login form (with error message).
|
|
45 |
$this->drawLoginForm(validation_errors()); |
|
46 |
} else { |
|
47 |
$username = $this->input->post('username'); |
|
48 |
$password = $this->input->post('password'); |
|
49 |
||
50 |
//Try to login
|
|
51 |
if ($this->user->login($username, $password)) { |
|
|
42.1.2
by Albin Larsson
Fixed bad URL formatting |
52 |
redirect(base_url().'home', 'refresh'); |
|
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')); |
|
66 |
||
67 |
//Display the view
|
|
68 |
$data['error'] = $errors; |
|
|
52.1.1
by b11johgu
Added controllers for examplepage, templatelayout. |
69 |
$headData['list'] = array('headTag', 'header', 'login_view'); |
70 |
$this->load->view('headTag', $headData); |
|
71 |
$this->load->model('user'); |
|
72 |
$userInfo = array( |
|
73 |
'userType' => $this->user->getUserType(), |
|
74 |
'userName' => $this->user->getUserName() |
|
75 |
);
|
|
76 |
||
77 |
$this->load->view('header', $userInfo); |
|
|
36.3.1
by Daniel Hermansson
Added login functionality |
78 |
$this->load->view('login_view', $data); |
79 |
}
|
|
80 |
}
|
|
81 |
?>
|