/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: Simon Bergöö
  • Date: 2013-05-15 12:41:14 UTC
  • mfrom: (66 lenasys_b)
  • mto: This revision was merged to the branch mainline in revision 67.
  • Revision ID: a11simbe@student.his.se-20130515124114-d9fctkf2rkqq6xg6
Merged from trunk, statred working on the popups model.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
                        
30
30
                        //If a matching DB record is found.
31
31
                        if($result) {
32
 
                            foreach($result as $row) {
33
 
                                $hint = $row->passwdHint;
34
 
                                
35
 
                                //Return hint
36
 
                                return $hint;
37
 
                            }
 
32
                                foreach($result as $row) {
 
33
                                        $hint = $row->passwdHint;
 
34
                                        
 
35
                                        //Return hint
 
36
                                        return $hint;
 
37
                                }
38
38
                        }
39
39
                        
40
40
                        //No such user
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
        
54
53
                        //Query-structure
55
 
                        $this->db->select('userName, name, passwd, userType, ssn');
 
54
                        $this->db->select('userName, name, passwd, userType, ssn'); // Tog bort firstLogin här.
56
55
                        $this->db->from('Users');
57
56
                        $this->db->where('userName', $username);
58
57
                        $this->db->where('passwd', $hash);
71
70
                                                'username' => $row->userName,
72
71
                                                'name' => $row->name,
73
72
                                                'usertype' => $row->userType,
74
 
                                                'ssn' => $row->ssn
 
73
                                                'ssn' => $row->ssn,
 
74
                                        //      'firstLogin' => $row->firstLogin
75
75
                                        );
76
76
                                }
77
77
                                
78
78
                                //Set session data
79
79
                                $this->session->set_userdata('authenticated', $userDetails);
80
80
                                
 
81
                                //Log attempt as valid
 
82
                                $this->logLogin($username, 1);
 
83
                                
81
84
                                //Return success
82
85
                                return TRUE;
83
86
                        }
84
87
 
 
88
                        //Log attempt as invalid
 
89
                        $this->logLogin($username, 0);
 
90
 
85
91
                        //Return fail
86
 
                        return FALSE;   
 
92
                        return FALSE;
87
93
                }
88
94
 
89
95
 
90
96
                /*
91
97
                 *      This function logs the user out.
92
98
                 *  RESTRICTED-LEVEL: Self
93
 
                 */ 
94
 
                public function logout() {              
 
99
                 */
 
100
                public function logout() {
95
101
                        //Unset session data
96
102
                        $this->session->unset_userdata('authenticated');
97
103
                }
98
 
                
 
104
 
99
105
 
100
106
                /*
101
107
                 *      This function changes the users password.
102
108
                 *  RESTRICTED-LEVEL: Self
103
 
                 */ 
 
109
                 */
104
110
                public function changePassword($pwdOld, $pwdNew, $pwdHint) {
105
111
                        //Check that a user is logged in.
106
112
                        if($this->isLoggedIn()) {
128
134
                                        $this->db->update('Users', $data);
129
135
                                        
130
136
                                        //Return Success!
131
 
                                        return TRUE;                                                    
 
137
                                        return TRUE;
132
138
                                }
133
139
                        }
134
140
                        
160
166
                                if($result) {
161
167
                                        //Return success
162
168
                                        return TRUE;
163
 
                                } 
 
169
                                }
164
170
                        }
165
171
                        
166
172
                        //Return error
167
173
                        return FALSE;
168
174
                }
169
 
                
170
 
                
 
175
 
 
176
 
171
177
                /*
172
178
                 *      This function removes users from the database.
173
179
                 *  RESTRICTED-LEVEL: Teacher
174
 
                 */ 
 
180
                 */
175
181
                public function removeUser($userName) {
176
182
                        //Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
177
183
                        if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
178
184
                                //Query-structure
179
185
                                $this->db->where('userName', $userName);
180
 
                                $result = $this->db->delete('Users');            
 
186
                                $result = $this->db->delete('Users');
181
187
                                
182
188
                                //Check for my-sql error
183
189
                                if($result) {
184
190
                                        //Return success
185
191
                                        return TRUE;
186
 
                                } 
187
 
                        }
188
 
                        
189
 
                        //Return error
190
 
                        return FALSE;
191
 
                }
192
 
                
 
192
                                }
 
193
                        }
 
194
                        
 
195
                        //Return error
 
196
                        return FALSE;
 
197
                }
 
198
 
 
199
 
 
200
                /*
 
201
                 *      This reset the password for the user.
 
202
                 *  RESTRICTED-LEVEL: Teacher
 
203
                 */
 
