feat: 新增 Migration 確保 admin 使用者自動被設為 super-admin 角色
All checks were successful
Koori-ERP-Deploy-System / deploy-demo (push) Successful in 45s
Koori-ERP-Deploy-System / deploy-production (push) Has been skipped

This commit is contained in:
2026-01-13 17:15:32 +08:00
parent 566dfa31ae
commit 2e7aeef367

View File

@@ -0,0 +1,56 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
/**
* Run the migrations.
* 確保 username 'admin' 的使用者被指派為 super-admin 角色
*/
public function up(): void
{
// 取得 super-admin 角色
$role = DB::table('roles')->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 不需要復原邏輯
}
};