diff --git a/app/Config/Constants.php b/app/Config/Constants.php index 6258547..b7ca92c 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -123,13 +123,15 @@ define('MESSAGES', [ 'NOT_SYNC_NOTHING' => '동기화할 데이터가 없습니다.', 'NOT_SYNC_NOTHING_RESULT' => '동기화 결과가 없습니다.', 'NOT_SYNC_NOTHING_ERROR' => '동기화 결과가 없습니다.', + 'LOGIN' => '로그인 하셨습니다.', + 'LOGOUT' => '로그아웃 하셨습니다.' ]); //URL define('URLS', [ - 'LOGIN' => '/user/login', - 'GOOGLE_LOGIN' => '/user/google_login', - 'SIGNUP' => '/user/signup', - 'LOGOUT' => '/user/logout', + 'LOGIN' => '/auth/login', + 'GOOGLE_LOGIN' => '/auth/google_login', + 'SIGNUP' => '/auth/signup', + 'LOGOUT' => '/auth/logout', ]); //회원ROLE define('ROLES', [ diff --git a/app/Config/Routes.php b/app/Config/Routes.php index 8ec1d0d..f1c911a 100644 --- a/app/Config/Routes.php +++ b/app/Config/Routes.php @@ -14,11 +14,11 @@ $routes->addPlaceholder('uuid', '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4} $routes->group('cli', ['namespace' => 'App\Controllers\CLI'], function ($routes) {}); $routes->group('', ['namespace' => 'App\Controllers'], function ($routes) { $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'); + $routes->group('auth', ['namespace' => 'App\Controllers\Auth'], function ($routes) { + $routes->get('login', 'LocalController::login_form'); + $routes->post('login', 'LocalController::login'); + $routes->get('google_login', 'GoogleController::login'); + $routes->get('logout', 'LocalController::logout'); }); }); $routes->group('admin', ['namespace' => 'App\Controllers\Admin', 'filter' => 'authFilter:manager'], function ($routes) { diff --git a/app/Controllers/Admin/MyLogController.php b/app/Controllers/Admin/MyLogController.php index 52d240a..bb238e3 100644 --- a/app/Controllers/Admin/MyLogController.php +++ b/app/Controllers/Admin/MyLogController.php @@ -69,8 +69,9 @@ class MyLogController extends AdminController protected function view_process($uid): mixed { $fields = [ - 'fields' => ['user_uid', 'class_name', 'method_name', $this->getService()->getModel()::TITLE, 'created_at', 'status', 'content'], + 'fields' => ['user_uid', 'class_name', 'method_name', $this->getService()->getModel()::TITLE, 'status', 'created_at', 'content'], ]; + $this->init('view', $fields); return parent::view_process($uid); } } diff --git a/app/Controllers/Auth/AuthController.php b/app/Controllers/Auth/AuthController.php new file mode 100644 index 0000000..1f29128 --- /dev/null +++ b/app/Controllers/Auth/AuthController.php @@ -0,0 +1,109 @@ +layout = "auth"; + $this->uri_path = "auth/"; + $this->view_path = "auth" . DIRECTORY_SEPARATOR; + } + abstract protected function login_process(): Entity; + + final public function getSocket() + { + if (!$this->_socket) { + $this->_socket = new GoogleSocket(); + } + return $this->_socket; + } + final public function getUserService(): UserService + { + if (!$this->_userService) { + $this->_userService = new UserService($this->request); + } + return $this->_userService; + } + + //Index,FieldForm관련 + public function getFields(): array + { + return ['id', 'passwd']; + } + public function getFilterFields(): array + { + return ['role', 'status']; + } + public function getBatchJobFields(): array + { + return ['status']; + } + //Index,FieldForm관련 + + protected function getResultPageByActon(string $action, string $message = MESSAGES["SUCCESS"]): RedirectResponse|string + { + switch ($action) { + case 'login_form': + $result = view($this->view_path . $action, ['viewDatas' => $this->getViewDatas()]); + break; + default: + $result = parent::getResultPageByActon($action, $message); + break; + } + return $result; + } + + //로그인화면 + final public function login_form(): RedirectResponse|string + { + try { + $this->init(__FUNCTION__); + helper(['form']); + //구글 로그인 BUTTON용 + $this->google_url = $this->getSocket()->createAuthUrl(); + $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []]; + return $this->getResultPageByActon($this->action); + } catch (\Exception $e) { + return redirect()->back()->withInput()->with('error', $e->getMessage()); + } + } + //로그인 + final public function login(): RedirectResponse|string + { + try { + $this->entity = $this->login_process(); + return $this->getResultPageByActon($this->action, MESSAGES['LOGIN']); + } catch (\Exception $e) { + return redirect()->back()->withInput()->with('error', $e->getMessage()); + } + } + //로그아웃 + final public function logout(): RedirectResponse + { + try { + $this->init(__FUNCTION__); + $this->getService()->logout(); + // 홈페이지로 리다이렉트 + return redirect()->route('/')->with('error', MESSAGES['LOGOUT']); + } catch (\Exception $e) { + log_message("error", $e->getMessage()); + return redirect()->back()->with('error', "로그아웃 중 오류가 발생했습니다."); + } + } +} diff --git a/app/Controllers/Auth/GoogleController.php b/app/Controllers/Auth/GoogleController.php new file mode 100644 index 0000000..3456c12 --- /dev/null +++ b/app/Controllers/Auth/GoogleController.php @@ -0,0 +1,41 @@ +uri_path .= strtolower($this->getService()->getClassName()) . '/'; + $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->getSocket(), $this->request); + } + return $this->_service; + } + + //로그인처리 + protected function login_process(): Entity + { + $access_code = $this->request->getVar('code'); + if (!$access_code) { + throw new \Exception("구글 로그인 실패"); + } + return $this->getService()->login($this->getService()->checkUser($access_code)); + } +} diff --git a/app/Controllers/Auth/LocalController.php b/app/Controllers/Auth/LocalController.php new file mode 100644 index 0000000..4b1e878 --- /dev/null +++ b/app/Controllers/Auth/LocalController.php @@ -0,0 +1,39 @@ +uri_path .= strtolower($this->getService()->getClassName()) . '/'; + $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; + } + + //로그인처리 + protected function login_process(): Entity + { + $this->init(__FUNCTION__); + $this->formDatas = $this->doValidate($this->action, $this->fields); + return $this->getService()->login($this->getService()->checkUser($this->formDatas)); + } +} diff --git a/app/Controllers/CommonController.php b/app/Controllers/CommonController.php index d4da23b..58e0651 100644 --- a/app/Controllers/CommonController.php +++ b/app/Controllers/CommonController.php @@ -115,7 +115,7 @@ abstract class CommonController extends BaseController { switch ($field) { default: - $validation->setRule($field, $field, $rule ?? $this->getService()->getModel()->getFieldRule($action, $field)); + $validation->setRule($field, $field, $rule ?? $this->getService()->getFieldRule($action, $field)); break; } return $validation; @@ -133,10 +133,12 @@ abstract class CommonController extends BaseController $this->batchjob_fields = array_key_exists('batchjob_fields', $fields) && is_array($fields['batchjob_fields']) && count($fields['batchjob_fields']) ? $fields['filter_fields'] : $this->getBatchJobFields(); } //데이터 검증 - final protected function doValidate(string $action, array $fields): array + final protected function doValidate(string $action, array $fields, ?Validation $validation = null): array { //변경할 값 확인 : Upload된 파일 검증시 $this->request->getPOST()보다 먼처 체크필요 - $validation = service('validation'); + if (!$validation) { + $validation = service('validation'); + } // var_dump($this->field_rules); // exit; foreach ($fields as $field) { @@ -191,12 +193,7 @@ abstract class CommonController extends BaseController //수정관련 protected function modify_form_process(mixed $uid): mixed { - $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); - $entity = $this->getService()->getEntity(); - if (!$entity) { - throw new \Exception("해당 정보를 찾을수 없습니다."); - } - return $entity; + return $this->getService()->getEntity($uid); } final public function modify_form(mixed $uid): RedirectResponse|string { @@ -215,11 +212,7 @@ abstract class CommonController extends BaseController //데이터 검증 $this->formDatas = $this->doValidate($this->action, $this->fields); //자신정보정의 - $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); - $entity = $this->getService()->getEntity(); - if (!$entity) { - throw new \Exception(__FUNCTION__ . " => {$uid} 정보를 찾을수 없습니다."); - } + $entity = $this->getService()->getEntity($uid); return $this->getService()->modify($entity, $this->formDatas); } final public function modify(int $uid): RedirectResponse|string @@ -245,11 +238,7 @@ abstract class CommonController extends BaseController //데이터 검증 $this->formDatas = $this->doValidate($this->action, $this->fields); //자신정보정의 - $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); - $entity = $this->getService()->getEntity(); - if (!$entity) { - throw new \Exception(__FUNCTION__ . " => {$uid} 정보를 찾을수 없습니다."); - } + $entity = $this->getService()->getEntity($uid); return $this->getService()->modify($entity, $this->formDatas); } final public function toggle(mixed $uid, string $field): RedirectResponse @@ -270,23 +259,19 @@ abstract class CommonController extends BaseController } } //일괄처리작업 - protected function batchjob_process(array $uids): array + protected function batchjob_process(array $entities): array { //데이터 검증 $this->formDatas = $this->doValidate($this->action, $this->fields); $temps = []; - foreach ($uids as $uid) { - $entity = $this->getService()->getEntity(); - if (!$entity) { - throw new \Exception("{$uid} 정보를 찾을수 없습니다."); - } - $temps[] = $entity; + foreach ($entities as $entity) { + // echo var_dump($entity->toArray()); + // echo "
"; + // echo $this->formDatas['status']; + // exit; + $temps[] = $this->getService()->modify($entity, $this->formDatas); } - $entities = []; - foreach ($temps as $entity) { - $entities[] = $this->getService()->modify($entity, $this->formDatas); - } - return $entities; + return $temps; } final public function batchjob(): RedirectResponse { @@ -295,8 +280,8 @@ abstract class CommonController extends BaseController try { $this->init(__FUNCTION__); //변경할 UIDS - $uids = $this->request->getVar('batchjob_uids'); - if (!$uids) { + $uids = $this->request->getVar('batchjob_uids[]'); + if (!is_array($uids) || !count($uids)) { throw new \Exception("적용할 리스트를 선택하셔야합니다."); } //데이터가 있는경우 Field만 처리하기위해 @@ -306,8 +291,16 @@ abstract class CommonController extends BaseController $fields[] = $field; } } + if (!count($fields)) { + throw new \Exception("변경할 정보를 선택하셔야합니다."); + } $this->fields = $fields; - $this->entities = $this->batchjob_process(explode(",", $uids)); + $entities = []; + foreach ($uids as $uid) { + $entity = $this->getService()->getEntity($uid); + $entities[] = $entity; + } + $this->entities = $this->batchjob_process($entities); $this->getService()->getModel()->transCommit(); $this->getMyLogService()->save($this->getService(), __FUNCTION__, $this->myauth, MESSAGES["SUCCESS"]); return $this->getResultPageByActon($this->action); @@ -333,11 +326,7 @@ abstract class CommonController extends BaseController $this->getService()->getModel()->transStart(); try { $this->init(__FUNCTION__); - $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); - $entity = $this->getService()->getEntity(); - if (!$entity) { - throw new \Exception("{$uid} 정보를 찾을수 없습니다."); - } + $entity = $this->getService()->getEntity($uid); $this->entity = $this->delete_process($entity); $this->getService()->getModel()->transCommit(); $this->getMyLogService()->save($this->getService(), __FUNCTION__, $this->myauth, MESSAGES["SUCCESS"]); @@ -349,27 +338,20 @@ abstract class CommonController extends BaseController } } //일괄삭제 - protected function batchjob_delete_process(array $uids): void + protected function batchjob_delete_process(array $entities): void { - $entities = []; - foreach ($uids as $uid) { - $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); - $entity = $this->getService()->getEntity(); - if (!$entity) { - throw new \Exception("{$uid} 정보를 찾을수 없습니다."); - } - $entities[] = $entity; - } - $fail_cnt = 0; + $cnt = 0; foreach ($entities as $entity) { $result = $this->getService()->delete($entity); if (!$result) { LogCollector::error("[{$entity->getTitle()}] 삭제실패"); - $fail_cnt++; + $cnt++; } } - if ($fail_cnt) { - throw new \Exception("총:" . count($entities) . "중에 " . $fail_cnt . "개를 실패"); + if ($cnt) { + $message = "총:" . count($entities) . "중에 " . $cnt . "개를 실패"; + LogCollector::error($message); + throw new \Exception($message); } } final public function batchjob_delete(): RedirectResponse|string @@ -379,11 +361,16 @@ abstract class CommonController extends BaseController try { $this->init(__FUNCTION__); //변경할 UIDS - $uids = $this->request->getVar('batchjob_uids'); - if (!$uids) { + $uids = $this->request->getVar('batchjob_uids[]'); + if (!is_array($uids) || !count($uids)) { throw new \Exception("적용할 리스트를 선택하셔야합니다."); } - $this->batchjob_delete_process(explode(",", $uids)); + $entities = []; + foreach ($uids as $uid) { + $entity = $this->getService()->getEntity($uid); + $entities[] = $entity; + } + $this->batchjob_delete_process($entities); $this->getService()->getModel()->transCommit(); $this->getMyLogService()->save($this->getService(), __FUNCTION__, $this->myauth, MESSAGES["SUCCESS"]); return $this->getResultPageByActon($this->action); @@ -398,8 +385,7 @@ abstract class CommonController extends BaseController protected function view_process($uid): mixed { //자신정보정의 - $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); - $entity = $this->getService()->getEntity(); + $entity = $this->getService()->getEntity($uid); if (!$entity) { throw new \Exception("해당 사용자정보를 찾을수 없습니다."); } @@ -531,7 +517,7 @@ abstract class CommonController extends BaseController case 'pdf': // string buffer에서 읽어오는 경우 $this->entities = $this->index_process(); - $html = view('templates' . DIRECTORY_SEPARATOR . $this->action, ['viewDatas' => $this->getViewDatas()]); + $html = view('templates' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . __FUNCTION__, ['viewDatas' => $this->getViewDatas()]); //data loading $reader = new Html(); $loaded_data = $reader->loadFromString($html); @@ -542,11 +528,7 @@ abstract class CommonController extends BaseController if (!$uid) { throw new \Exception("{$output_type}은 반드시 uid의 값이 필요합니다."); } - $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); - $this->entity = $this->getService()->getEntity(); - if (!$this->entity) { - throw new \Exception("{$uid} 정보를 찾을수 없습니다."); - } + $this->entity = $this->getService()->getEntity($uid); list($file_name, $uploaded_filename) = $this->entity->getDownlaodFile(); $full_path = WRITEPATH . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . $uploaded_filename; break; diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php deleted file mode 100644 index a93dbb1..0000000 --- a/app/Controllers/UserController.php +++ /dev/null @@ -1,122 +0,0 @@ -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', "로그아웃 중 오류가 발생했습니다."); - } - } -} diff --git a/app/Entities/CommonEntity.php b/app/Entities/CommonEntity.php index 49a0143..6d0c634 100644 --- a/app/Entities/CommonEntity.php +++ b/app/Entities/CommonEntity.php @@ -27,7 +27,7 @@ abstract class CommonEntity extends Entity $field = constant("static::TITLE"); return $this->attributes[$field]; } - final public function getUpdatedAt(): string + final public function getUpdatedAt(): string|null { return $this->attributes['updated_at']; } diff --git a/app/Helpers/AuthHelper.php b/app/Helpers/AuthHelper.php new file mode 100644 index 0000000..8195c5c --- /dev/null +++ b/app/Helpers/AuthHelper.php @@ -0,0 +1,31 @@ + "form-control", "required" => "", ...$extras] : ["class" => "form-control", ...$extras]; + } + $value = $value ?: DEFAULTS['EMPTY']; + switch ($field) { + case 'id': + case $this->getService()->getModel()::TITLE: + $form = form_input($field, $value, $extras); + break; + case 'passwd': + $form = form_password($field, "", ["autocomplete" => $field, ...$extras]); + break; + default: + $form = parent::getFieldForm($field, $value, $viewDatas, $extras); + break; + } + return $form; + } // +} diff --git a/app/Helpers/CommonHelper.php b/app/Helpers/CommonHelper.php index cd9cfb8..c29599c 100644 --- a/app/Helpers/CommonHelper.php +++ b/app/Helpers/CommonHelper.php @@ -288,14 +288,14 @@ class CommonHelper $action = form_submit("batchjob_submit", '일괄처리', [ "formaction" => current_url() . '/batchjob', "class" => "btn btn-outline btn-warning", - "onclick" => "return submitBatchJob()" + // "onclick" => "return submitBatchJob()" ]); break; case 'batchjob_delete': $action = form_submit("batchjob_submit", '일괄삭제', [ "formaction" => current_url() . '/batchjob_delete', "class" => "btn btn-outline btn-danger", - "onclick" => "return submitBatchJobDelete()" + // "onclick" => "return submitBatchJobDelete()" ]); break; } diff --git a/app/Libraries/MySocket/GoogleSocket/API.php b/app/Libraries/MySocket/GoogleSocket/API.php index 29c5b7e..f1ae508 100644 --- a/app/Libraries/MySocket/GoogleSocket/API.php +++ b/app/Libraries/MySocket/GoogleSocket/API.php @@ -115,14 +115,14 @@ class API extends GoogleSocket // 'picture' => 'https://lh3.googleusercontent.com/a/VDSJj3D925VP-pt9ppnwsPtm4pyYE6IO7bei-RyVM0Q=s96-c', // 'verifiedEmail' => true, // )) - public function getEntity(): Entity + public function signup(): Entity { $this->getClient()->setAccessToken($this->getToken()); $oauth = new Oauth2($this->getClient()); $userInfo = $oauth->userinfo->get(); $detail = var_export($userInfo, true); // log_message("debug", $detail); - // 사용자정보 설정하기 - return $this->setEntity($userInfo->id, $userInfo->name, $userInfo->email, $detail); + // 사용자정보 등록하기 + return $this->signup_process($userInfo->id, $userInfo->name, $userInfo->email, $detail); } } diff --git a/app/Libraries/MySocket/GoogleSocket/CURL.php b/app/Libraries/MySocket/GoogleSocket/CURL.php index 7e1eb90..a29b37b 100644 --- a/app/Libraries/MySocket/GoogleSocket/CURL.php +++ b/app/Libraries/MySocket/GoogleSocket/CURL.php @@ -133,7 +133,7 @@ class CURL extends GoogleSocket // 'picture' => 'https://lh3.googleusercontent.com/a/AAcHTteFSgefsdfsdRJBkJA2tBEmg4PQrvI1Ta_5IXu5=s96-c', // 'verifiedEmail' => true, // )) - public function getEntity(): Entity + public function signup(): Entity { $options = [ "headers" => [ @@ -165,7 +165,7 @@ class CURL extends GoogleSocket log_message("error", $message); throw new \Exception($message); } - // 사용자정보 설정하기 - return $this->setEntity($userInfo["id"], $userInfo["name"], $userInfo["email"], $detail); + // 사용자정보 등록하기 + return $this->signup_process($userInfo["id"], $userInfo["name"], $userInfo["email"], $detail); } } diff --git a/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php b/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php index cd3a285..77d4c48 100644 --- a/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php +++ b/app/Libraries/MySocket/GoogleSocket/GoogleSocket.php @@ -21,7 +21,7 @@ abstract class GoogleSocket extends MySocket public function __construct() {} abstract public function createAuthUrl(): string; abstract public function setToken(string $access_code): void; - abstract public function getEntity(): Entity; + abstract public function signup(): Entity; final public function getSession(): Session { if ($this->_session == null) { @@ -44,11 +44,10 @@ abstract class GoogleSocket extends MySocket } return $this->_service; } - final protected function setEntity(string $id, string $name, string $email, string $detail): Entity + final protected function signup_process(string $id, string $name, string $email, string $detail): Entity { - $this->getService()->getModel()->where(Model::SITE, $this->getSite()); - $this->getService()->getModel()->where('id', $id); - $entity = $this->getEntity(); + //이미 등록된 사용자인지 확인 후 없으면 등록 처리리 + $entity = $this->getService()->getEntity([Model::SITE => $this->getSite(), 'id' => $id], false); if (!$entity) { //Transaction Start $this->getService()->getModel()->transStart(); diff --git a/app/Models/CommonModel.php b/app/Models/CommonModel.php index 46afe74..dc03863 100644 --- a/app/Models/CommonModel.php +++ b/app/Models/CommonModel.php @@ -138,26 +138,22 @@ abstract class CommonModel extends Model private function save_process($entity): mixed { - try { - // 최종 변경사항이 없으면 - if (!$entity->hasChanged()) { - throw new \Exception(__FUNCTION__ . " 변경된 내용이 없습니다."); - } - // 최종 저장 시 오류 발생하면 - if (!$this->save($entity)) { - throw new \Exception(var_export($this->errors(), true)); - } + // 최종 변경사항이 없으면 + if (!$entity->hasChanged()) { return $entity; - } catch (\Exception $e) { + } + // 최종 저장 시 오류 발생하면 + if (!$this->save($entity)) { $message = sprintf( - "\n------%s SQL오류-----\n%s\n%s\n------------------------------\n", + "\n------%s 오류-----\n%s\n%s\n------------------------------\n", __FUNCTION__, $this->getLastQuery(), - $e->getMessage() + var_export($this->errors(), true) ); LogCollector::error($message); throw new \Exception($message); } + return $entity; } public function create(array $formDatas, mixed $entity): mixed { @@ -165,6 +161,7 @@ abstract class CommonModel extends Model $this->setValidationRules($this->getFieldRules('create', $this->allowedFields)); // 저장하기 전에 데이터 값 변경이 필요한 Field foreach (array_keys($formDatas) as $field) { + LogCollector::debug("{$field}:{$formDatas[$field]}"); $entity->$field = $this->convertEntityData($field, $formDatas); } // dd($entity); @@ -182,12 +179,12 @@ abstract class CommonModel extends Model // Field에 맞는 Validation Rule 재정의 $this->setValidationRules($this->getFieldRules('modify', $this->allowedFields)); // 저장하기 전에 데이터 값 변경이 필요한 Field + LogCollector::debug("[{$entity->getPK()}/{$entity->getTitle()}] 변경내용"); foreach (array_keys($formDatas) as $field) { + LogCollector::debug("{$field}/{$entity->$field}=>{$formDatas[$field]}"); $entity->$field = $this->convertEntityData($field, $formDatas); } - $this->save_process($entity); - // log_message("debug", $this->getTable() . " => " . __FUNCTION__ . " DB 작업 완료"); - return $entity; + return $this->save_process($entity); } //List관련 diff --git a/app/Services/Auth/AuthService.php b/app/Services/Auth/AuthService.php index 0534a09..1233e82 100644 --- a/app/Services/Auth/AuthService.php +++ b/app/Services/Auth/AuthService.php @@ -3,15 +3,18 @@ namespace App\Services\Auth; use App\Entities\UserEntity as Entity; -use App\Models\UserModel as Model; use App\Services\CommonService; +use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\Session\Session; // 참고:https://github.com/SyntaxPhoenix/iloclient abstract class AuthService extends CommonService { private ?Session $_session = null; - public function __construct() {} + public function __construct(?IncomingRequest $request = null) + { + parent::__construct($request); + } final public function getSession(): Session { if (!$this->_session) { @@ -80,10 +83,11 @@ abstract class AuthService extends CommonService return '/'; // 기본 URL } - final public function login(Entity $entity): void + public function login(Entity $entity): Entity { $this->getSession()->set(SESSION_NAMES['ISLOGIN'], true); $this->getSession()->set(SESSION_NAMES['AUTH'], ['uid' => $entity->getPK(), 'id' => $entity->getID(), 'name' => $entity->getTitle(), 'role' => $entity->role]); + return $entity; } final public function logout(): void diff --git a/app/Services/Auth/GoogleService.php b/app/Services/Auth/GoogleService.php index 02c3620..c0567d2 100644 --- a/app/Services/Auth/GoogleService.php +++ b/app/Services/Auth/GoogleService.php @@ -2,24 +2,23 @@ namespace App\Services\Auth; -use App\Models\UserModel as Model; use App\Entities\UserEntity as Entity; +use App\Models\UserModel as Model; // use App\Libraries\MySocket\GoogleSocket\CURL; use CodeIgniter\Exceptions\PageNotFoundException; +use CodeIgniter\HTTP\IncomingRequest; class GoogleService extends AuthService { private $_mySocket = null; - private string $_access_code = ""; - - public function __construct(mixed $mySocket) + public function __construct(mixed $mySocket, ?IncomingRequest $request = null) { $this->_mySocket = $mySocket; - parent::__construct(); + parent::__construct($request); } final public function getClassName(): string { - return "AuthGoogle"; + return "GoogleAuth"; } final public function getClassPath(): string { @@ -34,29 +33,24 @@ class GoogleService extends AuthService return Entity::class; } - public function getMySocket(string $access_code): mixed - { - if (!$this->_mySocket) { - throw new \Exception("Socket 방식이 지정되지 않았습니다."); - } - $this->_mySocket->setToken($this->_access_code); - return $this->_mySocket; - } - public function checkUser(string $access_code): Entity { try { // Google 서비스 설정 - $userSNS_entity = $this->getMySocket($access_code)->getUserSNSEntity(); + if (!$this->_mySocket) { + throw new \Exception("Socket 방식이 지정되지 않았습니다."); + } + $this->_mySocket->setToken($access_code); + $sns_entity = $this->_mySocket->signup(); // local db 사용와의 연결 확인 - $this->getModel()->where($this->getModel()::PK, $userSNS_entity->getParent()); + $this->getModel()->where($this->getModel()::PK, $sns_entity->getParent()); $entity = $this->getEntity(); if (!$entity) { - throw new PageNotFoundException("회원[{$userSNS_entity->getTitle()}]님은 아직 로컬사용자 연결이 이루어지지 않았습니다."); + throw new PageNotFoundException("회원[{$sns_entity->getTitle()}]님은 아직 로컬사용자 연결이 이루어지지 않았습니다."); } return $entity; } catch (\Google_Service_Exception $e) { - log_message('error', '구글 서비스 예외: ' . $e->getMessage()); + log_message('error', '구글 서비스 예외발생: ' . $e->getMessage()); throw new PageNotFoundException("구글 로그인 중 오류가 발생했습니다. 다시 시도해 주세요."); } catch (\Exception $e) { log_message('error', $e->getMessage()); diff --git a/app/Services/Auth/LocalService.php b/app/Services/Auth/LocalService.php index d0886e3..a91f3cf 100644 --- a/app/Services/Auth/LocalService.php +++ b/app/Services/Auth/LocalService.php @@ -2,18 +2,19 @@ namespace App\Services\Auth; -use App\Models\UserModel as Model; use App\Entities\UserEntity as Entity; +use App\Models\UserModel as Model; +use CodeIgniter\HTTP\IncomingRequest; class LocalService extends AuthService { - public function __construct() + public function __construct(?IncomingRequest $request = null) { - parent::__construct(); + parent::__construct($request); } final public function getClassName(): string { - return "AuthLocal"; + return "LocalAuth"; } final public function getClassPath(): string { @@ -30,13 +31,6 @@ class LocalService extends AuthService public function checkUser(array $formDatas): Entity { - if (!isset($formDatas['id']) || !$formDatas['id']) { - throw new \Exception("사용자ID를 입력해주세요!"); - } - if (!isset($formDatas['passwd']) || !$formDatas['passwd']) { - throw new \Exception("암호를 입력해주세요!"); - } - $this->getModel()->where("id", $formDatas['id']); $entity = $this->getEntity(); if (is_null($entity) || !isset($entity->passwd)) { diff --git a/app/Services/CommonService.php b/app/Services/CommonService.php index f2b618f..a583f92 100644 --- a/app/Services/CommonService.php +++ b/app/Services/CommonService.php @@ -10,7 +10,7 @@ abstract class CommonService private $_serviceDatas = []; private $_model = null; protected ?IncomingRequest $request = null; - public function __construct(IncomingRequest $_request) + public function __construct(?IncomingRequest $_request = null) { $this->request = $_request; } @@ -41,9 +41,15 @@ abstract class CommonService } return $this->_model; } - final public function getEntity(): mixed + final public function getEntity(mixed $where, $isThrow = true): mixed { - return $this->getModel()->first(); + $entity = is_array($where) ? $this->getModel()->where($where)->first() : $this->getModel()->find($where); + if (!$entity) { + if ($isThrow) { + throw new \Exception(__FUNCTION__ . " => 해당 정보를 찾을수 없습니다."); + } + } + return $entity; } final public function getEntities(array $columns = ['*']): array { @@ -87,7 +93,6 @@ abstract class CommonService } public function modify(mixed $entity, array $formDatas): mixed { - // die(var_export($formDatas, true)); $entity = $this->getModel()->modify($entity, $formDatas); LogCollector::info("[{$entity->getTitle()}]" . MESSAGES["UPDATED"] . ":"); return $entity; diff --git a/app/Services/MyLogService.php b/app/Services/MyLogService.php index 41e51d8..2752af4 100644 --- a/app/Services/MyLogService.php +++ b/app/Services/MyLogService.php @@ -10,7 +10,7 @@ use App\Libraries\LogCollector; class MyLogService extends CommonService { - public function __construct(IncomingRequest $request) + public function __construct(?IncomingRequest $request = null) { parent::__construct($request); } diff --git a/app/Services/UserService.php b/app/Services/UserService.php index b7827d1..3578ba8 100644 --- a/app/Services/UserService.php +++ b/app/Services/UserService.php @@ -9,7 +9,7 @@ use CodeIgniter\HTTP\IncomingRequest; class UserService extends CommonService { protected ?IncomingRequest $request = null; - public function __construct(IncomingRequest $request) + public function __construct(?IncomingRequest $request = null) { parent::__construct($request); } @@ -59,8 +59,4 @@ class UserService extends CommonService // die(var_export($formDatas, true)); return parent::modify($entity, $formDatas); } - public function delete(mixed $entity): bool - { - return parent::delete($entity); - } } diff --git a/app/Views/admin/index.php b/app/Views/admin/index.php index e150d46..3c11b21 100644 --- a/app/Views/admin/index.php +++ b/app/Views/admin/index.php @@ -17,6 +17,7 @@
include("templates/{$viewDatas['layout']}/index_content_top"); ?> + 'batchjob_form', 'method' => "post"]) ?> @@ -44,6 +45,7 @@
include("templates/{$viewDatas['layout']}/index_content_batchjob"); ?> +
include("templates/common/modal_fetch"); ?>
diff --git a/app/Views/user/login.php b/app/Views/auth/login_form.php similarity index 90% rename from app/Views/user/login.php rename to app/Views/auth/login_form.php index 395d19f..e1c63e0 100644 --- a/app/Views/user/login.php +++ b/app/Views/auth/login_form.php @@ -1,13 +1,14 @@ +section('content') ?> alert($error) ?>