/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
2
	Class User extends CI_Model {
3
		/*
4
		 *	Constructor
5
		 */
6
		function __construct() {
7
			parent::__construct();
8
			
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
9
			//Load required libraries and drivers
36.3.1 by Daniel Hermansson
Added login functionality
10
			$this->load->database();
11
			$this->load->library('session');
12
		}
13
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
14
15
		/*
16
		 *	This function returns the users password hint (or FALSE if user isn't logged in).
17
		 *  RESTRICTED-LEVEL: None
18
		 */ 
19
		public function getPasswordHint($user) {
20
			//Query-structure
21
			$this->db->select('passwdHint');
22
			$this->db->from('Users');
23
			$this->db->where('userName', $user);
24
			$this->db->limit(1);
25
			
26
			//Execute query
27
			$query = $this->db->get();
28
			$result = $query->result();
29
			
30
			//If a matching DB record is found.
31
			if($result) {
64.1.1 by b11johgu
ExamplesController:
32
				foreach($result as $row) {
33
					$hint = $row->passwdHint;
34
					
35
					//Return hint
36
					return $hint;
37
				}
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
38
			}
39
			
40
			//No such user
41
			return FALSE;
42
		}
43
44
36.3.1 by Daniel Hermansson
Added login functionality
45
		/*
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
46
		 *	This function logs the user in (returns FALSE on fail).
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
47
		 *  RESTRICTED-LEVEL: None
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
48
		 */
49
		public function login($username, $password) {			
36.3.1 by Daniel Hermansson
Added login functionality
50
			//Generate a salted hash
51
			$hash = $this->getSaltedHash($password);
52
	
53
			//Query-structure
64.1.1 by b11johgu
ExamplesController:
54
			$this->db->select('userName, name, passwd, userType, ssn'); // Tog bort firstLogin här.
36.3.1 by Daniel Hermansson
Added login functionality
55
			$this->db->from('Users');
56
			$this->db->where('userName', $username);
57
			$this->db->where('passwd', $hash);
58
			$this->db->limit(1);
59
			
60
			//Execute query
61
			$query = $this->db->get();
62
			$result = $query->result();
63
			
64
			//If a matching DB record is found.
65
			if($result) {
66
				//Prepare session data
67
				$userDetails = array();
68
				foreach($result as $row) {
69
					$userDetails = array(
70
						'username' => $row->userName,
71
						'name' => $row->name,
72
						'usertype' => $row->userType,
64.1.1 by b11johgu
ExamplesController:
73
						'ssn' => $row->ssn,
74
					// 	'firstLogin' => $row->firstLogin
36.3.1 by Daniel Hermansson
Added login functionality
75
					);
76
				}
77
				
78
				//Set session data
79
				$this->session->set_userdata('authenticated', $userDetails);
80
				
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
81
				//Log attempt as valid
82
				$this->logLogin($username, 1);
83
				
36.3.1 by Daniel Hermansson
Added login functionality
84
				//Return success
85
				return TRUE;
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
86
			}
87
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
88
			//Log attempt as invalid
89
			$this->logLogin($username, 0);
90
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
91
			//Return fail
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
92
			return FALSE;
36.3.1 by Daniel Hermansson
Added login functionality
93
		}
94
95
96
		/*
97
		 *	This function logs the user out.
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
98
		 *  RESTRICTED-LEVEL: Self
64.1.1 by b11johgu
ExamplesController:
99
		 */
100
		public function logout() {
36.3.1 by Daniel Hermansson
Added login functionality
101
			//Unset session data
102
			$this->session->unset_userdata('authenticated');
103
		}
64.1.1 by b11johgu
ExamplesController:
104
36.3.1 by Daniel Hermansson
Added login functionality
105
106
		/*
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
107
		 *	This function changes the users password.
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
108
		 *  RESTRICTED-LEVEL: Self
64.1.1 by b11johgu
ExamplesController:
109
		 */
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
110
		public function changePassword($pwdOld, $pwdNew, $pwdHint) {
111
			//Check that a user is logged in.
112
			if($this->isLoggedIn()) {
113
				$user = $this->getUserName();
114
				$oldHash = $this->getSaltedHash($pwdOld);
115
				$newHash = $this->getSaltedHash($pwdNew);
116
				
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
117
				//Validate input with database
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
118
				$this->db->select('userName');
119
				$this->db->from('Users');
120
				$this->db->where('userName', $user);
121
				$this->db->where('passwd', $oldHash);
122
				$this->db->limit(1);
123
				$query = $this->db->get();
124
				$result = $query->result();
125
				
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
126
				//If a matching DB record is found, update database.
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
127
				if($result) {
128
					$data = array(
129
						'passwd' => $newHash,
130
						'passwdHint' => $pwdHint
131
					);
132
					
133
					$this->db->where('userName', $user);
134
					$this->db->update('Users', $data);
135
					
136
					//Return Success!
64.1.1 by b11johgu
ExamplesController:
137
					return TRUE;
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
138
				}
139
			}
140
			
141
			//Return error
142
			return FALSE;
143
		}
144
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
145
146
		/*
147
		 *	This function registers user into the database.
148
		 *  RESTRICTED-LEVEL: Teacher
149
		 */
150
		public function addUser($userName, $name, $ssn, $password, $userType, $pwdHint) {
151
			//Check that a user is logged in and has the right privileges (is teacher)
152
			if($this->isLoggedIn() && $this->getUserType() === 'Teacher') {
153
				//Generate a salted hash
154
				$hash = $this->getSaltedHash($password);
155
				
156
				//Query-structure (All values are escaped automatically by codeigninte, producing safer queries.)
157
				$this->db->set('userName', $userName);
158
				$this->db->set('name', $name);
159
				$this->db->set('ssn', $ssn);
160
				$this->db->set('passwd', $password);
161
				$this->db->set('userType', $userType);
162
				$this->db->set('passwdHint', $pwdHint);
163
				$result = $this->db->insert('Users');
164
				
165
				//Check for my-sql error
166
				if($result) {
167
					//Return success
168
					return TRUE;
64.1.1 by b11johgu
ExamplesController:
169
				}
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
170
			}
171
			
172
			//Return error
173
			return FALSE;
174
		}
64.1.1 by b11johgu
ExamplesController:
175
176
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
177
		/*
178
		 *	This function removes users from the database.
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
179
		 *  RESTRICTED-LEVEL: Teacher
64.1.1 by b11johgu
ExamplesController:
180
		 */
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
181
		public function removeUser($userName) {
182
			//Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
183
			if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
184
				//Query-structure
185
				$this->db->where('userName', $userName);
64.1.1 by b11johgu
ExamplesController:
186
				$result = $this->db->delete('Users');
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
187
				
188
				//Check for my-sql error
189
				if($result) {
190
					//Return success
191
					return TRUE;
64.1.1 by b11johgu
ExamplesController:
192
				}
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
193
			}
194
			
195
			//Return error
196
			return FALSE;
197
		}
