224 lines
7.2 KiB
PHP
224 lines
7.2 KiB
PHP
<?php
|
|
namespace App\Controllers;
|
|
|
|
use App\Controllers\BaseController;
|
|
use App\Models\common\LoginModel;
|
|
use CodeIgniter\Session\Handlers\DatabaseHandler;
|
|
|
|
class Login extends BaseController
|
|
{
|
|
private $loginModel;
|
|
|
|
function __construct()
|
|
{
|
|
$this->loginModel = new LoginModel();
|
|
}
|
|
|
|
/**
|
|
* Redis 폴백 상태 체크 (세션이 DatabaseHandler로 동작 중인지)
|
|
*/
|
|
private function getSystemStatus(): array
|
|
{
|
|
$status = [
|
|
'redis_fallback' => false,
|
|
'warning_message' => ''
|
|
];
|
|
|
|
// Session 설정에서 현재 드라이버 확인
|
|
$sessionConfig = config('Session');
|
|
if ($sessionConfig->driver === DatabaseHandler::class) {
|
|
$status['redis_fallback'] = true;
|
|
$status['warning_message'] = '세션 서버(Redis) 장애로 임시 모드로 운영 중입니다. 시스템 관리자에게 문의하세요.';
|
|
}
|
|
|
|
return $status;
|
|
}
|
|
|
|
/**
|
|
* JSON 응답에 시스템 상태 추가
|
|
*/
|
|
private function jsonResponse(array $data): \CodeIgniter\HTTP\ResponseInterface
|
|
{
|
|
$systemStatus = $this->getSystemStatus();
|
|
$data['system'] = $systemStatus;
|
|
|
|
return $this->response->setJSON($data);
|
|
}
|
|
|
|
public function index(): string
|
|
{
|
|
$user_id = get_cookie('save_id');
|
|
if (!empty($user_id)) {
|
|
$this->data['user_id'] = $user_id;
|
|
} else {
|
|
delete_cookie('save_id');
|
|
}
|
|
|
|
return view('pages/login', $this->data);
|
|
}
|
|
|
|
// 로그인
|
|
public function chkLogin()
|
|
{
|
|
$logs = [
|
|
'usr_id' => $this->request->getPost('user_id'),
|
|
'userIp' => $this->get_user_ip(),
|
|
'userAgent' => $_SERVER['HTTP_USER_AGENT'] ?: '',
|
|
];
|
|
|
|
try {
|
|
|
|
/** ------------------------------------
|
|
* 1) 유효성 검사
|
|
* ------------------------------------*/
|
|
$rules = [
|
|
'user_id' => [
|
|
'rules' => 'required|min_length[4]|max_length[20]',
|
|
'errors' => [
|
|
'required' => '아이디를 입력해주세요.',
|
|
'min_length' => '아이디는 최소 {param}자 이상이어야 합니다.',
|
|
'max_length' => '아이디는 최대 {param}자까지 가능합니다.',
|
|
],
|
|
],
|
|
'user_pw' => [
|
|
'rules' => 'required|min_length[4]|max_length[30]',
|
|
'errors' => [
|
|
'required' => '비밀번호를 입력해주세요.',
|
|
'min_length' => '비밀번호는 최소 {param}자 이상이어야 합니다.',
|
|
'max_length' => '비밀번호는 최대 {param}자까지 가능합니다.',
|
|
],
|
|
],
|
|
];
|
|
|
|
if (!$this->validate($rules)) {
|
|
return $this->jsonResponse([
|
|
'code' => '1',
|
|
'errors' => $this->validator->getErrors()
|
|
]);
|
|
}
|
|
|
|
/** ------------------------------------
|
|
* 2) 로그인 정보 조회
|
|
* ------------------------------------*/
|
|
$userId = $this->request->getPost('user_id');
|
|
$userPw = $this->request->getPost('user_pw');
|
|
$saveId = $this->request->getPost('saveId');
|
|
|
|
$this->loginModel = new LoginModel();
|
|
$user = $this->loginModel->getUserByIdPw($userId, $userPw);
|
|
|
|
if (!$user) {
|
|
$logs['results'] = 0;
|
|
$logs['usr_sq'] = null;
|
|
$logs['reason'] = '존재하지 않는 아이디입니다.';
|
|
|
|
$this->loginModel->insertUserLog($logs);
|
|
|
|
return $this->jsonResponse([
|
|
'code' => '1',
|
|
'msg' => '존재하지 않는 아이디입니다.'
|
|
]);
|
|
}
|
|
|
|
|
|
if (strcmp($user['usr_pw'], $user['chk_pw']) !== 0) {
|
|
$logs['results'] = 0;
|
|
$logs['usr_sq'] = $user['usr_sq'];
|
|
$logs['reason'] = '잘못된 비밀번호 입니다.';
|
|
|
|
$this->loginModel->insertUserLog($logs);
|
|
|
|
return $this->jsonResponse([
|
|
'code' => '1',
|
|
'msg' => '잘못된 비밀번호 입니다.'
|
|
]);
|
|
}
|
|
|
|
|
|
/** ------------------------------------
|
|
* 3) 세션 저장
|
|
* ------------------------------------*/
|
|
$newdata = [
|
|
'usr_sq' => $user['usr_sq'],
|
|
'usr_id' => $user['usr_id'],
|
|
'usr_nm' => $user['usr_nm'],
|
|
'dept_sq' => $user['dept_sq'],
|
|
'dept_nm' => $user['dept_nm'],
|
|
'bonbu_sq' => $user['bonbu_sq'],
|
|
'bonbu_nm' => $user['bonbu_nm'],
|
|
'usr_level' => $user['usr_level'],
|
|
'depth' => $user['depth'],
|
|
'logged_in' => true
|
|
];
|
|
|
|
// 아이디 저장
|
|
if (!empty($saveId)) {
|
|
if ($saveId === "Y") {
|
|
$period = 60 * 60 * 24 * 90;
|
|
set_cookie('save_id', $userId, $period);
|
|
}
|
|
}
|
|
|
|
$logs['results'] = 1;
|
|
$logs['usr_sq'] = $user['usr_sq'];
|
|
$logs['reason'] = '로그인 성공';
|
|
|
|
$this->loginModel->insertUserLog($logs);
|
|
|
|
$this->session->set($newdata);
|
|
|
|
return $this->jsonResponse([
|
|
'code' => '0',
|
|
'msg' => 'success'
|
|
]);
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
/** ------------------------------------
|
|
* 4) 예외발생 처리 (DB 오류, 세션 오류 등)
|
|
* ------------------------------------*/
|
|
log_message('error', '[LOGIN ERROR] ' . $e->getMessage());
|
|
log_message('error', $e->getTraceAsString());
|
|
|
|
// 세션 관련 에러인지 확인
|
|
$errorMessage = $e->getMessage();
|
|
if (stripos($errorMessage, 'session') !== false ||
|
|
stripos($errorMessage, 'redis') !== false) {
|
|
return $this->jsonResponse([
|
|
'code' => '8',
|
|
'msg' => '세션 서비스 오류입니다. 시스템 관리자에게 문의해주세요.'
|
|
]);
|
|
}
|
|
|
|
return $this->jsonResponse([
|
|
'code' => '9',
|
|
'msg' => '서버 내부 오류가 발생했습니다. 잠시 후 다시 시도해주세요.'
|
|
]);
|
|
}
|
|
}
|
|
|
|
|
|
public function out()
|
|
{
|
|
$this->session->destroy();
|
|
return redirect()
|
|
->to('/login')
|
|
->deleteCookie('save_id');
|
|
}
|
|
|
|
|
|
private function get_user_ip()
|
|
{
|
|
$ip_address = '';
|
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
|
$ip_address = $_SERVER['HTTP_CLIENT_IP'];
|
|
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
// Check for multiple IPs in the header, take the first one (most likely the client)
|
|
$ip_address = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR'])[0];
|
|
} else {
|
|
$ip_address = $_SERVER['REMOTE_ADDR'];
|
|
}
|
|
return $ip_address;
|
|
}
|
|
|
|
} |