73 lines
2.2 KiB
PHP
73 lines
2.2 KiB
PHP
<?php
|
||
|
||
namespace Database\Seeders;
|
||
|
||
use App\Models\AdminUser;
|
||
use Illuminate\Database\Seeder;
|
||
use Illuminate\Support\Facades\DB;
|
||
|
||
/**
|
||
* 后台角色(super_admin)、若干权限占位;本地演示:账号 **admin** / **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],
|
||
[],
|
||
);
|
||
}
|
||
|
||
$username = 'admin';
|
||
AdminUser::query()->updateOrCreate(
|
||
['username' => $username],
|
||
[
|
||
'name' => '超级管理员',
|
||
'email' => null,
|
||
/** 明文;模型 casts `password => hashed`,勿在生产库使用种子弱口令 */
|
||
'password' => '123456',
|
||
'status' => 0,
|
||
],
|
||
);
|
||
|
||
/** @var int $uid */
|
||
$uid = (int) AdminUser::query()->where('username', $username)->value('id');
|
||
DB::table('admin_user_roles')->updateOrInsert(
|
||
['admin_user_id' => $uid, 'role_id' => $rid],
|
||
[],
|
||
);
|
||
}
|
||
}
|