cfmgrv4 init...2

This commit is contained in:
최준흠 2024-10-07 23:14:31 +09:00
parent 375c6cd48a
commit d18f24981c
9 changed files with 90 additions and 90 deletions

View File

@ -110,6 +110,7 @@ define('FORMS', [
//URL //URL
define('URLS', [ define('URLS', [
'LOGIN' => '/user/login', 'LOGIN' => '/user/login',
'GOOGLE_LOGIN' => '/user/google_login',
'SIGNUP' => '/user/signup', 'SIGNUP' => '/user/signup',
'LOGOUT' => '/user/logout', 'LOGOUT' => '/user/logout',
]); ]);
@ -158,6 +159,7 @@ define('ICONS', [
'LOGO' => '<img src="/images/logo/android-icon-48x48.png">', 'LOGO' => '<img src="/images/logo/android-icon-48x48.png">',
'EXCEL' => '<img src="/images/common/excel.png"/>', 'EXCEL' => '<img src="/images/common/excel.png"/>',
'PDF' => '<img src="/images/common/pdf.png"/>', 'PDF' => '<img src="/images/common/pdf.png"/>',
'GOOGLE' => '<img src="https://www.google.com/favicon.ico" alt="Google" width="20" height="20" class="me-2">',
'MEMBER' => '<i class="bi bi-people"></i>', 'MEMBER' => '<i class="bi bi-people"></i>',
'LOGIN' => '<i class="bi bi-shield-check"></i>', 'LOGIN' => '<i class="bi bi-shield-check"></i>',
'LOGOUT' => '<i class="bi bi-sign-stop-fill"></i>', 'LOGOUT' => '<i class="bi bi-sign-stop-fill"></i>',
@ -182,7 +184,6 @@ define('ICONS', [
'LEFT' => '<i class="bi bi-arrow-left"></i>', 'LEFT' => '<i class="bi bi-arrow-left"></i>',
'RIGHT' => '<i class="bi bi-arrow-right"></i>', 'RIGHT' => '<i class="bi bi-arrow-right"></i>',
'IMAGE_FILE' => '<i class="bi bi-file-earmark-image"></i>', 'IMAGE_FILE' => '<i class="bi bi-file-earmark-image"></i>',
'GOOGLE' => '<i class="bi bi-google"></i>',
'CLOUD' => '<i class="bi bi-cloud"></i>', 'CLOUD' => '<i class="bi bi-cloud"></i>',
'SIGNPOST' => '<i class="bi bi-signpost"></i>', 'SIGNPOST' => '<i class="bi bi-signpost"></i>',
'LOCK' => '<i class="bi bi-lock"></i>', 'LOCK' => '<i class="bi bi-lock"></i>',

View File

@ -15,6 +15,7 @@ $routes->get('/', 'Home::index');
$routes->group('/user', function ($routes) { $routes->group('/user', function ($routes) {
$routes->get('login', 'UserController::login_form'); $routes->get('login', 'UserController::login_form');
$routes->post('login', 'UserController::login'); $routes->post('login', 'UserController::login');
$routes->get('google_login', 'UserController::google_login');
$routes->get('logout', 'UserController::logout', ['filter' => 'authFilter:user']); $routes->get('logout', 'UserController::logout', ['filter' => 'authFilter:user']);
}); });
$routes->group('cli', ['namespace' => 'App\CLI'], function ($routes) { $routes->group('cli', ['namespace' => 'App\CLI'], function ($routes) {

View File

@ -20,8 +20,8 @@ class UserController extends AdminController
parent::initController($request, $response, $logger); parent::initController($request, $response, $logger);
$this->class_name = "User"; $this->class_name = "User";
$this->class_path .= $this->class_name; $this->class_path .= $this->class_name;
$this->title = lang("{$this->class_path}.title"); $this->title = lang("{$this->class_path}.title");
$this->helper = new UserHelper(); $this->helper = new UserHelper();
} }
protected function getModel(): UserModel protected function getModel(): UserModel
{ {
@ -70,11 +70,11 @@ class UserController extends AdminController
} }
private function init(string $action): void private function init(string $action): void
{ {
$this->action = $action; $this->action = $action;
$this->fields = ['id', 'passwd', 'confirmpassword', $this->getModel()::TITLE, 'email', 'mobile', 'role']; $this->fields = ['id', 'passwd', 'confirmpassword', $this->getModel()::TITLE, 'email', 'mobile', 'role'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->filter_fields = ['role', 'status']; $this->filter_fields = ['role', 'status'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields); $this->field_options = $this->getFormFieldOptions($this->filter_fields);
} }
//생성 //생성
public function create_form(): RedirectResponse|string public function create_form(): RedirectResponse|string
@ -101,30 +101,30 @@ class UserController extends AdminController
//일괄작업 //일괄작업
public function batcjob(): RedirectResponse public function batcjob(): RedirectResponse
{ {
$this->action = __FUNCTION__; $this->action = __FUNCTION__;
$this->fields = ['status']; $this->fields = ['status'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
return $this->batcjob_procedure(); return $this->batcjob_procedure();
} }
// 리스트 // 리스트
public function index(): string public function index(): string
{ {
$this->action = __FUNCTION__; $this->action = __FUNCTION__;
$this->fields = ['id', $this->getModel()::TITLE, 'email', 'mobile', 'role', 'status']; $this->fields = ['id', $this->getModel()::TITLE, 'email', 'mobile', 'role', 'status'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->filter_fields = ['role', 'status']; $this->filter_fields = ['role', 'status'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields); $this->field_options = $this->getFormFieldOptions($this->filter_fields);
$this->batchjob_fields = ['status']; $this->batchjob_fields = ['role', 'status'];
return $this->list_procedure(); return $this->list_procedure();
} }
// Download // Download
public function download(string $output_type, $uid = false): DownloadResponse|string public function download(string $output_type, $uid = false): DownloadResponse|string
{ {
$this->action = __FUNCTION__; $this->action = __FUNCTION__;
$this->fields = ['id', $this->getModel()::TITLE, 'email', 'mobile', 'role', 'status']; $this->fields = ['id', $this->getModel()::TITLE, 'email', 'mobile', 'role', 'status'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
$this->filter_fields = ['role', 'status']; $this->filter_fields = ['role', 'status'];
$this->field_options = $this->getFormFieldOptions($this->filter_fields); $this->field_options = $this->getFormFieldOptions($this->filter_fields);
$this->batchjob_fields = ['status']; $this->batchjob_fields = ['status'];
return $this->download_procedure($output_type, $uid); return $this->download_procedure($output_type, $uid);
} }

View File

@ -100,22 +100,33 @@ class UserController extends FrontController
//Transaction Start //Transaction Start
$this->getModel()->transStart(); $this->getModel()->transStart();
try { try {
$site = $this->request->getVar('site'); $this->create_validate($this->action, $this->fields);
switch ($site) { $this->formDatas = $this->getFormDatas();
case 'local': $auth = new LocalAuth();
$this->create_validate($this->action, $this->fields); $auth->setLogin($auth->checkUser($this->formDatas));
$this->formDatas = $this->getFormDatas(); $this->message = "로그인 성공";
$auth = new LocalAuth(); $this->getModel()->transCommit();
$entity = $auth->checkUser($this->formDatas); log_message("notice", $this->message);
break; $this->session->setFlashdata(SESSION_NAMES['RETURN_MSG'], $this->message);
case 'google': return redirect()->to($this->session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/");
$auth = new GoogleAuth(); } catch (\Exception $e) {
$entity = $auth->checkUser(); //Transaction Rollback
break; $this->getModel()->transRollback();
default: log_message("error", $e->getMessage());
throw new \Exception("{$site}는 아직 지원하지 않는 사이트입니다."); $this->session->setFlashdata(SESSION_NAMES['RETURN_MSG'], "로그인 실패하였습니다.\n" . $e->getMessage());
} $this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']);
$auth->setLogin($entity); return redirect()->back()->withInput();
}
}
public function google_login(): RedirectResponse|string
{
$this->init('login');
$this->init('login');
//Transaction Start
$this->getModel()->transStart();
try {
$auth = new GoogleAuth();
$auth->setLogin($auth->checkUser());
$this->message = "로그인 성공"; $this->message = "로그인 성공";
$this->getModel()->transCommit(); $this->getModel()->transCommit();
log_message("notice", $this->message); log_message("notice", $this->message);

View File

@ -1,30 +1,29 @@
<?php <?php
return [ return [
'title' => "계정정보", 'title' => "계정정보",
'label' => [ 'label' => [
'uid' => "번호", 'uid' => "번호",
'id' => "계정", 'id' => "계정",
'passwd' => "암호", 'passwd' => "암호",
'confirmpassword' => "암호확인", 'confirmpassword' => "암호확인",
'email' => "메일", 'email' => "메일",
'mobile' => "연락처", 'mobile' => "연락처",
'role' => "권한", 'role' => "권한",
'name' => "이름", 'name' => "이름",
'status' => "상태", 'status' => "상태",
'updated_at' => "수정일", 'updated_at' => "수정일",
'created_at' => "작성일", 'created_at' => "작성일",
], ],
"ROLE" => [ "ROLE" => [
"guest" => "비회원", "user" => "일반회원",
"user" => "일반회원", "vip" => "VIP회원",
"vip" => "VIP회원", "manager" => "관리자",
"manager" => "관리자",
"cloudflare" => "Cloudflare관리자", "cloudflare" => "Cloudflare관리자",
"director" => "감독자", "director" => "감독자",
"master" => "마스터", "master" => "마스터",
], ],
"STATUS" => [ "STATUS" => [
"use" => "사용", "use" => "사용",
"unuse" => "사용않함", "unuse" => "사용않함",
], ],
]; ];

View File

@ -7,7 +7,6 @@ use App\Models\UserModel;
use App\Models\SNSUserModel; use App\Models\SNSUserModel;
use App\Libraries\MySocket\GoogleSocket; use App\Libraries\MySocket\GoogleSocket;
use App\Entities\UserEntity; use App\Entities\UserEntity;
use App\Entities\SNSUserEntity;
class GoogleAuth extends MyAuth class GoogleAuth extends MyAuth
{ {
@ -23,7 +22,7 @@ class GoogleAuth extends MyAuth
{ {
if ($this->_mySocket === null) { if ($this->_mySocket === null) {
$this->_mySocket = new GoogleSocket(); $this->_mySocket = new GoogleSocket();
$this->_mySocket->setAccessToken(); $this->_mySocket->setMyToken();
} }
return $this->_mySocket; return $this->_mySocket;
} }
@ -31,7 +30,7 @@ class GoogleAuth extends MyAuth
public function getAuthButton() public function getAuthButton()
{ {
$button = ""; $button = "";
if (!$this->getMySocket()->getAccessToken()) { if (!$this->getMySocket()->getMyToken()) {
$button = anchor( $button = anchor(
getenv("socket.google.api.url"), getenv("socket.google.api.url"),
ICONS['GOOGLE'], ICONS['GOOGLE'],
@ -90,10 +89,10 @@ class GoogleAuth extends MyAuth
if ($entity === null) { if ($entity === null) {
//없다면 새로 등록 //없다면 새로 등록
$formDatas = [ $formDatas = [
'site' => $this->_site, 'site' => $this->_site,
'id' => $authInfo['id'], 'id' => $authInfo['id'],
'name' => $authInfo['name'], 'name' => $authInfo['name'],
'email' => $authInfo['email'], 'email' => $authInfo['email'],
'detail' => json_encode($authInfo), 'detail' => json_encode($authInfo),
'status' => 'standby', 'status' => 'standby',
]; ];
@ -107,11 +106,11 @@ class GoogleAuth extends MyAuth
} }
//local db 사용와의 연결 확인 //local db 사용와의 연결 확인
$userModel = new UserModel(); $userModel = new UserModel();
$userEntity = $userModel->getEntityByID($entity->getID()); $user_entity = $userModel->getEntityByID($entity->getID());
if ($userEntity === null) { if ($user_entity === null) {
throw new \Exception("{$this->_site}{$authInfo['email']}:{$authInfo['name']}님은 아직 사용자 연결이 이루어지지 않았습니다. "); throw new \Exception("{$this->_site}{$authInfo['email']}:{$authInfo['name']}님은 아직 사용자 연결이 이루어지지 않았습니다. ");
} }
return $userEntity; return $user_entity;
} catch (\Exception $e) { } catch (\Exception $e) {
throw new \Exception("관리자에게 문의하시기 바랍니다.<BR>{$e->getMessage()}"); throw new \Exception("관리자에게 문의하시기 바랍니다.<BR>{$e->getMessage()}");
} }

View File

@ -2,12 +2,15 @@
namespace App\Libraries\MySocket; namespace App\Libraries\MySocket;
class GoogleSocket extends Google_Client use Google\Client;
class GoogleSocket extends Client
{ {
private $_session = null; private $_session = null;
private $_access_code = ""; private $_access_code = "";
public function __construct() public function __construct(string $access_code)
{ {
$this->_access_code = $access_code;
parent::__construct(); parent::__construct();
$this->_session = \Config\Services::session(); $this->_session = \Config\Services::session();
$this->setClientId(getenv("socket.google.client.id")); $this->setClientId(getenv("socket.google.client.id"));
@ -17,22 +20,10 @@ class GoogleSocket extends Google_Client
$this->addScope('profile'); $this->addScope('profile');
} }
public function getAccessCode(): string public function setMyToken(): void
{
if ($this->_access_code === "") {
throw new \Exception("access_code가 지정되지 않았습니다.");
}
return $this->_access_code;
}
public function setAccessCode(string $access_code)
{
$this->_access_code = $access_code;
}
public function setAccessToken()
{ {
//2.토큰정보 가져오기 //2.토큰정보 가져오기
$tokenInfo = $this->fetchAccessTokenWithAuthCode($this->getAccessCode()); $tokenInfo = $this->fetchAccessTokenWithAuthCode($this->_access_code);
if (isset($tokenInfo['error'])) { if (isset($tokenInfo['error'])) {
throw new \Exception($tokenInfo['error']); throw new \Exception($tokenInfo['error']);
} }
@ -42,7 +33,7 @@ class GoogleSocket extends Google_Client
//4. Google에 로그인이 했으므로 세션에 Token값 설정 //4. Google에 로그인이 했으므로 세션에 Token값 설정
$this->_session->set(getenv("socket.google.client.token_name"), $token); $this->_session->set(getenv("socket.google.client.token_name"), $token);
} }
public function getAccessToken(): ?string public function getMyToken(): ?string
{ {
return $this->_session->get(getenv("socket.google.client.token_name")); return $this->_session->get(getenv("socket.google.client.token_name"));
} }

View File

@ -13,11 +13,8 @@
<input type="password" class="form-control" id="userPassword" name="passwd" required> <input type="password" class="form-control" id="userPassword" name="passwd" required>
</div> </div>
<div class="d-flex justify-content-between align-items-center"> <div class="d-flex justify-content-between align-items-center">
<button type="submit" class="btn btn-primary" name="site" value="local">로그인</button> <button type="submit" class="btn btn-primary">로그인</button>
<button type="submit" class="btn btn-danger" name="site" value="google"> <?= anchor(URLS['GOOGLE_LOGIN'], ICONS['GOOGLE'] . 'Google 로그인', ["class" => "btn btn-danger"]) ?>
<img src="https://www.google.com/favicon.ico" alt="Google" width="20" height="20" class="me-2">
Google 로그인
</button>
<button type="button" class="btn btn-outline-primary">회원가입</button> <button type="button" class="btn btn-outline-primary">회원가입</button>
</div> </div>
<?= form_close(); ?> <?= form_close(); ?>

View File

@ -13,6 +13,7 @@
"php": "^8.3", "php": "^8.3",
"cloudflare/sdk": "^1.3", "cloudflare/sdk": "^1.3",
"codeigniter4/framework": "^4.5", "codeigniter4/framework": "^4.5",
"google/apiclient": "^2.0",
"guzzlehttp/guzzle": "^7.9", "guzzlehttp/guzzle": "^7.9",
"phpoffice/phpspreadsheet": "^1.27", "phpoffice/phpspreadsheet": "^1.27",
"symfony/css-selector": "^7.1", "symfony/css-selector": "^7.1",