feat: 增强管理员功能与数据处理
- 在多个控制器中引入 agent_node_id,以支持基于代理节点的权限和数据过滤。 - 更新 AdminRole 和 AdminUser 模型,新增角色范围和代理节点相关功能,提升角色管理的灵活性。 - 在请求验证中添加 agent_node_id 字段,确保 API 接口支持代理节点的相关操作。 - 优化 LotterySettings 服务,支持批量写入设置,提升配置管理的效率。 - 更新仪表板和报告服务,增强数据统计功能,确保管理员能够获取更全面的统计信息。
This commit is contained in:
@@ -11,6 +11,10 @@ final class AdminRole extends Model
|
||||
{
|
||||
public const ROLE_SUPER_ADMIN = 'super_admin';
|
||||
|
||||
public const SCOPE_SYSTEM = 'system';
|
||||
|
||||
public const SCOPE_AGENT = 'agent';
|
||||
|
||||
protected $table = 'admin_roles';
|
||||
|
||||
protected static function booted(): void
|
||||
@@ -30,8 +34,32 @@ final class AdminRole extends Model
|
||||
'status',
|
||||
'is_system',
|
||||
'sort_order',
|
||||
'owner_agent_id',
|
||||
'delegated_from_role_id',
|
||||
'scope_type',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'owner_agent_id' => 'integer',
|
||||
'delegated_from_role_id' => 'integer',
|
||||
'status' => 'integer',
|
||||
'is_system' => 'boolean',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
public function isAgentScoped(): bool
|
||||
{
|
||||
return $this->scope_type === self::SCOPE_AGENT && $this->owner_agent_id !== null;
|
||||
}
|
||||
|
||||
public function isReadOnlyTemplate(): bool
|
||||
{
|
||||
return $this->delegated_from_role_id !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsToMany<AdminMenuAction, AdminRole>
|
||||
*/
|
||||
@@ -103,6 +131,15 @@ final class AdminRole extends Model
|
||||
|
||||
public function assignedUserCount(): int
|
||||
{
|
||||
$agentCount = (int) DB::table('admin_user_agent_roles')
|
||||
->where('role_id', $this->id)
|
||||
->distinct()
|
||||
->count('admin_user_id');
|
||||
|
||||
if ($this->isAgentScoped()) {
|
||||
return $agentCount;
|
||||
}
|
||||
|
||||
return (int) DB::table('admin_user_site_roles')
|
||||
->where('role_id', $this->id)
|
||||
->distinct()
|
||||
|
||||
@@ -78,6 +78,78 @@ final class AdminUser extends Authenticatable
|
||||
*
|
||||
* @param list<string> $slugs
|
||||
*/
|
||||
/**
|
||||
* @param list<int> $roleIds
|
||||
*/
|
||||
public function syncAgentRoleIds(int $agentNodeId, array $roleIds): void
|
||||
{
|
||||
$roleIds = array_values(array_unique(array_map(static fn ($id): int => (int) $id, $roleIds)));
|
||||
|
||||
DB::transaction(function () use ($agentNodeId, $roleIds): void {
|
||||
DB::table('admin_user_agent_roles')
|
||||
->where('admin_user_id', $this->id)
|
||||
->where('agent_node_id', $agentNodeId)
|
||||
->delete();
|
||||
|
||||
$now = now();
|
||||
foreach ($roleIds as $roleId) {
|
||||
DB::table('admin_user_agent_roles')->insert([
|
||||
'admin_user_id' => $this->id,
|
||||
'agent_node_id' => $agentNodeId,
|
||||
'role_id' => $roleId,
|
||||
'granted_at' => $now,
|
||||
]);
|
||||
}
|
||||
|
||||
$siteId = (int) (AgentNode::query()->where('id', $agentNodeId)->value('admin_site_id') ?? 0);
|
||||
if ($siteId > 0) {
|
||||
DB::table('admin_user_site_roles')
|
||||
->where('admin_user_id', $this->id)
|
||||
->where('site_id', $siteId)
|
||||
->delete();
|
||||
|
||||
foreach ($roleIds as $roleId) {
|
||||
DB::table('admin_user_site_roles')->insert([
|
||||
'admin_user_id' => $this->id,
|
||||
'site_id' => $siteId,
|
||||
'role_id' => $roleId,
|
||||
'granted_at' => $now,
|
||||
]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function roleMenuActionPermissionCodes(): array
|
||||
{
|
||||
$agentId = $this->primaryAgentNodeId();
|
||||
if ($agentId !== null) {
|
||||
$fromAgent = DB::table('admin_user_agent_roles as uar')
|
||||
->join('admin_role_menu_actions as rma', 'rma.role_id', '=', 'uar.role_id')
|
||||
->join('admin_menu_actions as ma', 'ma.id', '=', 'rma.menu_action_id')
|
||||
->where('uar.admin_user_id', $this->id)
|
||||
->where('uar.agent_node_id', $agentId)
|
||||
->where('ma.status', 1)
|
||||
->pluck('ma.permission_code')
|
||||
->all();
|
||||
|
||||
if ($fromAgent !== []) {
|
||||
return $fromAgent;
|
||||
}
|
||||
}
|
||||
|
||||
return DB::table('admin_user_site_roles as usr')
|
||||
->join('admin_role_menu_actions as rma', 'rma.role_id', '=', 'usr.role_id')
|
||||
->join('admin_menu_actions as ma', 'ma.id', '=', 'rma.menu_action_id')
|
||||
->where('usr.admin_user_id', $this->id)
|
||||
->where('ma.status', 1)
|
||||
->pluck('ma.permission_code')
|
||||
->all();
|
||||
}
|
||||
|
||||
public function syncRoleSlugsForDefaultSite(array $slugs): void
|
||||
{
|
||||
$siteId = self::defaultAdminSiteId();
|
||||
@@ -114,6 +186,25 @@ final class AdminUser extends Authenticatable
|
||||
return $this->roles()->where('admin_roles.slug', self::ROLE_SUPER_ADMIN)->exists();
|
||||
}
|
||||
|
||||
public function primaryAgentNodeId(): ?int
|
||||
{
|
||||
$id = DB::table('admin_user_agents')
|
||||
->where('admin_user_id', $this->id)
|
||||
->value('agent_node_id');
|
||||
|
||||
return $id !== null ? (int) $id : null;
|
||||
}
|
||||
|
||||
public function primaryAgentNode(): ?AgentNode
|
||||
{
|
||||
$id = $this->primaryAgentNodeId();
|
||||
if ($id === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return AgentNode::query()->find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 可访问的 admin_sites.id 列表;`null` 表示不限制(超管)。
|
||||
*
|
||||
@@ -125,6 +216,11 @@ final class AdminUser extends Authenticatable
|
||||
return null;
|
||||
}
|
||||
|
||||
$agent = $this->primaryAgentNode();
|
||||
if ($agent !== null) {
|
||||
return [(int) $agent->admin_site_id];
|
||||
}
|
||||
|
||||
$ids = DB::table('admin_user_site_roles')
|
||||
->where('admin_user_id', $this->id)
|
||||
->distinct()
|
||||
@@ -193,13 +289,7 @@ final class AdminUser extends Authenticatable
|
||||
return array_keys($out);
|
||||
}
|
||||
|
||||
$fromRoles = DB::table('admin_user_site_roles as usr')
|
||||
->join('admin_role_menu_actions as rma', 'rma.role_id', '=', 'usr.role_id')
|
||||
->join('admin_menu_actions as ma', 'ma.id', '=', 'rma.menu_action_id')
|
||||
->where('usr.admin_user_id', $this->id)
|
||||
->where('ma.status', 1)
|
||||
->pluck('ma.permission_code')
|
||||
->all();
|
||||
$fromRoles = $this->roleMenuActionPermissionCodes();
|
||||
|
||||
$merged = [];
|
||||
foreach (array_merge($fromRoles, $this->directMenuActionPermissionCodes()) as $c) {
|
||||
|
||||
82
app/Models/AgentNode.php
Normal file
82
app/Models/AgentNode.php
Normal file
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
final class AgentNode extends Model
|
||||
{
|
||||
protected $table = 'agent_nodes';
|
||||
|
||||
protected $fillable = [
|
||||
'admin_site_id',
|
||||
'parent_id',
|
||||
'path',
|
||||
'depth',
|
||||
'code',
|
||||
'name',
|
||||
'status',
|
||||
'created_by',
|
||||
'extra_json',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'admin_site_id' => 'integer',
|
||||
'parent_id' => 'integer',
|
||||
'depth' => 'integer',
|
||||
'status' => 'integer',
|
||||
'extra_json' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
return (int) $this->status === 1;
|
||||
}
|
||||
|
||||
public function isRoot(): bool
|
||||
{
|
||||
return $this->parent_id === null || (int) $this->depth === 0;
|
||||
}
|
||||
|
||||
/** @return BelongsTo<AdminSite, AgentNode> */
|
||||
public function adminSite(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(AdminSite::class, 'admin_site_id');
|
||||
}
|
||||
|
||||
/** @return BelongsTo<AgentNode, AgentNode> */
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(self::class, 'parent_id');
|
||||
}
|
||||
|
||||
/** @return HasMany<AgentNode, AgentNode> */
|
||||
public function children(): HasMany
|
||||
{
|
||||
return $this->hasMany(self::class, 'parent_id')->orderBy('code');
|
||||
}
|
||||
|
||||
public function isDescendantOf(self $ancestor): bool
|
||||
{
|
||||
if ((int) $this->admin_site_id !== (int) $ancestor->admin_site_id) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$ancestorPath = (string) $ancestor->path;
|
||||
if ($ancestorPath === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
return str_starts_with((string) $this->path, $ancestorPath);
|
||||
}
|
||||
|
||||
public function isSameOrDescendantOf(self $ancestor): bool
|
||||
{
|
||||
return (int) $this->id === (int) $ancestor->id || $this->isDescendantOf($ancestor);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
/**
|
||||
@@ -12,6 +13,7 @@ final class Player extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'site_code',
|
||||
'agent_node_id',
|
||||
'site_player_id',
|
||||
'username',
|
||||
'nickname',
|
||||
@@ -23,6 +25,7 @@ final class Player extends Model
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'agent_node_id' => 'integer',
|
||||
'last_login_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
@@ -31,4 +34,9 @@ final class Player extends Model
|
||||
{
|
||||
return $this->hasMany(PlayerWallet::class);
|
||||
}
|
||||
|
||||
public function agentNode(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(AgentNode::class, 'agent_node_id');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user