- 在 SyncAdminAuthorizationCommand 中新增对代理线路和结算菜单操作的同步功能,确保缺失的菜单操作行能够被创建。 - 更新多个控制器中的权限检查逻辑,使用 hasPermissionCode 替代原有的权限验证方式,提升权限管理的灵活性。 - 在 AdminPlayerStoreController 中引入对玩家创建能力的验证,确保只有具备相应权限的管理员能够创建玩家。 - 更新请求验证逻辑,新增 credit_limit、rebate_rate 和 extra_rebate_rate 字段,以支持更细粒度的玩家管理。 - 在 AdminUser 和 AgentNode 模型中增强角色与用户的权限管理功能,支持更细粒度的权限控制。
168 lines
4.2 KiB
PHP
168 lines
4.2 KiB
PHP
<?php
|
||
|
||
namespace App\Support;
|
||
|
||
use App\Models\AdminUser;
|
||
use App\Models\AgentNode;
|
||
use App\Models\Player;
|
||
use Illuminate\Database\Eloquent\Builder;
|
||
|
||
/**
|
||
* 代理子树数据范围(P1:节点访问;P2 起叠加玩家 agent_node_id)。
|
||
*/
|
||
final class AdminAgentScope
|
||
{
|
||
public static function primaryAgentNode(AdminUser $admin): ?AgentNode
|
||
{
|
||
if ($admin->isSuperAdmin()) {
|
||
return null;
|
||
}
|
||
|
||
$agentId = $admin->primaryAgentNodeId();
|
||
if ($agentId === null) {
|
||
return null;
|
||
}
|
||
|
||
return AgentNode::query()->find($agentId);
|
||
}
|
||
|
||
public static function nodeVisibleTo(AdminUser $admin, AgentNode $node): bool
|
||
{
|
||
if ($admin->isSuperAdmin()) {
|
||
return true;
|
||
}
|
||
|
||
$actor = self::primaryAgentNode($admin);
|
||
if ($actor === null) {
|
||
return false;
|
||
}
|
||
|
||
return $node->isSameOrDescendantOf($actor);
|
||
}
|
||
|
||
public static function playerAccessible(AdminUser $admin, Player $player): bool
|
||
{
|
||
if ($admin->isSuperAdmin()) {
|
||
return true;
|
||
}
|
||
|
||
$actor = self::primaryAgentNode($admin);
|
||
if ($actor === null) {
|
||
return true;
|
||
}
|
||
|
||
if ($player->agent_node_id === null) {
|
||
return false;
|
||
}
|
||
|
||
$playerAgent = AgentNode::query()->find((int) $player->agent_node_id);
|
||
if ($playerAgent === null) {
|
||
return false;
|
||
}
|
||
|
||
return $playerAgent->isSameOrDescendantOf($actor);
|
||
}
|
||
|
||
public static function nodeManageableBy(AdminUser $admin, AgentNode $node): bool
|
||
{
|
||
if ($admin->isSuperAdmin()) {
|
||
return true;
|
||
}
|
||
|
||
if (! $admin->hasPermissionCode('agent.node.manage')) {
|
||
return false;
|
||
}
|
||
|
||
return self::nodeVisibleTo($admin, $node);
|
||
}
|
||
|
||
/**
|
||
* @return Builder<AgentNode>
|
||
*/
|
||
public static function visibleNodesQuery(AdminUser $admin, int $adminSiteId): Builder
|
||
{
|
||
$query = AgentNode::query()
|
||
->where('admin_site_id', $adminSiteId)
|
||
->orderBy('path');
|
||
|
||
if ($admin->isSuperAdmin()) {
|
||
return $query;
|
||
}
|
||
|
||
$actor = self::primaryAgentNode($admin);
|
||
if ($actor === null || (int) $actor->admin_site_id !== $adminSiteId) {
|
||
return $query->whereRaw('0 = 1');
|
||
}
|
||
|
||
return $query->where('path', 'like', $actor->path.'%');
|
||
}
|
||
|
||
/**
|
||
* 玩家必须落在当前代理子树(agent_node_id 必填,由迁移回填根代理)。
|
||
*
|
||
* @param Builder<Player> $query
|
||
*/
|
||
public static function applyToPlayerQuery(Builder $query, AdminUser $admin): void
|
||
{
|
||
if ($admin->isSuperAdmin()) {
|
||
return;
|
||
}
|
||
|
||
$actor = self::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('agent_node_id', $subtreeIds);
|
||
}
|
||
|
||
/**
|
||
* 在已有站点/代理范围上,再按指定节点子树收窄(超管筛选用)。
|
||
*
|
||
* @param Builder<Player> $query
|
||
*/
|
||
public static function applyRequestedAgentNodeFilter(Builder $query, AdminUser $admin, int $agentNodeId): void
|
||
{
|
||
$node = AgentNode::query()->find($agentNodeId);
|
||
if ($node === null || ! self::nodeVisibleTo($admin, $node)) {
|
||
$query->whereRaw('0 = 1');
|
||
|
||
return;
|
||
}
|
||
|
||
if (! \Illuminate\Support\Facades\Schema::hasColumn('players', 'agent_node_id')) {
|
||
return;
|
||
}
|
||
|
||
$subtreeIds = AgentNode::query()
|
||
->where('path', 'like', $node->path.'%')
|
||
->pluck('id')
|
||
->all();
|
||
|
||
if ($subtreeIds === []) {
|
||
$query->whereRaw('0 = 1');
|
||
|
||
return;
|
||
}
|
||
|
||
$query->whereIn('agent_node_id', $subtreeIds);
|
||
}
|
||
}
|