/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
		/*
64.1.1 by b11johgu
ExamplesController:
249
		 *	This parser a user list from ladok.
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
		 */
64.1.1 by b11johgu
ExamplesController:
252
		public function parseLadok() {
253
			//Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
254
			if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
62.1.3 by Daniel Hermansson
Added functionality for parsing a user list from ladok into a user array (for future use)
255
			}
256
		}
64.1.1 by b11johgu
ExamplesController:
257
52.1.1 by b11johgu
Added controllers for examplepage, templatelayout.
258
259
		/*
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
260
		 *	Generates a salted password hash, encrypted with sha1.
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
261
		 *  RESTRICTED-LEVEL: System
262
		 */
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
263
		private function getSaltedHash($pwd) {
264
			//Salt = CodeIgniters encryption-key from config
265
			$salt = $this->config->item('encryption_key');
36.3.1 by Daniel Hermansson
Added login functionality
266
			
267
			//Generate SHA1 hash using salt
268
			$hash = sha1($salt.$pwd);
269
			
270
			return $hash;
271
		}
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
272
273
274
		/*
275
		 *	Log the login attempt.
276
		 *  RESTRICTED-LEVEL: System
277
		 */
278
		private function logLogin($userName, $valid) {
279
			$data = array(
280
				'userName' => $userName,
281
				'userAgent' => $this->session->userdata('user_agent'),
282
				'userIP' => $this->session->userdata('ip_address'),
64.1.1 by b11johgu
ExamplesController:
283
				'browserID' => $this->session->userdata('session_id'),
62.1.1 by Daniel Hermansson
Cleaned code and added logging of logins
284
				'success' => $valid
285
			);
286
			
287
			$this->db->insert('logUserLoginAttempts', $data);
288
		}
64.1.1 by b11johgu
ExamplesController:
289
290
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
291
		/*
292
		 *	This function return TRUE if the user is logged in and FALSE otherwise.
293
		 *  RESTRICTED-LEVEL: System
64.1.1 by b11johgu
ExamplesController:
294
		 */
295
		public function isLoggedIn() {
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
296
			if ($this->session->userdata('authenticated')) {
297
				return TRUE;
298
			}
64.1.1 by b11johgu
ExamplesController:
299
			else{
300
				return FALSE;
301
			}
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
302
		}
64.1.1 by b11johgu
ExamplesController:
303
304
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
305
		/*
306
		 *	This function returns the users type (or FALSE if user isn't logged in).
307
		 *  RESTRICTED-LEVEL: System
64.1.1 by b11johgu
ExamplesController:
308
		 */
309
		public function getUserType() {
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
310
			if($this->isLoggedIn()) {
311
				$temp = $this->session->userdata('authenticated');
312
				return $temp['usertype'];
313
			}
314
			
315
			return FALSE;
316
		}
64.1.1 by b11johgu
ExamplesController:
317
318
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
319
		/*
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)
320
		 *	This function returns a boolean containing information if it is the first login.
321
		 *  RESTRICTED-LEVEL: System
322
		 */
323
		public function isFirstLogin() {
324
			if($this->isLoggedIn()) {
325
				$temp = $this->session->userdata('authenticated');
326
				if ($temp['firstLogin'] == 1) {
327
					return TRUE;
328
				}
329
			}
330
			
331
			return FALSE;
332
		}
64.1.1 by b11johgu
ExamplesController:
333
334
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)
335
		/*
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
336
		 *	This function returns the username (or FALSE if user isn't logged in).
337
		 *  RESTRICTED-LEVEL: System
338
		 */ 
64.1.1 by b11johgu
ExamplesController:
339
		public function getUserName() {
53.2.2 by Daniel Hermansson
Activated hash salting, and cleaned up some code.
340
			if($this->isLoggedIn()) {
341
				$temp = $this->session->userdata('authenticated');
342
				return $temp['username'];
343
			}
344
			
345
			return FALSE;
346
		}
36.3.1 by Daniel Hermansson
Added login functionality
347
	}
64.1.1 by b11johgu
ExamplesController:
348
	
36.3.1 by Daniel Hermansson
Added login functionality
349
?>