/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
85.1.8 by a11andoh
added the cms controller to load all content pages.
54
			$this->db->select('userName, name, passwd, userType, ssn, activeCourse'); // 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,
85.1.8 by a11andoh
added the cms controller to load all content pages.
74
						'activeCourse' => $row->activeCourse, 
64.1.1 by b11johgu
ExamplesController:
75
					// 	'firstLogin' => $row->firstLogin
36.3.1 by Daniel Hermansson
Added login functionality
76
					);
77
				}
78
				
79
				//Set session data
80
				$this->session->set_userdata('authenticated', $userDetails);
81
				
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
82
				//Log attempt as valid
83
				$this->logLogin($username, 1);
84
				
36.3.1 by Daniel Hermansson
Added login functionality
85
				//Return success
86
				return TRUE;
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
87
			}
88
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
89
			//Log attempt as invalid
90
			$this->logLogin($username, 0);
91
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
92
			//Return fail
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
93
			return FALSE;
36.3.1 by Daniel Hermansson
Added login functionality
94
		}
95
96
97
		/*
98
		 *	This function logs the user out.
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
99
		 *  RESTRICTED-LEVEL: Self
64.1.1 by b11johgu
ExamplesController:
100
		 */
101
		public function logout() {
36.3.1 by Daniel Hermansson
Added login functionality
102
			//Unset session data
103
			$this->session->unset_userdata('authenticated');
104
		}
64.1.1 by b11johgu
ExamplesController:
105
36.3.1 by Daniel Hermansson
Added login functionality
106
107
		/*
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
108
		 *	This function changes the users password.
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
109
		 *  RESTRICTED-LEVEL: Self
64.1.1 by b11johgu
ExamplesController:
110
		 */
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
111
		public function changePassword($pwdOld, $pwdNew, $pwdHint) {
112
			//Check that a user is logged in.
113
			if($this->isLoggedIn()) {
114
				$user = $this->getUserName();
115
				$oldHash = $this->getSaltedHash($pwdOld);
116
				$newHash = $this->getSaltedHash($pwdNew);
117
				
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
118
				//Validate input with database
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
119
				$this->db->select('userName');
120
				$this->db->from('Users');
121
				$this->db->where('userName', $user);
122
				$this->db->where('passwd', $oldHash);
123
				$this->db->limit(1);
124
				$query = $this->db->get();
125
				$result = $query->result();
126
				
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
127
				//If a matching DB record is found, update database.
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
128
				if($result) {
129
					$data = array(
130
						'passwd' => $newHash,
131
						'passwdHint' => $pwdHint
132
					);
133
					
134
					$this->db->where('userName', $user);
135
					$this->db->update('Users', $data);
136
					
137
					//Return Success!
64.1.1 by b11johgu
ExamplesController:
138
					return TRUE;
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
139
				}
140
			}
141
			
142
			//Return error
143
			return FALSE;
144
		}
145
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
146
147
		/*
148
		 *	This function registers user into the database.
149
		 *  RESTRICTED-LEVEL: Teacher
150
		 */
69.1.1 by Daniel Hermansson
Modified addUser-method to also take email as a argument
151
		public function addUser($userName, $name, $ssn, $password, $userType, $pwdHint, $email) {
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
152
			//Check that a user is logged in and has the right privileges (is teacher)
153
			if($this->isLoggedIn() && $this->getUserType() === 'Teacher') {
154
				//Generate a salted hash
155
				$hash = $this->getSaltedHash($password);
156
				
157
				//Query-structure (All values are escaped automatically by codeigninte, producing safer queries.)
158
				$this->db->set('userName', $userName);
159
				$this->db->set('name', $name);
160
				$this->db->set('ssn', $ssn);
69.1.1 by Daniel Hermansson
Modified addUser-method to also take email as a argument
161
				$this->db->set('passwd', $hash);
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
162
				$this->db->set('userType', $userType);
163
				$this->db->set('passwdHint', $pwdHint);
69.1.1 by Daniel Hermansson
Modified addUser-method to also take email as a argument
164
				$this->db->set('email', $email);
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
165
				$result = $this->db->insert('Users');
166
				
167
				//Check for my-sql error
168
				if($result) {
169
					//Return success
170
					return TRUE;
69.1.1 by Daniel Hermansson
Modified addUser-method to also take email as a argument
171
				} 
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
172
			}
173
			
174
			//Return error
175
			return FALSE;
176
		}
