2
Class User extends CI_Model {
6
function __construct() {
9
//Load required libraries and drivers
10
$this->load->database();
11
$this->load->library('session');
16
* This function returns the users password hint (or FALSE if user isn't logged in).
17
* RESTRICTED-LEVEL: None
19
public function getPasswordHint($user) {
21
$this->db->select('passwdHint');
22
$this->db->from('Users');
23
$this->db->where('userName', $user);
27
$query = $this->db->get();
28
$result = $query->result();
30
//If a matching DB record is found.
32
foreach($result as $row) {
33
$hint = $row->passwdHint;
46
* This function logs the user in (returns FALSE on fail).
47
* RESTRICTED-LEVEL: None
49
public function login($username, $password) {
50
//Generate a salted hash
51
$hash = $this->getSaltedHash($password);
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);
61
$query = $this->db->get();
62
$result = $query->result();
64
//If a matching DB record is found.
66
//Prepare session data
67
$userDetails = array();
68
foreach($result as $row) {
70
'username' => $row->userName,
72
'usertype' => $row->userType,
74
// 'firstLogin' => $row->firstLogin
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);
97
* This function logs the user out.
98
* RESTRICTED-LEVEL: Self
100
public function logout() {
102
$this->session->unset_userdata('authenticated');
107
* This function changes the users password.
108
* RESTRICTED-LEVEL: Self
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);
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);
123
$query = $this->db->get();
124
$result = $query->result();
126
//If a matching DB record is found, update database.
129
'passwd' => $newHash,
130
'passwdHint' => $pwdHint
133
$this->db->where('userName', $user);
134
$this->db->update('Users', $data);
147
* This function registers user into the database.
148
* RESTRICTED-LEVEL: Teacher
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);
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');
166
//Check for my-sql error
179
* This function removes users from the database.
180
* RESTRICTED-LEVEL: Teacher
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) {
186
$this->db->where('userName', $userName);
187
$result = $this->db->delete('Users');
189
//Check for my-sql error
202
* This reset the password for the user.
203
* RESTRICTED-LEVEL: Teacher
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) {
209
$this->db->select('userName, userType, ssn, email');
210
$this->db->from('Users');
211
$this->db->where('userName', $username);
213
$query = $this->db->get();
214
$result = $query->result();
216
//If a matching DB record is found.
218
//Prepare new hash depending on user-type
221
if ($row->userType == 'Student') {
222
$newPwdHash = $this->getSaltedHash($row->ssn);
224
else if ($row->userType == 'Teacher') {
225
//$newPwdHash = $this->getSaltedHash($row->email);
226
$newPwdHash = $this->getSaltedHash($row->email);
231
'passwd' => $newPwdHash,
232
'passwdHint' => 'default',
236
$this->db->where('userName', $userName);
237
$this->db->update('Users', $data);
250
* This parses a user list from ladok and returns an array with users.
251
* RESTRICTED-LEVEL: Teacher
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();
258
//Populate array with users from ladok
259
$ladokUsers = preg_split( '/\r\n|\r|\n/', $string);
262
foreach ($ladokUsers as $key => $value) {
263
$ladokUsers[$key] = trim($ladokUsers[$key]);
266
//Split after last name
267
foreach ($ladokUsers as $key => $value) {
268
$ladokUsers[$key] = explode(',', trim($ladokUsers[$key]));
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]));
278
//Generate user array
279
foreach ($ladokUsers as $key => $value) {
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))
286
$temp['username'] = substr($temp['email'], 0, (stripos($temp['email'], '@')));
287
array_push($userArray, $temp);
290
//Return parsed user array
300
* Generates a salted password hash, encrypted with sha1.
301
* RESTRICTED-LEVEL: System
303
private function getSaltedHash($pwd) {
304
//Salt = CodeIgniters encryption-key from config
305
$salt = $this->config->item('encryption_key');
307
//Generate SHA1 hash using salt
308
$hash = sha1($salt.$pwd);
315
* Log the login attempt.
316
* RESTRICTED-LEVEL: System
318
private function logLogin($userName, $valid) {
320
'userName' => $userName,
321
'userAgent' => $this->session->userdata('user_agent'),
322
'userIP' => $this->session->userdata('ip_address'),
323
'browserID' => $this->session->userdata('session_id'),
327
$this->db->insert('logUserLoginAttempts', $data);
332
* This function return TRUE if the user is logged in and FALSE otherwise.
333
* RESTRICTED-LEVEL: System
335
public function isLoggedIn() {
336
if ($this->session->userdata('authenticated')) {
346
* This function returns the users type (or FALSE if user isn't logged in).
347
* RESTRICTED-LEVEL: System
349
public function getUserType() {
350
if($this->isLoggedIn()) {
351
$temp = $this->session->userdata('authenticated');
352
return $temp['usertype'];
360
* This function returns a boolean containing information if it is the first login.
361
* RESTRICTED-LEVEL: System
363
public function isFirstLogin() {
364
if($this->isLoggedIn()) {
365
$temp = $this->session->userdata('authenticated');
366
if ($temp['firstLogin'] == 1) {
376
* This function returns the username (or FALSE if user isn't logged in).
377
* RESTRICTED-LEVEL: System
379
public function getUserName() {
380
if($this->isLoggedIn()) {
381
$temp = $this->session->userdata('authenticated');
382
return $temp['username'];
b'\\ No newline at end of file'