/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
178
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
179
		/*
180
		 *	This function removes users from the database.
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
181
		 *  RESTRICTED-LEVEL: Teacher
64.1.1 by b11johgu
ExamplesController:
182
		 */
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
183
		public function removeUser($userName) {
184
			//Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
185
			if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
186
				//Query-structure
187
				$this->db->where('userName', $userName);
64.1.1 by b11johgu
ExamplesController:
188
				$result = $this->db->delete('Users');
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
189
				
190
				//Check for my-sql error
191
				if($result) {
192
					//Return success
193
					return TRUE;
64.1.1 by b11johgu
ExamplesController:
194
				}
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
195
			}
196
			
197
			//Return error
198
			return FALSE;
199
		}
64.1.1 by b11johgu
ExamplesController:
200
201
62.1.2 by Daniel Hermansson
Added functionality for resetting a users password
202
		/*
203
		 *	This reset the password for the user.
204
		 *  RESTRICTED-LEVEL: Teacher
205
		 */
206
		public function resetUser($userName) {
207
			//Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
208
			if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
209
				//Check user type
210
				$this->db->select('userName, userType, ssn, email');
211
				$this->db->from('Users');
212
				$this->db->where('userName', $username);
213
				$this->db->limit(1);
214
				$query = $this->db->get();
215
				$result = $query->result();
216
				
217
				//If a matching DB record is found.
218
				if($result) {
219
					//Prepare new hash depending on user-type
220
					$newPwdHash = '';
221
					
222
					if ($row->userType == 'Student') {
223
						$newPwdHash = $this->getSaltedHash($row->ssn);
224
					}
225
					else if ($row->userType == 'Teacher') {
226
						//$newPwdHash = $this->getSaltedHash($row->email);
227
						$newPwdHash = $this->getSaltedHash($row->email);
228
					}
229
					
230
					//Execute reset
231
					$data = array(
232
						'passwd' => $newPwdHash,
64.1.1 by b11johgu
ExamplesController:
233
						'passwdHint' => 'default',
234
						'firstLogin' => 1
62.1.2 by Daniel Hermansson
Added functionality for resetting a users password
235
					);
236
					
237
					$this->db->where('userName', $userName);
238
					$this->db->update('Users', $data);
239
					
240
					//Return Success!
241
					return TRUE;
242
				}
243
			}
244
			
245
			//Return error
246
			return FALSE;
247
		}
64.1.1 by b11johgu
ExamplesController:
248
249
62.1.3 by Daniel Hermansson
Added functionality for parsing a user list from ladok into a user array (for future use)
250
		/*
68.1.1 by Daniel Hermansson
Modified and corrected parser functionality.
251
		 *	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)
252
		 *  RESTRICTED-LEVEL: Teacher
253
		 */
68.1.1 by Daniel Hermansson
Modified and corrected parser functionality.
254
		public function parseLadok($string) {
255
			//Check that a user is logged in and has the right privileges (is teacher).
256
			if($this->isLoggedIn() && $this->getUserType() === 'Teacher') {
257
				$userArray = array();
258
				
259
				//Populate array with users from ladok
260
				$ladokUsers = preg_split( '/\r\n|\r|\n/', $string);
261
				
262
				//Trim lines
263
				foreach ($ladokUsers as $key => $value) {
264
					$ladokUsers[$key] = trim($ladokUsers[$key]);
265
				}
266
				
267
				//Split after last name
268
				foreach ($ladokUsers as $key => $value) {
269
					$ladokUsers[$key] = explode(',', trim($ladokUsers[$key]));
270
				}
271
				
272
				//Replace whitespaces and tabs with divider.
273
				foreach ($ladokUsers as $key => $value) {
274
					foreach ($ladokUsers[$key] as $key2 => $value2) {
275
						$ladokUsers[$key][$key2] = preg_replace('/\s+/', ' ', trim($ladokUsers[$key][$key2]));
276
					}
277
				}
278
				
279
				//Generate user array
280
				foreach ($ladokUsers as $key => $value) {
281
					$temp = array(
282
						'ssn' => substr($ladokUsers[$key][0], 0, 11),
283
						'lastname' => substr($ladokUsers[$key][0], 12, strlen($ladokUsers[$key][0])),
284
						'firstname' => substr($ladokUsers[$key][1], 0, stripos($ladokUsers[$key][1], ' ')),
285
						'email' => substr($ladokUsers[$key][1], (strrpos($ladokUsers[$key][1], ' ') + 1))
286
					);
69.1.2 by Daniel Hermansson
Modified ladok parser, functionality for getting username was missing
287
					$temp['username'] = substr($temp['email'], 0, (stripos($temp['email'], '@')));
68.1.1 by Daniel Hermansson
Modified and corrected parser functionality.
288
					array_push($userArray, $temp);
289
				}
290
				
291
				//Return parsed user array
292
				return $userArray;
62.1.3 by Daniel Hermansson
Added functionality for parsing a user list from ladok into a user array (for future use)
293
			}
68.1.1 by Daniel Hermansson
Modified and corrected parser functionality.
294
			
295
			//If not authed
296
			return FALSE;
62.1.3 by Daniel Hermansson
Added functionality for parsing a user list from ladok into a user array (for future use)
297
		}