64.1.1 by b11johgu
ExamplesController:
198
199
62.1.2 by Daniel Hermansson
Added functionality for resetting a users password
200
		/*
201
		 *	This reset the password for the user.
202
		 *  RESTRICTED-LEVEL: Teacher
203
		 */
204
		public function resetUser($userName) {
205
			//Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
206
			if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
207
				//Check user type
208
				$this->db->select('userName, userType, ssn, email');
209
				$this->db->from('Users');
210
				$this->db->where('userName', $username);
211
				$this->db->limit(1);
212
				$query = $this->db->get();
213
				$result = $query->result();
214
				
215
				//If a matching DB record is found.
216
				if($result) {
217
					//Prepare new hash depending on user-type
218
					$newPwdHash = '';
219
					
220
					if ($row->userType == 'Student') {
221
						$newPwdHash = $this->getSaltedHash($row->ssn);
222
					}
223
					else if ($row->userType == 'Teacher') {
224
						//$newPwdHash = $this->getSaltedHash($row->email);
225
						$newPwdHash = $this->getSaltedHash($row->email);
226
					}
227
					
228
					//Execute reset
229
					$data = array(
230
						'passwd' => $newPwdHash,
64.1.1 by b11johgu
ExamplesController:
231
						'passwdHint' => 'default',
232
						'firstLogin' => 1
62.1.2 by Daniel Hermansson
Added functionality for resetting a users password
233
					);
234
					
235
					$this->db->where('userName', $userName);
236
					$this->db->update('Users', $data);
237
					
238
					//Return Success!
239
					return TRUE;
240
				}
241
			}
242
			
243
			//Return error
244
			return FALSE;
245
		}
64.1.1 by b11johgu
ExamplesController:
246
247
62.1.3 by Daniel Hermansson
Added functionality for parsing a user list from ladok into a user array (for future use)
248
		/*
68.1.1 by Daniel Hermansson
Modified and corrected parser functionality.
249
		 *	This parses a user list from ladok and returns an array with users.
62.1.3 by Daniel Hermansson
Added functionality for parsing a user list from ladok into a user array (for future use)
250
		 *  RESTRICTED-LEVEL: Teacher
251
		 */
