- IP pool: sheet import (xlsx/csv) matched by address, bulk allocate/revoke with notification email (ip_allocated/ip_revoked), ip_pool_logs history. - Orders/invoices: Excel export on branded template (BrandedReportWriter), shared billing filters, order item service info in list responses. - Web design quote Excel export; activity log export. - Service attachments with copy-to-invoice; deployment invoice attachments. - Customer email attachments stored on disk and attached by path. - Admin password change endpoint; user.fullname backfill migration; orders.user_id backfill from logs. - Disable deploy workflow until CI/CD is set up. - Ignore stored customer email and service attachment uploads. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
157 lines
4.9 KiB
PHP
157 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\Auth;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\UpdateProfileRequest;
|
|
use App\Http\Requests\UpdateAdminPasswordRequest;
|
|
use App\Http\Resources\Api\Admin\AdminUserResource;
|
|
use App\Services\ActivityLogger;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Tymon\JWTAuth\Facades\JWTAuth;
|
|
use Tymon\JWTAuth\Exceptions\JWTException;
|
|
|
|
class AuthController extends Controller
|
|
{
|
|
private const LOGIN_MAX_ATTEMPTS = 5;
|
|
private const LOGIN_DECAY_SECONDS = 60;
|
|
|
|
public function __construct(private readonly ActivityLogger $activityLogger) {}
|
|
|
|
public function login(Request $request): JsonResponse
|
|
{
|
|
$request->validate([
|
|
'username' => 'required|string',
|
|
'password' => 'required|string',
|
|
]);
|
|
|
|
$throttleKey = 'admin-login:' . strtolower($request->string('username')->toString()) . '|' . $request->ip();
|
|
|
|
if (RateLimiter::tooManyAttempts($throttleKey, self::LOGIN_MAX_ATTEMPTS)) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'Bạn đã đăng nhập sai quá nhiều lần. Vui lòng thử lại sau ít phút.',
|
|
], 429);
|
|
}
|
|
|
|
$token = Auth::guard('admin')->attempt($request->only('username', 'password'));
|
|
|
|
if (!$token) {
|
|
RateLimiter::hit($throttleKey, self::LOGIN_DECAY_SECONDS);
|
|
|
|
$this->activityLogger->log(
|
|
event: 'login_failed',
|
|
logName: 'auth',
|
|
description: '[admin] Đăng nhập thất bại với username: ' . $request->username,
|
|
);
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'Thông tin đăng nhập không chính xác.',
|
|
], 401);
|
|
}
|
|
|
|
RateLimiter::clear($throttleKey);
|
|
|
|
$user = Auth::guard('admin')->user();
|
|
Auth::guard('admin')->getProvider()->rehashPasswordIfRequired($user, $request->only('password'));
|
|
|
|
$this->activityLogger->logAuth('login', 'admin', $user->id, $user->username ?? $user->email);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => [
|
|
'token' => $token,
|
|
'token_type' => 'bearer',
|
|
'expires_in' => config('jwt.ttl') * 60,
|
|
'user' => new AdminUserResource($user),
|
|
],
|
|
]);
|
|
}
|
|
|
|
public function me(): JsonResponse
|
|
{
|
|
$user = Auth::guard('admin')->user();
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => ['user' => new AdminUserResource($user)],
|
|
]);
|
|
}
|
|
|
|
public function updateProfile(UpdateProfileRequest $request): JsonResponse
|
|
{
|
|
$user = Auth::guard('admin')->user();
|
|
|
|
$user->update($request->validated());
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Cập nhật hồ sơ thành công.',
|
|
'data' => ['user' => new AdminUserResource($user->fresh())],
|
|
]);
|
|
}
|
|
|
|
public function updatePassword(UpdateAdminPasswordRequest $request): JsonResponse
|
|
{
|
|
$user = Auth::guard('admin')->user();
|
|
|
|
$user->update([
|
|
'password' => Hash::make($request->string('password')->toString()),
|
|
]);
|
|
|
|
$this->activityLogger->logAuth(
|
|
'password_changed',
|
|
'admin',
|
|
$user->id,
|
|
$user->username ?? $user->email,
|
|
);
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Đổi mật khẩu thành công.',
|
|
]);
|
|
}
|
|
|
|
public function refresh(): JsonResponse
|
|
{
|
|
try {
|
|
$newToken = JWTAuth::parseToken()->refresh();
|
|
$user = JWTAuth::setToken($newToken)->toUser();
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => [
|
|
'token' => $newToken,
|
|
'token_type' => 'bearer',
|
|
'expires_in' => config('jwt.ttl') * 60,
|
|
'user' => new AdminUserResource($user),
|
|
],
|
|
]);
|
|
} catch (JWTException) {
|
|
return response()->json([
|
|
'error' => true,
|
|
'message' => 'Không thể làm mới phiên đăng nhập. Vui lòng đăng nhập lại.',
|
|
], 401);
|
|
}
|
|
}
|
|
|
|
public function logout(): JsonResponse
|
|
{
|
|
$user = Auth::guard('admin')->user();
|
|
if ($user) {
|
|
$this->activityLogger->logAuth('logout', 'admin', $user->id, $user->username ?? $user->email);
|
|
}
|
|
|
|
Auth::guard('admin')->logout();
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Đã đăng xuất thành công.',
|
|
]);
|
|
}
|
|
}
|