/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: elof.bigestans at gmail
  • Date: 2013-05-24 10:00:47 UTC
  • mto: This revision was merged to the branch mainline in revision 95.
  • Revision ID: elof.bigestans@gmail.com-20130524100047-bqgk67s2g9o3l85x
Solved some bugs in ManageCourses and related files (CSS, JS, View, Controller)

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'); // 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
                                        //      'firstLogin' => $row->firstLogin
 
75
                                        );
 
76
                                }
 
77
                                
 
78
                                //Set session data
 
79
                                $this->session->set_userdata('authenticated', $userDetails);
 
80
                                
 
81
                                //Log attempt as valid
 
82
                                $this->logLogin($username, 1);
 
83
                                
 
84
                                //Return success
 
85
                                return TRUE;
 
86
                        }
 
87
 
 
88
                        //Log attempt as invalid
 
89
                        $this->logLogin($username, 0);
 
90
 
 
91
                        //Return fail
 
92
                        return FALSE;
 
93
                }
 
94
 
 
95
 
 
96
                /*
 
97
                 *      This function logs the user out.
 
98
                 *  RESTRICTED-LEVEL: Self
 
99
                 */
 
100
                public function logout() {
 
101
                        //Unset session data
 
102
                        $this->session->unset_userdata('authenticated');
 
103
                }
 
104
 
 
105
 
 
106
                /*
 
107
                 *      This function changes the users password.
 
108
                 *  RESTRICTED-LEVEL: Self
 
109
                 */
 
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
                                
 
117
                                //Validate input with database
 
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
                                
 
126
                                //If a matching DB record is found, update database.
 
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!
 
137
                                        return TRUE;
 
138
                                }
 
139
                        }
 
140
                        
 
141
                        //Return error
 
142
                        return FALSE;
 
143
                }
 
144
 
 
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, $email) {
 
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', $hash);
 
161
                                $this->db->set('userType', $userType);
 
162
                                $this->db->set('passwdHint', $pwdHint);
 
163
                                $this->db->set('email', $email);
 
164
                                $result = $this->db->insert('Users');
 
165
                                
 
166
                                //Check for my-sql error
 
167
                                if($result) {
 
168
                                        //Return success
 
169
                                        return TRUE;
 
170
                                } 
 
171
                        }
 
172
                        
 
173
                        //Return error
 
174
                        return FALSE;
 
175
                }
 
176
 
 
177
 
 
178
                /*
 
179
                 *      This function removes users from the database.
 
180
                 *  RESTRICTED-LEVEL: Teacher
 
181
                 */
 
182
                public function removeUser($userName) {
 
183
                        //Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
 
184
                        if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
 
185
                                //Query-structure
 
186
                                $this->db->where('userName', $userName);
 
187
                                $result = $this->db->delete('Users');
 
188
                                
 
189
                                //Check for my-sql error
 
190
                                if($result) {
 
191
                                        //Return success
 
192
                                        return TRUE;
 
193
                                }
 
194
                        }
 
195
                        
 
196
                        //Return error
 
197
                        return FALSE;
 
198
                }
 
199
 
 
200
 
 
201
                /*
 
202
                 *      This reset the password for the user.
 
203
                 *  RESTRICTED-LEVEL: Teacher
 
204
                 */
 
205
                public function resetUser($userName) {
 
206
                        //Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
 
207
                        if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
 
208
                                //Check user type
 
209
                                $this->db->select('userName, userType, ssn, email');
 
210
                                $this->db->from('Users');
 
211
                                $this->db->where('userName', $username);
 
212
                                $this->db->limit(1);
 
213
                                $query = $this->db->get();
 
214
                                $result = $query->result();
 
215
                                
 
216
                                //If a matching DB record is found.
 
217
                                if($result) {
 
218
                                        //Prepare new hash depending on user-type
 
219
                                        $newPwdHash = '';
 
220
                                        
 
221
                                        if ($row->userType == 'Student') {
 
222
                                                $newPwdHash = $this->getSaltedHash($row->ssn);
 
223
                                        }
 
224
                                        else if ($row->userType == 'Teacher') {
 
225
                                                //$newPwdHash = $this->getSaltedHash($row->email);
 
226
                                                $newPwdHash = $this->getSaltedHash($row->email);
 
227
                                        }
 
228
                                        
 
229
                                        //Execute reset
 
230
                                        $data = array(
 
231
                                                'passwd' => $newPwdHash,
 
232
                                                'passwdHint' => 'default',
 
233
                                                'firstLogin' => 1
 
234
                                        );
 
235
                                        
 
236
                                        $this->db->where('userName', $userName);
 
237
                                        $this->db->update('Users', $data);
 
238
                                        
 
239
                                        //Return Success!
 
240
                                        return TRUE;
 
241
                                }
 
242
                        }
 
243
                        
 
244
                        //Return error
 
245
                        return FALSE;
 
246
                }
 
247
 
 
248
 
 
249
                /*
 
250
                 *      This parses a user list from ladok and returns an array with users.
 
251
                 *  RESTRICTED-LEVEL: Teacher
 
252
                 */
 
