46
46
* This function logs the user in (returns FALSE on fail).
47
47
* RESTRICTED-LEVEL: None
49
public function login($username, $password)
49
public function login($username, $password) {
51
50
//Generate a salted hash
52
51
$hash = $this->getSaltedHash($password);
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
// 'firstLogin' => $row->firstLogin
79
79
$this->session->set_userdata('authenticated', $userDetails);
81
//Log attempt as valid
82
$this->logLogin($username, 1);
88
//Log attempt as invalid
89
$this->logLogin($username, 0);
91
97
* This function logs the user out.
92
98
* RESTRICTED-LEVEL: Self
94
public function logout() {
100
public function logout() {
95
101
//Unset session data
96
102
$this->session->unset_userdata('authenticated');
101
107
* This function changes the users password.
102
108
* RESTRICTED-LEVEL: Self
104
110
public function changePassword($pwdOld, $pwdNew, $pwdHint) {
105
111
//Check that a user is logged in.
106
112
if($this->isLoggedIn()) {
172
178
* This function removes users from the database.
173
179
* RESTRICTED-LEVEL: Teacher
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');
182
188
//Check for my-sql error
201
* This reset the password for the user.
202
* RESTRICTED-LEVEL: Teacher
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) {
208
$this->db->select('userName, userType, ssn, email');
209
$this->db->from('Users');
210
$this->db->where('userName', $username);
212
$query = $this->db->get();
213
$result = $query->result();
215
//If a matching DB record is found.
217
//Prepare new hash depending on user-type
220
if ($row->userType == 'Student') {
221
$newPwdHash = $this->getSaltedHash($row->ssn);
223
else if ($row->userType == 'Teacher') {
224
//$newPwdHash = $this->getSaltedHash($row->email);
225
$newPwdHash = $this->getSaltedHash($row->email);
230
'passwd' => $newPwdHash,
231
'passwdHint' => 'default',
235
$this->db->where('userName', $userName);
236
$this->db->update('Users', $data);
249
* This parser a user list from ladok.
250
* RESTRICTED-LEVEL: Teacher
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) {
195
260
* Generates a salted password hash, encrypted with sha1.
261
* RESTRICTED-LEVEL: System
197
263
private function getSaltedHash($pwd) {
198
264
//Salt = CodeIgniters encryption-key from config
199
265
$salt = $this->config->item('encryption_key');
275
* Log the login attempt.
276
* RESTRICTED-LEVEL: System
278
private function logLogin($userName, $valid) {
280
'userName' => $userName,
281
'userAgent' => $this->session->userdata('user_agent'),
282
'userIP' => $this->session->userdata('ip_address'),
283
'browserID' => $this->session->userdata('session_id'),
287
$this->db->insert('logUserLoginAttempts', $data);
209
292
* This function return TRUE if the user is logged in and FALSE otherwise.
210
293
* RESTRICTED-LEVEL: System
212
public function isLoggedIn() {
295
public function isLoggedIn() {
213
296
if ($this->session->userdata('authenticated')) {
222
306
* This function returns the users type (or FALSE if user isn't logged in).
223
307
* RESTRICTED-LEVEL: System
225
public function getUserType() {
309
public function getUserType() {
226
310
if($this->isLoggedIn()) {
227
311
$temp = $this->session->userdata('authenticated');
228
312
return $temp['usertype'];
320
* This function returns a boolean containing information if it is the first login.
321
* RESTRICTED-LEVEL: System
323
public function isFirstLogin() {
324
if($this->isLoggedIn()) {
325
$temp = $this->session->userdata('authenticated');
326
if ($temp['firstLogin'] == 1) {
236
336
* This function returns the username (or FALSE if user isn't logged in).
237
337
* RESTRICTED-LEVEL: System
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'];