64.1.1 by b11johgu
ExamplesController:
177
83.2.15 by elof.bigestans at gmail
Continuing work on viewstudents
178
		/* 
179
		 * Updates the details of a user. Takes a username and an associative array of data with the details to be changed 
180
		 */
181
		public function updateUser($username, $data) {
182
			$this->db->where("username", $username);
183
			$this->db->update("Users", $data);
184
		}
185
64.1.1 by b11johgu
ExamplesController:
186
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
187
		/*
188
		 *	This function removes users from the database.
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
189
		 *  RESTRICTED-LEVEL: Teacher
64.1.1 by b11johgu
ExamplesController:
190
		 */
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
191
		public function removeUser($userName) {
192
			//Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
193
			if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
194
				//Query-structure
195
				$this->db->where('userName', $userName);
64.1.1 by b11johgu
ExamplesController:
196
				$result = $this->db->delete('Users');
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
197
				
198
				//Check for my-sql error
199
				if($result) {
200
					//Return success
201
					return TRUE;
64.1.1 by b11johgu
ExamplesController:
202
				}
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
203
			}
204
			
205
			//Return error
206
			return FALSE;
207
		}
64.1.1 by b11johgu
ExamplesController:
208
209
62.1.2 by Daniel Hermansson
Added functionality for resetting a users password
210
		/*
211
		 *	This reset the password for the user.
212
		 *  RESTRICTED-LEVEL: Teacher
213
		 */
214
		public function resetUser($userName) {
215
			//Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
216
			if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
217
				//Check user type
218
				$this->db->select('userName, userType, ssn, email');
219
				$this->db->from('Users');
220
				$this->db->where('userName', $username);
221
				$this->db->limit(1);
222
				$query = $this->db->get();
223
				$result = $query->result();
224
				
225
				//If a matching DB record is found.
226
				if($result) {
227
					//Prepare new hash depending on user-type
228
					$newPwdHash = '';
229
					
230
					if ($row->userType == 'Student') {
231
						$newPwdHash = $this->getSaltedHash($row->ssn);
232
					}
233
					else if ($row->userType == 'Teacher') {
234
						//$newPwdHash = $this->getSaltedHash($row->email);
235
						$newPwdHash = $this->getSaltedHash($row->email);
236
					}
237
					
238
					//Execute reset
239
					$data = array(
240
						'passwd' => $newPwdHash,
64.1.1 by b11johgu
ExamplesController:
241
						'passwdHint' => 'default',
242
						'firstLogin' => 1
62.1.2 by Daniel Hermansson
Added functionality for resetting a users password
243
					);
244
					
245
					$this->db->where('userName', $userName);
246
					$this->db->update('Users', $data);
247
					
248
					//Return Success!
249
					return TRUE;
250
				}
251
			}
252
			
253
			//Return error
254
			return FALSE;
255
		}
64.1.1 by b11johgu
ExamplesController:
256
257
62.1.3 by Daniel Hermansson
Added functionality for parsing a user list from ladok into a user array (for future use)
258
		/*
68.1.1 by Daniel Hermansson
Modified and corrected parser functionality.
259
		 *	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)
260
		 *  RESTRICTED-LEVEL: Teacher
261
		 */
