/lenasys/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/trunk

« back to all changes in this revision

Viewing changes to codeigniter/application/models/user.php

  • Committer: Gustav Hatvigsson
  • Date: 2013-05-30 12:08:07 UTC
  • mfrom: (129 lenasys_b)
  • mto: This revision was merged to the branch mainline in revision 131.
  • Revision ID: gustav.hartvigsson@gmail.com-20130530120807-q2mad1xrq003ss2n
mergedĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
                function __construct() {
7
7
                        parent::__construct();
8
8
                        
9
 
                        //Load required libraries
 
9
                        //Load required libraries and drivers
10
10
                        $this->load->database();
11
11
                        $this->load->library('session');
12
12
                }
13
13
 
14
 
                //Queries the DB and for a matching username and password. Returns result or false.
 
14
 
15
15
                /*
16
 
                 *      Constructor
 
16
                 *      This function returns the users password hint (or FALSE if user isn't logged in).
 
17
                 *  RESTRICTED-LEVEL: None
17
18
                 */ 
18
 
                public function login($username, $password)
19
 
                {                       
 
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) {
 
32
                                foreach($result as $row) {
 
33
                                        $hint = $row->passwdHint;
 
34
                                        
 
35
                                        //Return hint
 
36
                                        return $hint;
 
37
                                }
 
38
                        }
 
39
                        
 
40
                        //No such user
 
41
                        return FALSE;
 
42
                }
 
43
 
 
44
 
 
45
                /*
 
46
                 *      This function logs the user in (returns FALSE on fail).
 
47
                 *  RESTRICTED-LEVEL: None
 
48
                 */
 
49
                public function login($username, $password) {                   
20
50
                        //Generate a salted hash
21
51
                        $hash = $this->getSaltedHash($password);
22
52
        
23
53
                        //Query-structure
24
 
                        $this->db->select('userName, name, passwd, userType, ssn');
 
54
                        $this->db->select('userName, name, passwd, userType, ssn, activeCourse'); // Tog bort firstLogin hƤr.
25
55
                        $this->db->from('Users');
26
56
                        $this->db->where('userName', $username);
27
57
                        $this->db->where('passwd', $hash);
40
70
                                                'username' => $row->userName,
41
71
                                                'name' => $row->name,
42
72
                                                'usertype' => $row->userType,
43
 
                                                'ssn' => $row->ssn
 
73
                                                'ssn' => $row->ssn,
 
74
                                                'activeCourse' => $row->activeCourse, 
 
75
                                        //      'firstLogin' => $row->firstLogin
44
76
                                        );
45
77
                                }
46
78
                                
47
79
                                //Set session data
48
80
                                $this->session->set_userdata('authenticated', $userDetails);
49
81
                                
 
82
                                //Log attempt as valid
 
83
                                $this->logLogin($username, 1);
 
84
                                
50
85
                                //Return success
51
86
                                return TRUE;
52
 
                        } else {
53
 
                                //Return fail
54
 
                                return FALSE;
55
 
                        }       
 
87
                        }
 
88
 
 
89
                        //Log attempt as invalid
 
90
                        $this->logLogin($username, 0);
 
91
 
 
92
                        //Return fail
 
93
                        return FALSE;
56
94
                }
57
95
 
58
96
 
59
97
                /*
60
98
                 *      This function logs the user out.
61
 
                 */ 
62
 
                public function logout() {              
 
99
                 *  RESTRICTED-LEVEL: Self
 
100
                 */
 
101
                public function logout() {
63
102
                        //Unset session data
64
103
                        $this->session->unset_userdata('authenticated');
65
104
                }
66
 
                
67
 
 
68
 
                /*
69
 
                 *      This function return TRUE if the user is logged in and FALSE otherwise.
70
 
                 */ 
71
 
                public function isLoggedIn() {  
72
 
                        if ($this->session->userdata('authenticated')) {
73
 
                                return TRUE;
74
 
                        } else {
75
 
                                return FALSE;
76
 
                        }
77
 
                }
78
 
                
79
 
                
80
 
                //Generates a password hash using a user-unique salt.
81
 
                private function getSaltedHash($pwd)
82
 
                {       
83
 
                        /* Salt algorithm:
84
 
                         * Step 1: Get encryption key
85
 
                         * Step 2: Salt is set to password (reversed)
86
 
                         * Step 3: Salt is encrypted with MD5
87
 
                         * Step 4: Salt is reversed (again)
88
 
                         * Step 5: Salt is encrypted with SHA1
89
 
                         */
90
 
                        
91
 
                        $salt = '';
92
 
                        
93
 
                        //UNCOMMENT TO ACTIVATE SALT LATER IN DEVELOPMENT!!!
94
 
                        //$salt = sha1(strrev(md5(strrev($this->config->item('encryption_key')))));
 
105
 
 
106
 
 
107
                /*
 
108
                 *      This function changes the users password.
 
109
                 *  RESTRICTED-LEVEL: Self
 
110
                 */
 
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
                                
 
118
                                //Validate input with database
 
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
                                
 
127
                                //If a matching DB record is found, update database.
 
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!
 
138
                                        return TRUE;
 
139
                                }
 
140
                        }
 
