diff --git a/app/Controllers/Admin/UserController.php b/app/Controllers/Admin/UserController.php index 3bfca73..4f824bf 100644 --- a/app/Controllers/Admin/UserController.php +++ b/app/Controllers/Admin/UserController.php @@ -40,22 +40,43 @@ class UserController extends AdminController { return ['status']; } - protected function setValidation(string $action, string $field, Validation $validation): Validation + protected function setValidation(string $action, string $field, string $rule, Validation $validation): Validation { switch ($field) { case 'role': //아래 Rule Array는 필드명.* checkbox를 사용 - $validation->setRule("{$field}.*", $field, $this->getService()->getModel()->getFieldRule($action, $field)); + $validation->setRule("{$field}.*", $field, $rule); break; default: - $validation = parent::setValidation($action, $field, $validation); + $validation = parent::setValidation($action, $field, $rule, $validation); break; } return $validation; } - //Index,FieldForm관련 + //Index,FieldForm관련. + + protected function getResultPageByActon(string $action, string $message = MESSAGES["SUCCESS"]): RedirectResponse|string + { + $result = parent::getResultPageByActon($action, $message); + switch ($action) { + case 'create': + case 'modify': + $url = strtolower(base_url() . $this->uri_path . $this->getService()->getClassName()) . "/view/" . $this->entity->getPK(); + $result = redirect()->to($url)->with('error', $message); + break; + } + return $result; + } //생성 + protected function create_form_process(): void + { + $fields = [ + 'fields' => ['id', 'passwd', 'confirmpassword', $this->getService()->getModel()::TITLE, 'email', 'mobile', 'role'], + ]; + $this->init('create_form', $fields); + parent::create_form_process(); + } protected function create_process(): mixed { $fields = [ @@ -65,6 +86,14 @@ class UserController extends AdminController return parent::create_process(); } //수정 + protected function modify_form_process(mixed $uid): mixed + { + $fields = [ + 'fields' => ['id', 'passwd', 'confirmpassword', $this->getService()->getModel()::TITLE, 'email', 'mobile', 'role'], + ]; + $this->init('modify_form', $fields); + return parent::modify_form_process($uid); + } protected function modify_process($uid): mixed { $fields = [ diff --git a/app/Controllers/CommonController.php b/app/Controllers/CommonController.php index a1533e3..7bd1d7f 100644 --- a/app/Controllers/CommonController.php +++ b/app/Controllers/CommonController.php @@ -48,6 +48,21 @@ abstract class CommonController extends BaseController } return $this->_service; } + protected function getResultPageByActon(string $action, string $message = MESSAGES["SUCCESS"]): RedirectResponse|string + { + switch ($action) { + case 'create_form': + case 'modify_form': + case 'index': + case 'view': + $result = view($this->view_path . $action, ['viewDatas' => $this->getViewDatas()]); + break; + default: + $result = redirect()->to($this->myauth->popPreviousUrl())->with('error', $message); + break; + } + return $result; + } final public function getViewDatas(): array { return $this->_viewDatas; @@ -72,23 +87,6 @@ abstract class CommonController extends BaseController } return $rules; } - protected function setValidation(string $action, string $field, Validation $validation): Validation - { - switch ($field) { - default: - $validation->setRule($field, $field, $this->getService()->getModel()->getFieldRule($action, $field)); - break; - } - return $validation; - } - final protected function getValidation(string $action, array $fields): Validation - { - $validation = service('validation'); - foreach ($fields as $field) { - $validation = $this->setValidation($action, $field, $validation); - } - return $validation; - } protected function getFormFieldOption(string $field, array $options): array { switch ($field) { @@ -111,6 +109,15 @@ abstract class CommonController extends BaseController // dd($options); return $options; } + protected function setValidation(string $action, string $field, string $rule, Validation $validation): Validation + { + switch ($field) { + default: + $validation->setRule($field, $field, $this->getService()->getModel()->getFieldRule($action, $field)); + break; + } + return $validation; + } //Index,FieldForm관련 //Field관련 @@ -127,7 +134,12 @@ abstract class CommonController extends BaseController final protected function doValidate(string $action, array $fields): array { //변경할 값 확인 : Upload된 파일 검증시 $this->request->getPOST()보다 먼처 체크필요 - $validation = $this->getValidation($action, $fields); + $validation = service('validation'); + // var_dump($this->field_rules); + // exit; + foreach ($this->field_rules as $field => $rule) { + $validation = $this->setValidation($this->action, $field, $rule, $validation); + } if (!$validation->withRequest($this->request)->run()) { throw new \Exception("{$this->getService()->getClassName()} 작업 데이터 검증 오류발생\n" . implode( "\n", @@ -142,17 +154,18 @@ abstract class CommonController extends BaseController public function create_form(): RedirectResponse|string { try { - $this->init('create'); + $this->init(__FUNCTION__); helper(['form']); $this->create_form_process(); $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []]; - return view($this->view_path . "create", data: ['viewDatas' => $this->getViewDatas()]); + return $this->getResultPageByActon($this->action); } catch (\Exception $e) { - return redirect()->back()->with('error', $e->getMessage()); + return redirect()->back()->withInput()->with('error', $e->getMessage()); } } protected function create_process(): mixed { + //데이터 검증 $this->formDatas = $this->doValidate($this->action, $this->fields); return $this->getService()->create($this->formDatas); } @@ -165,12 +178,7 @@ abstract class CommonController extends BaseController helper(['form']); $this->entity = $this->create_process(); $this->getService()->getModel()->transCommit(); - //평소에는 index로 전환됨 - $url = $this->myauth->popPreviousUrl(); - if ($this->redirect) { //redirect가 선언되어 있으면 - $url = strtolower(base_url() . $this->uri_path . $this->getService()->getClassName()) . "/" . $this->redirect . "/" . $this->entity->getPK(); - } - return redirect()->to($url)->with('error', MESSAGES["SUCCESS"]); + return $this->getResultPageByActon($this->action); } catch (\Exception $e) { //Transaction Rollback $this->getService()->getModel()->transRollback(); @@ -191,19 +199,18 @@ abstract class CommonController extends BaseController public function modify_form(mixed $uid): RedirectResponse|string { try { - $this->init('modify'); + $this->init(__FUNCTION__); helper(['form']); $this->entity = $this->modify_form_process($uid); $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []]; - return view($this->view_path . "modify", data: ['viewDatas' => $this->getViewDatas()]); + return $this->getResultPageByActon($this->action); } catch (\Exception $e) { - return redirect()->back()->with('error', $e->getMessage()); + return redirect()->back()->withInput()->with('error', $e->getMessage()); } } - - //modify,toggle,batchjob 공통사용 protected function modify_process(mixed $uid): mixed { + //데이터 검증 $this->formDatas = $this->doValidate($this->action, $this->fields); //자신정보정의 $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); @@ -222,12 +229,7 @@ abstract class CommonController extends BaseController helper(['form']); $this->entity = $this->modify_process($uid); $this->getService()->getModel()->transCommit(); - //평소에는 index로 전환됨 - $url = $this->myauth->popPreviousUrl(); - if ($this->redirect) { //redirect가 선언되어 있으면 - $url = strtolower(base_url() . $this->uri_path . $this->getService()->getClassName()) . "/" . $this->redirect . "/" . $this->entity->getPK(); - } - return redirect()->to($url)->with('error', MESSAGES["SUCCESS"]); + return $this->getResultPageByActon($this->action); } catch (\Exception $e) { //Transaction Rollback $this->getService()->getModel()->transRollback(); @@ -235,6 +237,18 @@ abstract class CommonController extends BaseController } } //단일필드작업 + protected function toggle_process(mixed $uid): mixed + { + //데이터 검증 + $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} 정보를 찾을수 없습니다."); + } + return $this->getService()->modify($entity, $this->formDatas); + } final public function toggle(mixed $uid, string $field): RedirectResponse { //Transaction Start @@ -242,21 +256,38 @@ abstract class CommonController extends BaseController try { $this->action = __FUNCTION__; $this->fields = [$field]; - $this->entity = $this->modify_process($uid); + $this->entity = $this->toggle_process($uid); $this->getService()->getModel()->transCommit(); - return redirect()->to($this->myauth->popPreviousUrl())->with('error', MESSAGES["SUCCESS"]); + return $this->getResultPageByActon($this->action); } catch (\Exception $e) { $this->getService()->getModel()->transRollback(); - return redirect()->back()->with('error', $e->getMessage()); + return redirect()->back()->withInput()->with('error', $e->getMessage()); } } //일괄처리작업 + protected function batchjob_process(mixed $uid): mixed + { + //데이터 검증 + $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} 정보를 찾을수 없습니다."); + } + return $this->getService()->modify($entity, $this->formDatas); + } final public function batchjob(): RedirectResponse { $this->init(__FUNCTION__); //Transaction Start $this->getService()->getModel()->transStart(); try { + //변경할 UIDS + $uids = $this->request->getVar('batchjob_uids'); + if (!$uids) { + throw new \Exception("적용할 리스트를 선택하셔야합니다."); + } //데이터가 있는경우 Field만 처리하기위해 $fields = []; foreach ($this->batchjob_fields as $field) { @@ -265,22 +296,17 @@ abstract class CommonController extends BaseController } } $this->fields = $fields; - //변경할 UIDS - $uids = $this->request->getVar('batchjob_uids'); - if (!$uids) { - throw new \Exception("적용할 리스트를 선택하셔야합니다."); - } $entities = []; foreach (explode(",", $uids) as $uid) { - $entities[$uid] = $this->modify_process($uid); + $entities[$uid] = $this->batchjob_process($uid); } $this->entities = $entities; $this->getService()->getModel()->transCommit(); - return redirect()->to($this->myauth->popPreviousUrl())->with('error', MESSAGES["SUCCESS"]); + return $this->getResultPageByActon($this->action); } catch (\Exception $e) { //Transaction Rollback $this->getService()->getModel()->transRollback(); - return redirect()->back()->with('error', $e->getMessage()); + return redirect()->back()->withInput()->with('error', $e->getMessage()); } } @@ -288,7 +314,8 @@ abstract class CommonController extends BaseController protected function delete_process(mixed $uid): mixed { //자신정보정의 - $entity = $this->getService()->getModel()->getEntityByPK($uid); + $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); + $entity = $this->getService()->getModel()->getEntity(); if ($entity === null) { throw new \Exception("{$uid} 정보를 찾을수 없습니다."); } @@ -301,11 +328,11 @@ abstract class CommonController extends BaseController try { $this->entity = $this->delete_process($uid); $this->getService()->getModel()->transCommit(); - return redirect()->to($this->myauth->popPreviousUrl())->with('error', MESSAGES["SUCCESS"]); + return $this->getResultPageByActon($this->action); } catch (\Exception $e) { //Transaction Rollback $this->getService()->getModel()->transRollback(); - return redirect()->back()->with('error', $e->getMessage()); + return redirect()->back()->withInput()->with('error', $e->getMessage()); } } //일괄삭제 @@ -325,11 +352,11 @@ abstract class CommonController extends BaseController } $this->entities = $entities; $this->getService()->getModel()->transCommit(); - return redirect()->to($this->myauth->popPreviousUrl())->with('error', MESSAGES["SUCCESS"]); + return $this->getResultPageByActon($this->action); } catch (\Exception $e) { //Transaction Rollback $this->getService()->getModel()->transRollback(); - return redirect()->back()->with('error', $e->getMessage()); + return redirect()->back()->withInput()->with('error', $e->getMessage()); } } @@ -351,9 +378,9 @@ abstract class CommonController extends BaseController $this->entity = $this->view_process($uid); helper(['form']); $this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []]; - return view($this->view_path . "view", data: ['viewDatas' => $this->getViewDatas()]); + return $this->getResultPageByActon($this->action); } catch (\Exception $e) { - return redirect()->back()->with('error', $e->getMessage()); + return redirect()->back()->withInput()->with('error', $e->getMessage()); } } @@ -435,14 +462,14 @@ abstract class CommonController extends BaseController // 현재 URL을 스택에 저장 $this->myauth->pushCurrentUrl($this->request->getUri()->getPath() . ($this->request->getUri()->getQuery() ? "?" . $this->request->getUri()->getQuery() : "")); helper(['form']); - return view($this->view_path . "index", ['viewDatas' => $this->getViewDatas()]); + return $this->getResultPageByActon($this->action); } catch (\Exception $e) { - return $this->helper->alert($e->getMessage()); + return redirect()->back()->withInput()->with('error', $e->getMessage()); } } //OUPUT Document 관련 - private function output_save_process(string $document_type, mixed $loaded_data): array + private function download_process_save(string $document_type, mixed $loaded_data): array { $full_path = WRITEPATH . DIRECTORY_SEPARATOR . "excel"; switch ($document_type) { @@ -488,14 +515,15 @@ abstract class CommonController extends BaseController //data loading $reader = new Html(); $loaded_data = $reader->loadFromString($html); - list($full_path, $file_name) = $this->output_save_process($output_type, $loaded_data); + list($full_path, $file_name) = $this->download_process_save($output_type, $loaded_data); $full_path .= DIRECTORY_SEPARATOR . $file_name; break; default: if (!$uid) { throw new \Exception("{$output_type}은 반드시 uid의 값이 필요합니다."); } - $this->entity = $this->getService()->getModel()->getEntityByPK($uid); + $this->getService()->getModel()->where($this->getService()->getModel()::PK, $uid); + $this->entity = $this->getService()->getModel()->getEntity(); if ($this->entity === null) { throw new \Exception("{$uid} 정보를 찾을수 없습니다."); } @@ -505,7 +533,7 @@ abstract class CommonController extends BaseController } return $this->response->download($full_path, null)->setFileName($file_name); } catch (\Exception $e) { - return $this->helper->alert($e->getMessage()); + return redirect()->back()->withInput()->with('error', $e->getMessage()); } } // Download diff --git a/app/Controllers/UserController.php b/app/Controllers/UserController.php index 9711fdb..4ea21f5 100644 --- a/app/Controllers/UserController.php +++ b/app/Controllers/UserController.php @@ -73,7 +73,7 @@ class UserController extends CommonController { try { $this->login_init('login'); - $this->formDatas = $this->create_validate($this->action, $this->fields); + $this->formDatas = $this->doValidate($this->action, $this->fields); $auth = new LocalService(); $auth->login($auth->checkUser($this->formDatas)); $this->message = "로그인 성공"; diff --git a/app/Helpers/UserHelper.php b/app/Helpers/UserHelper.php index e22afa3..f6267e0 100644 --- a/app/Helpers/UserHelper.php +++ b/app/Helpers/UserHelper.php @@ -30,7 +30,7 @@ class UserHelper extends CommonHelper $form = form_input($field, $value, ["placeholder" => "예)010-0010-0010", ...$extras]); break; case 'role': - if (in_array($viewDatas['action'], ['create', 'modify'])) { + if (in_array($viewDatas['action'], ['create_form', 'modify_form'])) { $forms = []; foreach ($viewDatas['field_options'][$field] as $key => $label) { $values = is_array($value) ? $value : explode(DEFAULTS["DELIMITER_ROLE"], $value); diff --git a/app/Models/CommonModel.php b/app/Models/CommonModel.php index 081221c..9c8c91f 100644 --- a/app/Models/CommonModel.php +++ b/app/Models/CommonModel.php @@ -80,7 +80,7 @@ abstract class CommonModel extends Model // 수동입력인 경우 if (!$this->useAutoIncrement) { $rule = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]"; - $rule .= $action == "create" ? "|is_unique[{$this->table}.{$field}]" : ""; + $rule .= in_array($action, ["create", "create_form"]) ? "|is_unique[{$this->table}.{$field}]" : ""; } else { $rule = "required|numeric"; } @@ -183,6 +183,8 @@ abstract class CommonModel extends Model // Field에 맞는 Validation Rule 재정의 $this->setValidationRules($this->getFieldRules('modify', $this->allowedFields)); // 저장하기 전에 데이터 값 변경이 필요한 Field + echo var_dump($formDatas); + exit; foreach (array_keys($formDatas) as $field) { $entity->$field = $this->convertEntityData($field, $formDatas); } diff --git a/app/Models/UserModel.php b/app/Models/UserModel.php index 3e6ad65..f859942 100644 --- a/app/Models/UserModel.php +++ b/app/Models/UserModel.php @@ -18,7 +18,7 @@ class UserModel extends CommonModel "passwd", "name", "email", - "mobild", + "mobile", "role", "status" ]; @@ -34,13 +34,13 @@ class UserModel extends CommonModel switch ($field) { case "id": $rule = "required|trim|min_length[4]|max_length[20]"; - $rule .= $action == "create" ? "|is_unique[{$this->table}.{$field}]" : ""; + $rule .= in_array($action, ["create", "create_form"]) ? "|is_unique[{$this->table}.{$field}]" : ""; break; case "passwd": - $rule = $action == "create" ? "required" : "if_exist" . "|trim|string"; + $rule = in_array($action, ["create", "create_form"]) ? "required|trim|string" : "if_exist|trim|string"; break; case "confirmpassword": - $rule = $action == "create" ? "required" : "if_exist" . "|trim|string|matches[passwd]"; + $rule = in_array($action, ["create", "create_form"]) ? "required|trim|string|matches[passwd]" : "if_exist|trim|string|matches[passwd]"; break; case "email": $rule = "required|trim|valid_email"; diff --git a/app/Services/Auth/AuthService.php b/app/Services/Auth/AuthService.php index e36c362..73b37d6 100644 --- a/app/Services/Auth/AuthService.php +++ b/app/Services/Auth/AuthService.php @@ -87,12 +87,7 @@ abstract class AuthService extends CommonService final public function login(Entity $entity): void { $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 - ]); + $this->getSession()->set(SESSION_NAMES['AUTH'], ['uid' => $entity->getPK(), 'id' => $entity->getID(), 'name' => $entity->getTitle(), 'role' => $entity->role]); } final public function logout(): void @@ -100,24 +95,13 @@ abstract class AuthService extends CommonService // 세션 데이터 삭제 $this->getSession()->remove(SESSION_NAMES['ISLOGIN']); $this->getSession()->remove(SESSION_NAMES['AUTH']); - // 모든 세션 데이터 삭제 $this->getSession()->destroy(); - // 세션 쿠키 삭제 if (ini_get("session.use_cookies")) { $params = session_get_cookie_params(); - setcookie( - session_name(), - '', - time() - 42000, - $params["path"], - $params["domain"], - $params["secure"], - $params["httponly"] - ); + setcookie(session_name(), '', time() - 42000, $params["path"], $params["domain"], $params["secure"], $params["httponly"]); } - // 세션 재생성 session_start(); $this->getSession()->regenerate(true); diff --git a/app/Services/Auth/GoogleService.php b/app/Services/Auth/GoogleService.php index 97ac9d8..befe13b 100644 --- a/app/Services/Auth/GoogleService.php +++ b/app/Services/Auth/GoogleService.php @@ -49,11 +49,12 @@ class GoogleService extends AuthService // Google 서비스 설정 $userSNS_entity = $this->getMySocket($access_code)->getUserSNSEntity(); // local db 사용와의 연결 확인 - $user_entity = $this->getModel()->getEntityByPK($userSNS_entity->getParent()); - if ($user_entity === null) { + $this->getModel()->where($this->getModel()::PK, $userSNS_entity->getParent()); + $entity = $this->getModel()->getEntity(); + if ($entity === null) { throw new PageNotFoundException("회원[{$userSNS_entity->getTitle()}]님은 아직 로컬사용자 연결이 이루어지지 않았습니다."); } - return $user_entity; + return $entity; } catch (\Google_Service_Exception $e) { log_message('error', '구글 서비스 예외: ' . $e->getMessage()); throw new PageNotFoundException("구글 로그인 중 오류가 발생했습니다. 다시 시도해 주세요."); diff --git a/app/Views/admin/create.php b/app/Views/admin/create_form.php similarity index 100% rename from app/Views/admin/create.php rename to app/Views/admin/create_form.php diff --git a/app/Views/admin/modify.php b/app/Views/admin/modify_form.php similarity index 100% rename from app/Views/admin/modify.php rename to app/Views/admin/modify_form.php