68.1.1 by Daniel Hermansson
Modified and corrected parser functionality.
262
		public function parseLadok($string) {
263
			//Check that a user is logged in and has the right privileges (is teacher).
264
			if($this->isLoggedIn() && $this->getUserType() === 'Teacher') {
265
				$userArray = array();
266
				
267
				//Populate array with users from ladok
268
				$ladokUsers = preg_split( '/\r\n|\r|\n/', $string);
269
				
270
				//Trim lines
271
				foreach ($ladokUsers as $key => $value) {
272
					$ladokUsers[$key] = trim($ladokUsers[$key]);
273
				}
274
				
275
				//Split after last name
276
				foreach ($ladokUsers as $key => $value) {
277
					$ladokUsers[$key] = explode(',', trim($ladokUsers[$key]));
278
				}
279
				
280
				//Replace whitespaces and tabs with divider.
281
				foreach ($ladokUsers as $key => $value) {
282
					foreach ($ladokUsers[$key] as $key2 => $value2) {
283
						$ladokUsers[$key][$key2] = preg_replace('/\s+/', ' ', trim($ladokUsers[$key][$key2]));
284
					}
285
				}
286
				
287
				//Generate user array
288
				foreach ($ladokUsers as $key => $value) {
289
					$temp = array(
290
						'ssn' => substr($ladokUsers[$key][0], 0, 11),
291
						'lastname' => substr($ladokUsers[$key][0], 12, strlen($ladokUsers[$key][0])),
292
						'firstname' => substr($ladokUsers[$key][1], 0, stripos($ladokUsers[$key][1], ' ')),
293
						'email' => substr($ladokUsers[$key][1], (strrpos($ladokUsers[$key][1], ' ') + 1))
294
					);
69.1.2 by Daniel Hermansson
Modified ladok parser, functionality for getting username was missing
295
					$temp['username'] = substr($temp['email'], 0, (stripos($temp['email'], '@')));
68.1.1 by Daniel Hermansson
Modified and corrected parser functionality.
296
					array_push($userArray, $temp);
297
				}
298
				
299
				//Return parsed user array
300
				return $userArray;
62.1.3 by Daniel Hermansson
Added functionality for parsing a user list from ladok into a user array (for future use)
301
			}
68.1.1 by Daniel Hermansson
Modified and corrected parser functionality.
302
			
303
			//If not authed
304
			return FALSE;
62.1.3 by Daniel Hermansson
Added functionality for parsing a user list from ladok into a user array (for future use)
305
		}
64.1.1 by b11johgu
ExamplesController:
306
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
307
308
		/*
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
309
		 *	Generates a salted password hash, encrypted with sha1.
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
310
		 *  RESTRICTED-LEVEL: System
311
		 */
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
312
		private function getSaltedHash($pwd) {
313
			//Salt = CodeIgniters encryption-key from config
314
			$salt = $this->config->item('encryption_key');
36.3.1 by Daniel Hermansson
Added login functionality
315
			
316
			//Generate SHA1 hash using salt
317
			$hash = sha1($salt.$pwd);
318
			
319
			return $hash;
320
		}
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
321
322
323
		/*
324
		 *	Log the login attempt.
325
		 *  RESTRICTED-LEVEL: System
326
		 */
327
		private function logLogin($userName, $valid) {
328
			$data = array(
329
				'userName' => $userName,
330
				'userAgent' => $this->session->userdata('user_agent'),
331
				'userIP' => $this->session->userdata('ip_address'),
64.1.1 by b11johgu
ExamplesController:
332
				'browserID' => $this->session->userdata('session_id'),
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
333
				'success' => $valid
334
			);
335
			
336
			$this->db->insert('logUserLoginAttempts', $data);
337
		}
64.1.1 by b11johgu
ExamplesController:
338
339
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
340
		/*
341
		 *	This function return TRUE if the user is logged in and FALSE otherwise.
342
		 *  RESTRICTED-LEVEL: System
64.1.1 by b11johgu
ExamplesController:
343
		 */
344
		public function isLoggedIn() {
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
345
			if ($this->session->userdata('authenticated')) {
346
				return TRUE;
347
			}
64.1.1 by b11johgu
ExamplesController:
348
			else{
349
				return FALSE;
350
			}
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
351
		}
