From d18f24981c53d6b4a314496f1aaf41c7f0725963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Mon, 7 Oct 2024 23:14:31 +0900 Subject: [PATCH] cfmgrv4 init...2 --- app/Config/Constants.php | 3 +- app/Config/Routes.php | 1 + app/Controllers/Admin/UserController.php | 42 +++++++++++------------ app/Controllers/UserController.php | 43 +++++++++++++++--------- app/Language/en/User.php | 39 +++++++++++---------- app/Libraries/MyAuth/GoogleAuth.php | 19 +++++------ app/Libraries/MySocket/GoogleSocket.php | 25 +++++--------- app/Views/front/login.php | 7 ++-- composer.json | 1 + 9 files changed, 90 insertions(+), 90 deletions(-) diff --git a/app/Config/Constants.php b/app/Config/Constants.php index b9ebf79..1004f2a 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -110,6 +110,7 @@ define('FORMS', [ //URL define('URLS', [ 'LOGIN' => '/user/login', + 'GOOGLE_LOGIN' => '/user/google_login', 'SIGNUP' => '/user/signup', 'LOGOUT' => '/user/logout', ]); @@ -158,6 +159,7 @@ define('ICONS', [ 'LOGO' => '', 'EXCEL' => '', 'PDF' => '', + 'GOOGLE' => 'Google', 'MEMBER' => '', 'LOGIN' => '', 'LOGOUT' => '', @@ -182,7 +184,6 @@ define('ICONS', [ 'LEFT' => '', 'RIGHT' => '', 'IMAGE_FILE' => '', - 'GOOGLE' => '', 'CLOUD' => '', 'SIGNPOST' => '', 'LOCK' => '', diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 9de8a33..e661e4d 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -15,6 +15,7 @@ $routes->get('/', 'Home::index'); $routes->group('/user', function ($routes) { $routes->get('login', 'UserController::login_form'); $routes->post('login', 'UserController::login'); + $routes->get('google_login', 'UserController::google_login'); $routes->get('logout', 'UserController::logout', ['filter' => 'authFilter:user']); }); $routes->group('cli', ['namespace' => 'App\CLI'], function ($routes) { diff --git a/app/Controllers/Admin/UserController.php b/app/Controllers/Admin/UserController.php index 2e90b03..7cd053c 100644 --- a/app/Controllers/Admin/UserController.php +++ b/app/Controllers/Admin/UserController.php @@ -20,8 +20,8 @@ class UserController extends AdminController parent::initController($request, $response, $logger); $this->class_name = "User"; $this->class_path .= $this->class_name; - $this->title = lang("{$this->class_path}.title"); - $this->helper = new UserHelper(); + $this->title = lang("{$this->class_path}.title"); + $this->helper = new UserHelper(); } protected function getModel(): UserModel { @@ -70,11 +70,11 @@ class UserController extends AdminController } private function init(string $action): void { - $this->action = $action; - $this->fields = ['id', 'passwd', 'confirmpassword', $this->getModel()::TITLE, 'email', 'mobile', 'role']; - $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); - $this->filter_fields = ['role', 'status']; - $this->field_options = $this->getFormFieldOptions($this->filter_fields); + $this->action = $action; + $this->fields = ['id', 'passwd', 'confirmpassword', $this->getModel()::TITLE, 'email', 'mobile', 'role']; + $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); + $this->filter_fields = ['role', 'status']; + $this->field_options = $this->getFormFieldOptions($this->filter_fields); } //생성 public function create_form(): RedirectResponse|string @@ -101,30 +101,30 @@ class UserController extends AdminController //일괄작업 public function batcjob(): RedirectResponse { - $this->action = __FUNCTION__; - $this->fields = ['status']; - $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); + $this->action = __FUNCTION__; + $this->fields = ['status']; + $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); return $this->batcjob_procedure(); } // 리스트 public function index(): string { - $this->action = __FUNCTION__; - $this->fields = ['id', $this->getModel()::TITLE, 'email', 'mobile', 'role', 'status']; - $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); - $this->filter_fields = ['role', 'status']; - $this->field_options = $this->getFormFieldOptions($this->filter_fields); - $this->batchjob_fields = ['status']; + $this->action = __FUNCTION__; + $this->fields = ['id', $this->getModel()::TITLE, 'email', 'mobile', 'role', 'status']; + $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); + $this->filter_fields = ['role', 'status']; + $this->field_options = $this->getFormFieldOptions($this->filter_fields); + $this->batchjob_fields = ['role', 'status']; return $this->list_procedure(); } // Download public function download(string $output_type, $uid = false): DownloadResponse|string { - $this->action = __FUNCTION__; - $this->fields = ['id', $this->getModel()::TITLE, 'email', 'mobile', 'role', 'status']; - $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); - $this->filter_fields = ['role', 'status']; - $this->field_options = $this->getFormFieldOptions($this->filter_fields); + $this->action = __FUNCTION__; + $this->fields = ['id', $this->getModel()::TITLE, 'email', 'mobile', 'role', 'status']; + $this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields); + $this->filter_fields = ['role', 'status']; + $this->field_options = $this->getFormFieldOptions($this->filter_fields); $this->batchjob_fields = ['status']; return $this->download_procedure($output_type, $uid); } diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index ca23582..7244b01 100644 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -100,22 +100,33 @@ class UserController extends FrontController //Transaction Start $this->getModel()->transStart(); try { - $site = $this->request->getVar('site'); - switch ($site) { - case 'local': - $this->create_validate($this->action, $this->fields); - $this->formDatas = $this->getFormDatas(); - $auth = new LocalAuth(); - $entity = $auth->checkUser($this->formDatas); - break; - case 'google': - $auth = new GoogleAuth(); - $entity = $auth->checkUser(); - break; - default: - throw new \Exception("{$site}는 아직 지원하지 않는 사이트입니다."); - } - $auth->setLogin($entity); + $this->create_validate($this->action, $this->fields); + $this->formDatas = $this->getFormDatas(); + $auth = new LocalAuth(); + $auth->setLogin($auth->checkUser($this->formDatas)); + $this->message = "로그인 성공"; + $this->getModel()->transCommit(); + log_message("notice", $this->message); + $this->session->setFlashdata(SESSION_NAMES['RETURN_MSG'], $this->message); + return redirect()->to($this->session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/"); + } catch (\Exception $e) { + //Transaction Rollback + $this->getModel()->transRollback(); + log_message("error", $e->getMessage()); + $this->session->setFlashdata(SESSION_NAMES['RETURN_MSG'], "로그인 실패하였습니다.\n" . $e->getMessage()); + $this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']); + 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->getModel()->transCommit(); log_message("notice", $this->message); diff --git a/app/Language/en/User.php b/app/Language/en/User.php index c4b5230..ae45d5f 100644 --- a/app/Language/en/User.php +++ b/app/Language/en/User.php @@ -1,30 +1,29 @@ "계정정보", - 'label' => [ - 'uid' => "번호", - 'id' => "계정", - 'passwd' => "암호", + 'title' => "계정정보", + 'label' => [ + 'uid' => "번호", + 'id' => "계정", + 'passwd' => "암호", 'confirmpassword' => "암호확인", - 'email' => "메일", - 'mobile' => "연락처", - 'role' => "권한", - 'name' => "이름", - 'status' => "상태", - 'updated_at' => "수정일", - 'created_at' => "작성일", + 'email' => "메일", + 'mobile' => "연락처", + 'role' => "권한", + 'name' => "이름", + 'status' => "상태", + 'updated_at' => "수정일", + 'created_at' => "작성일", ], - "ROLE" => [ - "guest" => "비회원", - "user" => "일반회원", - "vip" => "VIP회원", - "manager" => "관리자", + "ROLE" => [ + "user" => "일반회원", + "vip" => "VIP회원", + "manager" => "관리자", "cloudflare" => "Cloudflare관리자", - "director" => "감독자", - "master" => "마스터", + "director" => "감독자", + "master" => "마스터", ], "STATUS" => [ - "use" => "사용", + "use" => "사용", "unuse" => "사용않함", ], ]; diff --git a/app/Libraries/MyAuth/GoogleAuth.php b/app/Libraries/MyAuth/GoogleAuth.php index 0e14569..c0ed7c1 100644 --- a/app/Libraries/MyAuth/GoogleAuth.php +++ b/app/Libraries/MyAuth/GoogleAuth.php @@ -7,7 +7,6 @@ use App\Models\UserModel; use App\Models\SNSUserModel; use App\Libraries\MySocket\GoogleSocket; use App\Entities\UserEntity; -use App\Entities\SNSUserEntity; class GoogleAuth extends MyAuth { @@ -23,7 +22,7 @@ class GoogleAuth extends MyAuth { if ($this->_mySocket === null) { $this->_mySocket = new GoogleSocket(); - $this->_mySocket->setAccessToken(); + $this->_mySocket->setMyToken(); } return $this->_mySocket; } @@ -31,7 +30,7 @@ class GoogleAuth extends MyAuth public function getAuthButton() { $button = ""; - if (!$this->getMySocket()->getAccessToken()) { + if (!$this->getMySocket()->getMyToken()) { $button = anchor( getenv("socket.google.api.url"), ICONS['GOOGLE'], @@ -90,10 +89,10 @@ class GoogleAuth extends MyAuth if ($entity === null) { //없다면 새로 등록 $formDatas = [ - 'site' => $this->_site, - 'id' => $authInfo['id'], - 'name' => $authInfo['name'], - 'email' => $authInfo['email'], + 'site' => $this->_site, + 'id' => $authInfo['id'], + 'name' => $authInfo['name'], + 'email' => $authInfo['email'], 'detail' => json_encode($authInfo), 'status' => 'standby', ]; @@ -107,11 +106,11 @@ class GoogleAuth extends MyAuth } //local db 사용와의 연결 확인 $userModel = new UserModel(); - $userEntity = $userModel->getEntityByID($entity->getID()); - if ($userEntity === null) { + $user_entity = $userModel->getEntityByID($entity->getID()); + if ($user_entity === null) { throw new \Exception("{$this->_site}의{$authInfo['email']}:{$authInfo['name']}님은 아직 사용자 연결이 이루어지지 않았습니다. "); } - return $userEntity; + return $user_entity; } catch (\Exception $e) { throw new \Exception("관리자에게 문의하시기 바랍니다.
{$e->getMessage()}"); } diff --git a/app/Libraries/MySocket/GoogleSocket.php b/app/Libraries/MySocket/GoogleSocket.php index 4894226..33e8b2b 100644 --- a/app/Libraries/MySocket/GoogleSocket.php +++ b/app/Libraries/MySocket/GoogleSocket.php @@ -2,12 +2,15 @@ namespace App\Libraries\MySocket; -class GoogleSocket extends Google_Client +use Google\Client; + +class GoogleSocket extends Client { private $_session = null; private $_access_code = ""; - public function __construct() + public function __construct(string $access_code) { + $this->_access_code = $access_code; parent::__construct(); $this->_session = \Config\Services::session(); $this->setClientId(getenv("socket.google.client.id")); @@ -17,22 +20,10 @@ class GoogleSocket extends Google_Client $this->addScope('profile'); } - public function getAccessCode(): string - { - 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() + public function setMyToken(): void { //2.토큰정보 가져오기 - $tokenInfo = $this->fetchAccessTokenWithAuthCode($this->getAccessCode()); + $tokenInfo = $this->fetchAccessTokenWithAuthCode($this->_access_code); if (isset($tokenInfo['error'])) { throw new \Exception($tokenInfo['error']); } @@ -42,7 +33,7 @@ class GoogleSocket extends Google_Client //4. Google에 로그인이 했으므로 세션에 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")); } diff --git a/app/Views/front/login.php b/app/Views/front/login.php index d37bc8b..1d510c0 100644 --- a/app/Views/front/login.php +++ b/app/Views/front/login.php @@ -13,11 +13,8 @@
- - + + "btn btn-danger"]) ?>
diff --git a/composer.json b/composer.json index 5e9592b..cad1496 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,7 @@ "php": "^8.3", "cloudflare/sdk": "^1.3", "codeigniter4/framework": "^4.5", + "google/apiclient": "^2.0", "guzzlehttp/guzzle": "^7.9", "phpoffice/phpspreadsheet": "^1.27", "symfony/css-selector": "^7.1",