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(', ')}] + + )}