feat: 增强管理员功能与数据处理
- 在多个控制器中引入 agent_node_id,以支持基于代理节点的权限和数据过滤。 - 更新 AdminRole 和 AdminUser 模型,新增角色范围和代理节点相关功能,提升角色管理的灵活性。 - 在请求验证中添加 agent_node_id 字段,确保 API 接口支持代理节点的相关操作。 - 优化 LotterySettings 服务,支持批量写入设置,提升配置管理的效率。 - 更新仪表板和报告服务,增强数据统计功能,确保管理员能够获取更全面的统计信息。
This commit is contained in:
137
app/Support/AdminDataScope.php
Normal file
137
app/Support/AdminDataScope.php
Normal file
@@ -0,0 +1,137 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Models\AdminUser;
|
||||
use App\Models\AgentNode;
|
||||
use Illuminate\Database\Query\Builder;
|
||||
|
||||
/**
|
||||
* 站点 + 代理子树:用于报表等 Query Builder(players 表别名)。
|
||||
*/
|
||||
final class AdminDataScope
|
||||
{
|
||||
/**
|
||||
* @param Builder<mixed> $query 已 join `players as {alias}`
|
||||
*/
|
||||
public static function applyToPlayersAlias(
|
||||
Builder $query,
|
||||
AdminUser $admin,
|
||||
string $alias = 'p',
|
||||
?int $requestedAgentNodeId = null,
|
||||
): void {
|
||||
if ($admin->isSuperAdmin()) {
|
||||
if ($requestedAgentNodeId !== null && $requestedAgentNodeId > 0) {
|
||||
self::applyAgentNodeIdOnAlias($query, $admin, $alias, $requestedAgentNodeId);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$codes = AdminSiteScope::accessibleSiteCodes($admin);
|
||||
if ($codes !== null) {
|
||||
if ($codes === []) {
|
||||
$query->whereRaw('0 = 1');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$query->whereIn($alias.'.site_code', $codes);
|
||||
}
|
||||
|
||||
$actor = AdminAgentScope::primaryAgentNode($admin);
|
||||
if ($actor === null) {
|
||||
$query->whereRaw('0 = 1');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (! \Illuminate\Support\Facades\Schema::hasColumn('players', 'agent_node_id')) {
|
||||
return;
|
||||
}
|
||||
|
||||
$subtreeIds = AgentNode::query()
|
||||
->where('path', 'like', $actor->path.'%')
|
||||
->pluck('id')
|
||||
->all();
|
||||
|
||||
if ($subtreeIds === []) {
|
||||
$query->whereRaw('0 = 1');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$query->whereIn($alias.'.agent_node_id', $subtreeIds);
|
||||
|
||||
if ($requestedAgentNodeId !== null && $requestedAgentNodeId > 0) {
|
||||
self::applyAgentNodeIdOnAlias($query, $admin, $alias, $requestedAgentNodeId);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<mixed> $query
|
||||
*/
|
||||
private static function applyAgentNodeIdOnAlias(
|
||||
Builder $query,
|
||||
AdminUser $admin,
|
||||
string $alias,
|
||||
int $agentNodeId,
|
||||
): void {
|
||||
$node = AgentNode::query()->find($agentNodeId);
|
||||
if ($node === null || ! AdminAgentScope::nodeVisibleTo($admin, $node)) {
|
||||
$query->whereRaw('0 = 1');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$subtreeIds = AgentNode::query()
|
||||
->where('path', 'like', $node->path.'%')
|
||||
->pluck('id')
|
||||
->all();
|
||||
|
||||
if ($subtreeIds === []) {
|
||||
$query->whereRaw('0 = 1');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$query->whereIn($alias.'.agent_node_id', $subtreeIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* Eloquent 模型经 player 关联做站点 + 代理子树过滤。
|
||||
*
|
||||
* @param \Illuminate\Database\Eloquent\Builder<mixed> $query
|
||||
*/
|
||||
public static function applyEloquentViaPlayer(
|
||||
\Illuminate\Database\Eloquent\Builder $query,
|
||||
AdminUser $admin,
|
||||
string $relation = 'player',
|
||||
): void {
|
||||
if ($admin->isSuperAdmin()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$query->whereHas($relation, static function (Builder $playerQuery) use ($admin): void {
|
||||
AdminSiteScope::applyToPlayerQuery($playerQuery, $admin);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 约束 ticket_orders(别名 o)仅统计可见玩家。
|
||||
*
|
||||
* @param Builder<mixed> $query
|
||||
*/
|
||||
public static function applyToTicketOrdersViaPlayer(Builder $query, ?AdminUser $admin, string $orderAlias = 'o'): void
|
||||
{
|
||||
if ($admin === null || $admin->isSuperAdmin()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$query->whereExists(function (Builder $sub) use ($admin, $orderAlias): void {
|
||||
$sub->from('players as scope_p')
|
||||
->whereColumn('scope_p.id', $orderAlias.'.player_id');
|
||||
self::applyToPlayersAlias($sub, $admin, 'scope_p');
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user