123 lines
4.6 KiB
PHP
123 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
|
|
use App\Helpers\UserHelper as Helper;
|
|
use App\Libraries\MySocket\GoogleSocket\API as GoogleSocket;
|
|
use App\Services\Auth\GoogleService;
|
|
use App\Services\Auth\LocalService;
|
|
|
|
use App\Services\UserService as Service;
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class UserController extends CommonController
|
|
{
|
|
private $_service = null;
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
$this->uri_path .= strtolower($this->getService()->getClassName()) . DIRECTORY_SEPARATOR;
|
|
$this->view_path = $this->uri_path;
|
|
$this->title = lang("{$this->getService()->getClassPath()}.title");;
|
|
$this->helper = new Helper($this->getService());
|
|
}
|
|
final public function getService(): Service
|
|
{
|
|
if (!$this->_service) {
|
|
$this->_service = new Service($this->request);
|
|
}
|
|
return $this->_service;
|
|
}
|
|
//Index,FieldForm관련
|
|
public function getFields(): array
|
|
{
|
|
return ['id', $this->getService()->getModel()::TITLE, 'email', 'mobile', 'role', 'status'];
|
|
}
|
|
public function getFilterFields(): array
|
|
{
|
|
return ['role', 'status'];
|
|
}
|
|
public function getBatchJobFields(): array
|
|
{
|
|
return ['status'];
|
|
}
|
|
//Index,FieldForm관련
|
|
|
|
private function login_init(string $action, array $fields = []): void
|
|
{
|
|
$this->action = $action;
|
|
$fields = ['fields' => ['id', 'passwd'],];
|
|
$this->init($action, $fields);
|
|
}
|
|
//로그인화면
|
|
public function login_form(): RedirectResponse|string
|
|
{
|
|
try {
|
|
$this->login_init('login');
|
|
helper(['form']);
|
|
//구글 로그인 BUTTON용
|
|
$google_socket = new GoogleSocket();
|
|
$this->google_url = $google_socket->createAuthUrl();
|
|
$this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
|
|
return view($this->view_path . "login", data: ['viewDatas' => $this->getViewDatas()]);
|
|
} catch (\Exception $e) {
|
|
log_message("error", $e->getMessage());
|
|
return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
|
|
}
|
|
}
|
|
//로그인처리
|
|
public function login(): RedirectResponse|string
|
|
{
|
|
try {
|
|
$this->login_init('login');
|
|
$this->formDatas = $this->doValidate($this->action, $this->fields);
|
|
$auth = new LocalService();
|
|
$auth->login($auth->checkUser($this->formDatas));
|
|
$this->message = "로그인 성공";
|
|
log_message("notice", __FUNCTION__ . $this->message);
|
|
// 이전 URL로 리다이렉트
|
|
return redirect()->to($this->myauth->popPreviousUrl())->with('message', $this->message);
|
|
} catch (\Exception $e) {
|
|
log_message("error", $e->getMessage());
|
|
return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
|
|
}
|
|
}
|
|
public function google_login(): RedirectResponse|string
|
|
{
|
|
try {
|
|
$access_code = $this->request->getVar('code');
|
|
if (!$access_code) {
|
|
throw new \Exception("구글 로그인 실패");
|
|
}
|
|
$auth = new GoogleService(new GoogleSocket());
|
|
$auth->login($auth->checkUser($access_code));
|
|
$this->message = "로그인 성공";
|
|
log_message("notice", __FUNCTION__ . $this->message);
|
|
// 이전 URL로 리다이렉트
|
|
return redirect()->to($this->myauth->popPreviousUrl())->with('message', $this->message);
|
|
} catch (\Exception $e) {
|
|
log_message("error", $e->getMessage());
|
|
return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
|
|
}
|
|
}
|
|
//로그아웃
|
|
public function logout(): RedirectResponse
|
|
{
|
|
try {
|
|
$auth = new LocalService();
|
|
$auth->logout();
|
|
// 성공 메시지 설정
|
|
$message = "로그아웃 되었습니다.";
|
|
// 홈페이지로 리다이렉트
|
|
return redirect()->route('/')->with('message', $message);
|
|
} catch (\Exception $e) {
|
|
log_message("error", $e->getMessage());
|
|
return redirect()->back()->with('error', "로그아웃 중 오류가 발생했습니다.");
|
|
}
|
|
}
|
|
}
|