122 lines
3.7 KiB
PHP
122 lines
3.7 KiB
PHP
<?php
|
||
|
||
namespace Database\Seeders;
|
||
|
||
use App\Models\AdminRole;
|
||
use App\Models\AdminUser;
|
||
use Illuminate\Database\Seeder;
|
||
use Illuminate\Support\Facades\DB;
|
||
use App\Support\AdminPermissionBridge;
|
||
|
||
/**
|
||
* 后台 RBAC:与 {@see AdminUser::ROLE_SUPER_ADMIN} 及 `config/admin_permissions.php` 对齐。
|
||
*
|
||
* 演示账号 **admin** / **123456**(仅限非 production)。
|
||
*/
|
||
final class AdminRbacAndUserSeeder extends Seeder
|
||
{
|
||
/** @param list<string> $legacySlugs */
|
||
private function syncRoleMenuActions(AdminRole $role, array $legacySlugs): void
|
||
{
|
||
$codes = [];
|
||
foreach ($legacySlugs as $slug) {
|
||
$codes = array_merge($codes, AdminPermissionBridge::menuActionCodesForLegacy($slug));
|
||
}
|
||
$codes = array_values(array_unique($codes));
|
||
|
||
$ids = DB::table('admin_menu_actions')
|
||
->whereIn('permission_code', $codes)
|
||
->where('status', 1)
|
||
->pluck('id')
|
||
->all();
|
||
|
||
DB::table('admin_role_menu_actions')->where('role_id', $role->id)->delete();
|
||
foreach ($ids as $mid) {
|
||
DB::table('admin_role_menu_actions')->insert([
|
||
'role_id' => $role->id,
|
||
'menu_action_id' => (int) $mid,
|
||
]);
|
||
}
|
||
}
|
||
|
||
/** @return list<string> */
|
||
private function allCatalogSlugs(): array
|
||
{
|
||
return AdminPermissionBridge::allLegacySlugs();
|
||
}
|
||
|
||
public function run(): void
|
||
{
|
||
$super = AdminRole::query()->updateOrCreate(
|
||
['slug' => AdminUser::ROLE_SUPER_ADMIN],
|
||
['name' => '超级管理员'],
|
||
);
|
||
$this->syncRoleMenuActions($super, $this->allCatalogSlugs());
|
||
|
||
$risk = AdminRole::query()->updateOrCreate(
|
||
['slug' => 'risk_operator'],
|
||
['name' => '风控运营员'],
|
||
);
|
||
$this->syncRoleMenuActions($risk, [
|
||
'prd.play_switch.manage',
|
||
'prd.odds.manage',
|
||
'prd.risk_cap.manage',
|
||
'prd.rebate.manage',
|
||
'prd.jackpot.manage',
|
||
'prd.draw_result.manage',
|
||
'prd.payout.review',
|
||
'prd.wallet_reconcile.view',
|
||
'prd.audit.self',
|
||
'prd.player_freeze.manage',
|
||
]);
|
||
|
||
$finance = AdminRole::query()->updateOrCreate(
|
||
['slug' => 'finance'],
|
||
['name' => '财务/对账员'],
|
||
);
|
||
$this->syncRoleMenuActions($finance, [
|
||
'prd.users.view_finance',
|
||
'prd.risk_cap.view',
|
||
'prd.rebate.view',
|
||
'prd.jackpot.view',
|
||
'prd.draw_result.view',
|
||
'prd.payout.view',
|
||
'prd.wallet_reconcile.manage',
|
||
'prd.wallet_adjust.manage',
|
||
'prd.audit.finance',
|
||
]);
|
||
|
||
$cs = AdminRole::query()->updateOrCreate(
|
||
['slug' => 'customer_service'],
|
||
['name' => '客服人员'],
|
||
);
|
||
$this->syncRoleMenuActions($cs, [
|
||
'prd.users.view_cs',
|
||
'prd.draw_result.view',
|
||
'prd.wallet_reconcile.view_cs',
|
||
]);
|
||
|
||
$username = 'admin';
|
||
AdminUser::query()->updateOrCreate(
|
||
['username' => $username],
|
||
[
|
||
'name' => '超级管理员',
|
||
'email' => null,
|
||
'password' => '123456',
|
||
'status' => 0,
|
||
],
|
||
);
|
||
|
||
/** @var AdminUser $admin */
|
||
$admin = AdminUser::query()->where('username', $username)->firstOrFail();
|
||
$siteId = AdminUser::defaultAdminSiteId();
|
||
$superId = (int) $super->getKey();
|
||
$admin->roles()->sync([
|
||
$superId => [
|
||
'site_id' => $siteId,
|
||
'granted_at' => now(),
|
||
],
|
||
]);
|
||
}
|
||
}
|