253
                public function parseLadok($string) {
 
254
                        //Check that a user is logged in and has the right privileges (is teacher).
 
255
                        if($this->isLoggedIn() && $this->getUserType() === 'Teacher') {
 
256
                                $userArray = array();
 
257
                                
 
258
                                //Populate array with users from ladok
 
259
                                $ladokUsers = preg_split( '/\r\n|\r|\n/', $string);
 
260
                                
 
261
                                //Trim lines
 
262
                                foreach ($ladokUsers as $key => $value) {
 
263
                                        $ladokUsers[$key] = trim($ladokUsers[$key]);
 
264
                                }
 
265
                                
 
266
                                //Split after last name
 
267
                                foreach ($ladokUsers as $key => $value) {
 
268
                                        $ladokUsers[$key] = explode(',', trim($ladokUsers[$key]));
 
269
                                }
 
270
                                
 
271
                                //Replace whitespaces and tabs with divider.
 
272
                                foreach ($ladokUsers as $key => $value) {
 
273
                                        foreach ($ladokUsers[$key] as $key2 => $value2) {
 
274
                                                $ladokUsers[$key][$key2] = preg_replace('/\s+/', ' ', trim($ladokUsers[$key][$key2]));
 
275
                                        }
 
276
                                }
 
277
                                
 
278
                                //Generate user array
 
279
                                foreach ($ladokUsers as $key => $value) {
 
280
                                        $temp = array(
 
281
                                                'ssn' => substr($ladokUsers[$key][0], 0, 11),
 
282
                                                'lastname' => substr($ladokUsers[$key][0], 12, strlen($ladokUsers[$key][0])),
 
283
                                                'firstname' => substr($ladokUsers[$key][1], 0, stripos($ladokUsers[$key][1], ' ')),
 
284
                                                'email' => substr($ladokUsers[$key][1], (strrpos($ladokUsers[$key][1], ' ') + 1))
 
285
                                        );
 
286
                                        $temp['username'] = substr($temp['email'], 0, (stripos($temp['email'], '@')));
 
287
                                        array_push($userArray, $temp);
 
288
                                }
 
289
                                
 
290
                                //Return parsed user array
 
291
                                return $userArray;
 
292
                        }
 
293
                        
 
294
                        //If not authed
 
295
                        return FALSE;
 
296
                }
 
297
 
 
298
 
 
299
                /*
 
300
                 *      Generates a salted password hash, encrypted with sha1.
 
301
                 *  RESTRICTED-LEVEL: System
 
302
                 */
 
303
                private function getSaltedHash($pwd) {
 
304
                        //Salt = CodeIgniters encryption-key from config
 
305
                        $salt = $this->config->item('encryption_key');
 
306
                        
 
307
                        //Generate SHA1 hash using salt
 
308
                        $hash = sha1($salt.$pwd);
 
309
                        
 
310
                        return $hash;
 
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'),
 
324
                                'success' => $valid
 
325
                        );
 
326
                        
 
327
                        $this->db->insert('logUserLoginAttempts', $data);
 
328
                }
 
329
 
 
330
 
 
331
                /*
 
332
                 *      This function return TRUE if the user is logged in and FALSE otherwise.
 
333
                 *  RESTRICTED-LEVEL: System
 
334
                 */
 
335
                public function isLoggedIn() {
 
336
                        if ($this->session->userdata('authenticated')) {
 
337
                                return TRUE;
 
338
                        }
 
339
                        else{
 
340
                                return FALSE;
 
341
                        }
 
342
                }
 
343
 
 
344
 
 
345
                /*
 
346
                 *      This function returns the users type (or FALSE if user isn't logged in).
 
347
                 *  RESTRICTED-LEVEL: System
 
348
                 */
 
349
                public function getUserType() {
 
350
                        if($this->isLoggedIn()) {
 
351
                                $temp = $this->session->userdata('authenticated');
 
352
                                return $temp['usertype'];
 
353
                        }
 
354
                        
 
355
                        return FALSE;
 
356
                }
 
357
 
 
358
 
 
359
                /*
 
360
                 *      This function returns a boolean containing information if it is the first login.
 
361
                 *  RESTRICTED-LEVEL: System
 
362
                 */
 
363
                public function isFirstLogin() {
 
364
                        if($this->isLoggedIn()) {
 
365
                                $temp = $this->session->userdata('authenticated');
 
366
                                if ($temp['firstLogin'] == 1) {
 
367
                                        return TRUE;
 
368
                                }
 
369
                        }
 
370
                        
 
371
                        return FALSE;
 
372
                }
 
373
 
 
374
 
 
375
                /*
 
376
                 *      This function returns the username (or FALSE if user isn't logged in).
 
377
                 *  RESTRICTED-LEVEL: System
 
378
                 */ 
 
379
                public function getUserName() {
 
380
                        if($this->isLoggedIn()) {
 
381
                                $temp = $this->session->userdata('authenticated');
 
382
                                return $temp['username'];
 
383
                        }
 
384
                        
 
385
                        return FALSE;
 
386
                }
 
387
        }
 
388
        
 
389
?>
 
 
b'\\ No newline at end of file'