64.1.1 by b11johgu
ExamplesController:
352
353
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
354
		/*
355
		 *	This function returns the users type (or FALSE if user isn't logged in).
356
		 *  RESTRICTED-LEVEL: System
64.1.1 by b11johgu
ExamplesController:
357
		 */
358
		public function getUserType() {
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
359
			if($this->isLoggedIn()) {
360
				$temp = $this->session->userdata('authenticated');
361
				return $temp['usertype'];
362
			}
363
			
364
			return FALSE;
365
		}
64.1.1 by b11johgu
ExamplesController:
366
367
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
368
		/*
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)
369
		 *	This function returns a boolean containing information if it is the first login.
370
		 *  RESTRICTED-LEVEL: System
371
		 */
372
		public function isFirstLogin() {
373
			if($this->isLoggedIn()) {
374
				$temp = $this->session->userdata('authenticated');
375
				if ($temp['firstLogin'] == 1) {
376
					return TRUE;
377
				}
378
			}
379
			
380
			return FALSE;
381
		}
64.1.1 by b11johgu
ExamplesController:
382
383
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)
384
		/*
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
385
		 *	This function returns the username (or FALSE if user isn't logged in).
386
		 *  RESTRICTED-LEVEL: System
387
		 */ 
64.1.1 by b11johgu
ExamplesController:
388
		public function getUserName() {
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
389
			if($this->isLoggedIn()) {
390
				$temp = $this->session->userdata('authenticated');
391
				return $temp['username'];
392
			}
393
			
394
			return FALSE;
395
		}
85.1.8 by a11andoh
added the cms controller to load all content pages.
396
		
85.1.27 by a11andoh
added function to get ssn and name for the registrationspages in the user model.
397
				/*
398
		 *	This function returns the name (or FALSE if user isn't logged in).
399
		 *  RESTRICTED-LEVEL: System
400
		 */ 
401
		public function getName() {
402
			if($this->isLoggedIn()) {
403
				$temp = $this->session->userdata('authenticated');
404
				return $temp['name'];
405
			}
406
			
407
			return FALSE;
408
		}
409
		
410
				/*
411
		 *	This function returns the SSN (or FALSE if user isn't logged in).
412
		 *  RESTRICTED-LEVEL: System
413
		 */ 
414
		public function getSSN() {
415
			if($this->isLoggedIn()) {
416
				$temp = $this->session->userdata('authenticated');
417
				return $temp['ssn'];
418
			}
419
			
420
			return FALSE;
421
		}
422
		
423
		
85.1.8 by a11andoh
added the cms controller to load all content pages.
424
		/*
90.1.9 by a11emmjo
Updated branch.
425
		* This function fetches the active course info
85.1.8 by a11andoh
added the cms controller to load all content pages.
426
		*/
427
		public function getActiveCourse(){
90.1.9 by a11emmjo
Updated branch.
428
			
85.1.8 by a11andoh
added the cms controller to load all content pages.
429
			if($this->isLoggedIn()) {
430
				$temp = $this->session->userdata('authenticated');
90.1.9 by a11emmjo
Updated branch.
431
				$courseID = $temp['activeCourse'];
432
				$courseName;
433
			
434
				//Query-structure
435
				$this->db->select('name');
436
				$this->db->from('Courses');
437
				$this->db->where('courseID', $courseID);
438
				$this->db->limit(1);
439
				//Execute query
440
				$query = $this->db->get();
441
				$result = $query->result();
442
443
				foreach($result as $row) {
444
					$courseName = $row->name;
445
				}
446
				
447
				$data = array(
448
					'courseID' => $courseID,
449
					'courseName' => $courseName
450
				);
451
				
452
				return $data;
85.1.8 by a11andoh
added the cms controller to load all content pages.
453
			}
454
			return FALSE;
455
		}
36.3.1 by Daniel Hermansson
Added login functionality
456
	}
457
?>