menambahkan fitur edit data pengguna dan memperbarui landing page

This commit is contained in:
powji17
2026-06-10 17:39:36 +07:00
parent 7c1b4375e7
commit 6158f4b1a4
16 changed files with 869 additions and 455 deletions
@@ -0,0 +1,19 @@
<?php
require_once '../../auth.php';
requireRole('admin');
header('Content-Type: application/json');
$conn = new mysqli("localhost", "root", "", "webgis");
$id = (int) $_GET['id'];
$stmt = $conn->prepare("SELECT id, nama, username, role FROM users WHERE id = ?");
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();
$data = $result->fetch_assoc();
echo $data ? json_encode($data) : json_encode(["status" => "error", "message" => "User tidak ditemukan"]);
$stmt->close();
$conn->close();
?>
@@ -0,0 +1,28 @@
<?php
require_once '../../auth.php';
requireRole('admin');
header('Content-Type: application/json');
$conn = new mysqli("localhost", "root", "", "webgis");
$data = json_decode(file_get_contents("php://input"), true);
$id = (int) $data['id'];
// Tidak boleh hapus diri sendiri
if ($id === (int) $_SESSION['user']) {
echo json_encode(["status" => "error", "message" => "Tidak bisa menghapus akun sendiri"]);
exit;
}
$stmt = $conn->prepare("DELETE FROM users WHERE id = ?");
$stmt->bind_param("i", $id);
if ($stmt->execute()) {
echo json_encode(["status" => "success"]);
} else {
echo json_encode(["status" => "error", "message" => $stmt->error]);
}
$stmt->close();
$conn->close();
?>
@@ -0,0 +1,55 @@
<?php
require_once '../../auth.php';
requireRole('admin');
header('Content-Type: application/json');
$conn = new mysqli("localhost", "root", "", "webgis");
$data = json_decode(file_get_contents("php://input"), true);
if (!$data || !isset($data['id'])) {
echo json_encode(["status" => "error", "message" => "Data tidak lengkap"]);
exit;
}
$id = (int) $data['id'];
$nama = trim($data['nama']);
$username = trim($data['username']);
$role = $data['role'];
if (!$nama || !$username || !in_array($role, ['admin', 'operator'])) {
echo json_encode(["status" => "error", "message" => "Data tidak valid"]);
exit;
}
// Cek username sudah dipakai user lain
$cek = $conn->prepare("SELECT id FROM users WHERE username = ? AND id != ?");
$cek->bind_param("si", $username, $id);
$cek->execute();
$cek->store_result();
if ($cek->num_rows > 0) {
echo json_encode(["status" => "error", "message" => "Username sudah digunakan user lain"]);
$cek->close();
$conn->close();
exit;
}
$cek->close();
// Update dengan atau tanpa password baru
if (!empty($data['password'])) {
$hash = password_hash($data['password'], PASSWORD_DEFAULT);
$stmt = $conn->prepare("UPDATE users SET nama=?, username=?, password=?, role=? WHERE id=?");
$stmt->bind_param("ssssi", $nama, $username, $hash, $role, $id);
} else {
$stmt = $conn->prepare("UPDATE users SET nama=?, username=?, role=? WHERE id=?");
$stmt->bind_param("sssi", $nama, $username, $role, $id);
}
if ($stmt->execute()) {
echo json_encode(["status" => "success"]);
} else {
echo json_encode(["status" => "error", "message" => $stmt->error]);
}
$stmt->close();
$conn->close();
?>