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