64.1.1 by b11johgu
ExamplesController:
298
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
299
300
		/*
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
301
		 *	Generates a salted password hash, encrypted with sha1.
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
302
		 *  RESTRICTED-LEVEL: System
303
		 */
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
304
		private function getSaltedHash($pwd) {
305
			//Salt = CodeIgniters encryption-key from config
306
			$salt = $this->config->item('encryption_key');
36.3.1 by Daniel Hermansson
Added login functionality
307
			
308
			//Generate SHA1 hash using salt
309
			$hash = sha1($salt.$pwd);
310
			
311
			return $hash;
312
		}
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
313
314
315
		/*
316
		 *	Log the login attempt.
317
		 *  RESTRICTED-LEVEL: System
318
		 */
319
		private function logLogin($userName, $valid) {
320
			$data = array(
321
				'userName' => $userName,
322
				'userAgent' => $this->session->userdata('user_agent'),
323
				'userIP' => $this->session->userdata('ip_address'),
64.1.1 by b11johgu
ExamplesController:
324
				'browserID' => $this->session->userdata('session_id'),
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
325
				'success' => $valid
326
			);
327
			
328
			$this->db->insert('logUserLoginAttempts', $data);
329
		}
64.1.1 by b11johgu
ExamplesController:
330
331
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
332
		/*
333
		 *	This function return TRUE if the user is logged in and FALSE otherwise.
334
		 *  RESTRICTED-LEVEL: System
64.1.1 by b11johgu
ExamplesController:
335
		 */
336
		public function isLoggedIn() {
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
337
			if ($this->session->userdata('authenticated')) {
338
				return TRUE;
339
			}
64.1.1 by b11johgu
ExamplesController:
340
			else{
341
				return FALSE;
342
			}
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
343
		}
64.1.1 by b11johgu
ExamplesController:
344
345
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
346
		/*
347
		 *	This function returns the users type (or FALSE if user isn't logged in).
348
		 *  RESTRICTED-LEVEL: System
64.1.1 by b11johgu
ExamplesController:
349
		 */
350
		public function getUserType() {
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
351
			if($this->isLoggedIn()) {
352
				$temp = $this->session->userdata('authenticated');
353
				return $temp['usertype'];
354
			}
355
			
356
			return FALSE;
357
		}
64.1.1 by b11johgu
ExamplesController:
358
359
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
360
		/*
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)
361
		 *	This function returns a boolean containing information if it is the first login.
362
		 *  RESTRICTED-LEVEL: System
363
		 */
364
		public function isFirstLogin() {
365
			if($this->isLoggedIn()) {
366
				$temp = $this->session->userdata('authenticated');
367
				if ($temp['firstLogin'] == 1) {
368
					return TRUE;
369
				}
370
			}
371
			
372
			return FALSE;
373
		}
64.1.1 by b11johgu
ExamplesController:
374
375
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)
376
		/*
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
377
		 *	This function returns the username (or FALSE if user isn't logged in).
378
		 *  RESTRICTED-LEVEL: System
379
		 */ 
64.1.1 by b11johgu
ExamplesController:
380
		public function getUserName() {
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
381
			if($this->isLoggedIn()) {
382
				$temp = $this->session->userdata('authenticated');
383
				return $temp['username'];
384
			}
385
			
386
			return FALSE;
387
		}
85.1.8 by a11andoh
added the cms controller to load all content pages.
388
		
389
		/*
390
		* This function fetches the active course from the user table
391
		*/
392
		public function getActiveCourse(){
393
			if($this->isLoggedIn()) {
394
				$temp = $this->session->userdata('authenticated');
395
				return $temp['activeCourse'];	
396
			}
397
			
398
			return FALSE;
399
		}
36.3.1 by Daniel Hermansson
Added login functionality
400
	}
64.1.1 by b11johgu
ExamplesController:
401
	
36.3.1 by Daniel Hermansson
Added login functionality
402
?>