141
                        
 
142
                        //Return error
 
143
                        return FALSE;
 
144
                }
 
145
 
 
146
 
 
147
                /*
 
148
                 *      This function registers user into the database.
 
149
                 *  RESTRICTED-LEVEL: Teacher
 
150
                 */
 
151
                public function addUser($userName, $name, $ssn, $password, $userType, $pwdHint, $email) {
 
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);
 
161
                                $this->db->set('passwd', $hash);
 
162
                                $this->db->set('userType', $userType);
 
163
                                $this->db->set('passwdHint', $pwdHint);
 
164
                                $this->db->set('email', $email);
 
165
                                $result = $this->db->insert('Users');
 
166
                                
 
167
                                //Check for my-sql error
 
168
                                if($result) {
 
169
                                        //Return success
 
170
                                        return TRUE;
 
171
                                } 
 
172
                        }
 
173
                        
 
174
                        //Return error
 
175
                        return FALSE;
 
176
                }
 
177
 
 
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
 
 
186
 
 
187
                /*
 
188
                 *      This function removes users from the database.
 
189
                 *  RESTRICTED-LEVEL: Teacher
 
190
                 */
 
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);
 
196
                                $result = $this->db->delete('Users');
 
197
                                
 
198
                                //Check for my-sql error
 
199
                                if($result) {
 
200
                                        //Return success
 
201
                                        return TRUE;
 
202
                                }
 
203
                        }
 
204
                        
 
205
                        //Return error
 
206
                        return FALSE;
 
207
                }
 
208
 
 
209
 
 
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,
 
241
                                                'passwdHint' => 'default',
 
242
                                                'firstLogin' => 1
 
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
                }
 
256
 
 
257
 
 
258
                /*
 
259
                 *      This parses a user list from ladok and returns an array with users.
 
260
                 *  RESTRICTED-LEVEL: Teacher
 
261
                 */
 
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
                                        );
 
295
                                        $temp['username'] = substr($temp['email'], 0, (stripos($temp['email'], '@')));
 
296
                                        array_push($userArray, $temp);
 
297
                                }
 
298
                                
 
299
                                //Return parsed user array
 
300
                                return $userArray;
 
301
                        }
 
302
                        
 
303
                        //If not authed
 
304
                        return FALSE;
 
305
                }
 
306
 
 
307
 
 
308
                /*
 
309
                 *      Generates a salted password hash, encrypted with sha1.
 
310
                 *  RESTRICTED-LEVEL: System
 
311
                 */
 
312
                private function getSaltedHash($pwd) {
 
313
                        //Salt = CodeIgniters encryption-key from config
 
314
                        $salt = $this->config->item('encryption_key');
95
315
                        
96
316
                        //Generate SHA1 hash using salt
97
317
                        $hash = sha1($salt.$pwd);
98
318
                        
99
319
                        return $hash;
100
320
                }
 
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'),
 
332
                                'browserID' => $this->session->userdata('session_id'),
 
333
                                'success' => $valid
 
334
                        );
 
335
                        
 
336
                        $this->db->insert('logUserLoginAttempts', $data);
 
337
                }
 
338
 
 
339
 
 
340
                /*
 
341
                 *      This function return TRUE if the user is logged in and FALSE otherwise.
 
342
                 *  RESTRICTED-LEVEL: System
 
343
                 */
 
344
                public function isLoggedIn() {
 
345
                        if ($this->session->userdata('authenticated')) {
 
346
                                return TRUE;
 
347
                        }
 
348
                        else{
 
349
                                return FALSE;
 
350
                        }
 
351
                }
 
352
 
 
353
 
 
354
                /*
 
355
                 *      This function returns the users type (or FALSE if user isn't logged in).
 
356
                 *  RESTRICTED-LEVEL: System
 
357
                 */
 
358
                public function getUserType() {
 
359
                        if($this->isLoggedIn()) {
 
360
                                $temp = $this->session->userdata('authenticated');
 
361
                                return $temp['usertype'];
 
362
                        }
 
363
                        
 
364
                        return FALSE;
 
365
                }
 
366
 
 
367
 
 
368
                /*
 
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
                }
 
382
 
 
383
 
 
384
                /*
 
385
                 *      This function returns the username (or FALSE if user isn't logged in).
 
386
                 *  RESTRICTED-LEVEL: System
 
387
                 */ 
 
388
                public function getUserName() {
 
389
                        if($this->isLoggedIn()) {
 
390
                                $temp = $this->session->userdata('authenticated');
 
391
                                return $temp['username'];
 
392
                        }
 
393
                        
 
394
                        return FALSE;
 
395
                }
 
396
                
 
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
                
 
424
                /*
 
425
                * This function fetches the active course info
 
426
                */
 
427
                public function getActiveCourse(){
 
428
                        
 
429
                        if($this->isLoggedIn()) {
 
430
                                $temp = $this->session->userdata('authenticated');
 
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;
 
453
                        }
 
454
                        return FALSE;
 
455
                }
101
456
        }
102
457
?>
 
 
b'\\ No newline at end of file'