refactor: 更新权限管理与请求验证逻辑
- 在多个控制器中将权限检查从 hasAdminPermission 更新为 hasPermissionCode,以增强权限管理的灵活性。 - 引入 AdminScopePolicy,优化基于代理节点的权限和数据过滤逻辑,确保管理员能够更精确地控制访问权限。 - 在请求验证中添加 agent_node_id 字段,确保 API 接口支持代理节点的相关操作。 - 更新 AdminUser 模型,新增 hasPermissionCode 方法,以支持更细粒度的权限检查。 - 优化审计日志记录逻辑,确保在处理请求时能够准确记录管理员的操作。
This commit is contained in:
@@ -14,6 +14,10 @@ use App\Models\DrawResultBatch;
|
||||
use App\Lottery\DrawResultBatchStatus;
|
||||
use App\Services\Draw\DrawHallSnapshotBuilder;
|
||||
use App\Support\AdminDataScope;
|
||||
use App\Support\AdminScopeContext;
|
||||
use App\Support\AdminAgentScope;
|
||||
use App\Support\AdminScopeContextResolver;
|
||||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
||||
|
||||
/**
|
||||
* 后台首页仪表盘:聚合大厅快照、当期财务、期号面板、风控摘要、异常转账计数。
|
||||
@@ -28,8 +32,9 @@ final class AdminDashboardSnapshotBuilder
|
||||
) {}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public function build(AdminUser $admin): array
|
||||
public function build(AdminScopeContext $scope): array
|
||||
{
|
||||
$admin = $scope->admin;
|
||||
$hall = $this->hallSnapshot->build();
|
||||
$canDraw = $this->canDrawFinanceAndRisk($admin);
|
||||
$canWallet = $this->canWalletReconcile($admin);
|
||||
@@ -53,11 +58,11 @@ final class AdminDashboardSnapshotBuilder
|
||||
];
|
||||
|
||||
if ($canDraw) {
|
||||
$this->fillPlatformOverview($out, $admin);
|
||||
$this->fillPlatformOverview($out, $scope);
|
||||
}
|
||||
|
||||
if ($canWallet) {
|
||||
$out['abnormal_transfer_total'] = $this->abnormalTransferTotal($admin);
|
||||
$out['abnormal_transfer_total'] = $this->abnormalTransferTotal($scope);
|
||||
}
|
||||
|
||||
if ($hall === null) {
|
||||
@@ -82,7 +87,7 @@ final class AdminDashboardSnapshotBuilder
|
||||
];
|
||||
|
||||
if ($canDraw) {
|
||||
$out['finance'] = $this->financeSummary($draw, $admin);
|
||||
$out['finance'] = $this->financeSummary($draw, $scope);
|
||||
$out['draw'] = $this->drawPanel($draw);
|
||||
$out['risk'] = $this->riskPanel($draw);
|
||||
}
|
||||
@@ -91,35 +96,48 @@ final class AdminDashboardSnapshotBuilder
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $out */
|
||||
private function fillPlatformOverview(array &$out, AdminUser $admin): void
|
||||
private function fillPlatformOverview(array &$out, AdminScopeContext $scope): void
|
||||
{
|
||||
$out['today_finance'] = $this->todayFinanceSummary($admin);
|
||||
$out['lifetime_finance'] = $this->reportQuery->platformLifetimeTotals($admin);
|
||||
$out['platform_risk'] = $this->platformRiskSummary();
|
||||
$out['result_batch_queue'] = $this->resultBatchQueue();
|
||||
$admin = $scope->admin;
|
||||
$out['today_finance'] = $this->todayFinanceSummary($scope);
|
||||
$out['lifetime_finance'] = $this->reportQuery->platformLifetimeTotals(
|
||||
$scope,
|
||||
);
|
||||
|
||||
if ($admin->isSuperAdmin()
|
||||
&& $scope->effectiveRequestedSiteCode() === null
|
||||
&& $scope->effectiveRequestedAgentNodeId() === null
|
||||
) {
|
||||
$out['platform_risk'] = $this->platformRiskSummary();
|
||||
$out['result_batch_queue'] = $this->resultBatchQueue();
|
||||
}
|
||||
}
|
||||
|
||||
private function canDrawFinanceAndRisk(AdminUser $admin): bool
|
||||
{
|
||||
return $admin->hasAdminPermission('prd.dashboard.view')
|
||||
|| $admin->hasAdminPermission('prd.draw_result.manage')
|
||||
|| $admin->hasAdminPermission('prd.draw_result.view')
|
||||
|| $admin->hasAdminPermission('prd.risk.view')
|
||||
|| $admin->hasAdminPermission('prd.risk.manage');
|
||||
return $admin->hasPermissionCode('dashboard.view')
|
||||
|| $admin->hasPermissionCode('draw.results.view')
|
||||
|| $admin->hasPermissionCode('draw.review.review')
|
||||
|| $admin->hasPermissionCode('draw.review.publish')
|
||||
|| $admin->hasPermissionCode('risk.monitor.view')
|
||||
|| $admin->hasPermissionCode('risk.monitor.manage');
|
||||
}
|
||||
|
||||
private function canWalletReconcile(AdminUser $admin): bool
|
||||
{
|
||||
return $admin->hasAdminPermission('prd.wallet_reconcile.manage')
|
||||
|| $admin->hasAdminPermission('prd.wallet_reconcile.view')
|
||||
|| $admin->hasAdminPermission('prd.wallet_reconcile.view_cs');
|
||||
return $admin->hasPermissionCode('service.reconcile.manage')
|
||||
|| $admin->hasPermissionCode('service.reconcile.view')
|
||||
|| $admin->hasPermissionCode('service.wallet.view')
|
||||
|| $admin->hasPermissionCode('service.wallet.manage');
|
||||
}
|
||||
|
||||
private function abnormalTransferTotal(AdminUser $admin): int
|
||||
private function abnormalTransferTotal(AdminScopeContext $scope): int
|
||||
{
|
||||
$admin = $scope->admin;
|
||||
$query = TransferOrder::query()
|
||||
->whereIn('status', ['processing', 'failed', 'pending_reconcile']);
|
||||
AdminDataScope::applyEloquentViaPlayer($query, $admin);
|
||||
$this->applyRequestedScopeViaPlayer($query, $scope);
|
||||
|
||||
return (int) $query->count();
|
||||
}
|
||||
@@ -129,10 +147,15 @@ final class AdminDashboardSnapshotBuilder
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function todayFinanceSummary(AdminUser $admin): array
|
||||
private function todayFinanceSummary(AdminScopeContext $scope): array
|
||||
{
|
||||
$admin = $scope->admin;
|
||||
$today = now()->toDateString();
|
||||
$rows = $this->reportQuery->dailyProfitRows($today, $today, $admin);
|
||||
$rows = $this->reportQuery->dailyProfitRows(
|
||||
$today,
|
||||
$today,
|
||||
$scope,
|
||||
);
|
||||
$row = $rows[0] ?? [
|
||||
'business_date' => $today,
|
||||
'total_bet_minor' => 0,
|
||||
@@ -140,10 +163,12 @@ final class AdminDashboardSnapshotBuilder
|
||||
'approx_house_gross_minor' => 0,
|
||||
];
|
||||
|
||||
$currencyCode = (string) (TicketOrder::query()
|
||||
$currencyQuery = TicketOrder::query()
|
||||
->join('draws', 'draws.id', '=', 'ticket_orders.draw_id')
|
||||
->where('draws.business_date', $today)
|
||||
->value('ticket_orders.currency_code') ?? '');
|
||||
->where('draws.business_date', $today);
|
||||
AdminDataScope::applyEloquentViaPlayer($currencyQuery, $admin);
|
||||
$this->applyRequestedScopeViaPlayer($currencyQuery, $scope);
|
||||
$currencyCode = (string) ($currencyQuery->value('ticket_orders.currency_code') ?? '');
|
||||
|
||||
return [
|
||||
'business_date' => (string) $row['business_date'],
|
||||
@@ -155,14 +180,17 @@ final class AdminDashboardSnapshotBuilder
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private function financeSummary(Draw $draw, AdminUser $admin): array
|
||||
private function financeSummary(Draw $draw, AdminScopeContext $scope): array
|
||||
{
|
||||
$admin = $scope->admin;
|
||||
$drawId = (int) $draw->id;
|
||||
|
||||
$orderQuery = TicketOrder::query()->where('draw_id', $drawId);
|
||||
$itemQuery = TicketItem::query()->where('draw_id', $drawId);
|
||||
AdminDataScope::applyEloquentViaPlayer($orderQuery, $admin);
|
||||
AdminDataScope::applyEloquentViaPlayer($itemQuery, $admin);
|
||||
$this->applyRequestedScopeViaPlayer($orderQuery, $scope);
|
||||
$this->applyRequestedScopeViaPlayer($itemQuery, $scope);
|
||||
|
||||
$totalBetMinor = (int) $orderQuery->sum('total_actual_deduct');
|
||||
$orderCount = (int) $orderQuery->count();
|
||||
@@ -387,4 +415,29 @@ final class AdminDashboardSnapshotBuilder
|
||||
|
||||
return 'other';
|
||||
}
|
||||
|
||||
/**
|
||||
* 叠加全局 scope 参数(site_code / agent_node_id)到 player 关联模型查询。
|
||||
*
|
||||
* @param EloquentBuilder<mixed> $query
|
||||
*/
|
||||
private function applyRequestedScopeViaPlayer(EloquentBuilder $query, AdminScopeContext $scope): void
|
||||
{
|
||||
$siteCode = $scope->effectiveRequestedSiteCode();
|
||||
$agentNodeId = $scope->effectiveRequestedAgentNodeId();
|
||||
if ($siteCode === null && $agentNodeId === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$admin = $scope->admin;
|
||||
$query->whereHas('player', static function (EloquentBuilder $playerQuery) use ($admin, $siteCode, $agentNodeId): void {
|
||||
if ($siteCode !== null) {
|
||||
$playerQuery->where('site_code', $siteCode);
|
||||
}
|
||||
|
||||
if ($agentNodeId !== null) {
|
||||
AdminAgentScope::applyRequestedAgentNodeFilter($playerQuery, $admin, $agentNodeId);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user