30
30
//If a matching DB record is found.
32
foreach($result as $row) {
33
$hint = $row->passwdHint;
32
foreach($result as $row) {
33
$hint = $row->passwdHint;
51
51
$hash = $this->getSaltedHash($password);
54
$this->db->select('userName, name, passwd, userType, ssn');
54
$this->db->select('userName, name, passwd, userType, ssn'); // Tog bort firstLogin här.
55
55
$this->db->from('Users');
56
56
$this->db->where('userName', $username);
57
57
$this->db->where('passwd', $hash);
96
97
* This function logs the user out.
97
98
* RESTRICTED-LEVEL: Self
99
public function logout() {
100
public function logout() {
100
101
//Unset session data
101
102
$this->session->unset_userdata('authenticated');
106
107
* This function changes the users password.
107
108
* RESTRICTED-LEVEL: Self
109
110
public function changePassword($pwdOld, $pwdNew, $pwdHint) {
110
111
//Check that a user is logged in.
111
112
if($this->isLoggedIn()) {
177
178
* This function removes users from the database.
178
179
* RESTRICTED-LEVEL: Teacher
180
181
public function removeUser($userName) {
181
182
//Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
182
183
if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
183
184
//Query-structure
184
185
$this->db->where('userName', $userName);
185
$result = $this->db->delete('Users');
186
$result = $this->db->delete('Users');
187
188
//Check for my-sql error
200
201
* This reset the password for the user.
201
202
* RESTRICTED-LEVEL: Teacher
247
* This parses a user list from ladok and returns an array with users.
249
* This parser a user list from ladok.
248
250
* RESTRICTED-LEVEL: Teacher
250
public function parseLadok($string) {
251
//Check that a user is logged in and has the right privileges (is teacher).
252
if($this->isLoggedIn() && $this->getUserType() === 'Teacher') {
253
$userArray = array();
255
//Populate array with users from ladok
256
$ladokUsers = preg_split( '/\r\n|\r|\n/', $string);
259
foreach ($ladokUsers as $key => &$value) {
260
$ladokUsers[$key] = trim($ladokUsers[$key]);
263
//Split after last name
264
foreach ($ladokUsers as $key => &$value) {
265
$ladokUsers[$key] = explode(',', trim($ladokUsers[$key]));
268
//Replace whitespaces and tabs with divider.
269
foreach ($ladokUsers as $key => &$value) {
270
foreach ($ladokUsers[$key] as $key2 => &$value2) {
271
$ladokUsers[$key][$key2] = preg_replace('/\s+/', ' ', trim($ladokUsers[$key][$key2]));
275
//Explode on whitespace on second split
276
foreach ($ladokUsers as $key => &$value) {
277
$ladokUsers[$key][1] = explode(' ', trim($ladokUsers[$key][1]));
280
//Generate user array
282
foreach ($ladokUsers as $key => $value) {
283
$userArray[$i]['ssn'] = substr($ladokUsers[$key][0], 0, 11);
284
$userArray[$i]['lastname'] = substr($ladokUsers[$key][0], 12, strlen($ladokUsers[$key][0]));
285
$userArray[$i]['firstname'] = $ladokUsers[$key][1][0];
286
$userArray[$i]['email'] = $ladokUsers[$key][1][3];
290
//Return parsed user array
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) {
300
260
* Generates a salted password hash, encrypted with sha1.
320
280
'userName' => $userName,
321
281
'userAgent' => $this->session->userdata('user_agent'),
322
282
'userIP' => $this->session->userdata('ip_address'),
323
'browserID' => $this->session->userdata('session_id'), //TODO: change later?
283
'browserID' => $this->session->userdata('session_id'),
324
284
'success' => $valid
327
287
$this->db->insert('logUserLoginAttempts', $data);
332
292
* This function return TRUE if the user is logged in and FALSE otherwise.
333
293
* RESTRICTED-LEVEL: System
335
public function isLoggedIn() {
295
public function isLoggedIn() {
336
296
if ($this->session->userdata('authenticated')) {
345
306
* This function returns the users type (or FALSE if user isn't logged in).
346
307
* RESTRICTED-LEVEL: System
348
public function getUserType() {
309
public function getUserType() {
349
310
if($this->isLoggedIn()) {
350
311
$temp = $this->session->userdata('authenticated');
351
312
return $temp['usertype'];
375
336
* This function returns the username (or FALSE if user isn't logged in).
376
337
* RESTRICTED-LEVEL: System
378
public function getUserName() {
339
public function getUserName() {
379
340
if($this->isLoggedIn()) {
380
341
$temp = $this->session->userdata('authenticated');
381
342
return $temp['username'];