204 lines
7.6 KiB
PHP
204 lines
7.6 KiB
PHP
<?php
|
|
session_start();
|
|
require 'config/database.php'; // gunakan class Database
|
|
|
|
// Buat koneksi PDO
|
|
$db = new Database();
|
|
$conn = $db->getConnection();
|
|
|
|
// CREATE
|
|
if (isset($_POST['create'])) {
|
|
$username = trim($_POST['username']);
|
|
$password = password_hash(trim($_POST['password']), PASSWORD_BCRYPT);
|
|
$role = $_POST['role'];
|
|
|
|
$stmt = $conn->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, ?)");
|
|
$stmt->execute([$username, $password, $role]);
|
|
|
|
header("Location: manajemen_users.php");
|
|
exit;
|
|
}
|
|
|
|
// UPDATE
|
|
if (isset($_POST['update'])) {
|
|
$id = $_POST['id'];
|
|
$username = trim($_POST['username']);
|
|
$role = $_POST['role'];
|
|
|
|
if (!empty($_POST['password'])) {
|
|
$password = password_hash(trim($_POST['password']), PASSWORD_BCRYPT);
|
|
$stmt = $conn->prepare("UPDATE users SET username=?, password=?, role=? WHERE id=?");
|
|
$stmt->execute([$username, $password, $role, $id]);
|
|
} else {
|
|
$stmt = $conn->prepare("UPDATE users SET username=?, role=? WHERE id=?");
|
|
$stmt->execute([$username, $role, $id]);
|
|
}
|
|
|
|
header("Location: manajemen_users.php");
|
|
exit;
|
|
}
|
|
|
|
// DELETE
|
|
if (isset($_GET['delete'])) {
|
|
$id = $_GET['delete'];
|
|
$stmt = $conn->prepare("DELETE FROM users WHERE id=?");
|
|
$stmt->execute([$id]);
|
|
|
|
header("Location: manajemen_users.php");
|
|
exit;
|
|
}
|
|
|
|
// READ
|
|
$stmt = $conn->query("SELECT * FROM users");
|
|
$users = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="id">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Manajemen Users - FTTH Megadata</title>
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
|
|
<style>
|
|
body {
|
|
margin: 0;
|
|
font-family: Arial, sans-serif;
|
|
height: 100vh;
|
|
background: url('bg.jpg') no-repeat center center fixed;
|
|
background-size: cover;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
padding: 15px;
|
|
}
|
|
.users-container {
|
|
background: rgba(255, 255, 255, 0.92);
|
|
padding: 30px;
|
|
border-radius: 15px;
|
|
box-shadow: 0 0 15px rgba(0,0,0,0.2);
|
|
width: 90%;
|
|
max-width: 900px;
|
|
}
|
|
h2 {
|
|
text-align: center;
|
|
margin-bottom: 20px;
|
|
color: #333;
|
|
}
|
|
.table thead {
|
|
background: #333;
|
|
color: #fff;
|
|
}
|
|
.btn-custom {
|
|
border-radius: 30px;
|
|
padding: 8px 16px;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="users-container">
|
|
<h2>👥 Manajemen Users</h2>
|
|
|
|
<!-- Link ke Register -->
|
|
<div class="mb-3 text-end">
|
|
<a href="register.php" class="btn btn-success btn-custom">+ Daftar User Baru</a>
|
|
</div>
|
|
<!--
|
|
|
|
Form Tambah User
|
|
<div class="card mb-4">
|
|
<div class="card-header">Tambah User</div>
|
|
<div class="card-body">
|
|
<form method="POST">
|
|
<div class="row">
|
|
<div class="col-md-4 mb-2">
|
|
<input type="text" name="username" class="form-control" placeholder="Username" required>
|
|
</div>
|
|
<div class="col-md-4 mb-2">
|
|
<input type="password" name="password" class="form-control" placeholder="Password" required>
|
|
</div>
|
|
<div class="col-md-3 mb-2">
|
|
<select name="role" class="form-control" required>
|
|
<option value="admin">Admin</option>
|
|
<option value="user">User</option>
|
|
</select>
|
|
</div>
|
|
<div class="col-md-1 mb-2">
|
|
<button type="submit" name="create" class="btn btn-primary w-100">Tambah</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
-->
|
|
|
|
<!-- Tabel Users -->
|
|
<div class="table-responsive">
|
|
<table class="table table-bordered align-middle text-center">
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Username</th>
|
|
<th>Role</th>
|
|
<th style="width: 180px;">Aksi</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($users as $row): ?>
|
|
<tr>
|
|
<td><?= $row['id'] ?></td>
|
|
<td><?= $row['username'] ?></td>
|
|
<td><?= $row['role'] ?></td>
|
|
<td>
|
|
<!-- Tombol Edit -->
|
|
<button class="btn btn-primary btn-sm" data-bs-toggle="modal" data-bs-target="#edit<?= $row['id'] ?>">Edit</button>
|
|
<!-- Tombol Hapus -->
|
|
<a href="?delete=<?= $row['id'] ?>" class="btn btn-danger btn-sm" onclick="return confirm('Yakin hapus user ini?')">Hapus</a>
|
|
</td>
|
|
</tr>
|
|
|
|
<!-- Modal Edit -->
|
|
<div class="modal fade" id="edit<?= $row['id'] ?>" tabindex="-1">
|
|
<div class="modal-dialog">
|
|
<div class="modal-content">
|
|
<form method="POST">
|
|
<div class="modal-header">
|
|
<h5 class="modal-title">Edit User</h5>
|
|
<button type="button" class="btn-close" data-bs-dismiss="modal"></button>
|
|
</div>
|
|
<div class="modal-body">
|
|
<input type="hidden" name="id" value="<?= $row['id'] ?>">
|
|
<div class="mb-2">
|
|
<input type="text" name="username" class="form-control" value="<?= $row['username'] ?>" required>
|
|
</div>
|
|
<div class="mb-2">
|
|
<input type="password" name="password" class="form-control" placeholder="Password baru (opsional)">
|
|
</div>
|
|
<div class="mb-2">
|
|
<select name="role" class="form-control">
|
|
<option value="admin" <?= $row['role']=="admin"?"selected":"" ?>>Admin</option>
|
|
<option value="user" <?= $row['role']=="user"?"selected":"" ?>>User</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer">
|
|
<button type="submit" name="update" class="btn btn-primary">Simpan</button>
|
|
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Batal</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<!-- Tombol kembali ke dashboard -->
|
|
<div class="text-center mt-3">
|
|
<a href="index.php" class="btn btn-dark btn-custom">⬅ Kembali ke Dashboard</a>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
</body>
|
|
</html>
|