From 2e7aeef367f1dc9f4a26a4a785b1a2c0220d03b9 Mon Sep 17 00:00:00 2001 From: sky121113 Date: Tue, 13 Jan 2026 17:15:32 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20Migration=20?= =?UTF-8?q?=E7=A2=BA=E4=BF=9D=20admin=20=E4=BD=BF=E7=94=A8=E8=80=85?= =?UTF-8?q?=E8=87=AA=E5=8B=95=E8=A2=AB=E8=A8=AD=E7=82=BA=20super-admin=20?= =?UTF-8?q?=E8=A7=92=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...300_assign_admin_user_super_admin_role.php | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 database/migrations/2026_01_13_171300_assign_admin_user_super_admin_role.php 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 不需要復原邏輯 + } +};