bzr branch
http://gegoxaren.bato24.eu/bzr/lenasys/trunk
|
36.3.1
by Daniel Hermansson
Added login functionality |
1 |
<?php
|
2 |
Class User extends CI_Model { |
|
3 |
/*
|
|
4 |
* Constructor
|
|
5 |
*/
|
|
6 |
function __construct() { |
|
7 |
parent::__construct(); |
|
8 |
||
9 |
//Load required libraries
|
|
10 |
$this->load->database(); |
|
11 |
$this->load->library('session'); |
|
12 |
}
|
|
13 |
||
14 |
/*
|
|
|
52.1.1
by b11johgu
Added controllers for examplepage, templatelayout. |
15 |
* This function logs the user in (returns FALSE on fail).
|
|
36.3.1
by Daniel Hermansson
Added login functionality |
16 |
*/
|
17 |
public function login($username, $password) |
|
18 |
{
|
|
19 |
//Generate a salted hash
|
|
20 |
$hash = $this->getSaltedHash($password); |
|
21 |
||
22 |
//Query-structure
|
|
23 |
$this->db->select('userName, name, passwd, userType, ssn'); |
|
24 |
$this->db->from('Users'); |
|
25 |
$this->db->where('userName', $username); |
|
26 |
$this->db->where('passwd', $hash); |
|
27 |
$this->db->limit(1); |
|
28 |
||
29 |
//Execute query
|
|
30 |
$query = $this->db->get(); |
|
31 |
$result = $query->result(); |
|
32 |
||
33 |
//If a matching DB record is found.
|
|
34 |
if($result) { |
|
35 |
//Prepare session data
|
|
36 |
$userDetails = array(); |
|
37 |
foreach($result as $row) { |
|
38 |
$userDetails = array( |
|
39 |
'username' => $row->userName, |
|
40 |
'name' => $row->name, |
|
41 |
'usertype' => $row->userType, |
|
42 |
'ssn' => $row->ssn |
|
43 |
);
|
|
44 |
}
|
|
45 |
||
46 |
//Set session data
|
|
47 |
$this->session->set_userdata('authenticated', $userDetails); |
|
48 |
||
49 |
//Return success
|
|
50 |
return TRUE; |
|
|
52.1.1
by b11johgu
Added controllers for examplepage, templatelayout. |
51 |
}
|
52 |
||
53 |
//Return fail
|
|
54 |
return FALSE; |
|
|
36.3.1
by Daniel Hermansson
Added login functionality |
55 |
}
|
56 |
||
57 |
||
58 |
/*
|
|
59 |
* This function logs the user out.
|
|
60 |
*/
|
|
61 |
public function logout() { |
|
62 |
//Unset session data
|
|
63 |
$this->session->unset_userdata('authenticated'); |
|
64 |
}
|
|
65 |
||
66 |
||
67 |
/*
|
|
68 |
* This function return TRUE if the user is logged in and FALSE otherwise.
|
|
69 |
*/
|
|
70 |
public function isLoggedIn() { |
|
71 |
if ($this->session->userdata('authenticated')) { |
|
72 |
return TRUE; |
|
|
52.1.1
by b11johgu
Added controllers for examplepage, templatelayout. |
73 |
}
|
74 |
||
75 |
return FALSE; |
|
76 |
}
|
|
77 |
||
78 |
||
79 |
/*
|
|
80 |
* This function returns the users type (or FALSE if user isn't logged in).
|
|
81 |
*/
|
|
82 |
public function getUserType() { |
|
83 |
if($this->isLoggedIn()) { |
|
84 |
$temp = $this->session->userdata('authenticated'); |
|
85 |
return $temp['usertype']; |
|
86 |
}
|
|
87 |
||
88 |
return FALSE; |
|
89 |
}
|
|
90 |
||
91 |
||
92 |
/*
|
|
93 |
* This function returns the username (or FALSE if user isn't logged in).
|
|
94 |
*/
|
|
95 |
public function getUserName() { |
|
96 |
if($this->isLoggedIn()) { |
|
97 |
$temp = $this->session->userdata('authenticated'); |
|
98 |
return $temp['username']; |
|
99 |
}
|
|
100 |
||
101 |
return FALSE; |
|
102 |
}
|
|
103 |
||
104 |
||
105 |
/*
|
|
106 |
* This function returns the users password hint (or FALSE if user isn't logged in).
|
|
107 |
*/
|
|
108 |
public function getPasswordHint($user) { |
|
109 |
$hint = ''; |
|
110 |
||
111 |
//Query-structure
|
|
112 |
$this->db->select('passwdHint'); |
|
113 |
$this->db->from('Users'); |
|
114 |
$this->db->where('userName', $user); |
|
115 |
$this->db->limit(1); |
|
116 |
||
117 |
//Execute query
|
|
118 |
$query = $this->db->get(); |
|
119 |
$result = $query->result(); |
|
120 |
||
121 |
//If a matching DB record is found.
|
|
122 |
if($result) { |
|
123 |
foreach($result as $row) { |
|
124 |
$hint = $row->passwdHint; |
|
125 |
return $hint; |
|
126 |
}
|
|
127 |
}
|
|
128 |
||
129 |
return FALSE; |
|
130 |
}
|
|
131 |
||
132 |
||
133 |
/*
|
|
134 |
* This function registers user into the database.
|
|
135 |
*/
|
|
136 |
public function addUser($userName, $name, $ssn, $password, $userType, $pwdHint) { |
|
137 |
//Check that a user is logged in and has the right privileges (is teacher)
|
|
138 |
if($this->isLoggedIn() && $this->getUserType() === 'Teacher') { |
|
139 |
//Generate a salted hash
|
|
140 |
$hash = $this->getSaltedHash($password); |
|
141 |
||
142 |
//Query-structure (All values are escaped automatically by codeigninte, producing safer queries.)
|
|
143 |
$this->db->set('userName', $userName); |
|
144 |
$this->db->set('name', $name); |
|
145 |
$this->db->set('ssn', $ssn); |
|
146 |
$this->db->set('passwd', $password); |
|
147 |
$this->db->set('userType', $userType); |
|
148 |
$this->db->set('passwdHint', $pwdHint); |
|
149 |
$result = $this->db->insert('Users'); |
|
150 |
||
151 |
//Check for my-sql error
|
|
152 |
if($result) { |
|
153 |
//Return success
|
|
154 |
return TRUE; |
|
155 |
}
|
|
156 |
}
|
|
157 |
||
158 |
//Return error
|
|
159 |
return FALSE; |
|
160 |
}
|
|
161 |
||
162 |
||
163 |
/*
|
|
164 |
* This function changes the users password.
|
|
165 |
*/
|
|
166 |
public function changePassword($pwdOld, $pwdNew, $pwdHint) { |
|
167 |
//Check that a user is logged in.
|
|
168 |
if($this->isLoggedIn()) { |
|
169 |
$user = $this->getUserName(); |
|
170 |
$oldHash = $this->getSaltedHash($pwdOld); |
|
171 |
$newHash = $this->getSaltedHash($pwdNew); |
|
172 |
||
173 |
//Validate
|
|
174 |
$this->db->select('userName'); |
|
175 |
$this->db->from('Users'); |
|
176 |
$this->db->where('userName', $user); |
|
177 |
$this->db->where('passwd', $oldHash); |
|
178 |
$this->db->limit(1); |
|
179 |
$query = $this->db->get(); |
|
180 |
$result = $query->result(); |
|
181 |
||
182 |
//If a matching DB record is found, update database with new info.
|
|
183 |
if($result) { |
|
184 |
$data = array( |
|
185 |
'passwd' => $newHash, |
|
186 |
'passwdHint' => $pwdHint |
|
187 |
);
|
|
188 |
||
189 |
$this->db->where('userName', $user); |
|
190 |
$this->db->update('Users', $data); |
|
191 |
||
192 |
//Return Success!
|
|
193 |
return TRUE; |
|
194 |
}
|
|
195 |
}
|
|
196 |
||
197 |
//Return error
|
|
198 |
return FALSE; |
|
199 |
}
|
|
200 |
||
201 |
||
202 |
/*
|
|
203 |
* This function removes users from the database.
|
|
204 |
*/
|
|
205 |
public function removeUser($userName) { |
|
206 |
//Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
|
|
207 |
if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) { |
|
208 |
//Query-structure
|
|
209 |
$this->db->where('userName', $userName); |
|
210 |
$result = $this->db->delete('Users'); |
|
211 |
||
212 |
//Check for my-sql error
|
|
213 |
if($result) { |
|
214 |
//Return success
|
|
215 |
return TRUE; |
|
216 |
}
|
|
217 |
}
|
|
218 |
||
219 |
//Return error
|
|
220 |
return FALSE; |
|
221 |
}
|
|
222 |
||
223 |
||
224 |
/*
|
|
225 |
* Generates a password hash using a user-unique salt.
|
|
226 |
*/
|
|
|
36.3.1
by Daniel Hermansson
Added login functionality |
227 |
private function getSaltedHash($pwd) |
228 |
{
|
|
229 |
/* Salt algorithm:
|
|
230 |
* Step 1: Get encryption key
|
|
231 |
* Step 2: Salt is set to password (reversed)
|
|
232 |
* Step 3: Salt is encrypted with MD5
|
|
233 |
* Step 4: Salt is reversed (again)
|
|
234 |
* Step 5: Salt is encrypted with SHA1
|
|
235 |
*/
|
|
236 |
||
237 |
$salt = ''; |
|
238 |
||
239 |
//UNCOMMENT TO ACTIVATE SALT LATER IN DEVELOPMENT!!!
|
|
240 |
//$salt = sha1(strrev(md5(strrev($this->config->item('encryption_key')))));
|
|
241 |
||
242 |
//Generate SHA1 hash using salt
|
|
243 |
$hash = sha1($salt.$pwd); |
|
244 |
||
245 |
return $hash; |
|
246 |
}
|
|
247 |
}
|
|
248 |
?>
|