Files
portal-api/app/Http/Controllers/Api/Admin/Setting/PopupBannerController.php
baodev0andClaude Sonnet 5 12d5b4951c feat: popup quảng cáo, đặt lại mật khẩu bằng token 80 ký tự, 2FA email, vá lỗ hổng bảo mật
- Popup/banner quảng cáo cấu hình từ admin (loại, link, lịch hiển thị)
- Đặt lại mật khẩu: token 80 ký tự kèm check digit Luhn thay cho token+email lộ trong URL, chỉ lưu bản băm, một lần dùng, hết hạn 60 phút
- Xác thực 2 lớp qua email khi đăng nhập (mã 6 số, ghi nhớ thiết bị 90 ngày)
- ApiAuthCheck chặn ngay tài khoản bị khoá dù JWT còn hạn (trước đây dùng được tới hết TTL)
- Handler trả 404 thay vì 500 khi ModelNotFoundException không bọc HttpException
- Test ClientTamperingTest: xác nhận IDOR/giả mạo dữ liệu từ client đều bị chặn

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-10-13 13:23:33 +07:00

92 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api\Admin\Setting;
use App\Http\Controllers\Controller;
use App\Models\PopupBanner;
use App\Support\ClientMenu;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rule;
class PopupBannerController extends Controller
{
public function index(): JsonResponse
{
return response()->json([
'error' => false,
'data' => PopupBanner::orderBy('sort_order')->orderBy('id')->get(),
'types' => PopupBanner::TYPES,
]);
}
public function store(Request $request): JsonResponse
{
$data = $this->validated($request, true);
$data['image_path'] = $request->file('image')->store('popup-banners', PopupBanner::DISK);
return response()->json([
'error' => false,
'message' => 'Đã thêm popup.',
'data' => PopupBanner::create($data),
], 201);
}
public function update(Request $request, int $id): JsonResponse
{
$popup = PopupBanner::findOrFail($id);
$data = $this->validated($request, false);
$oldImage = null;
if ($request->hasFile('image')) {
$oldImage = $popup->image_path;
$data['image_path'] = $request->file('image')->store('popup-banners', PopupBanner::DISK);
}
$popup->update($data);
if ($oldImage) {
Storage::disk(PopupBanner::DISK)->delete($oldImage);
}
return response()->json([
'error' => false,
'message' => 'Đã cập nhật popup.',
'data' => $popup->fresh(),
]);
}
public function destroy(int $id): JsonResponse
{
$popup = PopupBanner::findOrFail($id);
$popup->delete();
Storage::disk(PopupBanner::DISK)->delete($popup->image_path);
return response()->json(['error' => false, 'message' => 'Đã xoá popup.']);
}
/**
* @return array<string, mixed>
*/
private function validated(Request $request, bool $creating): array
{
$data = $request->validate([
'title' => 'required|string|max:150',
'type' => ['required', Rule::in(array_keys(PopupBanner::TYPES))],
'image' => [$creating ? 'required' : 'nullable', 'file', 'mimes:jpg,jpeg,png,webp,gif', 'max:5120'],
'link_url' => ['nullable', 'string', 'max:255', 'regex:' . ClientMenu::HREF_PATTERN],
'open_in_new_tab' => 'boolean',
'is_active' => 'boolean',
'starts_at' => 'nullable|date',
'ends_at' => 'nullable|date|after_or_equal:starts_at',
'sort_order' => 'integer|min:0|max:65535',
]);
unset($data['image']);
return $data;
}
}