This commit is contained in:
164
app/Controllers/Login.php
Normal file
164
app/Controllers/Login.php
Normal file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
namespace App\Controllers;
|
||||
|
||||
use App\Controllers\BaseController;
|
||||
use App\Models\common\LoginModel;
|
||||
|
||||
class Login extends BaseController
|
||||
{
|
||||
private $loginModel;
|
||||
|
||||
function __construct()
|
||||
{
|
||||
$this->loginModel = new LoginModel();
|
||||
}
|
||||
|
||||
public function index(): string
|
||||
{
|
||||
return view('pages/login');
|
||||
}
|
||||
|
||||
// 로그인
|
||||
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->response->setJSON([
|
||||
'code' => '1',
|
||||
'errors' => $this->validator->getErrors()
|
||||
]);
|
||||
}
|
||||
|
||||
/** ------------------------------------
|
||||
* 2) 로그인 정보 조회
|
||||
* ------------------------------------*/
|
||||
$userId = $this->request->getPost('user_id');
|
||||
$userPw = $this->request->getPost('user_pw');
|
||||
|
||||
$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->response->setJSON([
|
||||
'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->response->setJSON(body: [
|
||||
'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
|
||||
];
|
||||
|
||||
$logs['results'] = 1;
|
||||
$logs['usr_sq'] = $user['usr_sq'];
|
||||
$logs['reason'] = '로그인 성공';
|
||||
|
||||
$this->loginModel->insertUserLog($logs);
|
||||
|
||||
$this->session->set($newdata);
|
||||
|
||||
return $this->response->setJSON([
|
||||
'code' => '0',
|
||||
'msg' => 'success'
|
||||
]);
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
|
||||
/** ------------------------------------
|
||||
* 4) 예외발생 처리 (DB 오류, 세션 오류 등)
|
||||
* ------------------------------------*/
|
||||
log_message('error', '[LOGIN ERROR] ' . $e->getMessage());
|
||||
log_message('error', $e->getTraceAsString());
|
||||
|
||||
return $this->response->setJSON([
|
||||
'code' => '9',
|
||||
'msg' => '서버 내부 오류가 발생했습니다. 잠시 후 다시 시도해주세요.'
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function out()
|
||||
{
|
||||
$this->session->destroy();
|
||||
return redirect()->to('/login');
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user