File manager - Edit - /home/webapp69.cm.in.th/u69319090041/Shop41/includes/middleware.php
Back
<?php /** * middleware.php — Multi-Vendor Role Guard * ใช้ include ไฟล์นี้ต้น page เพื่อจำกัดสิทธิ์เข้าถึง * * วิธีใช้: * require_once __DIR__ . '/../includes/middleware.php'; * requireVendor(); // หน้า Vendor เท่านั้น * requireAdmin(); // หน้า Admin เท่านั้น (มีอยู่ใน auth.php แล้ว) */ require_once __DIR__ . '/auth.php'; // ───────────────────────────────────────────── // Role Checkers // ───────────────────────────────────────────── if (!function_exists('isVendor')) { function isVendor(): bool { return isLoggedIn() && isset($_SESSION['user']['role']) && $_SESSION['user']['role'] === 'vendor'; } } /** * ตรวจสอบว่า vendor account ได้รับการอนุมัติหรือยัง */ function isApprovedVendor(): bool { if (!isVendor()) return false; // Cache ใน session เพื่อลด DB query if (isset($_SESSION['vendor_status'])) { return $_SESSION['vendor_status'] === 'approved'; } $pdo = getDBConnection(); $stmt = $pdo->prepare("SELECT status FROM vendors WHERE user_id = ? LIMIT 1"); $stmt->execute([$_SESSION['user']['id']]); $vendor = $stmt->fetch(); $status = $vendor ? $vendor['status'] : 'pending'; $_SESSION['vendor_status'] = $status; return $status === 'approved'; } // ───────────────────────────────────────────── // Guard Functions (redirect ถ้าไม่มีสิทธิ์) // ───────────────────────────────────────────── /** * บังคับให้ต้อง login และต้องเป็น Vendor ที่อนุมัติแล้ว */ function requireVendor(): void { requireLogin(); if (!isVendor()) { $_SESSION['flash_error'] = 'หน้านี้สำหรับผู้ขายเท่านั้น'; header('Location: ' . SITE_URL . '/index.php'); exit; } if (!isApprovedVendor()) { // Vendor ที่ยังรออนุมัติ header('Location: ' . SITE_URL . '/vendor_pending.php'); exit; } } /** * บังคับให้ต้อง login และต้องเป็น Admin หรือ Vendor * (สำหรับหน้าที่ทั้งสองบทบาทเข้าถึงได้) */ function requireAdminOrVendor(): void { requireLogin(); if (!isAdmin() && !isApprovedVendor()) { $_SESSION['flash_error'] = 'ไม่มีสิทธิ์เข้าถึงหน้านี้'; header('Location: ' . SITE_URL . '/index.php'); exit; } } // ───────────────────────────────────────────── // Vendor Data Helpers // ───────────────────────────────────────────── /** * ดึง vendor_id ของ user ที่ login อยู่ * @return int|null */ function getCurrentVendorId(): ?int { if (!isVendor()) return null; $vendorData = getCurrentVendorData(); if ($vendorData) { return (int) $vendorData['id']; } return null; } /** * ดึงข้อมูลร้านค้าของ vendor ที่ login อยู่ทั้งหมด * @return array|null */ function getCurrentVendorData(): ?array { if (!isVendor()) return null; // Cache ใน session (refresh เมื่อ save profile) if (isset($_SESSION['vendor_data']) && (int)$_SESSION['vendor_data']['user_id'] === (int)$_SESSION['user']['id']) { return $_SESSION['vendor_data']; } $pdo = getDBConnection(); $stmt = $pdo->prepare("SELECT * FROM vendors WHERE user_id = ? LIMIT 1"); $stmt->execute([$_SESSION['user']['id']]); $vendor = $stmt->fetch(); if ($vendor) { $_SESSION['vendor_data'] = $vendor; $_SESSION['vendor_id'] = $vendor['id']; return $vendor; } return null; } /** * เคลียร์ vendor session cache (เรียกหลัง save profile) */ function clearVendorSessionCache(): void { unset($_SESSION['vendor_id']); unset($_SESSION['vendor_data']); unset($_SESSION['vendor_status']); } // ───────────────────────────────────────────── // Ownership Guards (ป้องกัน Vendor แก้ของคนอื่น) // ───────────────────────────────────────────── /** * ตรวจสอบว่า product_id เป็นของ vendor ที่ login อยู่หรือไม่ * Admin ผ่านได้เสมอ */ function requireProductOwnership(int $product_id): void { if (isAdmin()) return; // Admin ผ่านได้เสมอ $vendor_id = getCurrentVendorId(); if (!$vendor_id) { http_response_code(403); die('Access Denied'); } $pdo = getDBConnection(); $stmt = $pdo->prepare("SELECT id FROM products WHERE id = ? AND vendor_id = ? LIMIT 1"); $stmt->execute([$product_id, $vendor_id]); if (!$stmt->fetch()) { $_SESSION['flash_error'] = 'คุณไม่มีสิทธิ์จัดการสินค้านี้'; header('Location: ' . dirname(SITE_URL) . '/vendor_panel/products.php'); exit; } } /** * ตรวจสอบว่า vendor_category_id เป็นของ vendor ที่ login อยู่หรือไม่ */ function requireCategoryOwnership(int $category_id): void { if (isAdmin()) return; $vendor_id = getCurrentVendorId(); if (!$vendor_id) { http_response_code(403); die('Access Denied'); } $pdo = getDBConnection(); $stmt = $pdo->prepare("SELECT id FROM vendor_categories WHERE id = ? AND vendor_id = ? LIMIT 1"); $stmt->execute([$category_id, $vendor_id]); if (!$stmt->fetch()) { $_SESSION['flash_error'] = 'คุณไม่มีสิทธิ์จัดการหมวดหมู่นี้'; header('Location: ' . dirname(SITE_URL) . '/vendor_panel/categories.php'); exit; } } /** * ตรวจสอบว่า vendor_order_id เป็นของ vendor ที่ login อยู่หรือไม่ */ function requireVendorOrderOwnership(int $vendor_order_id): void { if (isAdmin()) return; $vendor_id = getCurrentVendorId(); if (!$vendor_id) { http_response_code(403); die('Access Denied'); } $pdo = getDBConnection(); $stmt = $pdo->prepare("SELECT id FROM vendor_orders WHERE id = ? AND vendor_id = ? LIMIT 1"); $stmt->execute([$vendor_order_id, $vendor_id]); if (!$stmt->fetch()) { $_SESSION['flash_error'] = 'คุณไม่มีสิทธิ์เข้าถึงออเดอร์นี้'; header('Location: ' . dirname(SITE_URL) . '/vendor_panel/orders.php'); exit; } }
| ver. 1.4 |
Github
|
.
| PHP 7.4.33 | Generation time: 0.25 |
proxy
|
phpinfo
|
Settings