/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-14 11:44:44 UTC
  • mfrom: (62.1.4 lenasys)
  • Revision ID: gustav.hartvigsson@gmail.com-20130514114444-vxeatanuc99lla34
Merged some changes for the logging of user activity.
And added a parser for Ladok user data.

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
                /*
46
46
                 *      This function logs the user in (returns FALSE on fail).
47
47
                 *  RESTRICTED-LEVEL: None
48
 
                 */ 
49
 
                public function login($username, $password)
50
 
                {                       
 
48
                 */
 
49
                public function login($username, $password) {                   
51
50
                        //Generate a salted hash
52
51
                        $hash = $this->getSaltedHash($password);
53
52
        
78
77
                                //Set session data
79
78
                                $this->session->set_userdata('authenticated', $userDetails);
80
79
                                
 
80
                                //Log attempt as valid
 
81
                                $this->logLogin($username, 1);
 
82
                                
81
83
                                //Return success
82
84
                                return TRUE;
83
85
                        }
84
86
 
 
87
                        //Log attempt as invalid
 
88
                        $this->logLogin($username, 0);
 
89
 
85
90
                        //Return fail
86
 
                        return FALSE;   
 
91
                        return FALSE;
87
92
                }
88
93
 
89
94
 
190
195
                        return FALSE;
191
196
                }
192
197
                
 
198
                
 
199
                /*
 
200
                 *      This reset the password for the user.
 
201
                 *  RESTRICTED-LEVEL: Teacher
 
202
                 */
 
203
                public function resetUser($userName) {
 
204
                        //Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
 
205
                        if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
 
206
                                //Check user type
 
207
                                $this->db->select('userName, userType, ssn, email');
 
208
                                $this->db->from('Users');
 
209
                                $this->db->where('userName', $username);
 
210
                                $this->db->limit(1);
 
211
                                $query = $this->db->get();
 
212
                                $result = $query->result();
 
213
                                
 
214
                                //If a matching DB record is found.
 
215
                                if($result) {
 
216
                                        //Prepare new hash depending on user-type
 
217
                                        $newPwdHash = '';
 
218
                                        
 
219
                                        if ($row->userType == 'Student') {
 
220
                                                $newPwdHash = $this->getSaltedHash($row->ssn);
 
221
                                        }
 
222
                                        else if ($row->userType == 'Teacher') {
 
223
                                                //$newPwdHash = $this->getSaltedHash($row->email);
 
224
                                                $newPwdHash = $this->getSaltedHash($row->email);
 
225
                                        }
 
226
                                        
 
227
                                        //Execute reset
 
228
                                        $data = array(
 
229
                                                'passwd' => $newPwdHash,
 
230
                                                'passwdHint' => 'default'
 
231
                                        );
 
232
                                        
 
233
                                        $this->db->where('userName', $userName);
 
234
                                        $this->db->update('Users', $data);
 
235
                                        
 
236
                                        //Return Success!
 
237
                                        return TRUE;
 
238
                                }
 
239
                        }
 
240
                        
 
241
                        //Return error
 
242
                        return FALSE;
 
243
                }
 
244
                
 
245
                
 
246
                /*
 
247
                 *      This parses a user list from ladok and returns an array with users.
 
248
                 *  RESTRICTED-LEVEL: Teacher
 
249
                 */
 
250
                public function parseLadok($string) {
 
251
                        //Check that a user is logged in and has the right privileges (is teacher).
 
252
                        if($this->isLoggedIn() && $this->getUserType() === 'Teacher') {
 
253
                                $userArray = array();
 
254
                                
 
255
                                //Populate array with users from ladok
 
256
                                $ladokUsers = preg_split( '/\r\n|\r|\n/', $string);
 
257
                                
 
258
                                //Trim lines
 
259
                                foreach ($ladokUsers as $key => &$value) {
 
260
                                        $ladokUsers[$key] = trim($ladokUsers[$key]);
 
261
                                }
 
262
                                
 
263
                                //Split after last name
 
264
                                foreach ($ladokUsers as $key => &$value) {
 
265
                                        $ladokUsers[$key] = explode(',', trim($ladokUsers[$key]));
 
266
                                }
 
267
                                
 
268
                                //Replace whitespaces and tabs with divider.
 
269
                                foreach ($ladokUsers as $key => &$value) {
 
270
                                        foreach ($ladokUsers[$key] as $key2 => &$value2) {
 
271
                                                $ladokUsers[$key][$key2] = preg_replace('/\s+/', ' ', trim($ladokUsers[$key][$key2]));
 
272
                                        }
 
273
                                }
 
274
                                
 
275
                                //Explode on whitespace on second split
 
276
                                foreach ($ladokUsers as $key => &$value) {
 
277
                                        $ladokUsers[$key][1] = explode(' ', trim($ladokUsers[$key][1]));
 
278
                                }       
 
279
                                
 
280
                                //Generate user array
 
281
                                $i = 0;
 
282
                                foreach ($ladokUsers as $key => $value) {
 
283
                                        $userArray[$i]['ssn'] = substr($ladokUsers[$key][0], 0, 11);
 
284
                                        $userArray[$i]['lastname'] = substr($ladokUsers[$key][0], 12, strlen($ladokUsers[$key][0]));
 
285
                                        $userArray[$i]['firstname'] = $ladokUsers[$key][1][0];
 
286
                                        $userArray[$i]['email'] = $ladokUsers[$key][1][3];
 
287
                                        $i++;
 
288
                                }
 
289
                                
 
290
                                //Return parsed user array
 
291
                                return $userArray;
 
292
                        }
 
293
                        
 
294
                        //If not authed
 
295
                        return FALSE;
 
296
                }
 
297
                
193
298
 
194
299
                /*
195
300
                 *      Generates a salted password hash, encrypted with sha1.
196
 
                 */             
 
301
                 *  RESTRICTED-LEVEL: System
 
302
                 */
197
303
                private function getSaltedHash($pwd) {
198
304
                        //Salt = CodeIgniters encryption-key from config
199
305
                        $salt = $this->config->item('encryption_key');
203
309
                        
204
310
                        return $hash;
205
311
                }
 
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'),
 
323
                                'browserID' => $this->session->userdata('session_id'), //TODO: change later?
 
324
                                'success' => $valid
 
325
                        );
 
326
                        
 
327
                        $this->db->insert('logUserLoginAttempts', $data);
 
328
                }
206
329
                
207
330
                
208
331
                /*
233
356
                
234
357
                
235
358
                /*
 
359
                 *      This function returns a boolean containing information if it is the first login.
 
360
                 *  RESTRICTED-LEVEL: System
 
361
                 */
 
362
                public function isFirstLogin() {
 
363
                        if($this->isLoggedIn()) {
 
364
                                $temp = $this->session->userdata('authenticated');
 
365
                                if ($temp['firstLogin'] == 1) {
 
366
                                        return TRUE;
 
367
                                }
 
368
                        }
 
369
                        
 
370
                        return FALSE;
 
371
                }
 
372
                
 
373
                
 
374
                /*
236
375
                 *      This function returns the username (or FALSE if user isn't logged in).
237
376
                 *  RESTRICTED-LEVEL: System
238
377
                 */