dbmsv4/app/Services/Auth/LocalService.php
2025-11-20 17:39:06 +09:00

75 lines
2.2 KiB
PHP

<?php
namespace App\Services\Auth;
use App\DTOs\Auth\AuthDTO; // 부모 클래스의 타입 힌트를 위해 사용 (알리아스 아님)
use App\DTOs\Auth\LocalDTO; // LocalDTO를 직접 사용
use App\Entities\UserEntity;
use App\Forms\Auth\LocalForm;
use App\Models\UserModel;
use RuntimeException;
class LocalService extends AuthService
{
private $_form = null;
public function __construct(UserModel $model)
{
parent::__construct($model);
$this->addClassPaths('Local');
}
public function getFormService(): LocalForm
{
if ($this->_form === null) {
$this->_form = new LocalForm();
$this->_form->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->_form;
}
public function action_init_process(string $action): void
{
$fields = ['id', 'passwd'];
$filters = [];
switch ($action) {
case 'login':
case 'login_form':
break;
}
$this->getFormService()->setFormFields($fields);
$this->getFormService()->setFormRules($action, $fields);
$this->getFormService()->setFormFilters($filters);
$this->getFormService()->setFormOptions($filters);
$this->getFormService()->setBatchjobFilters($filters);
}
protected function getEntity_process(mixed $entity): UserEntity
{
return $entity;
}
protected function login_process(array $formDatas): UserEntity
{
$entity = $this->getEntity(['id' => $formDatas['id'], 'status' => 'AVAILABLE'], true);
if (!$entity instanceof UserEntity) {
throw new \Exception("{$formDatas['id']}에 대한 로그인 정보를 찾을수 없습니다.");
}
if (!password_verify($formDatas['passwd'], $entity->getPassword())) {
throw new \Exception("암호가 맞지 않습니다.");
}
return $entity;
}
public function login(AuthDTO $dto): UserEntity
{
if (!$dto instanceof LocalDTO) {
throw new RuntimeException(__METHOD__ . "에서 오류발생: " . get_class($dto) . " DTO는 사용할 수 없습니다. LocalDTO만 허용됩니다.");
}
return parent::login($dto);
}
}