| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402 |
- <?php
- namespace App\Controllers\Api;
- use CodeIgniter\HTTP\ResponseInterface;
- class AdminController extends BaseApiController
- {
- protected $format = 'json';
- /**
- * Get all admins (관리자 목록)
- * GET /api/admin
- */
- public function index()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- try {
- $page = $this->request->getGet('page') ?? 1;
- $perPage = $this->request->getGet('per_page') ?? 10;
- $offset = ($page - 1) * $perPage;
- $db = $this->getDB();
- $builder = $db->table('admin_users');
- // 'admin' 계정 제외
- $builder->where('username !=', 'admin');
- // 검색 기능 - 아이디, 이름만 LIKE 검색
- $search = $this->request->getGet('search');
- if (!empty($search)) {
- $builder->groupStart()
- ->like('username', $search)
- ->orLike('name', $search)
- ->groupEnd();
- }
- // 역할 필터
- $role = $this->request->getGet('role');
- if (!empty($role)) {
- $builder->where('role', $role);
- }
- // 상태 필터
- $status = $this->request->getGet('status');
- if (!empty($status)) {
- $builder->where('status', $status);
- }
- // 전체 개수
- $total = $builder->countAllResults(false);
- // 비밀번호 제외하고 조회 (login_attempts 포함)
- $builder->select('id, username, name, email, department, role, status, COALESCE(login_attempts, 0) as login_attempts, last_failed_login, created_at, updated_at');
- $builder->orderBy('id', 'DESC');
- $builder->limit($perPage, $offset);
- // SQL 쿼리 로그
- $sql = $builder->getCompiledSelect(false);
- log_message('debug', 'AdminController SQL: ' . $sql);
- $items = $builder->get()->getResult();
- $result = [
- 'items' => $items,
- 'total' => $total,
- 'page' => (int)$page,
- 'per_page' => (int)$perPage,
- 'total_pages' => ceil($total / $perPage)
- ];
- return $this->respondSuccess($result);
- } catch (\Exception $e) {
- log_message('error', 'AdminController index error: ' . $e->getMessage());
- return $this->respondError('관리자 목록 조회 중 오류가 발생했습니다: ' . $e->getMessage(), ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
- }
- }
- /**
- * Get single admin (관리자 상세)
- * GET /api/admin/:id
- */
- public function show($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- if (empty($id)) {
- return $this->respondError('관리자 ID가 필요합니다.', ResponseInterface::HTTP_BAD_REQUEST);
- }
- try {
- $admin = $this->getDB()->table('admin_users')
- ->select('id, username, name, email, department, role, status, login_attempts, last_failed_login, created_at, updated_at')
- ->where('id', $id)
- ->get()
- ->getRow();
- if (!$admin) {
- return $this->respondError('관리자를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
- }
- return $this->respondSuccess($admin);
- } catch (\Exception $e) {
- log_message('error', 'AdminController show error: ' . $e->getMessage());
- return $this->respondError('관리자 조회 중 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
- }
- }
- /**
- * Check if username is available (아이디 중복 체크)
- * GET /api/admin/check-username
- */
- public function checkUsername()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- $username = $this->request->getGet('username');
- if (empty($username)) {
- return $this->respondError('아이디를 입력하세요.');
- }
- try {
- $existing = $this->getDB()->table('admin_users')
- ->where('username', $username)
- ->get()
- ->getRow();
- return $this->respondSuccess([
- 'available' => !$existing
- ]);
- } catch (\Exception $e) {
- log_message('error', 'AdminController checkUsername error: ' . $e->getMessage());
- return $this->respondError('중복 체크 중 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
- }
- }
- /**
- * Create new admin (관리자 생성)
- * POST /api/admin
- */
- public function create()
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- try {
- $data = $this->request->getJSON(true);
- // 필수 필드 검증
- $required = ['username', 'password', 'name', 'email'];
- foreach ($required as $field) {
- if (empty($data[$field])) {
- return $this->respondError("{$field}는 필수 항목입니다.", ResponseInterface::HTTP_BAD_REQUEST);
- }
- }
- // 중복 체크
- $existing = $this->getDB()->table('admin_users')
- ->where('username', $data['username'])
- ->orWhere('email', $data['email'])
- ->get()
- ->getRow();
- if ($existing) {
- if ($existing->username === $data['username']) {
- return $this->respondError('이미 사용 중인 아이디입니다.', ResponseInterface::HTTP_BAD_REQUEST);
- }
- if ($existing->email === $data['email']) {
- return $this->respondError('이미 사용 중인 이메일입니다.', ResponseInterface::HTTP_BAD_REQUEST);
- }
- }
- // 비밀번호 해싱
- $insertData = [
- 'username' => $data['username'],
- 'password' => password_hash($data['password'], PASSWORD_DEFAULT),
- 'name' => $data['name'],
- 'email' => $data['email'],
- 'department' => $data['department'] ?? '',
- 'role' => $data['role'] ?? 'admin',
- 'status' => $data['status'] ?? 'active',
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ];
- $builder = $this->getDB()->table('admin_users');
- $builder->insert($insertData);
- $insertId = $this->getDB()->insertID();
- // 생성된 관리자 정보 조회 (비밀번호 제외)
- $admin = $this->getDB()->table('admin_users')
- ->select('id, username, name, email, department, role, status, login_attempts, last_failed_login, created_at, updated_at')
- ->where('id', $insertId)
- ->get()
- ->getRow();
- return $this->respondSuccess($admin, '관리자가 생성되었습니다.', ResponseInterface::HTTP_CREATED);
- } catch (\Exception $e) {
- log_message('error', 'AdminController create error: ' . $e->getMessage());
- return $this->respondError('관리자 생성 중 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
- }
- }
- /**
- * Update admin (관리자 수정)
- * PUT /api/admin/:id
- */
- public function update($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- if (empty($id)) {
- return $this->respondError('관리자 ID가 필요합니다.', ResponseInterface::HTTP_BAD_REQUEST);
- }
- try {
- // 기존 관리자 확인
- $existing = $this->getDB()->table('admin_users')->where('id', $id)->get()->getRow();
- if (!$existing) {
- return $this->respondError('관리자를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
- }
- $data = $this->request->getJSON(true);
- // email 중복 체크 (자신 제외)
- if (!empty($data['email']) && $data['email'] !== $existing->email) {
- $duplicate = $this->getDB()->table('admin_users')
- ->where('email', $data['email'])
- ->where('id !=', $id)
- ->get()
- ->getRow();
- if ($duplicate) {
- return $this->respondError('이미 사용 중인 이메일입니다.', ResponseInterface::HTTP_BAD_REQUEST);
- }
- }
- $updateData = [
- 'updated_at' => date('Y-m-d H:i:s')
- ];
- if (isset($data['name'])) {
- $updateData['name'] = $data['name'];
- }
- if (isset($data['email'])) {
- $updateData['email'] = $data['email'];
- }
- if (isset($data['department'])) {
- $updateData['department'] = $data['department'];
- }
- if (isset($data['role'])) {
- $updateData['role'] = $data['role'];
- }
- if (isset($data['status'])) {
- $updateData['status'] = $data['status'];
- }
- $builder = $this->getDB()->table('admin_users');
- $builder->where('id', $id)->update($updateData);
- // 업데이트된 관리자 정보 조회
- $admin = $this->getDB()->table('admin_users')
- ->select('id, username, name, email, department, role, status, login_attempts, last_failed_login, created_at, updated_at')
- ->where('id', $id)
- ->get()
- ->getRow();
- return $this->respondSuccess($admin, '관리자 정보가 수정되었습니다.');
- } catch (\Exception $e) {
- log_message('error', 'AdminController update error: ' . $e->getMessage());
- return $this->respondError('관리자 수정 중 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
- }
- }
- /**
- * Delete admin (관리자 삭제)
- * DELETE /api/admin/:id
- */
- public function delete($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- if (empty($id)) {
- return $this->respondError('관리자 ID가 필요합니다.', ResponseInterface::HTTP_BAD_REQUEST);
- }
- try {
- $existing = $this->getDB()->table('admin_users')->where('id', $id)->get()->getRow();
- if (!$existing) {
- return $this->respondError('관리자를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
- }
- $builder = $this->getDB()->table('admin_users');
- $builder->where('id', $id)->delete();
- // 해당 관리자의 토큰도 삭제
- $this->getDB()->table('admin_tokens')->where('admin_id', $id)->delete();
- return $this->respondSuccess(null, '관리자가 삭제되었습니다.');
- } catch (\Exception $e) {
- log_message('error', 'AdminController delete error: ' . $e->getMessage());
- return $this->respondError('관리자 삭제 중 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
- }
- }
- /**
- * Change admin password (비밀번호 변경)
- * POST /api/admin/:id/password
- */
- public function changePassword($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- if (empty($id)) {
- return $this->respondError('관리자 ID가 필요합니다.', ResponseInterface::HTTP_BAD_REQUEST);
- }
- try {
- $data = $this->request->getJSON(true);
- if (empty($data['new_password'])) {
- return $this->respondError('새 비밀번호가 필요합니다.', ResponseInterface::HTTP_BAD_REQUEST);
- }
- $existing = $this->getDB()->table('admin_users')->where('id', $id)->get()->getRow();
- if (!$existing) {
- return $this->respondError('관리자를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
- }
- $updateData = [
- 'password' => password_hash($data['new_password'], PASSWORD_DEFAULT),
- 'password_changed_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ];
- $this->getDB()->table('admin_users')->where('id', $id)->update($updateData);
- return $this->respondSuccess(null, '비밀번호가 변경되었습니다.');
- } catch (\Exception $e) {
- log_message('error', 'AdminController changePassword error: ' . $e->getMessage());
- return $this->respondError('비밀번호 변경 중 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
- }
- }
- /**
- * Unlock admin account (계정 잠금 해제)
- * POST /api/admin/:id/unlock
- */
- public function unlockAccount($id = null)
- {
- $auth = $this->requireAuth();
- if ($auth instanceof ResponseInterface) {
- return $auth;
- }
- if (empty($id)) {
- return $this->respondError('관리자 ID가 필요합니다.', ResponseInterface::HTTP_BAD_REQUEST);
- }
- try {
- $existing = $this->getDB()->table('admin_users')->where('id', $id)->get()->getRow();
- if (!$existing) {
- return $this->respondError('관리자를 찾을 수 없습니다.', ResponseInterface::HTTP_NOT_FOUND);
- }
- $updateData = [
- 'login_attempts' => 0,
- 'last_failed_login' => null,
- 'updated_at' => date('Y-m-d H:i:s')
- ];
- $this->getDB()->table('admin_users')->where('id', $id)->update($updateData);
- return $this->respondSuccess(null, '계정 잠금이 해제되었습니다.');
- } catch (\Exception $e) {
- log_message('error', 'AdminController unlockAccount error: ' . $e->getMessage());
- return $this->respondError('계정 잠금 해제 중 오류가 발생했습니다.', ResponseInterface::HTTP_INTERNAL_SERVER_ERROR);
- }
- }
- }
|