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