[FIX] 員工卡編輯彈窗未回填所屬公司,並防止儲存時洗掉 company_id

1. 修正系統管理員開啟「編輯員工識別卡」時,所屬公司下拉顯示為「選擇公司」空值的問題:Preline 的 option value 為字串,原本直接以數字型別的 company_id 呼叫 setValue,嚴格比對失配導致無法回填,現統一轉為字串 (對齊 ads / welcome-gifts / pickup-codes 既有寫法)。
2. 強化 StaffCardController@update 的寫入安全 (RBAC 1.3):非系統管理員一律強制綁定自身 company_id;系統管理員若未帶入公司則保留原本歸屬,避免在上述空值狀態按下儲存時把 company_id 洗成 null,造成機台刷卡「查無此員工卡」的孤兒卡。
3. 影響範圍:資料設定 → 員工識別管理之新增/編輯彈窗 (resources/views/admin/data-config/staff-cards/index.blade.php) 與 App\Http\Controllers\Admin\StaffCardController;僅前端回填與後端寫入保護,不更動資料表結構與既有正常卡片資料。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
sky121113 2026-06-23 11:18:13 +08:00
parent e3593a4ba8
commit 99debbf259
2 changed files with 14 additions and 1 deletions

View File

@ -110,6 +110,15 @@ class StaffCardController extends Controller
$validated['status'] = $request->get('status', $staffCard->status); $validated['status'] = $request->get('status', $staffCard->status);
// 寫入安全company_id 不得由前端任意決定或被洗成 null
// - 非系統管理員:強制綁定自身公司 (租戶隔離)
// - 系統管理員:若未帶入公司,保留原本歸屬,避免變成無公司歸屬的孤兒卡 (機台將查無此卡)
if (!auth()->user()->isSystemAdmin()) {
$validated['company_id'] = auth()->user()->company_id;
} elseif (empty($validated['company_id'])) {
$validated['company_id'] = $staffCard->company_id;
}
$staffCard->update($validated); $staffCard->update($validated);
if ($request->ajax()) { if ($request->ajax()) {

View File

@ -448,7 +448,11 @@
this.$nextTick(() => { this.$nextTick(() => {
const select = HSSelect.getInstance('#modal-company-id', true); const select = HSSelect.getInstance('#modal-company-id', true);
if (select) { if (select) {
select.setValue(this.staffFormFields.company_id || ' '); // Preline 的 option value 為字串company_id 為數字時嚴格比對會失配導致回填失敗,
// 故統一轉成字串;空值則以 ' ' 對應 placeholder 選項
const cid = this.staffFormFields.company_id;
const valStr = (cid !== undefined && cid !== null && cid.toString().trim() !== '') ? cid.toString() : ' ';
select.setValue(valStr);
} }
}); });
}, },