diff --git a/app/Controllers/Admin/UserController.php b/app/Controllers/Admin/UserController.php index 12b63f4..2de4b42 100644 --- a/app/Controllers/Admin/UserController.php +++ b/app/Controllers/Admin/UserController.php @@ -32,7 +32,11 @@ class UserController extends AdminController } return parent::getFormRule($action, $field, $rule); } - //Index,FieldForm관련. + //Action작업관련 + protected function create_form_process(): void + { + new UserDTO($this->request->getPost()); + } protected function create_process(): RedirectResponse { //요청 데이터를 DTO 객체로 변환 @@ -63,7 +67,7 @@ class UserController extends AdminController } //요청 데이터를 DTO 객체로 변환 $dto = new UserDTO($this->request->getPost()); - $entity = $this->service->modify($dto); + $entity = $this->service->modify($entity, $dto); $redirect_url = $this->getAuthContext()->popPreviousUrl() ?? implode(DIRECTORY_SEPARATOR, $this->getActionPaths()); return redirect()->to($redirect_url)->with('success', "{$entity->getTitle()} 계정 수정이 완료되었습니다."); } diff --git a/app/Entities/CommonEntity.php b/app/Entities/CommonEntity.php index 2715270..c150685 100644 --- a/app/Entities/CommonEntity.php +++ b/app/Entities/CommonEntity.php @@ -6,7 +6,6 @@ use CodeIgniter\Entity\Entity; abstract class CommonEntity extends Entity { - const DEFAULT_STATUS = ""; protected $datamap = []; protected $dates = ['created_at', 'updated_at', 'deleted_at']; //사용법 : $client->created_at->format('Y-m-d') diff --git a/app/Entities/TrafficEntity.php b/app/Entities/TrafficEntity.php index 262d87a..e94ea21 100644 --- a/app/Entities/TrafficEntity.php +++ b/app/Entities/TrafficEntity.php @@ -8,7 +8,6 @@ class TrafficEntity extends CommonEntity { const PK = TrafficModel::PK; const TITLE = TrafficModel::TITLE; - const DEFAULT_STATUS = STATUS['AVAILABLE']; final public function getUserUID(): int|null { return $this->attributes['user_uid'] ?? null; diff --git a/app/Entities/UserEntity.php b/app/Entities/UserEntity.php index 8a97ae9..25bde51 100644 --- a/app/Entities/UserEntity.php +++ b/app/Entities/UserEntity.php @@ -4,23 +4,20 @@ namespace App\Entities; use App\Entities\CommonEntity; use App\Models\UserModel as Model; -use CodeIgniter\Entity\Entity; // Entity 클래스를 명시적으로 use 하는 것이 좋습니다. class UserEntity extends CommonEntity { const PK = Model::PK; const TITLE = Model::TITLE; - const DEFAULT_STATUS = STATUS['AVAILABLE']; - /** - * @var array Entity 속성을 DB에 저장하거나 DB에서 로드할 때의 형 변환 규칙 + * @var array DB 컬럼 타입이 VARCHAR(255)이고 CSV 형식으로 통일하기 위해 json-array 캐스팅을 제거합니다. */ protected $casts = [ - // 'role' 컬럼에 배열을 할당하면 DB에 JSON 문자열로 저장되며, - // DB에서 로드할 때 JSON 문자열이 자동으로 PHP 배열로 변환됩니다. - 'role' => 'json-array', + // 'role' => 'json-array', // 🚫 CSV 형식 저장을 위해 제거 ]; + // --- Getter Methods --- + public function getID(): string { return (string) $this->attributes['id']; @@ -31,8 +28,41 @@ class UserEntity extends CommonEntity return $this->attributes['passwd']; } - // $formDatas['passwd']에 평문 비밀번호가 들어있으면, - // Model->insert시 Entity 생성자($formDatas)가 setPasswd()를 자동으로 호출합니다. + /** + * 사용자의 역할을 배열 형태로 반환합니다. + * DB의 JSON 또는 CSV 형식 데이터를 모두 배열로 복구할 수 있는 로직을 포함합니다. + * @return array + */ + public function getRole(): array + { + $role = $this->attributes['role'] ?? null; + + // 1. 이미 배열인 경우 (방어적 코딩) + if (is_array($role)) { + return array_filter($role); + } + + // 2. 문자열 데이터인 경우 처리 + if (is_string($role) && !empty($role)) { + // 2-a. JSON 디코딩 시도 (기존 DB의 JSON 형식 처리) + $decodedRole = json_decode($role, true); + if (json_last_error() === JSON_ERROR_NONE && is_array($decodedRole)) { + return $decodedRole; + } + + // 2-b. JSON이 아니면 CSV로 가정하고 변환 + $parts = explode(',', $role); + // 각 요소의 불필요한 공백과 따옴표 제거 + $cleanedRoles = array_map(fn($item) => trim($item, " \t\n\r\0\x0B\""), $parts); + return array_filter($cleanedRoles); + } + + // 3. 변환에 실패했거나 데이터가 없는 경우 빈 배열 반환 + return []; + } + + // --- Setter Methods --- + public function setPasswd(string $password) { // 비밀번호를 암호화하여 저장합니다. @@ -40,35 +70,31 @@ class UserEntity extends CommonEntity } /** - * 사용자의 역할을 배열 형태로 반환합니다. - * $casts에 의해 DB에서 읽어올 때 이미 배열로 변환될 것을 기대합니다. - * @return array + * Role 데이터가 Entity에 설정될 때 호출되어, 입력된 CSV/JSON 문자열을 정리 후 + * DB에 적합한 CSV 문자열로 최종 저장합니다. + * @param mixed $role 입력 데이터 (문자열 또는 배열) */ - public function getRole(): array + public function setRole(mixed $role) { - $role = $this->attributes['role'] ?? []; + $roleArray = []; - // 1. $casts가 성공적으로 작동했거나, 이미 배열인 경우 바로 반환합니다. - if (is_array($role)) { - return $role; + if (is_string($role)) { + // 1. 양쪽의 불필요한 따옴표와 공백을 제거하여 깨끗한 문자열 확보 + $cleanRoleString = trim($role, " \t\n\r\0\x0B\""); + + if (!empty($cleanRoleString)) { + // 2. 쉼표를 기준으로 분리 후, 각 요소의 공백/따옴표를 다시 제거 + $parts = explode(',', $cleanRoleString); + $cleanedRoles = array_map(fn($item) => trim($item, " \t\n\r\0\x0B\""), $parts); + $roleArray = array_filter($cleanedRoles); + } + } else if (is_array($role)) { + // 이미 배열인 경우에도 데이터 정리를 한 번 거칩니다. + $cleanedRoles = array_map(fn($item) => trim($item, " \t\n\r\0\x0B\""), $role); + $roleArray = array_filter($cleanedRoles); } - // 2. 캐스팅에 실패했으나 원본이 문자열로 남아있는 경우 (JSON 또는 CSV) - if (is_string($role) && !empty($role)) { - // 2-a. JSON 디코딩을 시도합니다. - $decodedRole = json_decode($role, true); - - if (json_last_error() === JSON_ERROR_NONE && is_array($decodedRole)) { - return $decodedRole; // 유효한 JSON 배열인 경우 - } - - // 2-b. JSON이 아니면 레거시 CSV 형식이라고 가정하고 explode로 변환합니다. - if (defined('DEFAULTS') && isset(DEFAULTS['DELIMITER_ROLE'])) { - return explode(DEFAULTS['DELIMITER_ROLE'], $role); - } - } - - // 3. 변환에 실패했거나 데이터가 없는 경우 빈 배열 반환 - return []; + // 💡 핵심: 최종적으로 DB에 삽입될 단일 CSV 문자열로 변환하여 저장합니다. + $this->attributes['role'] = implode(',', $roleArray); } } diff --git a/app/Helpers/UserHelper.php b/app/Helpers/UserHelper.php index aa8d12b..90a8482 100644 --- a/app/Helpers/UserHelper.php +++ b/app/Helpers/UserHelper.php @@ -22,7 +22,6 @@ class UserHelper extends CommonHelper if (($viewDatas['control']['entity'] ?? null) instanceof UserEntity) { $value = $viewDatas['control']['entity']->getRole(); } - dd($value); $currentRoles = is_array($value) ? array_map('strtolower', array_map('trim', $value)) : []; @@ -30,11 +29,6 @@ class UserHelper extends CommonHelper //체크박스를 순회하며 생성 foreach ($viewDatas['control']['formOptions']['role'] as $key => $label) { $checked = in_array(strtolower(trim($key)), $currentRoles); - var_dump($currentRoles); - echo '
'; - echo $key; - echo $checked ? "{$key}TRUE" : "FALSE"; - dd($viewDatas['control']['formOptions']['role']); $form .= '