trafficmonitor/app/Services/Auth/LocalService.php
2025-11-06 16:51:52 +09:00

67 lines
2.0 KiB
PHP

<?php
namespace App\Services\Auth;
use App\DTOs\Auth\LocalDTO;
use App\Entities\UserEntity;
use App\Forms\Auth\LocalForm;
use App\Models\UserModel;
use CodeIgniter\Validation\Exceptions\ValidationException;
use RuntimeException;
class LocalService extends AuthService
{
public function __construct(UserModel $model)
{
parent::__construct($model);
$this->addClassPaths('Local');
}
public function getFormService(): LocalForm
{
if ($this->formServiceInstance === null) {
$this->formServiceInstance = new LocalForm();
$this->formServiceInstance->setAttributes([
'pk_field' => $this->model->getPKField(),
'title_field' => $this->model->getTitleField(),
'table' => $this->model->getTable(),
'useAutoIncrement' => $this->model->useAutoIncrement(),
'class_path' => $this->getClassPaths(false)
]);
}
return $this->formServiceInstance;
}
public function getFormFields(): array
{
return ["id", "passwd",];
}
public function getFormFilters(): array
{
return [];
}
public function getBatchjobButtons(): array
{
return [];
}
//기본기능
public function login(mixed $dto): UserEntity
{
if (!$dto instanceof LocalDTO) {
throw new RuntimeException(__METHOD__ . "에서 오류발생: LcoalDTO만 사용하실수 있습니다");
}
// DTO 객체를 배열로 변환하여 검증기에 전달
$formDatas = (array) $dto;
if (!service('validation')->setRules($this->getFormRules(__FUNCTION__))->run($formDatas)) {
// 검증 실패 시, ValidationException을 던집니다.
throw new ValidationException(implode("\n", service('validation')->getErrors()));
}
$entity = $this->getEntity(['id' => $dto->id, 'status' => STATUS['AVAILABLE']], false);
if (!$entity instanceof UserEntity) {
throw new \Exception("{$dto->id}에 대한 로그인 정보를 찾을수 없습니다.");
}
if (!password_verify($dto->passwd, $entity->getPassword())) {
throw new \Exception("암호가 맞지 않습니다.");
}
return $this->login_process($entity);
}
}