File manager - Edit - /home/webapp69.cm.in.th/u69319090028/Shop/app/Services/AdminUserService.php
Back
<?php /** * AdminUserService * Admin user listing, suspension, reactivation, and admin creation workflow */ require_once __DIR__ . '/../Models/User.php'; require_once __DIR__ . '/../Models/AdminCreationRequest.php'; require_once __DIR__ . '/../Models/DeleteAccountRequest.php'; require_once __DIR__ . '/../Models/AdminAuditLog.php'; class AdminUserService { private User $userModel; private AdminCreationRequest $requestModel; private DeleteAccountRequest $deleteRequestModel; private AdminAuditLog $auditLog; public function __construct() { $this->userModel = new User(); $this->requestModel = new AdminCreationRequest(); $this->deleteRequestModel = new DeleteAccountRequest(); $this->auditLog = new AdminAuditLog(); } public function getUsers(array $filters, int $page = 1, int $perPage = 20): array { $offset = ($page - 1) * $perPage; $users = $this->userModel->searchUsers($filters, $perPage, $offset); $total = $this->userModel->countUsers($filters); return [ 'users' => $users, 'total' => $total, 'page' => $page, 'per_page' => $perPage, 'total_pages'=> max(1, (int)ceil($total / $perPage)), ]; } public function getUserDetail(int $userId): ?array { $user = $this->userModel->findWithRoles($userId); if (!$user) return null; $user['login_history'] = $this->userModel->getUserLoginHistory($userId, 10); $user['pending_delete_request'] = $this->deleteRequestModel->getPendingRequestByUserId($userId); return $user; } public function suspendUser(int $targetUserId, int $adminId): array { $target = $this->userModel->findById($targetUserId); if (!$target) return ['success' => false, 'message' => 'ไม่พบผู้ใช้งาน']; if (!empty($target['is_super_admin'])) { return ['success' => false, 'message' => 'ไม่สามารถระงับบัญชี Super Admin ได้']; } $ok = $this->userModel->suspendUser($targetUserId); if ($ok) { $this->auditLog->record($adminId, 'user.suspend', 'users', 'user', $targetUserId, "Suspended user: {$target['username']}"); } return ['success' => $ok, 'message' => $ok ? 'ระงับบัญชีผู้ใช้เรียบร้อยแล้ว' : 'ไม่สามารถระงับบัญชีได้']; } public function reactivateUser(int $targetUserId, int $adminId): array { $target = $this->userModel->findById($targetUserId); if (!$target) return ['success' => false, 'message' => 'ไม่พบผู้ใช้งาน']; $ok = $this->userModel->reactivateUser($targetUserId); if ($ok) { $this->auditLog->record($adminId, 'user.reactivate', 'users', 'user', $targetUserId, "Reactivated user: {$target['username']}"); } return ['success' => $ok, 'message' => $ok ? 'เปิดใช้บัญชีเรียบร้อยแล้ว' : 'ไม่สามารถดำเนินการได้']; } // ---- Admin Creation Workflow ---- public function getAdminList(): array { return $this->userModel->getAdminUsers(); } /** * Super Admin creates a new admin directly (no approval needed) */ public function createAdmin(array $data, int $superAdminId): array { $email = trim($data['email'] ?? ''); $role = trim($data['role'] ?? ''); $validRoles = ['operation_admin', 'finance_admin', 'support_admin']; if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) { return ['success' => false, 'message' => 'อีเมลไม่ถูกต้อง']; } if (!in_array($role, $validRoles, true)) { return ['success' => false, 'message' => 'Role ไม่ถูกต้อง']; } $user = $this->userModel->findByEmail($email); if (!$user) { return ['success' => false, 'message' => 'ไม่พบบัญชีผู้ใช้ที่ใช้อีเมลนี้ในระบบ']; } $this->userModel->assignRole((int)$user['id'], $role); $this->auditLog->record($superAdminId, 'admin.create', 'admins', 'user', (int)$user['id'], "Granted {$role} to user: {$user['username']}"); return ['success' => true, 'message' => "กำหนด Role {$role} ให้ {$user['username']} เรียบร้อยแล้ว"]; } /** * Get pending admin creation requests for Super Admin approval */ public function getPendingRequests(): array { return $this->requestModel->getPendingRequests(); } public function getAllRequests(int $page = 1, int $perPage = 20): array { $offset = ($page - 1) * $perPage; $requests = $this->requestModel->getAllRequests($perPage, $offset); return ['requests' => $requests]; } /** * Super Admin approves: user gets the requested role */ public function approveRequest(int $requestId, int $superAdminId): array { $db = \Database::getInstance(); $sql = "SELECT * FROM admin_creation_requests WHERE id = :id AND status = 'pending' LIMIT 1"; $stmt = $db->prepare($sql); $stmt->execute([':id' => $requestId]); $req = $stmt->fetch(); if (!$req) return ['success' => false, 'message' => 'ไม่พบคำขอ หรือดำเนินการไปแล้ว']; $user = $this->userModel->findByEmail($req['target_email']); if (!$user) return ['success' => false, 'message' => 'ไม่พบบัญชีผู้ใช้ที่ใช้อีเมล ' . $req['target_email']]; $this->userModel->assignRole((int)$user['id'], $req['requested_role']); $this->requestModel->reviewRequest($requestId, 'approved', $superAdminId); $this->auditLog->record($superAdminId, 'admin.request.approve', 'admins', 'user', (int)$user['id'], "Approved admin request #{$requestId}: {$req['requested_role']} for {$user['username']}"); return ['success' => true, 'message' => 'อนุมัติคำขอและกำหนด Admin Role เรียบร้อยแล้ว']; } public function rejectRequest(int $requestId, int $superAdminId): array { $ok = $this->requestModel->reviewRequest($requestId, 'rejected', $superAdminId); if ($ok) { $this->auditLog->record($superAdminId, 'admin.request.reject', 'admins', null, null, "Rejected admin request #{$requestId}"); } return ['success' => $ok, 'message' => $ok ? 'ปฏิเสธคำขอเรียบร้อยแล้ว' : 'ไม่สามารถดำเนินการได้']; } public function suspendAdmin(int $targetAdminId, int $superAdminId): array { $target = $this->userModel->findById($targetAdminId); if (!$target) return ['success' => false, 'message' => 'ไม่พบ Admin']; if (!empty($target['is_super_admin'])) { return ['success' => false, 'message' => 'ไม่สามารถระงับบัญชี Super Admin']; } $ok = $this->userModel->suspendUser($targetAdminId); if ($ok) { $this->auditLog->record($superAdminId, 'admin.suspend', 'admins', 'user', $targetAdminId, "Suspended admin: {$target['username']}"); } return ['success' => $ok, 'message' => $ok ? 'ระงับบัญชี Admin เรียบร้อยแล้ว' : 'ไม่สามารถดำเนินการได้']; } public function reactivateAdmin(int $targetAdminId, int $superAdminId): array { $target = $this->userModel->findById($targetAdminId); if (!$target) return ['success' => false, 'message' => 'ไม่พบ Admin']; $ok = $this->userModel->reactivateUser($targetAdminId); if ($ok) { $this->auditLog->record($superAdminId, 'admin.reactivate', 'admins', 'user', $targetAdminId, "Reactivated admin: {$target['username']}"); } return ['success' => $ok, 'message' => $ok ? 'เปิดใช้บัญชี Admin เรียบร้อยแล้ว' : 'ไม่สามารถดำเนินการได้']; } // ---- Account Deletion Request Workflow ---- public function getDeleteAccountRequests(array $filters, int $page = 1, int $perPage = 20): array { $offset = ($page - 1) * $perPage; $requests = $this->deleteRequestModel->getRequests($filters, $perPage, $offset); $total = $this->deleteRequestModel->countRequests($filters); $counts = $this->deleteRequestModel->getStatusCounts(); return [ 'requests' => $requests, 'total' => $total, 'counts' => $counts, 'page' => $page, 'per_page' => $perPage, 'total_pages' => max(1, (int)ceil($total / $perPage)), ]; } public function approveDeleteRequest(int $requestId, int $adminId, ?string $adminNote = null): array { $req = $this->deleteRequestModel->findWithUser($requestId); if (!$req) { return ['success' => false, 'message' => 'ไม่พบคำขอลบบัญชี']; } if ($req['status'] !== 'pending') { return ['success' => false, 'message' => 'คำขอนี้ได้รับการดำเนินการไปแล้ว']; } if ((int)$req['user_id'] === $adminId) { return ['success' => false, 'message' => 'ไม่สามารถอนุมัติคำขอลบบัญชีของตัวเองได้']; } $ok = $this->deleteRequestModel->approveRequest($requestId, $adminId, $adminNote); if ($ok) { $this->auditLog->record($adminId, 'user.delete_approved', 'users', 'user', (int)$req['user_id'], "Approved account deletion for user: {$req['username']}. Note: {$adminNote}"); } return [ 'success' => $ok, 'message' => $ok ? "อนุมัติการลบบัญชีของ @{$req['username']} เรียบร้อยแล้ว (สถานะบัญชีเปลี่ยนเป็น deleted)" : 'ไม่สามารถดำเนินการได้' ]; } public function rejectDeleteRequest(int $requestId, int $adminId, ?string $adminNote = null): array { $req = $this->deleteRequestModel->findWithUser($requestId); if (!$req) { return ['success' => false, 'message' => 'ไม่พบคำขอลบบัญชี']; } if ($req['status'] !== 'pending') { return ['success' => false, 'message' => 'คำขอนี้ได้รับการดำเนินการไปแล้ว']; } $ok = $this->deleteRequestModel->rejectRequest($requestId, $adminId, $adminNote ?: 'ไม่ผ่านเกณฑ์การอนุมัติ'); if ($ok) { $this->auditLog->record($adminId, 'user.delete_rejected', 'users', 'user', (int)$req['user_id'], "Rejected account deletion for user: {$req['username']}. Note: {$adminNote}"); } return [ 'success' => $ok, 'message' => $ok ? "ปฏิเสธคำขอลบบัญชีของ @{$req['username']} เรียบร้อยแล้ว" : 'ไม่สามารถดำเนินการได้' ]; } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.23 |
proxy
|
phpinfo
|
Settings