cfmgrv4/app/Controllers/UserController.php
2024-10-05 23:56:06 +09:00

127 lines
4.4 KiB
PHP

<?php
namespace App\Controllers;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Validation\Validation;
use Psr\Log\LoggerInterface;
use App\Models\UserModel;
class UserController extends FrontController
{
private $_model = null;
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
{
parent::initController($request, $response, $logger);
$this->class_name .= "User";
$this->class_path .= $this->class_name;
$this->view_path = strtolower($this->view_root . $this->class_name);
$this->title = lang("{$this->class_path}.title");
helper($this->class_path);
}
protected function getModel(): UserModel
{
if ($this->_model === null) {
$this->_model = new UserModel();
}
return $this->_model;
}
protected function setFormFieldRule($field, Validation $validation, string $action): Validation
{
switch ($field) {
case 'role':
//아래 Rule Array는 필드명.* checkbox를 사용
$validation->setRule($field . ".*", $field, $this->getModel()->getFieldRule($action, $field));
break;
default:
$validation = parent::setFormFieldRule($field, $validation, $action);
break;
}
return $validation;
}
protected function getFormFieldOption(string $field, array $options = []): array
{
switch ($field) {
default:
$options = parent::getFormFieldOption($field, $options);
break;
}
return $options;
}
protected function getFormData(string $field, array $formDatas): array
{
switch ($field) {
case 'role':
$roles = $this->request->getVar($field) ?? [];
if (!count($roles)) {
throw new \Exception("권한이 지정되지 않았습니다.");
}
$formDatas[$field] = implode(DEFAULTS["DELIMITER_ROLE"], $roles);
break;
default:
$formDatas = parent::getFormData($field, $formDatas);
break;
}
return $formDatas;
}
private function init(string $action): void
{
$this->action = $action;
$this->fields = ['id', 'passwd'];
$this->field_rules = $this->getModel()->getFieldRules($this->action, $this->fields);
}
//로그인화면
public function create_form(): RedirectResponse|string
{
$this->init('create');
return $this->create_form_procedure();
}
//로그인처리
protected function create_process(): void
{
// $this->create_validate($this->action, $this->fields);
$this->formDatas = $this->getFormDatas();
if (!isset($this->formDatas['id']) || !$this->formDatas['id']) {
throw new \Exception("사용자ID를 입력해주세요!");
}
if (!isset($this->formDatas['passwd']) || !$this->formDatas['passwd']) {
throw new \Exception("암호를 입력해주세요!");
}
$entity = $this->getModel()->getEntityByID($this->formDatas['id']);
if (is_null($entity) || !isset($entity->passwd)) {
throw new \Exception("사용자ID: {$this->formDatas['id']}가 존재하지 않습니다.");
}
if (password_verify($this->formDatas['passwd'], $entity->passwd)) {
//Session에 Login 정보전달
$this->session->set([
SESSION_NAMES['AUTH'] => [
'uid' => $entity->getPK(),
'name' => $entity->getTitle(),
'email' => $entity->email,
'role' => $entity->role
],
SESSION_NAMES['ISLOGIN'] => true
]);
$this->message = "로그인 성공";
} else {
throw new \Exception("로그인 실패");
}
}
public function create(): RedirectResponse|string
{
$this->init(__FUNCTION__);
return $this->create_procedure();
}
//로그아웃
public function logout(): RedirectResponse
{
//Session에 Login 정보 삭제
session()->set([SESSION_NAMES['ISLOGIN'] => false]);
session_destroy();
return redirect()->route('/');
}
}