60 lines
2.3 KiB
PHP
60 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers\Admin;
|
|
|
|
use App\DTOs\UserDTO;
|
|
use App\Entities\UserEntity;
|
|
use CodeIgniter\HTTP\RedirectResponse;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use CodeIgniter\Validation\Validation;
|
|
use Psr\Log\LoggerInterface;
|
|
use App\Services\UserService;
|
|
|
|
class UserController extends AdminController
|
|
{
|
|
public const PATH = 'user';
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
parent::initController($request, $response, $logger);
|
|
if ($this->service === null) {
|
|
$this->service = service('userservice');
|
|
}
|
|
$this->addActionPaths($this::PATH);
|
|
}
|
|
protected function getFormRule(string $action, string $field, string $rule): array
|
|
{
|
|
switch ($field) {
|
|
case 'role':
|
|
$field = "{$field}.*";
|
|
break;
|
|
}
|
|
return parent::getFormRule($action, $field, $rule);
|
|
}
|
|
//Action작업관련
|
|
protected function create_process(): RedirectResponse
|
|
{
|
|
//요청 데이터를 DTO 객체로 변환
|
|
$dto = new UserDTO($this->request->getPost());
|
|
$entity = $this->service->create($dto);
|
|
$redirect_url = $this->getAuthContext()->popPreviousUrl() ?? implode(DIRECTORY_SEPARATOR, $this->getActionPaths());
|
|
return redirect()->to($redirect_url)->with('message', "{$entity->getTitle()} 계정 생성이 완료되었습니다.");
|
|
}
|
|
protected function modify_process($uid): RedirectResponse
|
|
{
|
|
if (!$uid) {
|
|
throw new \Exception("계정 번호가 정의 되지 않았습니다.");
|
|
}
|
|
$entity = $this->service->getEntity($uid);
|
|
if (!$entity instanceof UserEntity) {
|
|
throw new \Exception("{$uid}에 해당하는 계정을 찾을수 없습니다.");
|
|
}
|
|
//요청 데이터를 DTO 객체로 변환
|
|
$dto = new UserDTO($this->request->getPost());
|
|
$entity = $this->service->modify($entity, $dto);
|
|
$redirect_url = $this->getAuthContext()->popPreviousUrl() ?? implode(DIRECTORY_SEPARATOR, $this->getActionPaths());
|
|
return redirect()->to($redirect_url)->with('message', "{$entity->getTitle()} 계정 수정이 완료되었습니다.");
|
|
}
|
|
}
|