62 lines
1.5 KiB
PHP
62 lines
1.5 KiB
PHP
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
|
|
|
|
/**
|
|
* Auth library
|
|
*
|
|
* @author Anggy Trisnawan
|
|
*/
|
|
class Auth{
|
|
var $CI = NULL;
|
|
function __construct()
|
|
{
|
|
// get CI's object
|
|
$this->CI =& get_instance();
|
|
}
|
|
// untuk validasi login
|
|
function do_login($username,$password)
|
|
{
|
|
// cek di database, ada ga?
|
|
$this->CI->db->from('user');
|
|
$this->CI->db->where('username',$username);
|
|
$this->CI->db->where('password=("'.md5($password).'")','',false);
|
|
$result = $this->CI->db->get();
|
|
if($result->num_rows() == 0)
|
|
{
|
|
// username dan password tsb tidak ada
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
// ada, maka ambil informasi dari database
|
|
$userdata = $result->row();
|
|
if($userdata->role == "admin") $rule = 'Admin';
|
|
else $rule = 'Sales';
|
|
$session_data = array(
|
|
'username' => $userdata->username,
|
|
'nama' => $userdata->nama,
|
|
'role' => $rule,
|
|
'is_log_in' => TRUE
|
|
);
|
|
// buat session
|
|
$this->CI->session->set_userdata($session_data);
|
|
return true;
|
|
}
|
|
}
|
|
// untuk mengecek apakah user sudah login/belum
|
|
function is_logged_in()
|
|
{
|
|
if($this->CI->session->userdata('user_id') == '')
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
// untuk validasi di setiap halaman yang mengharuskan authentikasi
|
|
function restrict()
|
|
{
|
|
if($this->is_logged_in() == false)
|
|
{
|
|
redirect('home/login');
|
|
}
|
|
}
|
|
} |