71
70
public function isLoggedIn() {
72
71
if ($this->session->userdata('authenticated')) {
80
//Generates a password hash using a user-unique salt.
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
* This function changes the users password.
166
public function changePassword($pwdOld, $pwdNew, $pwdHint) {
167
//Check that a user is logged in.
168
if($this->isLoggedIn()) {
169
$user = $this->getUserName();
170
$oldHash = $this->getSaltedHash($pwdOld);
171
$newHash = $this->getSaltedHash($pwdNew);
174
$this->db->select('userName');
175
$this->db->from('Users');
176
$this->db->where('userName', $user);
177
$this->db->where('passwd', $oldHash);
179
$query = $this->db->get();
180
$result = $query->result();
182
//If a matching DB record is found, update database with new info.
185
'passwd' => $newHash,
186
'passwdHint' => $pwdHint
189
$this->db->where('userName', $user);
190
$this->db->update('Users', $data);
203
* This function removes users from the database.
205
public function removeUser($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->where('userName', $userName);
210
$result = $this->db->delete('Users');
212
//Check for my-sql error
225
* Generates a password hash using a user-unique salt.
81
227
private function getSaltedHash($pwd)
83
229
/* Salt algorithm: