/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: a11andoh
  • Date: 2013-05-24 11:28:43 UTC
  • mto: This revision was merged to the branch mainline in revision 97.
  • Revision ID: a11andoh@student.his.se-20130524112843-360je7hu7q13r171
added the cms controller to load all content pages.
added first time registration controller to load the views for the registration
pages.
added and fixed temporarypages for the controllers 
and edited the models to be able to get active courses.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
        Class User extends CI_Model {
 
3
                /*
 
4
                 *      Constructor
 
5
                 */
 
6
                function __construct() {
 
7
                        parent::__construct();
 
8
                        
 
9
                        //Load required libraries and drivers
 
10
                        $this->load->database();
 
11
                        $this->load->library('session');
 
12
                }
 
13
 
 
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) {
 
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) {                   
 
50
                        //Generate a salted hash
 
51
                        $hash = $this->getSaltedHash($password);
 
52
        
 
53
                        //Query-structure
 
54
                        $this->db->select('userName, name, passwd, userType, ssn, activeCourse'); // Tog bort firstLogin här.
 
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,
 
73
                                                'ssn' => $row->ssn,
 
74
                                                'activeCourse' => $row->activeCourse, 
 
75
                                        //      'firstLogin' => $row->firstLogin
 
76
                                        );
 
77
                                }
 
78
                                
 
79
                                //Set session data
 
80
                                $this->session->set_userdata('authenticated', $userDetails);
 
81
                                
 
82
                                //Log attempt as valid
 
83
                                $this->logLogin($username, 1);
 
84
                                
 
85
                                //Return success
 
86
                                return TRUE;
 
87
                        }
 
88
 
 
89
                        //Log attempt as invalid
 
90
                        $this->logLogin($username, 0);
 
91
 
 
92
                        //Return fail
 
93
                        return FALSE;
 
94
                }
 
95
 
 
96
 
 
97
                /*
 
98
                 *      This function logs the user out.
 
99
                 *  RESTRICTED-LEVEL: Self
 
100
                 */
 
101
                public function logout() {
 
102
                        //Unset session data
 
103
                        $this->session->unset_userdata('authenticated');
 
104
                }
 
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
                /*
 
180
                 *      This function removes users from the database.
 
181
                 *  RESTRICTED-LEVEL: Teacher
 
182
                 */
 
183
                public function removeUser($userName) {
 
184
                        //Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
 
185
                        if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
 
186
                                //Query-structure
 
187
                                $this->db->where('userName', $userName);
 
188
                                $result = $this->db->delete('Users');
 
189
                                
 
190
                                //Check for my-sql error
 
191
                                if($result) {
 
192
                                        //Return success
 
193
                                        return TRUE;
 
194
                                }
 
195
                        }
 
196
                        
 
197
                        //Return error
 
198
                        return FALSE;
 
199
                }
 
200
 
 
201
 
 
202
                /*
 
203
                 *      This reset the password for the user.
 
204
                 *  RESTRICTED-LEVEL: Teacher
 
205
                 */
 
206
                public function resetUser($userName) {
 
207
                        //Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
 
208
                        if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
 
209
                                //Check user type
 
210
                                $this->db->select('userName, userType, ssn, email');
 
211
                                $this->db->from('Users');
 
212
                                $this->db->where('userName', $username);
 
213
                                $this->db->limit(1);
 
214
                                $query = $this->db->get();
 
215
                                $result = $query->result();
 
216
                                
 
217
                                //If a matching DB record is found.
 
218
                                if($result) {
 
219
                                        //Prepare new hash depending on user-type
 
220
                                        $newPwdHash = '';
 
221
                                        
 
222
                                        if ($row->userType == 'Student') {
 
223
                                                $newPwdHash = $this->getSaltedHash($row->ssn);
 
224
                                        }
 
225
                                        else if ($row->userType == 'Teacher') {
 
226
                                                //$newPwdHash = $this->getSaltedHash($row->email);
 
227
                                                $newPwdHash = $this->getSaltedHash($row->email);
 
228
                                        }
 
229
                                        
 
230
                                        //Execute reset
 
231
                                        $data = array(
 
232
                                                'passwd' => $newPwdHash,
 
233
                                                'passwdHint' => 'default',
 
234
                                                'firstLogin' => 1
 
235
                                        );
 
236
                                        
 
237
                                        $this->db->where('userName', $userName);
 
238
                                        $this->db->update('Users', $data);
 
239
                                        
 
240
                                        //Return Success!
 
241
                                        return TRUE;
 
242
                                }
 
243
                        }
 
244
                        
 
245
                        //Return error
 
246
                        return FALSE;
 
247
                }
 
248
 
 
249
 
 
250
                /*
 
251
                 *      This parses a user list from ladok and returns an array with users.
 
252
                 *  RESTRICTED-LEVEL: Teacher
 
253
                 */
 
