| Server IP : 127.0.0.1 / Your IP : 192.168.99.1 Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9 System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64 User : Administrator ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /bpkd_29052026/admin/ |
Upload File : |
<?php
$page_title = 'Manajemen Pengguna';
require_once 'config/database.php';
require_once 'includes/init.php';
require_once 'includes/header.php';
// Check if user is admin
if (!in_array($_SESSION['role'], ['superadmin','admin'])) {
redirect('dashboard.php');
}
// Handle add user
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add'])) {
$username = sanitize($_POST['username']);
$password = md5($_POST['password']);
$email = sanitize($_POST['email']);
$full_name = sanitize($_POST['full_name']);
$role = sanitize($_POST['role']);
$conn->query("INSERT INTO users (username, password, email, full_name, role)
VALUES ('$username', '$password', '$email', '$full_name', '$role')");
redirect('users.php?msg=added');
}
// Handle edit user
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['edit'])) {
$id = (int)$_POST['id'];
$email = sanitize($_POST['email']);
$full_name = sanitize($_POST['full_name']);
$role = sanitize($_POST['role']);
$query = "UPDATE users SET email = '$email', full_name = '$full_name', role = '$role'";
if (!empty($_POST['password'])) {
$password = md5($_POST['password']);
$query .= ", password = '$password'";
}
$query .= " WHERE id = $id";
$conn->query($query);
redirect('users.php?msg=updated');
}
// Handle delete
if (isset($_GET['delete'])) {
$id = (int)$_GET['delete'];
if ($id != 1) { // Prevent deleting main admin
$conn->query("DELETE FROM users WHERE id = $id");
}
redirect('users.php?msg=deleted');
}
// Get all users
if ($_SESSION['role'] == 'superadmin') {
$users = $conn->query("SELECT * FROM users ORDER BY id");
}else{
$users = $conn->query("SELECT * FROM users WHERE role<>'superadmin' ORDER BY id");
}
// Get user for edit
$edit_user = null;
if (isset($_GET['edit'])) {
$id = (int)$_GET['edit'];
$result = $conn->query("SELECT * FROM users WHERE id = $id");
$edit_user = $result->fetch_assoc();
}
$msg = '';
if (isset($_GET['msg'])) {
if ($_GET['msg'] == 'added') $msg = '<div class="alert alert-success">Pengguna berhasil ditambahkan!</div>';
if ($_GET['msg'] == 'updated') $msg = '<div class="alert alert-success">Pengguna berhasil diupdate!</div>';
if ($_GET['msg'] == 'deleted') $msg = '<div class="alert alert-success">Pengguna berhasil dihapus!</div>';
}
?>
<div class="container-fluid">
<div class="d-sm-flex align-items-center justify-content-between mb-4">
<h1 class="h3 mb-0 text-gray-800">Manajemen Pengguna</h1>
</div>
<?php echo $msg; ?>
<div class="row">
<!-- Add/Edit User Form -->
<div class="col-md-4">
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-white">
<?php echo $edit_user ? 'Edit Pengguna' : 'Tambah Pengguna Baru'; ?>
</h6>
</div>
<div class="card-body">
<form method="POST">
<?php if ($edit_user): ?>
<input type="hidden" name="id" value="<?php echo $edit_user['id']; ?>">
<?php endif; ?>
<?php if (!$edit_user): ?>
<div class="mb-3">
<label class="form-label">Username <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="username" required>
</div>
<?php endif; ?>
<div class="mb-3">
<label class="form-label">Nama Lengkap <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="full_name" value="<?php echo $edit_user['full_name'] ?? ''; ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Email <span class="text-danger">*</span></label>
<input type="email" class="form-control" name="email" value="<?php echo $edit_user['email'] ?? ''; ?>" required>
</div>
<div class="mb-3">
<label class="form-label">Password <?php echo $edit_user ? '(Kosongkan jika tidak diubah)' : '<span class="text-danger">*</span>'; ?></label>
<input type="password" class="form-control" name="password" <?php echo $edit_user ? '' : 'required'; ?>>
</div>
<div class="mb-3">
<label class="form-label">Role</label>
<select class="form-control" name="role">
<option value="editor" <?php echo ($edit_user['role'] ?? '') == 'editor' ? 'selected' : ''; ?>>Editor</option>
<option value="admin" <?php echo ($edit_user['role'] ?? '') == 'admin' ? 'selected' : ''; ?>>Admin</option>
</select>
</div>
<button type="submit" name="<?php echo $edit_user ? 'edit' : 'add'; ?>" class="btn btn-primary">
<i class="fas fa-save me-2"></i><?php echo $edit_user ? 'Update' : 'Simpan'; ?>
</button>
<?php if ($edit_user): ?>
<a href="users.php" class="btn btn-secondary">
<i class="fas fa-times me-2"></i>Batal
</a>
<?php endif; ?>
</form>
</div>
</div>
</div>
<!-- Users List -->
<div class="col-md-8">
<div class="card shadow mb-4">
<div class="card-header py-3">
<h6 class="m-0 font-weight-bold text-white">Daftar Pengguna</h6>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th>Username</th>
<th>Nama Lengkap</th>
<th>Email</th>
<th>Role</th>
<th>Terdaftar</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php while($user = $users->fetch_assoc()): ?>
<tr>
<td><strong><?php echo $user['username']; ?></strong></td>
<td><?php echo $user['full_name']; ?></td>
<td><?php echo $user['email']; ?></td>
<td>
<?php
if($user['role'] == 'superadmin' ){
echo "<span class='badge bg-warning'>".ucfirst($user['role'])."</span>";
}elseif($user['role'] == 'admin' ){
echo "<span class='badge bg-primary'>".ucfirst($user['role']) ."</span>";
}else{
echo "<span class='badge bg-info'>".ucfirst($user['role']) ."</span>";
}
?>
</td>
<td><?php echo date('d M Y', strtotime($user['created_at'])); ?></td>
<td>
<a href="?edit=<?php echo $user['id']; ?>" class="btn btn-sm btn-info">
<i class="fas fa-edit"></i>
</a>
<?php if ($user['id'] != 1): ?>
<a href="?delete=<?php echo $user['id']; ?>" class="btn btn-sm btn-danger" onclick="return confirm('Yakin ingin menghapus?')">
<i class="fas fa-trash"></i>
</a>
<?php endif; ?>
</td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<?php require_once 'includes/footer.php'; ?>