From 269a1bd03800425c2ffa10366dd24db4ba4edf3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EC=A4=80=ED=9D=A0?= Date: Fri, 20 Sep 2024 19:51:36 +0900 Subject: [PATCH] Automation init...4 --- app/Config/Constants.php | 1 + app/Controllers/Admin/UserController.php | 4 +- .../Cloudflare/AccountController.php | 113 ++++++++++++++---- .../Cloudflare/Admin/AccountController.php | 2 +- .../Cloudflare/Admin/AuthController.php | 79 ++++++++++++ app/Controllers/CommonController.php | 99 ++++++++++----- app/Controllers/Mangboard/UserController.php | 2 +- app/Entities/Cloudflare/AuthEntity.php | 30 +++++ app/Libraries/CommonLibrary.php | 8 +- .../MySocket/Cloudflare/AccountSocket.php | 4 +- app/Libraries/MySocket/CloudflareSocket.php | 34 ++++-- .../MyStorage/Cloudflare/AccountStorage.php | 1 - .../MyStorage/Cloudflare/AuthStorage.php | 50 ++++++++ app/Models/Cloudflare/AccountModel.php | 14 +-- app/Models/Cloudflare/AuthModel.php | 63 ++++++++++ app/Models/CommonModel.php | 18 +-- app/Traits/AuthTrait.php | 24 ++-- 17 files changed, 436 insertions(+), 110 deletions(-) create mode 100644 app/Controllers/Cloudflare/Admin/AuthController.php create mode 100644 app/Entities/Cloudflare/AuthEntity.php create mode 100644 app/Libraries/MyStorage/Cloudflare/AuthStorage.php create mode 100644 app/Models/Cloudflare/AuthModel.php diff --git a/app/Config/Constants.php b/app/Config/Constants.php index 146c2f8..e7031ab 100644 --- a/app/Config/Constants.php +++ b/app/Config/Constants.php @@ -121,6 +121,7 @@ define('ROLES', [ //SESSION 관련 define('SESSION_NAMES', [ 'RETURN_URL' => "return_url", + 'RETURN_MSG' => "return_message", 'ISLOGIN' => "islogined", 'AUTH' => 'auth', ]); diff --git a/app/Controllers/Admin/UserController.php b/app/Controllers/Admin/UserController.php index 6ee6a38..5bfa30d 100644 --- a/app/Controllers/Admin/UserController.php +++ b/app/Controllers/Admin/UserController.php @@ -6,9 +6,9 @@ use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use Psr\Log\LoggerInterface; -use App\Traits\AuthTrait; use App\Controllers\CommonController; use App\Models\UserModel; +use App\Traits\AuthTrait; class UserController extends CommonController { @@ -17,7 +17,7 @@ class UserController extends CommonController public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { parent::initController($request, $response, $logger); - $this->session = $this->loginCheck_AuthTrait(); + $this->session = $this->session_AuthTrait(); } private function getModel(): UserModel { diff --git a/app/Controllers/Cloudflare/AccountController.php b/app/Controllers/Cloudflare/AccountController.php index 81dd6fa..68397c0 100644 --- a/app/Controllers/Cloudflare/AccountController.php +++ b/app/Controllers/Cloudflare/AccountController.php @@ -2,47 +2,116 @@ namespace App\Controllers\Cloudflare; -use Psr\Log\LoggerInterface; -use CodeIgniter\HTTP\ResponseInterface; -use CodeIgniter\HTTP\RequestInterface; - +use App\Entities\Cloudflare\AccountEntity; use App\Libraries\MySocket\Cloudflare\AccountSocket; -use App\Libraries\MyStorage\AccountStorage; + +use CodeIgniter\HTTP\RedirectResponse; +use CodeIgniter\HTTP\RequestInterface; +use CodeIgniter\HTTP\ResponseInterface; +use Psr\Log\LoggerInterface; + +use App\Models\Cloudflare\AccountModel; +use App\Traits\AccountTrait; class AccountController extends MyCloudflare { - private $_email = ""; - private $_api_key = ""; + use AccountTrait; private $_mySocket = null; - private $_myStorage = null; + private $_model = null; public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { parent::initController($request, $response, $logger); + $this->session = $this->session_AccountTrait(); + $this->class_name = 'Account'; + helper($this->class_name); } final protected function getMySocket(): AccountSocket { if ($this->_mySocket === null) { - $this->_mySocket = new AccountSocket($this->_email, $this->_api_key); + $this->_mySocket = new AccountSocket($auth_uid); } return $this->_mySocket; } - final protected function getMyStorage(): AccountStorage + + final protected function getModel(): AccountModel { - if ($this->_myStorage === null) { - $this->_myStorage = new AccountStorage(); + if ($this->_model === null) { + $this->_model = new AccountModel(); } - return $this->_myStorage; + return $this->_model; } - public function create(): void + protected function create_form_process(): void { - $this->_email = $this->request->getVar('email'); - $this->_api_key = $this->request->getVar('key'); - //전송 - $result = $this->getMySocket()->create_process($this->_email); - $formDatas = []; - $formDatas['key'] = $this->_api_key; - $entity = $this->getMyStorage()->create_process($result, $formDatas); - log_message("notice", __FUNCTION__ . "=> 작업을 완료하였습니다."); + $this->fields = ['id', 'authkey']; + $this->filter_fields = ['status']; + parent::create_form_process(); + } + public function create_form(): RedirectResponse|string + { + try { + $this->fields = ['id', 'authkey']; + $this->filter_fields = ['status']; + $this->action = 'create'; + $this->getModel()->setAction($this->action); + $this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']); + return view("/{$this->class_name}/insert", ['attributes' => $this->getAttributes]); + } catch (\Exception $e) { + log_message("error", $e->getMessage()); + return redirect()->to($this->session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/")->with(SESSION_NAMES['RETURN_MSG'], $e->getMessage()); + } + } + protected function create_process($model): AccountEntity + { + $datas = [ + 'name' => $name . "'s Account", + 'type' => 'standard', + ]; + $result = $this->getClient()->post("accounts", $datas); + $result = json_decode($result->getBody()); + if (!$result->success) { + throw new \Exception(var_export($result, true)); + } + //Result 형태 + // [ + // {"id":"078e88a7735965b661715af13031ecb0", + // "name":"Cloudwin002@idcjp.jp's Auth", + // "type":"standard", + // "settings":{ + // "enforce_twofactor":false, + // "api_access_enabled":null, + // "access_approval_expiry":null, + // "use_account_custom_ns_by_default":false + // }, + // "legacy_flags":{"enterprise_zone_quota":{"maximum":0,"current":0,"available":0}}, + // "created_on":"2017-06-26T05:44:49.470184Z"} + // ] + $formDatas[$this->getAuthModel()->PK()] = $result->id; + $formDatas[$this->getAuthModel()->getTitleField()] = $result->name; + $formDatas['type'] = $result->type; + $formDatas['status'] = 'use'; + $formDatas['updated_at'] = $result->created_on; + $formDatas['created_at'] = $result->created_on; + return parent::create_process($model); + } + public function create(): RedirectResponse + { + $this->getModel()->transStart(); + try { + $this->fields = ['id', 'authkey']; + $this->filter_fields = ['status']; + $this->action = 'create'; + $this->getModel()->setAction($this->action); + $entity = $this->create_process($this->getModel()); + log_message("notice", __FUNCTION__ . "=>{$entity->getTitle()} 작업을 완료하였습니다."); + 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'], __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage()); + $this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']); + return redirect()->back()->withInput(); + } } } diff --git a/app/Controllers/Cloudflare/Admin/AccountController.php b/app/Controllers/Cloudflare/Admin/AccountController.php index a3cb1e8..1003af3 100644 --- a/app/Controllers/Cloudflare/Admin/AccountController.php +++ b/app/Controllers/Cloudflare/Admin/AccountController.php @@ -21,7 +21,7 @@ class AccountController extends MyCloudflare public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { parent::initController($request, $response, $logger); - $this->session = $this->loginCheck_AuthTrait(); + $this->session = $this->session_AuthTrait(); } final protected function getMySocket(): AccountSocket { diff --git a/app/Controllers/Cloudflare/Admin/AuthController.php b/app/Controllers/Cloudflare/Admin/AuthController.php new file mode 100644 index 0000000..9ad8a2f --- /dev/null +++ b/app/Controllers/Cloudflare/Admin/AuthController.php @@ -0,0 +1,79 @@ +session = $this->session_AuthTrait(); + $this->class_name = 'Auth'; + helper($this->class_name); + } + final protected function getModel(): AuthModel + { + if ($this->_model === null) { + $this->_model = new AuthModel(); + } + return $this->_model; + } + + protected function create_form_process(): void + { + $this->fields = ['id', 'authkey']; + $this->filter_fields = ['status']; + parent::create_form_process(); + } + public function create_form(): RedirectResponse|string + { + try { + $this->fields = ['id', 'authkey']; + $this->filter_fields = ['status']; + $this->action = 'create'; + $this->getModel()->setAction($this->action); + $this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']); + return view("/{$this->class_name}/insert", ['attributes' => $this->getAttributes]); + } catch (\Exception $e) { + log_message("error", $e->getMessage()); + return redirect()->to($this->session->getFlashdata(SESSION_NAMES['RETURN_URL']) ?: "/")->with(SESSION_NAMES['RETURN_MSG'], $e->getMessage()); + } + } + protected function create_process($model): AuthEntity + { + return parent::create_process($model); + } + public function create(): RedirectResponse + { + $this->getModel()->transStart(); + try { + $this->fields = ['id', 'authkey']; + $this->filter_fields = ['status']; + $this->action = 'create'; + $this->getModel()->setAction($this->action); + $entity = $this->create_process($this->getModel()); + log_message("notice", __FUNCTION__ . "=>{$entity->getTitle()} 작업을 완료하였습니다."); + 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'], __FUNCTION__ . " 실패하였습니다.\n" . $e->getMessage()); + $this->session->keepFlashdata(SESSION_NAMES['RETURN_URL']); + return redirect()->back()->withInput(); + } + } +} diff --git a/app/Controllers/CommonController.php b/app/Controllers/CommonController.php index ff4bb11..6ca9686 100644 --- a/app/Controllers/CommonController.php +++ b/app/Controllers/CommonController.php @@ -3,63 +3,59 @@ namespace App\Controllers; use App\Controllers\BaseController; +use CodeIgniter\HTTP\RequestInterface; +use CodeIgniter\HTTP\ResponseInterface; +use Psr\Log\LoggerInterface; abstract class CommonController extends BaseController { - private $_options = []; + private $_attributes = []; + public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) + { + parent::initController($request, $response, $logger); + } final public function __get($name) { - if (!array_key_exists($name, $this->_options)) { + if (!array_key_exists($name, $this->_attributes)) { return null; } - return $this->_options[$name]; + return $this->_attributes[$name]; } - final public function __set($name, $value): void { - $this->_options[$name] = $value; + $this->_attributes[$name] = $value; } - - //전송된 값 검증 및 임시저장 - final public function getFormFieldDatas(array $fields, array $fieldRules, array $formDatas = []): array + final public function getAttributes(): array { - foreach ($fields as $field) { - $formDatas[$field] = rtrim($this->request->getVar($field)); - log_message("debug", "{$field} : {$formDatas[$field]}"); - } - //변경할 값 확인 - if (!$this->validate($fieldRules)) { - throw new \Exception("데이터 검증 오류발생\n" . implode("\n", $this->validator->getErrors())); - } - return $formDatas; + return $this->_attributes; } //Field별 Form Option용 - protected function getFormFieldOption(string $field, array $options): array + protected function getFormFilterOption(string $filter_field, array $filter_options): array { - switch ($field) { + switch ($filter_field) { default: - $temps = lang($this->_className . '.' . strtoupper($field)); + $temps = lang($this->class_name . '.' . strtoupper($filter_field)); if (!is_array($temps)) { - throw new \Exception(__FUNCTION__ . "에서 {$field}의 데이터가 array가 아닙니다.\n" . var_export($temps, true)); + throw new \Exception(__FUNCTION__ . "에서 {$filter_field}의 데이터가 array가 아닙니다.\n" . var_export($temps, true)); } - $options[$field] = [ - ["" => lang($this->_className . '.label.' . $field) . ' 선택'], - lang($this->_className . '.' . strtoupper($field)) + $filter_options[$filter_field] = [ + ["" => lang($this->class_name . '.label.' . $filter_field) . ' 선택'], + lang($this->class_name . '.' . strtoupper($filter_field)) ]; break; } - return $options; + return $filter_options; } //Field별 Form Option용 - final public function getFormFieldOptions(array $fields, array $options = []): array + final public function getFormFilterOptions(array $filter_options = []): array { - foreach ($fields as $field) { - if (is_array($field)) { - throw new \Exception(__FUNCTION__ . "에서 field가 array 입니다.\n" . var_export($field, true)); + foreach ($this->filter_fields as $filter_field) { + if (is_array($filter_field)) { + throw new \Exception(__FUNCTION__ . "에서 filter_field가 array 입니다.\n" . var_export($filter_field, true)); } - $options = $this->getFormFieldOption($field, $options); + $filter_options = $this->getFormFilterOption($filter_field, $filter_options); } - return $options; + return $filter_options; } protected function getFormFieldRule(string $field, array $rules): array { @@ -73,11 +69,48 @@ abstract class CommonController extends BaseController } return $rules; } - final public function getFormFieldRules(array $fields, array $rules = []): array + final public function getFormFieldRules(array $rules = []): array { - foreach ($fields as $field) { + foreach ($this->fields as $field) { $rules = $this->getFormFieldRule($field, $rules); } return $rules; } + //전송된 데이터 + protected function getFormData(string $field): void + { + switch ($field) { + default: + $this->formDatas[$field] = rtrim($this->request->getVar($field)); + break; + } + } + //전송된 데이터 재정의 + protected function convertFormData(string $field): void + { + switch ($field) { + default: + break; + } + } + protected function create_form_process(): void + { + helper(['form']); + } + protected function create_process($model): mixed + { + //변경할 값 확인 : Upload된 파일 검증시 $this->request->getVar()보다 먼처 체크필요 + $validation = \Config\Services::validation(); + $validation->setRules($model->getFieldRules($this->fields)); + if (!$validation->withRequest($this->request)->run()) { + throw new \Exception("데이터 검증 오류발생\n" . implode("\n", $this->validator->getErrors())); + } + foreach ($this->fields as $field) { + $this->getFormData($field); + } + foreach ($this->fields as $field) { + $this->convertFormData($field); + } + return $model->create($this->formDatas); + } } diff --git a/app/Controllers/Mangboard/UserController.php b/app/Controllers/Mangboard/UserController.php index 265dd0e..008d514 100644 --- a/app/Controllers/Mangboard/UserController.php +++ b/app/Controllers/Mangboard/UserController.php @@ -2,11 +2,11 @@ namespace App\Controllers\Mangboard\Admin; +use App\Controllers\CommonController; use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use Psr\Log\LoggerInterface; -use App\Controllers\CommonController; use App\Models\Mangboard\UserModel; class UserController extends CommonController diff --git a/app/Entities/Cloudflare/AuthEntity.php b/app/Entities/Cloudflare/AuthEntity.php new file mode 100644 index 0000000..b715937 --- /dev/null +++ b/app/Entities/Cloudflare/AuthEntity.php @@ -0,0 +1,30 @@ +getPK()}|{$this->getTitle()}|{$this->getKey()}|{$this->attributes['type']}|{$this->attributes['status']}"; + } + public function getPK(): int + { + return $this->attributes['uid']; + } + public function getTitle(): string + { + return $this->attributes['id']; + } + public function setTitle(string $title): void + { + $this->attributes['id'] = $title; + } + //Common Function + public function getKey(): string + { + return $this->attributes['key']; + } +} diff --git a/app/Libraries/CommonLibrary.php b/app/Libraries/CommonLibrary.php index 38b49f3..7c3c9f2 100644 --- a/app/Libraries/CommonLibrary.php +++ b/app/Libraries/CommonLibrary.php @@ -4,18 +4,18 @@ namespace App\Libraries; abstract class CommonLibrary { - private $_options = []; + private $_attributes = []; protected function __construct() {} final public function __get($name) { - if (!array_key_exists($name, $this->_options)) { + if (!array_key_exists($name, $this->_attributes)) { return null; } - return $this->_options[$name]; + return $this->_attributes[$name]; } final public function __set($name, $value): void { - $this->_options[$name] = $value; + $this->_attributes[$name] = $value; } } diff --git a/app/Libraries/MySocket/Cloudflare/AccountSocket.php b/app/Libraries/MySocket/Cloudflare/AccountSocket.php index dcf44ae..b0c220c 100644 --- a/app/Libraries/MySocket/Cloudflare/AccountSocket.php +++ b/app/Libraries/MySocket/Cloudflare/AccountSocket.php @@ -6,9 +6,9 @@ use App\Libraries\MySocket\CloudflareSocket; class AccountSocket extends CloudflareSocket { - public function __construct(string $email, string $api_key) + public function __construct() { - parent::__construct($email, $api_key); + parent::__construct(); } final public function create_process(string $name, string $type = "standard") diff --git a/app/Libraries/MySocket/CloudflareSocket.php b/app/Libraries/MySocket/CloudflareSocket.php index e443c21..8dbfa7c 100644 --- a/app/Libraries/MySocket/CloudflareSocket.php +++ b/app/Libraries/MySocket/CloudflareSocket.php @@ -3,6 +3,7 @@ namespace App\Libraries\MySocket; use App\Libraries\CommonLibrary; +use App\Models\Cloudflare\AccountModel; use Cloudflare\API\Adapter\Guzzle; use Cloudflare\API\Auth\APIKey; @@ -11,20 +12,37 @@ class CloudflareSocket extends CommonLibrary private static int $_request = 0; private static int $_request_max = 100; private static int $_request_timewait = 60; - private $_client = null; - public function __construct(string $email, string $api_key) + private $_accountModel = null; + private $_clients = []; + private $_auth_uid = ""; + public function __construct(string $auth_uid) { parent::__construct(); - $this->_email = $email; - $this->_api_key = $api_key; + $this->initClients(); + $this->_auth_uid = $auth_uid; self::$_request_max = getenv("cfmgr.request.max"); } + + final protected function getAccountModel(): AccountModel + { + if ($this->_accountModel === null) { + $this->_accountModel = new AccountModel(); + } + return $this->_accountModel; + } + final public function initClients(): void + { + foreach ($this->getAccountModel()->getEntitys() as $entity) { + $this->_clients[$entity->getKey()] = new Guzzle( + new APIKey($entity->getTitle(), $entity->getKey()) + ); + } + } + final public function getClient(): Guzzle { - if ($this->_client === null) { - $this->_client = new Guzzle( - $this->_client = new APIKey($this->_email, $this->_api_key) - ); + if (!array_key_exists($this->_auth_uid, $this->_clients)) { + throw new \Exception(+__FUNCTION__ . " => {$this->_auth_uid}에 해당하는 Adapter를 찾을수 없습니다."); } if (self::$_request >= self::$_request_max) { log_message('warning', sprintf("--Cloudflare API Call %s초 대기 시작--", self::$_request_timewait)); diff --git a/app/Libraries/MyStorage/Cloudflare/AccountStorage.php b/app/Libraries/MyStorage/Cloudflare/AccountStorage.php index 1cd282d..74a87ac 100644 --- a/app/Libraries/MyStorage/Cloudflare/AccountStorage.php +++ b/app/Libraries/MyStorage/Cloudflare/AccountStorage.php @@ -5,7 +5,6 @@ namespace App\Libraries\MyStorage; use App\Libraries\CommonLibrary; use App\Entities\Cloudflare\AccountEntity; use App\Models\Cloudflare\AccountModel; -use App\Traits\ImageTrait; class AccountStorage extends CommonLibrary { diff --git a/app/Libraries/MyStorage/Cloudflare/AuthStorage.php b/app/Libraries/MyStorage/Cloudflare/AuthStorage.php new file mode 100644 index 0000000..f789ae6 --- /dev/null +++ b/app/Libraries/MyStorage/Cloudflare/AuthStorage.php @@ -0,0 +1,50 @@ +_accountModel === null) { + $this->_accountModel = new AuthModel(); + } + return $this->_accountModel; + } + + final public function create_process($result, array $formDatas): AuthEntity + { + //Result 형태 + // [ + // {"id":"078e88a7735965b661715af13031ecb0", + // "name":"Cloudwin002@idcjp.jp's Auth", + // "type":"standard", + // "settings":{ + // "enforce_twofactor":false, + // "api_access_enabled":null, + // "access_approval_expiry":null, + // "use_account_custom_ns_by_default":false + // }, + // "legacy_flags":{"enterprise_zone_quota":{"maximum":0,"current":0,"available":0}}, + // "created_on":"2017-06-26T05:44:49.470184Z"} + // ] + $formDatas[$this->getAuthModel()->PK()] = $result->id; + $formDatas[$this->getAuthModel()->getTitleField()] = $result->name; + $formDatas['type'] = $result->type; + $formDatas['status'] = 'use'; + $formDatas['updated_at'] = $result->created_on; + $formDatas['created_at'] = $result->created_on; + $entity = $this->getAuthModel()->create($formDatas); + log_message("notice", __FUNCTION__ . "=> {$entity->getTitle()} 생성을 완료하였습니다."); + return $entity; + } +} diff --git a/app/Models/Cloudflare/AccountModel.php b/app/Models/Cloudflare/AccountModel.php index 5dc99a9..e89bbcd 100644 --- a/app/Models/Cloudflare/AccountModel.php +++ b/app/Models/Cloudflare/AccountModel.php @@ -12,23 +12,17 @@ class AccountModel extends Model protected $primaryKey = 'uid'; protected $useAutoIncrement = false; protected $returnType = AccountEntity::class; //object,array,entity명::class - protected $allowedFields = ['uid', 'email', 'key', 'oldkey', 'type', 'status', 'updated_at', 'created_at']; + protected $allowedFields = ['uid', 'auth_uid', 'title', 'type', 'status', 'updated_at', 'created_at']; protected $useTimestamps = true; public function getTitleField(): string { - return 'email'; + return 'title'; } public function getFieldRule(string $field, array $rules): array { switch ($field) { - case "email": - $rules[$field] = "required|valid_emailvalid_email|is_unique[account.email]"; - break; - case "key": - $rules[$field] = "required|trim|smin_length[10]|max_length[200]"; - break; - case "oldkey": - $rules[$field] = "if_exist|trim|smin_length[10]|max_length[200]"; + case "auth_uid": + $rules[$field] = $rules[$field] = "required|regex_match[/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/]";; break; case "type": $rules[$field] = "if_exist|trim|string"; diff --git a/app/Models/Cloudflare/AuthModel.php b/app/Models/Cloudflare/AuthModel.php new file mode 100644 index 0000000..14dfdc9 --- /dev/null +++ b/app/Models/Cloudflare/AuthModel.php @@ -0,0 +1,63 @@ +where($this->getPKField(), $uid); + return $this->getEntity(); + } + public function getEntityByID(string $id): null | AuthEntity + { + $this->where($this->getTitleField(), $id); + return $this->getEntity(); + } + + //create용 + public function create(array $formDatas = []): AuthEntity + { + $formDatas['oldkey'] = $formDatas['authkey']; + return $this->create_process(new AuthEntity(), $formDatas); + } + //modify용 + public function modify(AuthEntity $entity, array $formDatas): AuthEntity + { + //입력후 authkey값을 oldkey값에 넣어주기 위함 + $formDatas['oldkey'] = $entity->authkey; + return $this->modify_process($entity, $formDatas); + } +} diff --git a/app/Models/CommonModel.php b/app/Models/CommonModel.php index 3c73b77..c5bc02f 100644 --- a/app/Models/CommonModel.php +++ b/app/Models/CommonModel.php @@ -144,17 +144,8 @@ abstract class CommonModel extends Model } //create , modify 직전 작업용 작업 - final protected function convertEntityData(string $field, array $formDatas): string|int + final protected function convertEntityData(string $field, array $formDatas): mixed { - if ($formDatas[$field] === null) { - throw new \Exception( - sprintf( - "\n-------%s FormDatas 오류--------\n%s\n-----------------------\n", - __FUNCTION__, - var_export($formDatas, true) - ) - ); - } switch ($field) { case $this->getPKField(): //$formDatas에 전달된 값이 없는경우 @@ -181,11 +172,6 @@ abstract class CommonModel extends Model case "content": $value = htmlentities($formDatas[$field], ENT_QUOTES); break; - case "updated_at": - case "created_at": - case "deleted_at": - $rules[$field] = "if_exist|valid_date"; - break; default: $value = $formDatas[$field]; break; @@ -213,7 +199,6 @@ abstract class CommonModel extends Model //Field에 맞는 Validation Rule 재정의 $this->setAction(DB_ACTION['CREATE']); $this->setValidationRules($this->getFieldRules($this->allowedFields)); - //저장하기 전에 데이터 값 변경이 필요한 Field foreach (array_keys($formDatas) as $field) { $entity->$field = $this->convertEntityData($field, $formDatas); @@ -232,7 +217,6 @@ abstract class CommonModel extends Model //Field에 맞는 Validation Rule 재정의 $this->setAction(DB_ACTION['MODIFY']); $this->setValidationRules($this->getFieldRules($this->allowedFields)); - //저장하기 전에 데이터 값 변경이 필요한 Field foreach (array_keys($formDatas) as $field) { $entity->$field = $this->convertEntityData($field, $formDatas); diff --git a/app/Traits/AuthTrait.php b/app/Traits/AuthTrait.php index cd8bbfa..03d9a75 100644 --- a/app/Traits/AuthTrait.php +++ b/app/Traits/AuthTrait.php @@ -2,18 +2,24 @@ namespace App\Traits; +use CodeIgniter\Session\Session; + trait AuthTrait { - final protected function loginCheck_AuthTrait(): array + final protected function session_AuthTrait(): Session { - //사용자 기본 Role 지정 - $session[SESSION_NAMES['ISLOGIN']] = false; - $session['currentRoles'] = [DEFAULTS["ROLE"]]; - if (\Config\Services::session()->get(SESSION_NAMES['ISLOGIN'])) { - $session[SESSION_NAMES['ISLOGIN']] = true; - $session['auth'] = $this->_session->get(SESSION_NAMES['AUTH']); - $currentRoles = explode(DEFAULTS['DELIMITER_ROLE'], $session['auth'][AUTH_FIELDS['ROLE']]); - $session['currentRoles'] = is_array($currentRoles) ? $currentRoles : [DEFAULTS["ROLE"]]; + $session = \Config\Services::session(); + $session->set(SESSION_NAMES['ISLOGIN'], false); + $session->set('currentRoles', [DEFAULTS["ROLE"]]); + if ($session->get(SESSION_NAMES['ISLOGIN'])) { + $session->set(SESSION_NAMES['ISLOGIN'], true); + $session->set( + 'currentRoles', + explode( + DEFAULTS['DELIMITER_ROLE'], + $session->get(SESSION_NAMES['AUTH'])[AUTH_FIELDS['ROLE']] + ) + ); } return $session; }