Tugas 7
Tugas
Mengatur Akses User
Langkah-langkah
1. Membuat Tabel untuk Users
a, Buka Phpmyadmin, buat tabel bernama users dengan 11 kolom
b. isi dengan sql ini :
CREATE TABLE `users` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(64) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`full_name` varchar(255) NOT NULL,
`phone` varchar(20) NOT NULL,
`role` enum('admin','customer') NOT NULL DEFAULT 'customer',
`last_login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`photo` varchar(64) NOT NULL DEFAULT 'user_no_image.jpg',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`is_active` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`user_id`)
);
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(64) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(255) NOT NULL,
`full_name` varchar(255) NOT NULL,
`phone` varchar(20) NOT NULL,
`role` enum('admin','customer') NOT NULL DEFAULT 'customer',
`last_login` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`photo` varchar(64) NOT NULL DEFAULT 'user_no_image.jpg',
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`is_active` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`user_id`)
);
2. Membuat Model User
a. Buat model baru di dalam folder application/models/ dengan nama User_model.php, isi :
<?php
class User_model extends CI_Model
{
private $_table = "users";
public function doLogin(){
$post = $this->input->post();
// cari user berdasarkan email dan username
$this->db->where('email', $post["email"])
->or_where('username', $post["email"]);
$user = $this->db->get($this->_table)->row();
// jika user terdaftar
if($user){
// periksa password-nya
$isPasswordTrue = password_verify($post["password"], $user->password);
// periksa role-nya
$isAdmin = $user->role == "admin";
// jika password benar dan dia admin
if($isPasswordTrue && $isAdmin){
// login sukses yay!
$this->session->set_userdata(['user_logged' => $user]);
$this->_updateLastLogin($user->user_id);
return true;
}
}
// login gagal
return false;
}
public function isNotLogin(){
return $this->session->userdata('user_logged') === null;
}
private function _updateLastLogin($user_id){
$sql = "UPDATE {$this->_table} SET last_login=now() WHERE user_id={$user_id}";
$this->db->query($sql);
}
}
class User_model extends CI_Model
{
private $_table = "users";
public function doLogin(){
$post = $this->input->post();
// cari user berdasarkan email dan username
$this->db->where('email', $post["email"])
->or_where('username', $post["email"]);
$user = $this->db->get($this->_table)->row();
// jika user terdaftar
if($user){
// periksa password-nya
$isPasswordTrue = password_verify($post["password"], $user->password);
// periksa role-nya
$isAdmin = $user->role == "admin";
// jika password benar dan dia admin
if($isPasswordTrue && $isAdmin){
// login sukses yay!
$this->session->set_userdata(['user_logged' => $user]);
$this->_updateLastLogin($user->user_id);
return true;
}
}
// login gagal
return false;
}
public function isNotLogin(){
return $this->session->userdata('user_logged') === null;
}
private function _updateLastLogin($user_id){
$sql = "UPDATE {$this->_table} SET last_login=now() WHERE user_id={$user_id}";
$this->db->query($sql);
}
}
3. Membuat Controller Login
a. Membuat controller baru, di dalam folder controllers/admin/ dengan nama Login.php, isi :
<?php
class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("user_model");
$this->load->library('form_validation');
}
public function index()
{
// jika form login disubmit
if($this->input->post()){
if($this->user_model->doLogin()) redirect(site_url('admin'));
}
// tampilkan halaman login
$this->load->view("admin/login_page.php");
}
public function logout()
{
// hancurkan semua sesi
$this->session->sess_destroy();
redirect(site_url('admin/login'));
}
}
b. buka controller/admin/Overview.php dan controller/admin/Product.php, tambah kode ini :class Login extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model("user_model");
$this->load->library('form_validation');
}
public function index()
{
// jika form login disubmit
if($this->input->post()){
if($this->user_model->doLogin()) redirect(site_url('admin'));
}
// tampilkan halaman login
$this->load->view("admin/login_page.php");
}
public function logout()
{
// hancurkan semua sesi
$this->session->sess_destroy();
redirect(site_url('admin/login'));
}
}
$this->load->model("user_model");
if($this->user_model->isNotLogin()) redirect(site_url('admin/login'));
if($this->user_model->isNotLogin()) redirect(site_url('admin/login'));
4. Membuat View untuk Halaman Login
a. Buat file baru di dalam folder application/views/admin/ dengan nama login_page.php, isi :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Login Admin</title>
<!-- Bootstrap core CSS-->
<link href="<?php echo base_url('assets/bootstrap/css/bootstrap.min.css') ?>" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-12 col-md-6 text-center mt-5 mx-auto p-4">
<h1 class="h2">Login Admin</h1>
<p class="lead">Silahkan masuk ke Panel Admin</p>
</div>
</div>
<div class="row">
<div class="col-12 col-md-5 mx-auto mt-5">
<form action="<?= site_url('admin/login') ?>" method="POST">
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" name="email" placeholder="Pakai username juga bisa.." required />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" placeholder="Password.." required />
</div>
<div class="form-group">
<div class="d-flex justify-content-between">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" name="rememberme" id="rememberme" />
<label class="custom-control-label" for="rememberme"> Ingat Saya</label>
</div>
<a href="<?= site_url('reset_password') ?>">Lupa Password?</a>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success w-100" value="Login" />
</div>
</form>
</div>
</div>
</div>
</body>
</html>
b. buka file views/admin/_partials/modal.php, kemudian ubah alamat link Logout menjadi :<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Login Admin</title>
<!-- Bootstrap core CSS-->
<link href="<?php echo base_url('assets/bootstrap/css/bootstrap.min.css') ?>" rel="stylesheet">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-12 col-md-6 text-center mt-5 mx-auto p-4">
<h1 class="h2">Login Admin</h1>
<p class="lead">Silahkan masuk ke Panel Admin</p>
</div>
</div>
<div class="row">
<div class="col-12 col-md-5 mx-auto mt-5">
<form action="<?= site_url('admin/login') ?>" method="POST">
<div class="form-group">
<label for="email">Email</label>
<input type="text" class="form-control" name="email" placeholder="Pakai username juga bisa.." required />
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" placeholder="Password.." required />
</div>
<div class="form-group">
<div class="d-flex justify-content-between">
<div class="custom-control custom-checkbox">
<input type="checkbox" class="custom-control-input" name="rememberme" id="rememberme" />
<label class="custom-control-label" for="rememberme"> Ingat Saya</label>
</div>
<a href="<?= site_url('reset_password') ?>">Lupa Password?</a>
</div>
</div>
<div class="form-group">
<input type="submit" class="btn btn-success w-100" value="Login" />
</div>
</form>
</div>
</div>
</div>
</body>
</html>
<a class="btn btn-primary" href="<?= site_url('admin/login/logout') ?>">Logout</a>
0 komentar:
Posting Komentar