trafficmonitor/app/Controllers/Admin/UserController.php
2025-11-05 18:58:37 +09:00

88 lines
3.2 KiB
PHP

<?php
namespace App\Controllers\Admin;
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
{
/**
* @property UserService $service
* IDE에게 protected $service 속성이 LocalService 타입임을 알려줍니다.
*/
public const PATH = 'user';
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
if ($this->service === null) {
$this->service = service('localauth');
}
$this->_paths[] = self::PATH;
}
protected function getFormRule(string $action, string $field, string $rule): array
{
switch ($field) {
case 'role':
$field = "{$field}.*";
break;
}
return parent::getFormRule($action, $field, $rule);
}
//Index,FieldForm관련.
final public function profile_form(mixed $uid): RedirectResponse|string
{
try {
$this->getService()->setAction(__FUNCTION__);
$this->getService()->setFormFields();
$this->getService()->setFormFilters();
$this->getService()->setFormRules();
$this->getService()->setFormOptions();
//기본값정의
$this->getService()->setFormDatas($this->request->getGet());
//기존 Entity 가져오기
$entity = $this->getService()->getEntity($uid);
if (!$entity instanceof UserEntity) {
throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
}
$this->entity = $entity;
helper(['form']);
$this->forms = ['attributes' => ['method' => "post",], 'hiddens' => []];
return $this->getResultSuccess();
} catch (\Exception $e) {
return $this->getResultFail($e->getMessage());
}
}
final public function profile(int $uid): RedirectResponse|string
{
//Transaction Start
$db = \Config\Database::connect();
$this->_db->transStart();
try {
$this->getService()->setAction(__FUNCTION__);
$this->getService()->setFormFields();
$this->getService()->setFormFilters();
$this->getService()->setFormRules();
//전달값정의
$this->getService()->setFormDatas($this->request->getPost());
$this->doValidations();
//기존 Entity 가져오기
$entity = $this->getService()->getEntity($uid);
if (!$entity instanceof UserEntity) {
throw new \Exception("{$uid}에 대한 정보를 찾을수 없습니다.");
}
$this->entity = $this->modify_process($entity, $this->getService()->getFormDatas());
$this->_db->transCommit();
return $this->getResultSuccess();
} catch (\Exception $e) {
$this->_db->transRollback();
return $this->getResultFail($e->getMessage());
}
}
}