204
                public function resetUser($userName) {
 
205
                        //Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
 
206
                        if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
 
207
                                //Check user type
 
208
                                $this->db->select('userName, userType, ssn, email');
 
209
                                $this->db->from('Users');
 
210
                                $this->db->where('userName', $username);
 
211
                                $this->db->limit(1);
 
212
                                $query = $this->db->get();
 
213
                                $result = $query->result();
 
214
                                
 
215
                                //If a matching DB record is found.
 
216
                                if($result) {
 
217
                                        //Prepare new hash depending on user-type
 
218
                                        $newPwdHash = '';
 
219
                                        
 
220
                                        if ($row->userType == 'Student') {
 
221
                                                $newPwdHash = $this->getSaltedHash($row->ssn);
 
222
                                        }
 
223
                                        else if ($row->userType == 'Teacher') {
 
224
                                                //$newPwdHash = $this->getSaltedHash($row->email);
 
225
                                                $newPwdHash = $this->getSaltedHash($row->email);
 
226
                                        }
 
227
                                        
 
228
                                        //Execute reset
 
229
                                        $data = array(
 
230
                                                'passwd' => $newPwdHash,
 
231
                                                'passwdHint' => 'default',
 
232
                                                'firstLogin' => 1
 
233
                                        );
 
234
                                        
 
235
                                        $this->db->where('userName', $userName);
 
236
                                        $this->db->update('Users', $data);
 
237
                                        
 
238
                                        //Return Success!
 
239
                                        return TRUE;
 
240
                                }
 
241
                        }
 
242
                        
 
243
                        //Return error
 
244
                        return FALSE;
 
245
                }
 
246
 
 
247
 
 
248
                /*
 
249
                 *      This parser a user list from ladok.
 
250
                 *  RESTRICTED-LEVEL: Teacher
 
251
                 */
 
252
                public function parseLadok() {
 
253
                        //Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
 
254
                        if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
 
255
                        }
 
256
                }
 
257
 
193
258
 
194
259
                /*
195
260
                 *      Generates a salted password hash, encrypted with sha1.
196
 
                 */             
 
261
                 *  RESTRICTED-LEVEL: System
 
262
                 */
197
263
                private function getSaltedHash($pwd) {
198
264
                        //Salt = CodeIgniters encryption-key from config
199
265
                        $salt = $this->config->item('encryption_key');
203
269
                        
204
270
                        return $hash;
205
271
                }
206
 
                
207
 
                
 
272
 
 
273
 
 
274
                /*
 
275
                 *      Log the login attempt.
 
276
                 *  RESTRICTED-LEVEL: System
 
277
                 */
 
278
                private function logLogin($userName, $valid) {
 
279
                        $data = array(
 
280
                                'userName' => $userName,
 
281
                                'userAgent' => $this->session->userdata('user_agent'),
 
282
                                'userIP' => $this->session->userdata('ip_address'),
 
283
                                'browserID' => $this->session->userdata('session_id'),
 
284
                                'success' => $valid
 
285
                        );
 
286
                        
 
287
                        $this->db->insert('logUserLoginAttempts', $data);
 
288
                }
 
289
 
 
290
 
208
291
                /*
209
292
                 *      This function return TRUE if the user is logged in and FALSE otherwise.
210
293
                 *  RESTRICTED-LEVEL: System
211
 
                 */ 
212
 
                public function isLoggedIn() {  
 
294
                 */
 
295
                public function isLoggedIn() {
213
296
                        if ($this->session->userdata('authenticated')) {
214
297
                                return TRUE;
215
298
                        }
216
 
                        
217
 
                        return FALSE;
 
299
                        else{
 
300
                                return FALSE;
 
301
                        }
218
302
                }
219
 
                
220
 
                
 
303
 
 
304
 
221
305
                /*
222
306
                 *      This function returns the users type (or FALSE if user isn't logged in).
223
307
                 *  RESTRICTED-LEVEL: System
224
 
                 */ 
225
 
                public function getUserType() { 
 
308
                 */
 
309
                public function getUserType() {
226
310
                        if($this->isLoggedIn()) {
227
311
                                $temp = $this->session->userdata('authenticated');
228
312
                                return $temp['usertype'];
230
314
                        
231
315
                        return FALSE;
232
316
                }
233
 
                
234
 
                
 
317
 
 
318
 
 
319
                /*
 
320
                 *      This function returns a boolean containing information if it is the first login.
 
321
                 *  RESTRICTED-LEVEL: System
 
322
                 */
 
323
                public function isFirstLogin() {
 
324
                        if($this->isLoggedIn()) {
 
325
                                $temp = $this->session->userdata('authenticated');
 
326
                                if ($temp['firstLogin'] == 1) {
 
327
                                        return TRUE;
 
328
                                }
 
329
                        }
 
330
                        
 
331
                        return FALSE;
 
332
                }
 
333
 
 
334
 
235
335
                /*
236
336
                 *      This function returns the username (or FALSE if user isn't logged in).
237
337
                 *  RESTRICTED-LEVEL: System
238
338
                 */ 
239
 
                public function getUserName() { 
 
339
                public function getUserName() {
240
340
                        if($this->isLoggedIn()) {
241
341
                                $temp = $this->session->userdata('authenticated');
242
342
                                return $temp['username'];
245
345
                        return FALSE;
246
346
                }
247
347
        }
 
348
        
248
349
?>
 
 
b'\\ No newline at end of file'