68.1.1 by Daniel Hermansson
Modified and corrected parser functionality.
252
		public function parseLadok($string) {
253
			//Check that a user is logged in and has the right privileges (is teacher).
254
			if($this->isLoggedIn() && $this->getUserType() === 'Teacher') {
255
				$userArray = array();
256
				
257
				//Populate array with users from ladok
258
				$ladokUsers = preg_split( '/\r\n|\r|\n/', $string);
259
				
260
				//Trim lines
261
				foreach ($ladokUsers as $key => $value) {
262
					$ladokUsers[$key] = trim($ladokUsers[$key]);
263
				}
264
				
265
				//Split after last name
266
				foreach ($ladokUsers as $key => $value) {
267
					$ladokUsers[$key] = explode(',', trim($ladokUsers[$key]));
268
				}
269
				
270
				//Replace whitespaces and tabs with divider.
271
				foreach ($ladokUsers as $key => $value) {
272
					foreach ($ladokUsers[$key] as $key2 => $value2) {
273
						$ladokUsers[$key][$key2] = preg_replace('/\s+/', ' ', trim($ladokUsers[$key][$key2]));
274
					}
275
				}
276
				
277
				//Generate user array
278
				foreach ($ladokUsers as $key => $value) {
279
					$temp = array(
280
						'ssn' => substr($ladokUsers[$key][0], 0, 11),
281
						'lastname' => substr($ladokUsers[$key][0], 12, strlen($ladokUsers[$key][0])),
282
						'firstname' => substr($ladokUsers[$key][1], 0, stripos($ladokUsers[$key][1], ' ')),
283
						'email' => substr($ladokUsers[$key][1], (strrpos($ladokUsers[$key][1], ' ') + 1))
284
					);
285
					array_push($userArray, $temp);
286
				}
287
				
288
				//Return parsed user array
289
				return $userArray;
62.1.3 by Daniel Hermansson
Added functionality for parsing a user list from ladok into a user array (for future use)
290
			}
68.1.1 by Daniel Hermansson
Modified and corrected parser functionality.
291
			
292
			//If not authed
293
			return FALSE;
62.1.3 by Daniel Hermansson
Added functionality for parsing a user list from ladok into a user array (for future use)
294
		}
64.1.1 by b11johgu
ExamplesController:
295
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
296
297
		/*
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
298
		 *	Generates a salted password hash, encrypted with sha1.
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
299
		 *  RESTRICTED-LEVEL: System
300
		 */
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
301
		private function getSaltedHash($pwd) {
302
			//Salt = CodeIgniters encryption-key from config
303
			$salt = $this->config->item('encryption_key');
36.3.1 by Daniel Hermansson
Added login functionality
304
			
305
			//Generate SHA1 hash using salt
306
			$hash = sha1($salt.$pwd);
307
			
308
			return $hash;
309
		}
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
310
311
312
		/*
313
		 *	Log the login attempt.
314
		 *  RESTRICTED-LEVEL: System
315
		 */
316
		private function logLogin($userName, $valid) {
317
			$data = array(
318
				'userName' => $userName,
319
				'userAgent' => $this->session->userdata('user_agent'),
320
				'userIP' => $this->session->userdata('ip_address'),
64.1.1 by b11johgu
ExamplesController:
321
				'browserID' => $this->session->userdata('session_id'),
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
322
				'success' => $valid
323
			);
324
			
325
			$this->db->insert('logUserLoginAttempts', $data);
326
		}
64.1.1 by b11johgu
ExamplesController:
327
328
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
329
		/*
330
		 *	This function return TRUE if the user is logged in and FALSE otherwise.
331
		 *  RESTRICTED-LEVEL: System
64.1.1 by b11johgu
ExamplesController:
332
		 */
333
		public function isLoggedIn() {
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
334
			if ($this->session->userdata('authenticated')) {
335
				return TRUE;
336
			}
64.1.1 by b11johgu
ExamplesController:
337
			else{
338
				return FALSE;
339
			}
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
340
		}
64.1.1 by b11johgu
ExamplesController:
341
342
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
343
		/*
344
		 *	This function returns the users type (or FALSE if user isn't logged in).
345
		 *  RESTRICTED-LEVEL: System
64.1.1 by b11johgu
ExamplesController:
346
		 */
347
		public function getUserType() {
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
348
			if($this->isLoggedIn()) {
349
				$temp = $this->session->userdata('authenticated');
350
				return $temp['usertype'];
351
			}
352
			
353
			return FALSE;
354
		}
64.1.1 by b11johgu
ExamplesController:
355
356
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
357
		/*
62.1.4 by Daniel Hermansson
Added method for checking if its the first time a user logs on or not (to know when to require them to change password)
358
		 *	This function returns a boolean containing information if it is the first login.
359
		 *  RESTRICTED-LEVEL: System
360
		 */
361
		public function isFirstLogin() {
362
			if($this->isLoggedIn()) {
363
				$temp = $this->session->userdata('authenticated');
364
				if ($temp['firstLogin'] == 1) {
365
					return TRUE;
366
				}
367
			}
368
			
369
			return FALSE;
370
		}
64.1.1 by b11johgu
ExamplesController:
371
372
62.1.4 by Daniel Hermansson
Added method for checking if its the first time a user logs on or not (to know when to require them to change password)
373
		/*
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
374
		 *	This function returns the username (or FALSE if user isn't logged in).
375
		 *  RESTRICTED-LEVEL: System
376
		 */ 
64.1.1 by b11johgu
ExamplesController:
377
		public function getUserName() {
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
378
			if($this->isLoggedIn()) {
379
				$temp = $this->session->userdata('authenticated');
380
				return $temp['username'];
381
			}
382
			
383
			return FALSE;
384
		}
36.3.1 by Daniel Hermansson
Added login functionality
385
	}
64.1.1 by b11johgu
ExamplesController:
386
	
36.3.1 by Daniel Hermansson
Added login functionality
387
?>