dbms/app/Controllers/Auth/AuthController.php
2025-05-02 17:15:36 +09:00

110 lines
3.4 KiB
PHP

<?php
namespace App\Controllers\Auth;
use App\Controllers\CommonController;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use Psr\Log\LoggerInterface;
use App\Libraries\MySocket\GoogleSocket\API as GoogleSocket;
use App\Services\UserService;
use App\Entities\UserEntity as Entity;
abstract class AuthController extends CommonController
{
private ?GoogleSocket $_socket = null;
private ?UserService $_userService = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->layout = "auth";
$this->uri_path = "auth/";
$this->view_path = "auth" . DIRECTORY_SEPARATOR;
}
abstract protected function login_process(): Entity;
final public function getSocket()
{
if (!$this->_socket) {
$this->_socket = new GoogleSocket();
}
return $this->_socket;
}
final public function getUserService(): UserService
{
if (!$this->_userService) {
$this->_userService = new UserService($this->request);
}
return $this->_userService;
}
//Index,FieldForm관련
public function getFields(): array
{
return ['id', 'passwd'];
}
public function getFilterFields(): array
{
return ['role', 'status'];
}
public function getBatchJobFields(): array
{
return ['status'];
}
//Index,FieldForm관련
protected function getResultPageByActon(string $action, string $message = MESSAGES["SUCCESS"]): RedirectResponse|string
{
switch ($action) {
case 'login_form':
$result = view($this->view_path . $action, ['viewDatas' => $this->getViewDatas()]);
break;
default:
$result = parent::getResultPageByActon($action, $message);
break;
}
return $result;
}
//로그인화면
final public function login_form(): RedirectResponse|string
{
try {
$this->init(__FUNCTION__);
helper(['form']);
//구글 로그인 BUTTON용
$this->google_url = $this->getSocket()->createAuthUrl();
$this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
return $this->getResultPageByActon($this->action);
} catch (\Exception $e) {
return redirect()->back()->withInput()->with('error', $e->getMessage());
}
}
//로그인
final public function login(): RedirectResponse|string
{
try {
$this->entity = $this->login_process();
return $this->getResultPageByActon($this->action, MESSAGES['LOGIN']);
} catch (\Exception $e) {
return redirect()->back()->withInput()->with('error', $e->getMessage());
}
}
//로그아웃
final public function logout(): RedirectResponse
{
try {
$this->init(__FUNCTION__);
$this->getService()->logout();
// 홈페이지로 리다이렉트
return redirect()->route('/')->with('error', MESSAGES['LOGOUT']);
} catch (\Exception $e) {
log_message("error", $e->getMessage());
return redirect()->back()->with('error', "로그아웃 중 오류가 발생했습니다.");
}
}
}