142 lines
6.0 KiB
PHP
142 lines
6.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require __DIR__ . '/_common.php';
|
|
|
|
require_login();
|
|
require_tab('admins');
|
|
|
|
$me = current_admin();
|
|
if (!$me) {
|
|
json_response(['ok' => false, 'error' => 'Unauthorized'], 401);
|
|
}
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
if ($method === 'GET') {
|
|
$list = array_map('strip_admin', read_store()['admins'] ?? []);
|
|
json_response(['ok' => true, 'admins' => $list]);
|
|
}
|
|
|
|
if (!is_super($me)) {
|
|
json_response(['ok' => false, 'error' => 'เฉพาะ super admin จัดการบัญชีแอดมินได้'], 403);
|
|
}
|
|
|
|
if ($method === 'POST') {
|
|
$body = require_json_body();
|
|
$username = trim((string)($body['username'] ?? ''));
|
|
$password = (string)($body['password'] ?? '');
|
|
$role = ($body['role'] ?? 'admin') === 'super' ? 'super' : 'admin';
|
|
if ($role === 'super' && !is_super($me)) {
|
|
json_response(['ok' => false, 'error' => 'สร้าง super admin ได้เฉพาะ super เท่านั้น'], 403);
|
|
}
|
|
if ($username === '' || strlen($username) < 2) {
|
|
json_response(['ok' => false, 'error' => 'ชื่อผู้ใช้สั้นเกินไป'], 400);
|
|
}
|
|
if (strlen($password) < 8) {
|
|
json_response(['ok' => false, 'error' => 'รหัสผ่านอย่างน้อย 8 ตัวอักษร'], 400);
|
|
}
|
|
$store = read_store();
|
|
foreach ($store['admins'] ?? [] as $x) {
|
|
if (strcasecmp($x['username'] ?? '', $username) === 0) {
|
|
json_response(['ok' => false, 'error' => 'ชื่อผู้ใช้ซ้ำ'], 400);
|
|
}
|
|
}
|
|
/* สิทธิ์ระดับหมวด — super ไม่ต้องเก็บ (ได้ทุกหมวดเสมอ) · admin ที่ส่ง tabs ว่างมา = ทุกหมวด (พฤติกรรมเดิม) */
|
|
$store['admins'][] = [
|
|
'id' => new_id(),
|
|
'username' => $username,
|
|
'passwordHash' => password_hash($password, PASSWORD_DEFAULT),
|
|
'role' => $role,
|
|
'tabs' => $role === 'super' ? [] : sanitize_tabs($body['tabs'] ?? []),
|
|
'createdAt' => gmdate('c'),
|
|
];
|
|
if (!write_store($store)) {
|
|
json_response(['ok' => false, 'error' => 'บันทึกไม่สำเร็จ'], 500);
|
|
}
|
|
json_response(['ok' => true]);
|
|
}
|
|
|
|
if ($method === 'PATCH') {
|
|
$body = require_json_body();
|
|
$targetId = trim((string)($body['id'] ?? ''));
|
|
$newPassword = (string)($body['newPassword'] ?? '');
|
|
/* PATCH ทำได้ 2 อย่าง: ตั้งรหัสใหม่ และ/หรือ แก้สิทธิ์หมวด — ต้องส่งมาอย่างน้อย 1 อย่าง */
|
|
$wantTabs = array_key_exists('tabs', $body);
|
|
if ($targetId === '') {
|
|
json_response(['ok' => false, 'error' => 'ระบุ id แอดมิน'], 400);
|
|
}
|
|
if ($newPassword === '' && !$wantTabs) {
|
|
json_response(['ok' => false, 'error' => 'ไม่มีอะไรให้แก้ (ส่ง newPassword หรือ tabs)'], 400);
|
|
}
|
|
if ($newPassword !== '' && strlen($newPassword) < 8) {
|
|
json_response(['ok' => false, 'error' => 'รหัสใหม่อย่างน้อย 8 ตัวอักษร'], 400);
|
|
}
|
|
if ($newPassword !== '' && $targetId === $me['id']) {
|
|
json_response(['ok' => false, 'error' => 'เปลี่ยนรหัสตัวเองให้ใช้เมนู "เปลี่ยนรหัสผ่านของคุณ"'], 400);
|
|
}
|
|
$store = read_store();
|
|
$updated = false;
|
|
foreach ($store['admins'] ?? [] as $i => $a) {
|
|
if (($a['id'] ?? '') !== $targetId) {
|
|
continue;
|
|
}
|
|
if ($newPassword !== '') {
|
|
$store['admins'][$i]['passwordHash'] = password_hash($newPassword, PASSWORD_DEFAULT);
|
|
}
|
|
if ($wantTabs) {
|
|
/* super ได้ทุกหมวดอยู่แล้ว — เก็บ tabs ว่างไว้ กันเข้าใจผิดว่าถูกจำกัด */
|
|
$store['admins'][$i]['tabs'] = (($a['role'] ?? '') === 'super') ? [] : sanitize_tabs($body['tabs']);
|
|
}
|
|
$updated = true;
|
|
break;
|
|
}
|
|
if (!$updated) {
|
|
json_response(['ok' => false, 'error' => 'ไม่พบแอดมิน'], 404);
|
|
}
|
|
if (!write_store($store)) {
|
|
json_response(['ok' => false, 'error' => 'บันทึกไม่สำเร็จ'], 500);
|
|
}
|
|
json_response(['ok' => true, 'message' => $newPassword !== '' ? 'ตั้งรหัสใหม่แล้ว' : 'บันทึกสิทธิ์หมวดแล้ว']);
|
|
}
|
|
|
|
if ($method === 'DELETE') {
|
|
$id = trim((string)($_GET['id'] ?? ''));
|
|
if ($id === '') {
|
|
json_response(['ok' => false, 'error' => 'ระบุ id'], 400);
|
|
}
|
|
if ($id === $me['id']) {
|
|
json_response(['ok' => false, 'error' => 'ลบบัญชีตัวเองไม่ได้'], 400);
|
|
}
|
|
$store = read_store();
|
|
$admins = $store['admins'] ?? [];
|
|
$next = [];
|
|
$removed = false;
|
|
foreach ($admins as $a) {
|
|
if (($a['id'] ?? '') === $id) {
|
|
$removed = true;
|
|
continue;
|
|
}
|
|
$next[] = $a;
|
|
}
|
|
if (!$removed) {
|
|
json_response(['ok' => false, 'error' => 'ไม่พบผู้ใช้'], 404);
|
|
}
|
|
$supers = 0;
|
|
foreach ($next as $a) {
|
|
if (($a['role'] ?? '') === 'super') {
|
|
$supers++;
|
|
}
|
|
}
|
|
if ($supers < 1) {
|
|
json_response(['ok' => false, 'error' => 'ต้องมี super admin อย่างน้อย 1 คน'], 400);
|
|
}
|
|
$store['admins'] = $next;
|
|
if (!write_store($store)) {
|
|
json_response(['ok' => false, 'error' => 'บันทึกไม่สำเร็จ'], 500);
|
|
}
|
|
json_response(['ok' => true]);
|
|
}
|
|
|
|
json_response(['ok' => false, 'error' => 'Method not allowed'], 405);
|