6
6
function __construct() {
7
7
parent::__construct();
9
//Load required libraries
9
//Load required libraries and drivers
10
10
$this->load->database();
11
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;
15
46
* This function logs the user in (returns FALSE on fail).
47
* RESTRICTED-LEVEL: None
17
49
public function login($username, $password)
68
* This function return TRUE if the user is logged in and FALSE otherwise.
70
public function isLoggedIn() {
71
if ($this->session->userdata('authenticated')) {
80
* This function returns the users type (or FALSE if user isn't logged in).
82
public function getUserType() {
83
if($this->isLoggedIn()) {
84
$temp = $this->session->userdata('authenticated');
85
return $temp['usertype'];
93
* This function returns the username (or FALSE if user isn't logged in).
95
public function getUserName() {
96
if($this->isLoggedIn()) {
97
$temp = $this->session->userdata('authenticated');
98
return $temp['username'];
106
* This function returns the users password hint (or FALSE if user isn't logged in).
108
public function getPasswordHint($user) {
112
$this->db->select('passwdHint');
113
$this->db->from('Users');
114
$this->db->where('userName', $user);
118
$query = $this->db->get();
119
$result = $query->result();
121
//If a matching DB record is found.
123
foreach($result as $row) {
124
$hint = $row->passwdHint;
134
* This function registers user into the database.
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);
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');
151
//Check for my-sql error
164
101
* This function changes the users password.
102
* RESTRICTED-LEVEL: Self
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);
111
//Validate input with database
174
112
$this->db->select('userName');
175
113
$this->db->from('Users');
176
114
$this->db->where('userName', $user);
141
* This function registers user into the database.
142
* RESTRICTED-LEVEL: Teacher
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);
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');
159
//Check for my-sql error
203
172
* This function removes users from the database.
173
* RESTRICTED-LEVEL: Teacher
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.
225
* Generates a password hash using a user-unique salt.
195
* Generates a salted password hash, encrypted with sha1.
227
private function getSaltedHash($pwd)
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
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');
242
201
//Generate SHA1 hash using salt
243
202
$hash = sha1($salt.$pwd);
209
* This function return TRUE if the user is logged in and FALSE otherwise.
210
* RESTRICTED-LEVEL: System
212
public function isLoggedIn() {
213
if ($this->session->userdata('authenticated')) {
222
* This function returns the users type (or FALSE if user isn't logged in).
223
* RESTRICTED-LEVEL: System
225
public function getUserType() {
226
if($this->isLoggedIn()) {
227
$temp = $this->session->userdata('authenticated');
228
return $temp['usertype'];
236
* This function returns the username (or FALSE if user isn't logged in).
237
* RESTRICTED-LEVEL: System
239
public function getUserName() {
240
if($this->isLoggedIn()) {
241
$temp = $this->session->userdata('authenticated');
242
return $temp['username'];
b'\\ No newline at end of file'