200
* This reset the password for the user.
201
* RESTRICTED-LEVEL: Teacher
203
public function resetUser($userName) {
204
//Check that a user is logged in, has the right privileges (is teacher) and not is the users own username.
205
if($this->isLoggedIn() && $this->getUserType() === 'Teacher' && $this->getUserName() != $userName) {
207
$this->db->select('userName, userType, ssn, email');
208
$this->db->from('Users');
209
$this->db->where('userName', $username);
211
$query = $this->db->get();
212
$result = $query->result();
214
//If a matching DB record is found.
216
//Prepare new hash depending on user-type
219
if ($row->userType == 'Student') {
220
$newPwdHash = $this->getSaltedHash($row->ssn);
222
else if ($row->userType == 'Teacher') {
223
//$newPwdHash = $this->getSaltedHash($row->email);
224
$newPwdHash = $this->getSaltedHash($row->email);
229
'passwd' => $newPwdHash,
230
'passwdHint' => 'default'
233
$this->db->where('userName', $userName);
234
$this->db->update('Users', $data);
247
* This parses a user list from ladok and returns an array with users.
248
* 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
195
300
* Generates a salted password hash, encrypted with sha1.
301
* RESTRICTED-LEVEL: System
197
303
private function getSaltedHash($pwd) {
198
304
//Salt = CodeIgniters encryption-key from config
199
305
$salt = $this->config->item('encryption_key');
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'), //TODO: change later?
327
$this->db->insert('logUserLoginAttempts', $data);