feat: 更新数据库填充器以支持开发环境的管理员和玩家数据填充,新增示例钱包余额配置
This commit is contained in:
71
database/seeders/AdminRbacAndUserSeeder.php
Normal file
71
database/seeders/AdminRbacAndUserSeeder.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\AdminUser;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* 后台角色(super_admin)、若干权限占位;本地演示:**admin@admin.com** / **123456**(仅限非 production)。
|
||||
*/
|
||||
class AdminRbacAndUserSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$now = now();
|
||||
|
||||
DB::table('admin_roles')->updateOrInsert(
|
||||
['slug' => 'super_admin'],
|
||||
[
|
||||
'name' => 'Super Admin',
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
],
|
||||
);
|
||||
/** @var int $rid */
|
||||
$rid = (int) DB::table('admin_roles')->where('slug', 'super_admin')->value('id');
|
||||
|
||||
$perms = [
|
||||
['slug' => 'admin.dashboard', 'name' => 'Dashboard'],
|
||||
['slug' => 'admin.players.read', 'name' => 'View players'],
|
||||
['slug' => 'admin.wallet.read', 'name' => 'View wallets'],
|
||||
];
|
||||
foreach ($perms as $p) {
|
||||
DB::table('admin_permissions')->updateOrInsert(
|
||||
['slug' => $p['slug']],
|
||||
[
|
||||
'name' => $p['name'],
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
$pidRows = DB::table('admin_permissions')->whereIn('slug', array_column($perms, 'slug'))->pluck('id');
|
||||
foreach ($pidRows as $pid) {
|
||||
DB::table('admin_role_permissions')->updateOrInsert(
|
||||
['role_id' => $rid, 'permission_id' => $pid],
|
||||
[],
|
||||
);
|
||||
}
|
||||
|
||||
$email = 'admin@admin.com';
|
||||
AdminUser::query()->updateOrCreate(
|
||||
['email' => $email],
|
||||
[
|
||||
'name' => 'admin',
|
||||
/** 明文;模型 casts `password => hashed`,勿在生产库使用种子弱口令 */
|
||||
'password' => '123456',
|
||||
'status' => 0,
|
||||
],
|
||||
);
|
||||
|
||||
/** @var int $uid */
|
||||
$uid = (int) AdminUser::query()->where('email', $email)->value('id');
|
||||
DB::table('admin_user_roles')->updateOrInsert(
|
||||
['admin_user_id' => $uid, 'role_id' => $rid],
|
||||
[],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user