From e3afc0b64a32eb68d8e63c7fd513d52e0b31c706 Mon Sep 17 00:00:00 2001 From: sky121113 Date: Tue, 13 Jan 2026 17:27:19 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=A1=AF=E7=A4=BA=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E8=80=85=E8=A7=92=E8=89=B2=E4=BB=A5=E9=99=A4=E9=8C=AF=20&=20?= =?UTF-8?q?=E5=BC=B7=E5=8C=96=E6=AC=8A=E9=99=90=E6=8C=87=E6=B4=BE=E9=82=8F?= =?UTF-8?q?=E8=BC=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ..._13_172500_ensure_admin_is_super_admin.php | 64 +++++++++++++++++++ resources/js/Layouts/AuthenticatedLayout.tsx | 5 ++ 2 files changed, 69 insertions(+) create mode 100644 database/migrations/2026_01_13_172500_ensure_admin_is_super_admin.php diff --git a/database/migrations/2026_01_13_172500_ensure_admin_is_super_admin.php b/database/migrations/2026_01_13_172500_ensure_admin_is_super_admin.php new file mode 100644 index 0000000..faeb4c5 --- /dev/null +++ b/database/migrations/2026_01_13_172500_ensure_admin_is_super_admin.php @@ -0,0 +1,64 @@ +where('name', 'super-admin')->first(); + if (!$role) { + return; + } + + // 嘗試用多種條件抓取 admin 使用者 + $user = DB::table('users') + ->where('username', 'admin') + ->orWhere('email', 'admin@example.com') + ->orWhere('username', 'admin01') // 之前對話提到的可能 username + ->first(); + + if (!$user) { + // 如果都找不到,嘗試抓 ID = 1 或 2 (通常是建立的第一個使用者) + $user = DB::table('users')->orderBy('id')->first(); + } + + if (!$user) { + return; + } + + // 檢查是否已有此角色 + $exists = DB::table('model_has_roles') + ->where('role_id', $role->id) + ->where('model_type', 'App\\Models\\User') + ->where('model_id', $user->id) + ->exists(); + + if (!$exists) { + // 移除舊角色並指派新角色 + DB::table('model_has_roles') + ->where('model_type', 'App\\Models\\User') + ->where('model_id', $user->id) + ->delete(); + + DB::table('model_has_roles')->insert([ + 'role_id' => $role->id, + 'model_type' => 'App\\Models\\User', + 'model_id' => $user->id, + ]); + } + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + } +}; diff --git a/resources/js/Layouts/AuthenticatedLayout.tsx b/resources/js/Layouts/AuthenticatedLayout.tsx index 914f552..7c0c9f2 100644 --- a/resources/js/Layouts/AuthenticatedLayout.tsx +++ b/resources/js/Layouts/AuthenticatedLayout.tsx @@ -352,6 +352,11 @@ export default function AuthenticatedLayout({ {user.username || 'Administrator'} + {user.roles && user.roles.length > 0 && ( + + [{user.roles.join(', ')}] + + )}