diff --git a/database/migrations/2026_01_13_171300_assign_admin_user_super_admin_role.php b/database/migrations/2026_01_13_171300_assign_admin_user_super_admin_role.php new file mode 100644 index 0000000..b7cdfa7 --- /dev/null +++ b/database/migrations/2026_01_13_171300_assign_admin_user_super_admin_role.php @@ -0,0 +1,56 @@ +where('name', 'super-admin')->first(); + if (!$role) { + return; // 角色不存在則跳過 + } + + // 取得 admin 使用者 + $user = DB::table('users')->where('username', 'admin')->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(); + + // 指派 super-admin 角色 + 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 + { + // 此 Migration 不需要復原邏輯 + } +};