From 6849f349342cc4543cc9bf252b79a8069e57bb0e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Mon, 14 Oct 2024 09:53:44 +0900 Subject: [PATCH] cfmgrv4 init...2 --- app/Controllers/CommonController.php | 19 +----- app/Controllers/MVController.php | 14 ++--- app/Controllers/UserController.php | 4 +- app/Filters/AuthFilter.php | 16 ++--- app/Libraries/MyAuth/MyAuth.php | 61 +++++++++++++++---- app/Libraries/MySocket/GoogleSocket/API.php | 2 +- app/Libraries/MySocket/GoogleSocket/CURL.php | 2 +- .../MySocket/GoogleSocket/GoogleSocket.php | 18 +++--- app/Views/layouts/admin/top.php | 6 +- 9 files changed, 84 insertions(+), 58 deletions(-) diff --git a/app/Controllers/CommonController.php b/app/Controllers/CommonController.php index 406e09b..c18ca4a 100644 --- a/app/Controllers/CommonController.php +++ b/app/Controllers/CommonController.php @@ -13,8 +13,7 @@ abstract class CommonController extends BaseController public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { parent::initController($request, $response, $logger); - // 세션 서비스 초기화 - $this->session = \Config\Services::session(); + $this->myauth = service('myauth'); } final public function __get($name) { @@ -31,20 +30,4 @@ abstract class CommonController extends BaseController { return $this->_viewDatas; } - - final protected function pushCurrentUrl() - { - $urlStack = $this->session->get('url_stack') ?? []; - $urlStack[] = current_url() . $this->request->getUri()->getQuery() ? "?" . $this->request->getUri()->getQuery() : ""; - $this->session->set('url_stack', $urlStack); - } - - final protected function popPreviousUrl() - { - $urlStack = $this->session->get('url_stack') ?? []; - if (!empty($urlStack)) { - return array_pop($urlStack); - } - return '/'; // 기본 URL - } } diff --git a/app/Controllers/MVController.php b/app/Controllers/MVController.php index 3d9defb..ad3693a 100644 --- a/app/Controllers/MVController.php +++ b/app/Controllers/MVController.php @@ -88,9 +88,7 @@ abstract class MVController extends CommonController )); } } - protected function create_form_process(): void - { - } + protected function create_form_process(): void {} final protected function create_form_procedure(): RedirectResponse|string { try { @@ -214,7 +212,7 @@ abstract class MVController extends CommonController $this->message = "{$this->class_name} : 일괄처리작업이 완료되었습니다."; log_message("notice", __FUNCTION__ . $this->message); // 이전 URL로 리다이렉트 - return redirect()->to($this->popPreviousUrl())->with('error', $this->message); + return redirect()->to($this->myauth->popPreviousUrl())->with('error', $this->message); } catch (\Exception $e) { //Transaction Rollback $this->getModel()->transRollback(); @@ -235,7 +233,7 @@ abstract class MVController extends CommonController $this->getModel()->transCommit(); log_message("notice", __FUNCTION__ . $this->message); // 이전 URL로 리다이렉트 - return redirect()->to($this->popPreviousUrl())->with('error', $this->message); + return redirect()->to($this->myauth->popPreviousUrl())->with('error', $this->message); } catch (\Exception $e) { //Transaction Rollback $this->getModel()->transRollback(); @@ -263,7 +261,7 @@ abstract class MVController extends CommonController $this->getModel()->transCommit(); log_message("notice", __FUNCTION__ . $this->message); // 이전 URL로 리다이렉트 - return redirect()->to($this->popPreviousUrl())->with('error', $this->message); + return redirect()->to($this->myauth->popPreviousUrl())->with('error', $this->message); } catch (\Exception $e) { //Transaction Rollback $this->getModel()->transRollback(); @@ -336,7 +334,7 @@ abstract class MVController extends CommonController $this->order_value = $this->request->getVar('order_value') ?: DEFAULTS['EMPTY']; $this->getModel()->setList_OrderBy( $this->order_field !== DEFAULTS['EMPTY'] && - $this->order_value !== DEFAULTS['EMPTY'] ? "{$this->order_field} {$this->order_value}" : "" + $this->order_value !== DEFAULTS['EMPTY'] ? "{$this->order_field} {$this->order_value}" : "" ); if ($this->page) { $this->getModel()->limit( @@ -361,7 +359,7 @@ abstract class MVController extends CommonController //모델 처리 $this->entitys = $this->list_entitys_process(); // 현재 URL을 스택에 저장 - $this->pushCurrentUrl(); + $this->myauth->pushCurrentUrl(current_url() . $this->request->getUri()->getQuery() ? "?" . $this->request->getUri()->getQuery() : ""); return view( $this->view_path . "index", ['viewDatas' => $this->getViewDatas()] diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index 8de601a..26c0ad3 100644 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -108,7 +108,7 @@ class UserController extends FrontController $this->message = "로그인 성공"; log_message("notice", __FUNCTION__ . $this->message); // 이전 URL로 리다이렉트 - return redirect()->to($this->popPreviousUrl())->with('message', $this->message); + 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()); @@ -127,7 +127,7 @@ class UserController extends FrontController $this->message = "로그인 성공"; log_message("notice", __FUNCTION__ . $this->message); // 이전 URL로 리다이렉트 - return redirect()->to($this->popPreviousUrl())->with('message', $this->message); + 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()); diff --git a/app/Filters/AuthFilter.php b/app/Filters/AuthFilter.php index 4622bf7..2299a96 100644 --- a/app/Filters/AuthFilter.php +++ b/app/Filters/AuthFilter.php @@ -30,16 +30,18 @@ class AuthFilter implements FilterInterface // log_message("debug", var_export($arguments, true)); // 로그인 않했으면 if (!$auth->isLoggedIn()) { - $urlStack = session()->get('url_stack') ?? []; - $urlStack[] = current_url() . $request->getUri()->getQuery() ? "?" . $request->getUri()->getQuery() : ""; - session()->set('url_stack', $urlStack); + $currentURL = current_url(); + $queryString = $request->getUri()->getQuery(); + $auth->pushCurrentUrl($queryString ? "{$currentURL}?{$queryString}" : $currentURL); return redirect()->to(URLS['LOGIN'])->with('error', '로그인을하셔야합니다.'); } //User Role 비교 // 회원 ROLES이 필요ROLE($arguments[0]) 목록에 존재하지 않으면(ACL) - $auth = session()->get(SESSION_NAMES['AUTH']); - $roles = explode(DEFAULTS['DELIMITER_ROLE'], $auth['role']); - if (!isset($auth['role']) || !in_array($arguments[0], $roles)) { - return redirect()->back()->with('error', "회원[{$auth['name']}]님은 접속에 필요한 권한{$arguments[0]}이 없습니다. "); + if (!$auth->isAccessRole($arguments[0])) { + $auth->popPreviousUrl(); + return redirect()->back()->with( + 'error', + "회원[{$auth->getAuthInfo('name')}]님은 접속에 필요한 권한{$arguments[0]}이 없습니다. " + ); } } diff --git a/app/Libraries/MyAuth/MyAuth.php b/app/Libraries/MyAuth/MyAuth.php index 529e7af..41540df 100644 --- a/app/Libraries/MyAuth/MyAuth.php +++ b/app/Libraries/MyAuth/MyAuth.php @@ -4,24 +4,63 @@ namespace App\Libraries\MyAuth; use App\Entities\UserEntity; use App\Libraries\CommonLibrary; - +use CodeIgniter\Session\Session; // 참고:https://github.com/SyntaxPhoenix/iloclient class MyAuth extends CommonLibrary { - private $_session = null; - public function __construct() + private ?Session $_session = null; + public function __construct() {} + final public function getSession(): Session { - $this->_session = \Config\Services::session(); + if ($this->_session == null) { + $this->_session = \Config\Services::session(); + } + return $this->_session; } + final public function getAuthInfo(string $key = ""): array|string + { + if ($key) { + return isset($this->getSession()->get(SESSION_NAMES['AUTH'])[$key]) ? $this->getSession()->get(SESSION_NAMES['AUTH'])[$key] : ""; + } + return $this->getSession()->get(SESSION_NAMES['AUTH']); + } + final public function isLoggedIn(): bool { - return $this->_session->has(SESSION_NAMES['ISLOGIN']); + return $this->getSession()->has(SESSION_NAMES['ISLOGIN']); + } + final public function isAccessRole(string $role): bool + { + if ($this->getAuthInfo('role') === "") { + return false; + } + $roles = explode(DEFAULTS['DELIMITER_ROLE'], $this->getAuthInfo('role')); + if (!in_array($role, $roles)) { + return false; + } + return true; + } + + final public function pushCurrentUrl(string $url): void + { + $urlStack = $this->getSession()->get('url_stack') ?? []; + $urlStack[] = $url; + $this->getSession()->set('url_stack', $urlStack); + } + + final public function popPreviousUrl() + { + $urlStack = $this->getSession()->get('url_stack') ?? []; + if (!empty($urlStack)) { + return array_pop($urlStack); + } + return '/'; // 기본 URL } final public function login(UserEntity $entity): void { - $this->_session->set(SESSION_NAMES['ISLOGIN'], true); - $this->_session->set(SESSION_NAMES['AUTH'], [ + $this->getSession()->set(SESSION_NAMES['ISLOGIN'], true); + $this->getSession()->set(SESSION_NAMES['AUTH'], [ 'uid' => $entity->getPK(), 'name' => $entity->getTitle(), 'role' => $entity->role @@ -31,11 +70,11 @@ class MyAuth extends CommonLibrary final public function logout(): void { // 세션 데이터 삭제 - $this->_session->remove(SESSION_NAMES['ISLOGIN']); - $this->_session->remove(SESSION_NAMES['AUTH']); + $this->getSession()->remove(SESSION_NAMES['ISLOGIN']); + $this->getSession()->remove(SESSION_NAMES['AUTH']); // 모든 세션 데이터 삭제 - $this->_session->destroy(); + $this->getSession()->destroy(); // 세션 쿠키 삭제 if (ini_get("session.use_cookies")) { @@ -53,6 +92,6 @@ class MyAuth extends CommonLibrary // 세션 재생성 session_start(); - $this->_session->regenerate(true); + $this->getSession()->regenerate(true); } } diff --git a/app/Libraries/MySocket/GoogleSocket/API.php b/app/Libraries/MySocket/GoogleSocket/API.php index ab19aa5..16b0a1a 100644 --- a/app/Libraries/MySocket/GoogleSocket/API.php +++ b/app/Libraries/MySocket/GoogleSocket/API.php @@ -84,7 +84,7 @@ class API extends GoogleSocket $this->getClient()->refreshToken($tokenInfo['refresh_token']); } // 세션에 Token 값 설정 - $this->session->set($this->_token_name, $this->_access_token); + $this->getSession()->set($this->_token_name, $this->_access_token); } // DEBUG - 2024-10-10 12:00:13 --> \Google\Service\Oauth2\Userinfo::__set_state(array( diff --git a/app/Libraries/MySocket/GoogleSocket/CURL.php b/app/Libraries/MySocket/GoogleSocket/CURL.php index 2252668..7f54846 100644 --- a/app/Libraries/MySocket/GoogleSocket/CURL.php +++ b/app/Libraries/MySocket/GoogleSocket/CURL.php @@ -102,7 +102,7 @@ class CURL extends GoogleSocket // 토큰 정보 가져오기 $this->_access_token = $tokenInfo[$this->_token_name]; // 세션에 Token 값 설정 - $this->session->set($this->_token_name, $this->_access_token); + $this->getSession()->set($this->_token_name, $this->_access_token); } // throw new \Exception(__METHOD__ . "에서 데이터 처리 필요"); diff --git a/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php b/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php index 822db7a..f4cc27d 100644 --- a/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php +++ b/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php @@ -2,7 +2,7 @@ namespace App\Libraries\MySocket\GoogleSocket; -use Config\Services; +use CodeIgniter\Session\Session; use CodeIgniter\Exceptions\PageNotFoundException; use App\Models\UserSNSModel; use App\Libraries\MySocket\MySocket; @@ -13,19 +13,23 @@ abstract class GoogleSocket extends MySocket private string $_site = "GOOGLE"; private ?UserSNSModel $_model = null; protected $_client = null; - protected $session; + private ?Session $_session = null; protected string $_access_token = ""; protected string $_token_name = "access_token"; - public function __construct() - { - $this->session = Services::session(); - } + public function __construct() {} abstract public function createAuthUrl(): string; abstract public function setToken(string $access_code): void; abstract public function getUserSNSEntity(): UserSNSEntity; + final public function getSession(): Session + { + if ($this->_session == null) { + $this->_session = \Config\Services::session(); + } + return $this->_session; + } final public function getToken(): string { - return $this->session->get($this->_token_name); + return $this->getSession()->get($this->_token_name); } final public function getSite(): string { diff --git a/app/Views/layouts/admin/top.php b/app/Views/layouts/admin/top.php index 93f51ea..c2be2e2 100644 --- a/app/Views/layouts/admin/top.php +++ b/app/Views/layouts/admin/top.php @@ -21,10 +21,10 @@