cfmgrv4 init...2

This commit is contained in:
최준흠 2024-10-14 09:53:44 +09:00
parent d7e48a4ac8
commit 6849f34934
9 changed files with 84 additions and 58 deletions

View File

@ -13,8 +13,7 @@ abstract class CommonController extends BaseController
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{ {
parent::initController($request, $response, $logger); parent::initController($request, $response, $logger);
// 세션 서비스 초기화 $this->myauth = service('myauth');
$this->session = \Config\Services::session();
} }
final public function __get($name) final public function __get($name)
{ {
@ -31,20 +30,4 @@ abstract class CommonController extends BaseController
{ {
return $this->_viewDatas; 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
}
} }

View File

@ -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 final protected function create_form_procedure(): RedirectResponse|string
{ {
try { try {
@ -214,7 +212,7 @@ abstract class MVController extends CommonController
$this->message = "{$this->class_name} : 일괄처리작업이 완료되었습니다."; $this->message = "{$this->class_name} : 일괄처리작업이 완료되었습니다.";
log_message("notice", __FUNCTION__ . $this->message); log_message("notice", __FUNCTION__ . $this->message);
// 이전 URL로 리다이렉트 // 이전 URL로 리다이렉트
return redirect()->to($this->popPreviousUrl())->with('error', $this->message); return redirect()->to($this->myauth->popPreviousUrl())->with('error', $this->message);
} catch (\Exception $e) { } catch (\Exception $e) {
//Transaction Rollback //Transaction Rollback
$this->getModel()->transRollback(); $this->getModel()->transRollback();
@ -235,7 +233,7 @@ abstract class MVController extends CommonController
$this->getModel()->transCommit(); $this->getModel()->transCommit();
log_message("notice", __FUNCTION__ . $this->message); log_message("notice", __FUNCTION__ . $this->message);
// 이전 URL로 리다이렉트 // 이전 URL로 리다이렉트
return redirect()->to($this->popPreviousUrl())->with('error', $this->message); return redirect()->to($this->myauth->popPreviousUrl())->with('error', $this->message);
} catch (\Exception $e) { } catch (\Exception $e) {
//Transaction Rollback //Transaction Rollback
$this->getModel()->transRollback(); $this->getModel()->transRollback();
@ -263,7 +261,7 @@ abstract class MVController extends CommonController
$this->getModel()->transCommit(); $this->getModel()->transCommit();
log_message("notice", __FUNCTION__ . $this->message); log_message("notice", __FUNCTION__ . $this->message);
// 이전 URL로 리다이렉트 // 이전 URL로 리다이렉트
return redirect()->to($this->popPreviousUrl())->with('error', $this->message); return redirect()->to($this->myauth->popPreviousUrl())->with('error', $this->message);
} catch (\Exception $e) { } catch (\Exception $e) {
//Transaction Rollback //Transaction Rollback
$this->getModel()->transRollback(); $this->getModel()->transRollback();
@ -336,7 +334,7 @@ abstract class MVController extends CommonController
$this->order_value = $this->request->getVar('order_value') ?: DEFAULTS['EMPTY']; $this->order_value = $this->request->getVar('order_value') ?: DEFAULTS['EMPTY'];
$this->getModel()->setList_OrderBy( $this->getModel()->setList_OrderBy(
$this->order_field !== DEFAULTS['EMPTY'] && $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) { if ($this->page) {
$this->getModel()->limit( $this->getModel()->limit(
@ -361,7 +359,7 @@ abstract class MVController extends CommonController
//모델 처리 //모델 처리
$this->entitys = $this->list_entitys_process(); $this->entitys = $this->list_entitys_process();
// 현재 URL을 스택에 저장 // 현재 URL을 스택에 저장
$this->pushCurrentUrl(); $this->myauth->pushCurrentUrl(current_url() . $this->request->getUri()->getQuery() ? "?" . $this->request->getUri()->getQuery() : "");
return view( return view(
$this->view_path . "index", $this->view_path . "index",
['viewDatas' => $this->getViewDatas()] ['viewDatas' => $this->getViewDatas()]

View File

@ -108,7 +108,7 @@ class UserController extends FrontController
$this->message = "로그인 성공"; $this->message = "로그인 성공";
log_message("notice", __FUNCTION__ . $this->message); log_message("notice", __FUNCTION__ . $this->message);
// 이전 URL로 리다이렉트 // 이전 URL로 리다이렉트
return redirect()->to($this->popPreviousUrl())->with('message', $this->message); return redirect()->to($this->myauth->popPreviousUrl())->with('message', $this->message);
} catch (\Exception $e) { } catch (\Exception $e) {
log_message("error", $e->getMessage()); log_message("error", $e->getMessage());
return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage()); return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());
@ -127,7 +127,7 @@ class UserController extends FrontController
$this->message = "로그인 성공"; $this->message = "로그인 성공";
log_message("notice", __FUNCTION__ . $this->message); log_message("notice", __FUNCTION__ . $this->message);
// 이전 URL로 리다이렉트 // 이전 URL로 리다이렉트
return redirect()->to($this->popPreviousUrl())->with('message', $this->message); return redirect()->to($this->myauth->popPreviousUrl())->with('message', $this->message);
} catch (\Exception $e) { } catch (\Exception $e) {
log_message("error", $e->getMessage()); log_message("error", $e->getMessage());
return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage()); return redirect()->back()->withInput()->with('error', __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage());

View File

@ -30,16 +30,18 @@ class AuthFilter implements FilterInterface
// log_message("debug", var_export($arguments, true)); // log_message("debug", var_export($arguments, true));
// 로그인 않했으면 // 로그인 않했으면
if (!$auth->isLoggedIn()) { if (!$auth->isLoggedIn()) {
$urlStack = session()->get('url_stack') ?? []; $currentURL = current_url();
$urlStack[] = current_url() . $request->getUri()->getQuery() ? "?" . $request->getUri()->getQuery() : ""; $queryString = $request->getUri()->getQuery();
session()->set('url_stack', $urlStack); $auth->pushCurrentUrl($queryString ? "{$currentURL}?{$queryString}" : $currentURL);
return redirect()->to(URLS['LOGIN'])->with('error', '로그인을하셔야합니다.'); return redirect()->to(URLS['LOGIN'])->with('error', '로그인을하셔야합니다.');
} }
//User Role 비교 // 회원 ROLES이 필요ROLE($arguments[0]) 목록에 존재하지 않으면(ACL) //User Role 비교 // 회원 ROLES이 필요ROLE($arguments[0]) 목록에 존재하지 않으면(ACL)
$auth = session()->get(SESSION_NAMES['AUTH']); if (!$auth->isAccessRole($arguments[0])) {
$roles = explode(DEFAULTS['DELIMITER_ROLE'], $auth['role']); $auth->popPreviousUrl();
if (!isset($auth['role']) || !in_array($arguments[0], $roles)) { return redirect()->back()->with(
return redirect()->back()->with('error', "회원[{$auth['name']}]님은 접속에 필요한 권한{$arguments[0]}이 없습니다. "); 'error',
"회원[{$auth->getAuthInfo('name')}]님은 접속에 필요한 권한{$arguments[0]}이 없습니다. "
);
} }
} }

View File

@ -4,24 +4,63 @@ namespace App\Libraries\MyAuth;
use App\Entities\UserEntity; use App\Entities\UserEntity;
use App\Libraries\CommonLibrary; use App\Libraries\CommonLibrary;
use CodeIgniter\Session\Session;
// 참고:https://github.com/SyntaxPhoenix/iloclient // 참고:https://github.com/SyntaxPhoenix/iloclient
class MyAuth extends CommonLibrary class MyAuth extends CommonLibrary
{ {
private $_session = null; private ?Session $_session = null;
public function __construct() 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 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 final public function login(UserEntity $entity): void
{ {
$this->_session->set(SESSION_NAMES['ISLOGIN'], true); $this->getSession()->set(SESSION_NAMES['ISLOGIN'], true);
$this->_session->set(SESSION_NAMES['AUTH'], [ $this->getSession()->set(SESSION_NAMES['AUTH'], [
'uid' => $entity->getPK(), 'uid' => $entity->getPK(),
'name' => $entity->getTitle(), 'name' => $entity->getTitle(),
'role' => $entity->role 'role' => $entity->role
@ -31,11 +70,11 @@ class MyAuth extends CommonLibrary
final public function logout(): void final public function logout(): void
{ {
// 세션 데이터 삭제 // 세션 데이터 삭제
$this->_session->remove(SESSION_NAMES['ISLOGIN']); $this->getSession()->remove(SESSION_NAMES['ISLOGIN']);
$this->_session->remove(SESSION_NAMES['AUTH']); $this->getSession()->remove(SESSION_NAMES['AUTH']);
// 모든 세션 데이터 삭제 // 모든 세션 데이터 삭제
$this->_session->destroy(); $this->getSession()->destroy();
// 세션 쿠키 삭제 // 세션 쿠키 삭제
if (ini_get("session.use_cookies")) { if (ini_get("session.use_cookies")) {
@ -53,6 +92,6 @@ class MyAuth extends CommonLibrary
// 세션 재생성 // 세션 재생성
session_start(); session_start();
$this->_session->regenerate(true); $this->getSession()->regenerate(true);
} }
} }

View File

@ -84,7 +84,7 @@ class API extends GoogleSocket
$this->getClient()->refreshToken($tokenInfo['refresh_token']); $this->getClient()->refreshToken($tokenInfo['refresh_token']);
} }
// 세션에 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( // DEBUG - 2024-10-10 12:00:13 --> \Google\Service\Oauth2\Userinfo::__set_state(array(

View File

@ -102,7 +102,7 @@ class CURL extends GoogleSocket
// 토큰 정보 가져오기 // 토큰 정보 가져오기
$this->_access_token = $tokenInfo[$this->_token_name]; $this->_access_token = $tokenInfo[$this->_token_name];
// 세션에 Token 값 설정 // 세션에 Token 값 설정
$this->session->set($this->_token_name, $this->_access_token); $this->getSession()->set($this->_token_name, $this->_access_token);
} }
// throw new \Exception(__METHOD__ . "에서 데이터 처리 필요"); // throw new \Exception(__METHOD__ . "에서 데이터 처리 필요");

View File

@ -2,7 +2,7 @@
namespace App\Libraries\MySocket\GoogleSocket; namespace App\Libraries\MySocket\GoogleSocket;
use Config\Services; use CodeIgniter\Session\Session;
use CodeIgniter\Exceptions\PageNotFoundException; use CodeIgniter\Exceptions\PageNotFoundException;
use App\Models\UserSNSModel; use App\Models\UserSNSModel;
use App\Libraries\MySocket\MySocket; use App\Libraries\MySocket\MySocket;
@ -13,19 +13,23 @@ abstract class GoogleSocket extends MySocket
private string $_site = "GOOGLE"; private string $_site = "GOOGLE";
private ?UserSNSModel $_model = null; private ?UserSNSModel $_model = null;
protected $_client = null; protected $_client = null;
protected $session; private ?Session $_session = null;
protected string $_access_token = ""; protected string $_access_token = "";
protected string $_token_name = "access_token"; protected string $_token_name = "access_token";
public function __construct() public function __construct() {}
{
$this->session = Services::session();
}
abstract public function createAuthUrl(): string; abstract public function createAuthUrl(): string;
abstract public function setToken(string $access_code): void; abstract public function setToken(string $access_code): void;
abstract public function getUserSNSEntity(): UserSNSEntity; 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 final public function getToken(): string
{ {
return $this->session->get($this->_token_name); return $this->getSession()->get($this->_token_name);
} }
final public function getSite(): string final public function getSite(): string
{ {

View File

@ -21,10 +21,10 @@
</ul> </ul>
<ul class="nav justify-content-end"> <ul class="nav justify-content-end">
<li class="nav-item"> <li class="nav-item">
<?php if ($viewDatas['session']->get(SESSION_NAMES['ISLOGIN'])): ?> <?php if ($viewDatas['myauth']->isLoggedIn()): ?>
<div class="btn-group"> <div class="btn-group">
<button type="button" class="btn btn-outline-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false"> <button type="button" class="btn btn-outline-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">
<b><?= ICONS['LOGIN'] . $viewDatas['session']->get(SESSION_NAMES['AUTH'])['name'] ?></b> <b><?= ICONS['LOGIN'] . $viewDatas['myauth']->getAuthInfo('name') ?></b>
</button> </button>
<ul class="dropdown-menu dropdown-menu-end"> <ul class="dropdown-menu dropdown-menu-end">
<li><?= form_label( <li><?= form_label(
@ -32,7 +32,7 @@
"modify", "modify",
[ [
"class" => "dropdown-item", "class" => "dropdown-item",
"data-src" => "/admin/user/modify/" . $viewDatas['session']->get(SESSION_NAMES['AUTH'])['uid'], "data-src" => "/admin/user/modify/" . $viewDatas['myauth']->getAuthInfo('uid'),
"data-bs-toggle" => "modal", "data-bs-toggle" => "modal",
"data-bs-target" => "#index_action_form" "data-bs-target" => "#index_action_form"
] ]