feat: 增强管理员权限管理,添加 RBAC 支持,更新 AdminUser 模型以处理角色和权限,更新登录接口返回权限信息,扩展数据库填充器以同步角色权限
This commit is contained in:
@@ -37,7 +37,7 @@ test('admin login returns bearer token when captcha passes validation', function
|
||||
->assertJsonPath('code', ErrorCode::Success->value)
|
||||
->assertJsonPath('data.admin.username', 'tester')
|
||||
->assertJsonPath('data.admin.nickname', '测试昵称')
|
||||
->assertJsonStructure(['data' => ['token', 'token_type', 'admin' => ['id', 'username', 'nickname', 'email']]]);
|
||||
->assertJsonStructure(['data' => ['token', 'token_type', 'admin' => ['id', 'username', 'nickname', 'email', 'permissions']]]);
|
||||
|
||||
$token = $resp->json('data.token');
|
||||
expect($token)->not->toBeNull();
|
||||
|
||||
195
tests/Feature/AdminCsFinanceApisTest.php
Normal file
195
tests/Feature/AdminCsFinanceApisTest.php
Normal file
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
|
||||
use App\Models\AdminUser;
|
||||
use App\Models\Draw;
|
||||
use App\Models\Player;
|
||||
use App\Models\TicketItem;
|
||||
use App\Models\TicketOrder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
function mintCsFinanceAdminToken(): string
|
||||
{
|
||||
$admin = AdminUser::query()->create([
|
||||
'username' => 'cs_finance_admin',
|
||||
'name' => 'CS Finance QA',
|
||||
'email' => null,
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($admin);
|
||||
|
||||
return $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
}
|
||||
|
||||
test('admin lists ticket items for a player', function (): void {
|
||||
$token = mintCsFinanceAdminToken();
|
||||
|
||||
$player = Player::query()->create([
|
||||
'site_code' => 'main',
|
||||
'site_player_id' => 'csf-p1',
|
||||
'username' => 'csf_u1',
|
||||
'nickname' => null,
|
||||
'default_currency' => 'NPR',
|
||||
'status' => 0,
|
||||
]);
|
||||
|
||||
$draw = Draw::query()->create([
|
||||
'draw_no' => '20260520-001',
|
||||
'business_date' => '2026-05-20',
|
||||
'sequence_no' => 1,
|
||||
'status' => 'settled',
|
||||
'start_time' => now()->subDay(),
|
||||
'close_time' => now()->subDay(),
|
||||
'draw_time' => now()->subDay(),
|
||||
'cooling_end_time' => null,
|
||||
'result_source' => null,
|
||||
'current_result_version' => 1,
|
||||
'settle_version' => 1,
|
||||
'is_reopened' => false,
|
||||
]);
|
||||
|
||||
$order = TicketOrder::query()->create([
|
||||
'order_no' => 'ORD-CSF-1',
|
||||
'player_id' => $player->id,
|
||||
'draw_id' => $draw->id,
|
||||
'currency_code' => 'NPR',
|
||||
'total_bet_amount' => 1000,
|
||||
'total_rebate_amount' => 0,
|
||||
'total_actual_deduct' => 1000,
|
||||
'total_estimated_payout' => 0,
|
||||
'status' => 'settled',
|
||||
'submit_source' => 'h5',
|
||||
'client_trace_id' => null,
|
||||
]);
|
||||
|
||||
TicketItem::query()->create([
|
||||
'ticket_no' => 'TKCSF0001',
|
||||
'order_id' => $order->id,
|
||||
'player_id' => $player->id,
|
||||
'draw_id' => $draw->id,
|
||||
'original_number' => '1234',
|
||||
'normalized_number' => '1234',
|
||||
'play_code' => 'big',
|
||||
'dimension' => 4,
|
||||
'digit_slot' => null,
|
||||
'bet_mode' => null,
|
||||
'unit_bet_amount' => 1000,
|
||||
'total_bet_amount' => 1000,
|
||||
'rebate_rate_snapshot' => 0,
|
||||
'commission_rate_snapshot' => 0,
|
||||
'actual_deduct_amount' => 1000,
|
||||
'odds_snapshot_json' => null,
|
||||
'rule_snapshot_json' => null,
|
||||
'combination_count' => 1,
|
||||
'estimated_max_payout' => 0,
|
||||
'risk_locked_amount' => 0,
|
||||
'status' => 'settled',
|
||||
'fail_reason_code' => null,
|
||||
'fail_reason_text' => null,
|
||||
'win_amount' => 0,
|
||||
'jackpot_win_amount' => 0,
|
||||
'settled_at' => now(),
|
||||
]);
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->getJson('/api/v1/admin/players/'.$player->id.'/ticket-items?per_page=10')
|
||||
->assertOk()
|
||||
->assertJsonPath('data.player_id', $player->id)
|
||||
->assertJsonPath('data.total', 1)
|
||||
->assertJsonPath('data.items.0.ticket_no', 'TKCSF0001')
|
||||
->assertJsonPath('data.items.0.draw_no', '20260520-001');
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->getJson('/api/v1/admin/players/'.$player->id.'/ticket-items?draw_no=20260520-001')
|
||||
->assertOk()
|
||||
->assertJsonPath('data.total', 1);
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->getJson('/api/v1/admin/players/'.$player->id.'/ticket-items?draw_no=20991231-999')
|
||||
->assertOk()
|
||||
->assertJsonPath('data.total', 0);
|
||||
});
|
||||
|
||||
test('admin draw finance summary aggregates bet and payout', function (): void {
|
||||
$token = mintCsFinanceAdminToken();
|
||||
|
||||
$player = Player::query()->create([
|
||||
'site_code' => 'main',
|
||||
'site_player_id' => 'csf-p2',
|
||||
'username' => 'csf_u2',
|
||||
'nickname' => null,
|
||||
'default_currency' => 'NPR',
|
||||
'status' => 0,
|
||||
]);
|
||||
|
||||
$draw = Draw::query()->create([
|
||||
'draw_no' => '20260520-002',
|
||||
'business_date' => '2026-05-20',
|
||||
'sequence_no' => 2,
|
||||
'status' => 'settled',
|
||||
'start_time' => now()->subDay(),
|
||||
'close_time' => now()->subDay(),
|
||||
'draw_time' => now()->subDay(),
|
||||
'cooling_end_time' => null,
|
||||
'result_source' => null,
|
||||
'current_result_version' => 1,
|
||||
'settle_version' => 1,
|
||||
'is_reopened' => false,
|
||||
]);
|
||||
|
||||
$order = TicketOrder::query()->create([
|
||||
'order_no' => 'ORD-CSF-2',
|
||||
'player_id' => $player->id,
|
||||
'draw_id' => $draw->id,
|
||||
'currency_code' => 'NPR',
|
||||
'total_bet_amount' => 5000,
|
||||
'total_rebate_amount' => 0,
|
||||
'total_actual_deduct' => 5000,
|
||||
'total_estimated_payout' => 0,
|
||||
'status' => 'settled',
|
||||
'submit_source' => 'h5',
|
||||
'client_trace_id' => null,
|
||||
]);
|
||||
|
||||
TicketItem::query()->create([
|
||||
'ticket_no' => 'TKCSF0002',
|
||||
'order_id' => $order->id,
|
||||
'player_id' => $player->id,
|
||||
'draw_id' => $draw->id,
|
||||
'original_number' => '5678',
|
||||
'normalized_number' => '5678',
|
||||
'play_code' => 'big',
|
||||
'dimension' => 4,
|
||||
'digit_slot' => null,
|
||||
'bet_mode' => null,
|
||||
'unit_bet_amount' => 5000,
|
||||
'total_bet_amount' => 5000,
|
||||
'rebate_rate_snapshot' => 0,
|
||||
'commission_rate_snapshot' => 0,
|
||||
'actual_deduct_amount' => 5000,
|
||||
'odds_snapshot_json' => null,
|
||||
'rule_snapshot_json' => null,
|
||||
'combination_count' => 1,
|
||||
'estimated_max_payout' => 0,
|
||||
'risk_locked_amount' => 0,
|
||||
'status' => 'settled',
|
||||
'fail_reason_code' => null,
|
||||
'fail_reason_text' => null,
|
||||
'win_amount' => 2000,
|
||||
'jackpot_win_amount' => 500,
|
||||
'settled_at' => now(),
|
||||
]);
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->getJson('/api/v1/admin/draws/'.$draw->id.'/finance-summary')
|
||||
->assertOk()
|
||||
->assertJsonPath('data.draw_no', '20260520-002')
|
||||
->assertJsonPath('data.total_bet_minor', 5000)
|
||||
->assertJsonPath('data.total_win_payout_minor', 2000)
|
||||
->assertJsonPath('data.total_jackpot_win_minor', 500)
|
||||
->assertJsonPath('data.total_payout_minor', 2500)
|
||||
->assertJsonPath('data.approx_house_gross_minor', 2500);
|
||||
});
|
||||
@@ -20,6 +20,7 @@ function mintAdminBearer(): string
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($admin);
|
||||
|
||||
return $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
}
|
||||
|
||||
115
tests/Feature/AdminPhase15OperationsTest.php
Normal file
115
tests/Feature/AdminPhase15OperationsTest.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
use App\Lottery\ErrorCode;
|
||||
use App\Models\AdminPermission;
|
||||
use App\Models\AdminRole;
|
||||
use App\Models\AdminUser;
|
||||
use App\Models\AuditLog;
|
||||
use App\Models\ReconcileJob;
|
||||
use App\Models\ReportJob;
|
||||
use App\Services\AuditLogger;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
function phase15SuperToken(): string
|
||||
{
|
||||
$admin = AdminUser::query()->create([
|
||||
'username' => 'phase15_super',
|
||||
'name' => 'Phase15',
|
||||
'email' => null,
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($admin);
|
||||
|
||||
return $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
}
|
||||
|
||||
test('report job create list show and audit log index work for super admin', function (): void {
|
||||
AuditLogger::record('system', 0, 'bootstrap', 'test', null, null, null, null);
|
||||
|
||||
$token = phase15SuperToken();
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->getJson('/api/v1/admin/audit-logs?per_page=5')
|
||||
->assertOk()
|
||||
->assertJsonPath('code', ErrorCode::Success->value);
|
||||
|
||||
$create = $this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->postJson('/api/v1/admin/report-jobs', [
|
||||
'report_type' => 'wallet_txns_daily',
|
||||
'export_format' => 'csv',
|
||||
'filter_json' => ['currency_code' => 'NPR'],
|
||||
]);
|
||||
$create->assertOk()->assertJsonPath('code', ErrorCode::Success->value);
|
||||
$id = (int) $create->json('data.id');
|
||||
expect($id)->toBeGreaterThan(0);
|
||||
expect(ReportJob::query()->whereKey($id)->exists())->toBeTrue();
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->getJson('/api/v1/admin/report-jobs/'.$id)
|
||||
->assertOk()
|
||||
->assertJsonPath('data.report_type', 'wallet_txns_daily');
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->getJson('/api/v1/admin/report-jobs?per_page=10')
|
||||
->assertOk()
|
||||
->assertJsonPath('code', ErrorCode::Success->value);
|
||||
|
||||
expect(AuditLog::query()->where('module_code', 'report_jobs')->exists())->toBeTrue();
|
||||
});
|
||||
|
||||
test('reconcile job create with items and nested items index', function (): void {
|
||||
$token = phase15SuperToken();
|
||||
|
||||
$resp = $this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->postJson('/api/v1/admin/reconcile-jobs', [
|
||||
'reconcile_type' => 'wallet_transfer',
|
||||
'period_start' => '2026-05-01T00:00:00Z',
|
||||
'period_end' => '2026-05-02T00:00:00Z',
|
||||
'items' => [
|
||||
['side_a_ref' => 'TO-1', 'side_b_ref' => 'MAIN-1', 'difference_amount' => 100, 'status' => 'mismatch'],
|
||||
['side_a_ref' => 'TO-2', 'side_b_ref' => 'MAIN-2', 'difference_amount' => 0, 'status' => 'matched'],
|
||||
],
|
||||
]);
|
||||
$resp->assertOk();
|
||||
$id = (int) $resp->json('data.id');
|
||||
expect($id)->toBeGreaterThan(0);
|
||||
|
||||
$job = ReconcileJob::query()->whereKey($id)->firstOrFail();
|
||||
expect((int) $job->admin_user_id)->toBeGreaterThan(0);
|
||||
expect($job->items()->count())->toBe(2);
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->getJson('/api/v1/admin/reconcile-jobs/'.$id.'/items')
|
||||
->assertOk()
|
||||
->assertJsonPath('data.meta.total', 2);
|
||||
});
|
||||
|
||||
test('admin without report permission receives 403 on report-jobs', function (): void {
|
||||
$role = AdminRole::query()->create(['slug' => 'auditor_test', 'name' => 'Auditor Test']);
|
||||
$perm = AdminPermission::query()->create(['slug' => 'prd.audit.finance', 'name' => '§8 审计日志·资金相关']);
|
||||
$role->permissions()->sync([(int) $perm->getKey()]);
|
||||
|
||||
$user = AdminUser::query()->create([
|
||||
'username' => 'auditor_only',
|
||||
'name' => 'Auditor',
|
||||
'email' => null,
|
||||
'password' => Hash::make('pw-audit'),
|
||||
'status' => 0,
|
||||
]);
|
||||
$user->roles()->sync([(int) $role->getKey()]);
|
||||
|
||||
$token = $user->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->getJson('/api/v1/admin/audit-logs')
|
||||
->assertOk();
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
->postJson('/api/v1/admin/report-jobs', ['report_type' => 'x'])
|
||||
->assertStatus(403)
|
||||
->assertJsonPath('code', ErrorCode::AdminForbidden->value);
|
||||
});
|
||||
@@ -18,6 +18,7 @@ function mintRiskAdminToken(): string
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($admin);
|
||||
|
||||
return $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ function mintSettlementAdminToken(): string
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($admin);
|
||||
|
||||
return $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ function makeAdminToken(): string
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($admin);
|
||||
|
||||
return $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
}
|
||||
|
||||
@@ -150,6 +150,7 @@ test('draw tick rng awaits manual publish when review enabled', function (): voi
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($admin);
|
||||
$token = $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
|
||||
$this->withHeader('Authorization', 'Bearer '.$token)
|
||||
|
||||
@@ -43,6 +43,7 @@ function acceptanceMintAdminToken(): string
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($admin);
|
||||
|
||||
return $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
}
|
||||
|
||||
@@ -27,6 +27,7 @@ function mintConfigAdminToken(): string
|
||||
'password' => Hash::make('secret-strong'),
|
||||
'status' => 0,
|
||||
]);
|
||||
grantSuperAdminRole($admin);
|
||||
|
||||
return $admin->createToken('test', ['*'], now()->addDay())->plainTextToken;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
<?php
|
||||
|
||||
use App\Models\AdminUser;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Tests\TestCase;
|
||||
|
||||
/*
|
||||
@@ -44,7 +46,19 @@ expect()->extend('toBeOne', function () {
|
||||
|
|
||||
*/
|
||||
|
||||
function something()
|
||||
/** 为后台测试账号挂上 `super_admin` 角色(细粒度权限校验全放行)。 */
|
||||
function grantSuperAdminRole(AdminUser $admin): void
|
||||
{
|
||||
// ..
|
||||
$now = now();
|
||||
DB::table('admin_roles')->updateOrInsert(
|
||||
['slug' => AdminUser::ROLE_SUPER_ADMIN],
|
||||
['name' => 'Super Admin', 'created_at' => $now, 'updated_at' => $now],
|
||||
);
|
||||
$rid = (int) DB::table('admin_roles')->where('slug', AdminUser::ROLE_SUPER_ADMIN)->value('id');
|
||||
if (! DB::table('admin_user_roles')->where('admin_user_id', $admin->id)->where('role_id', $rid)->exists()) {
|
||||
DB::table('admin_user_roles')->insert([
|
||||
'admin_user_id' => $admin->id,
|
||||
'role_id' => $rid,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user