2
Class User extends CI_Model {
6
function __construct() {
9
//Load required libraries
10
$this->load->database();
11
$this->load->library('session');
14
//Queries the DB and for a matching username and password. Returns result or false.
18
public function login($username, $password)
20
//Generate a salted hash
21
$hash = $this->getSaltedHash($password);
24
$this->db->select('userName, name, passwd, userType, ssn');
25
$this->db->from('Users');
26
$this->db->where('userName', $username);
27
$this->db->where('passwd', $hash);
31
$query = $this->db->get();
32
$result = $query->result();
34
//If a matching DB record is found.
36
//Prepare session data
37
$userDetails = array();
38
foreach($result as $row) {
40
'username' => $row->userName,
42
'usertype' => $row->userType,
48
$this->session->set_userdata('authenticated', $userDetails);
60
* This function logs the user out.
62
public function logout() {
64
$this->session->unset_userdata('authenticated');
69
* This function return TRUE if the user is logged in and FALSE otherwise.
71
public function isLoggedIn() {
72
if ($this->session->userdata('authenticated')) {
80
//Generates a password hash using a user-unique salt.
81
private function getSaltedHash($pwd)
84
* Step 1: Get encryption key
85
* Step 2: Salt is set to password (reversed)
86
* Step 3: Salt is encrypted with MD5
87
* Step 4: Salt is reversed (again)
88
* Step 5: Salt is encrypted with SHA1
93
//UNCOMMENT TO ACTIVATE SALT LATER IN DEVELOPMENT!!!
94
//$salt = sha1(strrev(md5(strrev($this->config->item('encryption_key')))));
96
//Generate SHA1 hash using salt
97
$hash = sha1($salt.$pwd);
b'\\ No newline at end of file'