254
                public function parseLadok($string) {
 
255
                        //Check that a user is logged in and has the right privileges (is teacher).
 
256
                        if($this->isLoggedIn() && $this->getUserType() === 'Teacher') {
 
257
                                $userArray = array();
 
258
                                
 
259
                                //Populate array with users from ladok
 
260
                                $ladokUsers = preg_split( '/\r\n|\r|\n/', $string);
 
261
                                
 
262
                                //Trim lines
 
263
                                foreach ($ladokUsers as $key => $value) {
 
264
                                        $ladokUsers[$key] = trim($ladokUsers[$key]);
 
265
                                }
 
266
                                
 
267
                                //Split after last name
 
268
                                foreach ($ladokUsers as $key => $value) {
 
269
                                        $ladokUsers[$key] = explode(',', trim($ladokUsers[$key]));
 
270
                                }
 
271
                                
 
272
                                //Replace whitespaces and tabs with divider.
 
273
                                foreach ($ladokUsers as $key => $value) {
 
274
                                        foreach ($ladokUsers[$key] as $key2 => $value2) {
 
275
                                                $ladokUsers[$key][$key2] = preg_replace('/\s+/', ' ', trim($ladokUsers[$key][$key2]));
 
276
                                        }
 
277
                                }
 
278
                                
 
279
                                //Generate user array
 
280
                                foreach ($ladokUsers as $key => $value) {
 
281
                                        $temp = array(
 
282
                                                'ssn' => substr($ladokUsers[$key][0], 0, 11),
 
283
                                                'lastname' => substr($ladokUsers[$key][0], 12, strlen($ladokUsers[$key][0])),
 
284
                                                'firstname' => substr($ladokUsers[$key][1], 0, stripos($ladokUsers[$key][1], ' ')),
 
285
                                                'email' => substr($ladokUsers[$key][1], (strrpos($ladokUsers[$key][1], ' ') + 1))
 
286
                                        );
 
287
                                        $temp['username'] = substr($temp['email'], 0, (stripos($temp['email'], '@')));
 
288
                                        array_push($userArray, $temp);
 
289
                                }
 
290
                                
 
291
                                //Return parsed user array
 
292
                                return $userArray;
 
293
                        }
 
294
                        
 
295
                        //If not authed
 
296
                        return FALSE;
 
297
                }
 
298
 
 
299
 
 
300
                /*
 
301
                 *      Generates a salted password hash, encrypted with sha1.
 
302
                 *  RESTRICTED-LEVEL: System
 
303
                 */
 
304
                private function getSaltedHash($pwd) {
 
305
                        //Salt = CodeIgniters encryption-key from config
 
306
                        $salt = $this->config->item('encryption_key');
 
307
                        
 
308
                        //Generate SHA1 hash using salt
 
309
                        $hash = sha1($salt.$pwd);
 
310
                        
 
311
                        return $hash;
 
312
                }
 
313
 
 
314
 
 
315
                /*
 
316
                 *      Log the login attempt.
 
317
                 *  RESTRICTED-LEVEL: System
 
318
                 */
 
319
                private function logLogin($userName, $valid) {
 
320
                        $data = array(
 
321
                                'userName' => $userName,
 
322
                                'userAgent' => $this->session->userdata('user_agent'),
 
323
                                'userIP' => $this->session->userdata('ip_address'),
 
324
                                'browserID' => $this->session->userdata('session_id'),
 
325
                                'success' => $valid
 
326
                        );
 
327
                        
 
328
                        $this->db->insert('logUserLoginAttempts', $data);
 
329
                }
 
330
 
 
331
 
 
332
                /*
 
333
                 *      This function return TRUE if the user is logged in and FALSE otherwise.
 
334
                 *  RESTRICTED-LEVEL: System
 
335
                 */
 
336
                public function isLoggedIn() {
 
337
                        if ($this->session->userdata('authenticated')) {
 
338
                                return TRUE;
 
339
                        }
 
340
                        else{
 
341
                                return FALSE;
 
342
                        }
 
343
                }
 
344
 
 
345
 
 
346
                /*
 
347
                 *      This function returns the users type (or FALSE if user isn't logged in).
 
348
                 *  RESTRICTED-LEVEL: System
 
349
                 */
 
350
                public function getUserType() {
 
351
                        if($this->isLoggedIn()) {
 
352
                                $temp = $this->session->userdata('authenticated');
 
353
                                return $temp['usertype'];
 
354
                        }
 
355
                        
 
356
                        return FALSE;
 
357
                }
 
358
 
 
359
 
 
360
                /*
 
361
                 *      This function returns a boolean containing information if it is the first login.
 
362
                 *  RESTRICTED-LEVEL: System
 
363
                 */
 
364
                public function isFirstLogin() {
 
365
                        if($this->isLoggedIn()) {
 
366
                                $temp = $this->session->userdata('authenticated');
 
367
                                if ($temp['firstLogin'] == 1) {
 
368
                                        return TRUE;
 
369
                                }
 
370
                        }
 
371
                        
 
372
                        return FALSE;
 
373
                }
 
374
 
 
375
 
 
376
                /*
 
377
                 *      This function returns the username (or FALSE if user isn't logged in).
 
378
                 *  RESTRICTED-LEVEL: System
 
379
                 */ 
 
380
                public function getUserName() {
 
381
                        if($this->isLoggedIn()) {
 
382
                                $temp = $this->session->userdata('authenticated');
 
383
                                return $temp['username'];
 
384
                        }
 
385
                        
 
386
                        return FALSE;
 
387
                }
 
388
                
 
389
                /*
 
390
                * This function fetches the active course from the user table
 
391
                */
 
392
                public function getActiveCourse(){
 
393
                        if($this->isLoggedIn()) {
 
394
                                $temp = $this->session->userdata('authenticated');
 
395
                                return $temp['activeCourse'];   
 
396
                        }
 
397
                        
 
398
                        return FALSE;
 
399
                }
 
400
        }
 
401
        
 
402
?>
 
 
b'\\ No newline at end of file'