174 lines
5.7 KiB
PHP
174 lines
5.7 KiB
PHP
<?php
|
|
namespace App\Http\Controllers\Admin;
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Services\Admin\AdminAdminsService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Arr;
|
|
|
|
final class AdminAdminsController
|
|
{
|
|
public function __construct(
|
|
private readonly AdminAdminsService $service,
|
|
) {}
|
|
|
|
public function index(Request $request)
|
|
{
|
|
$data = $this->service->list($request->all());
|
|
return view('admin.admins.index', $data);
|
|
}
|
|
|
|
public function edit(int $id)
|
|
{
|
|
$data = $this->service->editData($id);
|
|
if (!$data['admin']) {
|
|
return redirect()->route('admin.admins.index')->with('error', '관리자를 찾을 수 없습니다.');
|
|
}
|
|
return view('admin.admins.edit', $data);
|
|
}
|
|
|
|
public function update(Request $request, int $id)
|
|
{
|
|
$res = $this->service->update(
|
|
$id,
|
|
$request->all(),
|
|
(int)auth('admin')->id(),
|
|
$request->ip(),
|
|
(string)($request->userAgent() ?? '')
|
|
);
|
|
|
|
|
|
|
|
if (!($res['ok'] ?? false)) {
|
|
return redirect()->back()->withInput()
|
|
->with('toast', [
|
|
'type' => 'danger',
|
|
'title' => '저장 실패',
|
|
'message' => $res['message'] ?? '수정에 실패했습니다.',
|
|
]);
|
|
}
|
|
|
|
// ✅ 수정 후에는 edit(GET)으로 보내기
|
|
return redirect()
|
|
->route('admin.admins.edit', ['id' => $id])
|
|
->with('toast', [
|
|
'type' => 'success',
|
|
'title' => '저장 완료',
|
|
'message' => '변경되었습니다.',
|
|
]);
|
|
}
|
|
|
|
public function resetPassword(Request $request, int $id)
|
|
{
|
|
$res = $this->service->resetPasswordToEmail(
|
|
$id,
|
|
(int)auth('admin')->id(),
|
|
$request->ip(),
|
|
(string)($request->userAgent() ?? '')
|
|
);
|
|
|
|
if (!($res['ok'] ?? false)) {
|
|
return redirect()->back()->withInput()
|
|
->with('toast', [
|
|
'type' => 'danger',
|
|
'title' => '저장 실패',
|
|
'message' => $res['message'] ?? '수정에 실패했습니다.',
|
|
]);
|
|
}
|
|
|
|
// ✅ 수정 후에는 edit(GET)으로 보내기
|
|
return redirect()
|
|
->route('admin.admins.edit', ['id' => $id])
|
|
->with('toast', [
|
|
'type' => 'success',
|
|
'title' => '저장 완료',
|
|
'message' => '변경되었습니다.',
|
|
]);
|
|
|
|
}
|
|
|
|
public function unlock(int $id)
|
|
{
|
|
$res = $this->service->unlock(
|
|
$id,
|
|
(int)auth('admin')->id(),
|
|
$request->ip(),
|
|
(string)($request->userAgent() ?? '')
|
|
);
|
|
|
|
if (!($res['ok'] ?? false)) {
|
|
return redirect()->back()->withInput()
|
|
->with('toast', [
|
|
'type' => 'danger',
|
|
'title' => '저장 실패',
|
|
'message' => $res['message'] ?? '수정에 실패했습니다.',
|
|
]);
|
|
}
|
|
|
|
// 수정 후에는 edit(GET)으로 보내기
|
|
return redirect()
|
|
->route('admin.admins.edit', ['id' => $id])
|
|
->with('toast', [
|
|
'type' => 'success',
|
|
'title' => '저장 완료',
|
|
'message' => '변경되었습니다.',
|
|
]);
|
|
}
|
|
|
|
public function create(Request $request)
|
|
{
|
|
// 단일 선택 role 목록 (value는 code)
|
|
$roles = $this->service->getAssignableRoles(); // 아래 서비스에 추가
|
|
|
|
return view('admin.admins.create', [
|
|
'roles' => $roles,
|
|
'filters' => $request->only(['q','status','page']),
|
|
]);
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$data = $request->validate([
|
|
'email' => ['required', 'string', 'email', 'max:190', 'unique:admin_users,email'],
|
|
'name' => ['required', 'string', 'max:60'],
|
|
'nickname' => ['required', 'string', 'max:60'],
|
|
'phone' => ['required', 'string', 'max:30'],
|
|
'role' => ['required', 'string', 'in:super_admin,finance,product,support'],
|
|
]);
|
|
|
|
// phoneDigits: 숫자만 10~11
|
|
$phoneDigits = preg_replace('/\D+/', '', (string)($data['phone'] ?? ''));
|
|
if (!preg_match('/^\d{10,11}$/', $phoneDigits)) {
|
|
return back()->withErrors(['phone' => '휴대폰 번호는 숫자만 10~11자리로 입력해 주세요.'])->withInput();
|
|
}
|
|
|
|
$actorId = (int) auth('admin')->id();
|
|
|
|
$res = $this->service->createAdmin([
|
|
'email' => strtolower(trim($data['email'])),
|
|
'name' => $data['name'],
|
|
'nickname' => $data['nickname'],
|
|
'phone_digits' => $phoneDigits,
|
|
'role' => $data['role'],
|
|
],
|
|
$actorId,
|
|
$request->ip(),
|
|
(string)($request->userAgent() ?? ''),
|
|
);
|
|
|
|
if (!($res['ok'] ?? false)) {
|
|
return back()->withErrors(['email' => (string)($res['message'] ?? '등록에 실패했습니다.')])->withInput();
|
|
}
|
|
|
|
// ✅ 임시 비밀번호는 “1회 안내”를 위해 메시지에 포함(원하면 문구만 바꿔도 됨)
|
|
$temp = (string)($res['temp_password'] ?? '');
|
|
|
|
return redirect()
|
|
->route('admin.admins.edit', ['id' => (int)($res['admin_id'] ?? 0)])
|
|
->with('status', $temp !== ''
|
|
? "관리자 계정이 등록되었습니다. 임시 비밀번호: {$temp} (다음 로그인 시 변경 강제)"
|
|
: "관리자 계정이 등록되었습니다. (다음 로그인 시 비밀번호 변경 강제)"
|
|
);
|
|
}
|
|
}
|