/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-07 08:05:14 UTC
  • mfrom: (53.2.2 lenasys)
  • Revision ID: gustav.hartvigsson@gmail.com-20130507080514-u53zto672m0t19wl
merded the new salting stuff for the user passwords and stuff...

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
 
 
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
 
14
45
                /*
15
46
                 *      This function logs the user in (returns FALSE on fail).
 
47
                 *  RESTRICTED-LEVEL: None
16
48
                 */ 
17
49
                public function login($username, $password)
18
50
                {                       
57
89
 
58
90
                /*
59
91
                 *      This function logs the user out.
 
92
                 *  RESTRICTED-LEVEL: Self
60
93
                 */ 
61
94
                public function logout() {              
62
95
                        //Unset session data
65
98
                
66
99
 
67
100
                /*
68
 
                 *      This function return TRUE if the user is logged in and FALSE otherwise.
69
 
                 */ 
70
 
                public function isLoggedIn() {  
71
 
                        if ($this->session->userdata('authenticated')) {
72
 
                                return TRUE;
73
 
                        }
74
 
                        
75
 
                        return FALSE;
76
 
                }
77
 
                
78
 
                
79
 
                /*
80
 
                 *      This function returns the users type (or FALSE if user isn't logged in).
81
 
                 */ 
82
 
                public function getUserType() { 
83
 
                        if($this->isLoggedIn()) {
84
 
                                $temp = $this->session->userdata('authenticated');
85
 
                                return $temp['usertype'];
86
 
                        }
87
 
                        
88
 
                        return FALSE;
89
 
                }
90
 
                
91
 
                
92
 
                /*
93
 
                 *      This function returns the username (or FALSE if user isn't logged in).
94
 
                 */ 
95
 
                public function getUserName() { 
96
 
                        if($this->isLoggedIn()) {
97
 
                                $temp = $this->session->userdata('authenticated');
98
 
                                return $temp['username'];
99
 
                        }
100
 
                        
101
 
                        return FALSE;
102
 
                }
103
 
                
104
 
                
105
 
                /*
106
 
                 *      This function returns the users password hint (or FALSE if user isn't logged in).
107
 
                 */ 
108
 
                public function getPasswordHint($user) {
109
 
                        $hint = '';
110
 
 
111
 
                        //Query-structure
112
 
                        $this->db->select('passwdHint');
113
 
                        $this->db->from('Users');
114
 
                        $this->db->where('userName', $user);
115
 
                        $this->db->limit(1);
116
 
                        
117
 
                        //Execute query
118
 
                        $query = $this->db->get();
119
 
                        $result = $query->result();
120
 
                        
121
 
                        //If a matching DB record is found.
122
 
                        if($result) {
123
 
                            foreach($result as $row) {
124
 
                                $hint = $row->passwdHint;
125
 
                                return $hint;
126
 
                            }
127
 
                        }
128
 
                        
129
 
                        return FALSE;
130
 
                }
131
 
                
132
 
                
133
 
                /*
134
 
                 *      This function registers user into the database.
135
 
                 */
136
 
                public function addUser($userName, $name, $ssn, $password, $userType, $pwdHint) {
137
 
                        //Check that a user is logged in and has the right privileges (is teacher)
138
 
                        if($this->isLoggedIn() && $this->getUserType() === 'Teacher') {
139
 
                                //Generate a salted hash
140
 
                                $hash = $this->getSaltedHash($password);
141
 
                                
142
 
                                //Query-structure (All values are escaped automatically by codeigninte, producing safer queries.)
143
 
                                $this->db->set('userName', $userName);
144
 
                                $this->db->set('name', $name);
145
 
                                $this->db->set('ssn', $ssn);
146
 
                                $this->db->set('passwd', $password);
147
 
                                $this->db->set('userType', $userType);
148
 
                                $this->db->set('passwdHint', $pwdHint);
149
 
                                $result = $this->db->insert('Users');
150
 
                                
151
 
                                //Check for my-sql error
152
 
                                if($result) {
153
 
                                        //Return success
154
 
                                        return TRUE;
155
 
                                } 
156
 
                        }
157
 
                        
158
 
                        //Return error
159
 
                        return FALSE;
160
 
                }
161
 
                
162
 
 
163
 
                /*
164
101
                 *      This function changes the users password.
 
102
                 *  RESTRICTED-LEVEL: Self
165
103
                 */ 
166
104
                public function changePassword($pwdOld, $pwdNew, $pwdHint) {
167
105
                        //Check that a user is logged in.
170
108
                                $oldHash = $this->getSaltedHash($pwdOld);
171
109
                                $newHash = $this->getSaltedHash($pwdNew);
172
110
                                
173
 
                                //Validate
 
111
                                //Validate input with database
174
112
                                $this->db->select('userName');
175
113
                                $this->db->from('Users');
176
114
                                $this->db->where('userName', $user);
179
117
                                $query = $this->db->get();
180
118
                                $result = $query->result();
181
119
                                
182
 
                                //If a matching DB record is found, update database with new info.
 
120
                                //If a matching DB record is found, update database.
183
121
                                if($result) {
184
122
                                        $data = array(
185
123
                                                'passwd' => $newHash,
198
136
                        return FALSE;
199
137
                }
200
138
 
 
139
 
 
140
                /*
 
141
                 *      This function registers user into the database.
 
142
                 *  RESTRICTED-LEVEL: Teacher
 
143
                 */
 
144
                public function addUser($userName, $name, $ssn, $password, $userType, $pwdHint) {
 
145
                        //Check that a user is logged in and has the right privileges (is teacher)
 
146
                        if($this->isLoggedIn() && $this->getUserType() === 'Teacher') {
 
147
                                //Generate a salted hash
 
148
                                $hash = $this->getSaltedHash($password);
 
149
                                
 
150
                                //Query-structure (All values are escaped automatically by codeigninte, producing safer queries.)
 
151
                                $this->db->set('userName', $userName);
 
152
                                $this->db->set('name', $name);
 
153
                                $this->db->set('ssn', $ssn);
 
154
                                $this->db->set('passwd', $password);
 
155
                                $this->db->set('userType', $userType);
 
156
                                $this->db->set('passwdHint', $pwdHint);
 
157
                                $result = $this->db->insert('Users');
 
158
                                
 
159
                                //Check for my-sql error
 
160
                                if($result) {
 
161
                                        //Return success
 
162
                                        return TRUE;
 
163
                                } 
 
164
                        }
 
165
                        
 
166
                        //Return error
 
167
                        return FALSE;
 
168
                }
 
169
                
201
170
                
202
171
                /*
203
172
                 *      This function removes users from the database.
 
173
                 *  RESTRICTED-LEVEL: Teacher
204
174
                 */ 
205
175
                public function removeUser($userName) {
206
176
                        //Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
222
192
                
223
193
 
224
194
                /*
225
 
                 *      Generates a password hash using a user-unique salt.
 
195
                 *      Generates a salted password hash, encrypted with sha1.
226
196
                 */             
227
 
                private function getSaltedHash($pwd)
228
 
                {       
229
 
                        /* Salt algorithm:
230
 
                         * Step 1: Get encryption key
231
 
                         * Step 2: Salt is set to password (reversed)
232
 
                         * Step 3: Salt is encrypted with MD5
233
 
                         * Step 4: Salt is reversed (again)
234
 
                         * Step 5: Salt is encrypted with SHA1
235
 
                         */
236
 
                        
237
 
                        $salt = '';
238
 
                        
239
 
                        //UNCOMMENT TO ACTIVATE SALT LATER IN DEVELOPMENT!!!
240
 
                        //$salt = sha1(strrev(md5(strrev($this->config->item('encryption_key')))));
 
197
                private function getSaltedHash($pwd) {
 
198
                        //Salt = CodeIgniters encryption-key from config
 
199
                        $salt = $this->config->item('encryption_key');
241
200
                        
242
201
                        //Generate SHA1 hash using salt
243
202
                        $hash = sha1($salt.$pwd);
244
203
                        
245
204
                        return $hash;
246
205
                }
 
206
                
 
207
                
 
208
                /*
 
209
                 *      This function return TRUE if the user is logged in and FALSE otherwise.
 
210
                 *  RESTRICTED-LEVEL: System
 
211
                 */ 
 
212
                public function isLoggedIn() {  
 
213
                        if ($this->session->userdata('authenticated')) {
 
214
                                return TRUE;
 
215
                        }
 
216
                        
 
217
                        return FALSE;
 
218
                }
 
219
                
 
220
                
 
221
                /*
 
222
                 *      This function returns the users type (or FALSE if user isn't logged in).
 
223
                 *  RESTRICTED-LEVEL: System
 
224
                 */ 
 
225
                public function getUserType() { 
 
226
                        if($this->isLoggedIn()) {
 
227
                                $temp = $this->session->userdata('authenticated');
 
228
                                return $temp['usertype'];
 
229
                        }
 
230
                        
 
231
                        return FALSE;
 
232
                }
 
233
                
 
234
                
 
235
                /*
 
236
                 *      This function returns the username (or FALSE if user isn't logged in).
 
237
                 *  RESTRICTED-LEVEL: System
 
238
                 */ 
 
239
                public function getUserName() { 
 
240
                        if($this->isLoggedIn()) {
 
241
                                $temp = $this->session->userdata('authenticated');
 
242
                                return $temp['username'];
 
243
                        }
 
244
                        
 
245
                        return FALSE;
 
246
                }
247
247
        }
248
248
?>
 
 
b'\\ No newline at end of file'