跳至主要内容

Scenario 1: 店員訪問訂單管理功能

User Story: US-002: 角色權限控制

Given: 系統初始狀態

已登入店員

{
"userId": "user-001",
"email": "staff@florist.com",
"name": "王小明",
"role": "ROLE_SALES",
"roles": ["ROLE_SALES"],
"tenantId": "tenant-abc",
"accessToken": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9..."
}

Token Payload(扁平化 roles 陣列):

{
"sub": "user-001",
"tenantId": "tenant-abc",
"email": "staff@florist.com",
"roles": ["ROLE_SALES"]
}

注意:店員的 roles 陣列只包含 ROLE_SALES,沒有繼承其他角色。


When: 執行操作

路由層級檢查

  1. 用戶訪問 /orders 頁面(訂單管理)

API 層級檢查

  1. 前端發送創建訂單請求
POST /api/v1/orders HTTP/1.1
Authorization: Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9...
Content-Type: application/json

{
"customerId": "cust-123",
"items": [{"productId": "prod-001", "quantity": 2, "price": 1200}],
"deliveryAddress": "台北市信義區信義路五段 7 號",
"deliveryPhone": "0912-345-678",
"deliveryDate": "2025-11-01",
"deliveryTimeSlot": "14:00-18:00"
}

Then: 預期結果

路由層級檢查結果

  • 允許訪問 /orders 頁面(因為 ROLE_SALES 在 orders Applet 的 allowedRoles 中)
  • 頁面正常渲染訂單列表
  • 進入 Applet 後,所有功能都可用(Applet 層級權限設計)

API 層級檢查結果 (201 Created)

{
"orderId": "order-001",
"orderNumber": "ABC-20251031-0001",
"status": "pending_confirmation",
"customerId": "cust-123",
"pricing": {
"subtotal": 2400,
"tax": 120,
"total": 2520
},
"createdAt": "2025-10-31T10:30:00Z",
"createdBy": "user-001"
}

審計日誌

{
"id": "log-001",
"tenantId": "tenant-abc",
"userId": "user-001",
"userRole": "ROLE_SALES",
"action": "ORDER_CREATED",
"resourceType": "Order",
"resourceId": "order-001",
"status": "SUCCESS",
"timestamp": "2025-10-31T10:30:00Z"
}

UI 層級權限顯示

Application Launcher(店員角色)

根據 allowedRoles 配置與角色繼承,店員可見的 Applet:

✅ 首頁           (空陣列 = 所有角色)
✅ 訂單管理 (allowedRoles: [SALES] → SALES 匹配)
✅ 客戶管理 (allowedRoles: [SALES] → SALES 匹配)
✅ 銷售工作台 (allowedRoles: [SALES] → SALES 匹配)
✅ 行事曆 (空陣列 = 所有角色)
✅ 訊息 (空陣列 = 所有角色)
❌ 商品管理 (allowedRoles: [PURCHASER, FLORIST] → 無匹配)
❌ 設計工作台 (allowedRoles: [FLORIST] → 無匹配)
❌ 配送工作台 (allowedRoles: [DELIVERY] → 無匹配)
❌ 報表 (allowedRoles: [ACCOUNTANT] → 無匹配)
❌ 系統設定 (allowedRoles: [ADMIN] → 無匹配)

訂單管理頁面(進入 Applet 後)

核心原則:進入 Applet 即可使用所有功能

✅ 創建訂單
✅ 編輯訂單
✅ 確認訂單
✅ 取消訂單
✅ 所有操作按鈕

注意:細粒度權限控制由後端 Spring Security 處理。前端不在組件層做權限判斷。


最後更新: 2025-12-20