173 lines
7.2 KiB
PHP
173 lines
7.2 KiB
PHP
<?php
|
|
require_once __DIR__ . '/layout_header.php';
|
|
|
|
// Hanya admin yang bisa akses
|
|
if ($_SESSION['role'] !== 'admin') {
|
|
echo "<div class='alert alert-danger'>Akses ditolak. Hanya Administrator yang dapat mengakses halaman ini.</div>";
|
|
require_once __DIR__ . '/layout_footer.php';
|
|
exit;
|
|
}
|
|
|
|
$success = '';
|
|
$error = '';
|
|
|
|
// Handle Delete
|
|
if (isset($_GET['delete'])) {
|
|
$id_to_delete = $_GET['delete'];
|
|
|
|
// Jangan hapus diri sendiri
|
|
if ($id_to_delete == $_SESSION['user_id']) {
|
|
$error = "Anda tidak dapat menghapus akun Anda sendiri.";
|
|
} else {
|
|
$stmt = db()->prepare("DELETE FROM users WHERE id = ?");
|
|
if ($stmt->execute([$id_to_delete])) {
|
|
$success = "Pengguna berhasil dihapus.";
|
|
} else {
|
|
$error = "Gagal menghapus pengguna.";
|
|
}
|
|
}
|
|
}
|
|
|
|
// Handle Add User
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'add') {
|
|
$username = $_POST['username'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
$nama_lengkap = $_POST['nama_lengkap'] ?? '';
|
|
$role = $_POST['role'] ?? 'pengurus';
|
|
|
|
if (empty($username) || empty($password) || empty($nama_lengkap)) {
|
|
$error = "Semua field harus diisi.";
|
|
} else {
|
|
// Cek apakah username sudah ada
|
|
$check = db()->prepare("SELECT id FROM users WHERE username = ?");
|
|
$check->execute([$username]);
|
|
if ($check->fetch()) {
|
|
$error = "Username sudah digunakan.";
|
|
} else {
|
|
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
$stmt = db()->prepare("INSERT INTO users (username, password, nama_lengkap, role) VALUES (?, ?, ?, ?)");
|
|
if ($stmt->execute([$username, $hashed_password, $nama_lengkap, $role])) {
|
|
$success = "Pengguna baru berhasil ditambahkan.";
|
|
} else {
|
|
$error = "Gagal menambahkan pengguna.";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Get all users
|
|
$users = db()->query("SELECT * FROM users ORDER BY created_at DESC")->fetchAll();
|
|
?>
|
|
|
|
<div class="d-flex justify-content-between align-items-center mb-4">
|
|
<div>
|
|
<h2 class="h3 mb-0">Manajemen Pengguna</h2>
|
|
<p class="text-muted">Kelola akun pengurus aplikasi SiWarga</p>
|
|
</div>
|
|
<button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#addUserModal">
|
|
<i class="bi bi-person-plus"></i> Tambah Pengguna
|
|
</button>
|
|
</div>
|
|
|
|
<?php if ($success): ?>
|
|
<div class="alert alert-success alert-dismissible fade show" role="alert">
|
|
<?= $success ?>
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($error): ?>
|
|
<div class="alert alert-danger alert-dismissible fade show" role="alert">
|
|
<?= $error ?>
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="card border-0 shadow-sm">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover align-middle mb-0">
|
|
<thead>
|
|
<tr>
|
|
<th>Nama Lengkap</th>
|
|
<th>Username</th>
|
|
<th>Role</th>
|
|
<th>Terdaftar</th>
|
|
<th class="text-end">Aksi</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($users as $u): ?>
|
|
<tr>
|
|
<td>
|
|
<div class="d-flex align-items-center">
|
|
<div class="bg-light rounded-circle d-flex align-items-center justify-content-center me-2" style="width: 32px; height: 32px;">
|
|
<i class="bi bi-person text-secondary"></i>
|
|
</div>
|
|
<span class="fw-medium"><?= htmlspecialchars($u['nama_lengkap']) ?></span>
|
|
</div>
|
|
</td>
|
|
<td><?= htmlspecialchars($u['username']) ?></td>
|
|
<td>
|
|
<span class="badge <?= $u['role'] === 'admin' ? 'bg-primary' : 'bg-info' ?>">
|
|
<?= ucfirst($u['role']) ?>
|
|
</span>
|
|
</td>
|
|
<td class="text-muted small"><?= date('d/m/Y H:i', strtotime($u['created_at'])) ?></td>
|
|
<td class="text-end">
|
|
<?php if ($u['id'] != $_SESSION['user_id']): ?>
|
|
<a href="?delete=<?= $u['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Yakin ingin menghapus pengguna ini?')">
|
|
<i class="bi bi-trash"></i>
|
|
</a>
|
|
<?php else: ?>
|
|
<span class="badge bg-light text-muted">Self</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Modal Tambah Pengguna -->
|
|
<div class="modal fade" id="addUserModal" tabindex="-1" aria-hidden="true">
|
|
<div class="modal-dialog modal-dialog-centered">
|
|
<div class="modal-content border-0 shadow">
|
|
<div class="modal-header border-bottom-0">
|
|
<h5 class="modal-title fw-bold">Tambah Pengguna Baru</h5>
|
|
<button type="button" class="btn-close" data-bs-toggle="modal" data-bs-target="#addUserModal"></button>
|
|
</div>
|
|
<form method="POST">
|
|
<input type="hidden" name="action" value="add">
|
|
<div class="modal-body">
|
|
<div class="mb-3">
|
|
<label class="form-label">Nama Lengkap</label>
|
|
<input type="text" name="nama_lengkap" class="form-control" required placeholder="Contoh: Budi Santoso">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Username</label>
|
|
<input type="text" name="username" class="form-control" required placeholder="Untuk login">
|
|
</div>
|
|
<div class="mb-3">
|
|
<label class="form-label">Password</label>
|
|
<input type="password" name="password" class="form-control" required placeholder="Minimal 6 karakter">
|
|
</div>
|
|
<div class="mb-0">
|
|
<label class="form-label">Role</label>
|
|
<select name="role" class="form-select" required>
|
|
<option value="pengurus">Pengurus</option>
|
|
<option value="admin">Administrator</option>
|
|
</select>
|
|
</div>
|
|
</div>
|
|
<div class="modal-footer border-top-0">
|
|
<button type="button" class="btn btn-light" data-bs-dismiss="modal">Batal</button>
|
|
<button type="submit" class="btn btn-primary px-4">Simpan</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<?php require_once __DIR__ . '/layout_footer.php'; ?>
|