*/ public static function baseSlugs(): array { return self::BASE_SLUGS; } /** * @return list */ public static function ownerSlugsForNode(AgentNode $node, ?AgentProfile $profile = null): array { if ($node->isRoot()) { return self::lineRootOwnerSlugs(); } $profile ??= AgentProfile::query()->where('agent_node_id', $node->id)->first(); if ($profile === null) { return self::defaultOwnerSlugsWithoutProfile(); } return self::ownerSlugsFromProfile($profile); } /** * @return list */ public static function lineRootOwnerSlugs(): array { return array_values(array_unique(array_merge( self::BASE_SLUGS, self::LINE_ROOT_EXTRA_SLUGS, ))); } /** * @return list */ public static function ownerSlugsFromProfile(AgentProfile $profile): array { $slugs = self::BASE_SLUGS; if ($profile->can_create_child_agent) { $slugs = array_merge($slugs, self::CHILD_AGENT_MANAGE_SLUGS); } if ($profile->can_create_player) { $slugs = array_merge($slugs, self::PLAYER_MANAGE_SLUGS); } return array_values(array_unique($slugs)); } /** * @return list */ public static function defaultOwnerSlugsWithoutProfile(): array { return array_values(array_unique(array_merge( self::BASE_SLUGS, self::PLAYER_MANAGE_SLUGS, ))); } /** * @param array $createPayload * @return list */ public static function ownerSlugsForNewChild(array $createPayload): array { $slugs = self::BASE_SLUGS; if ((bool) ($createPayload['can_create_child_agent'] ?? false)) { $slugs = array_merge($slugs, self::CHILD_AGENT_MANAGE_SLUGS); } if ((bool) ($createPayload['can_create_player'] ?? true)) { $slugs = array_merge($slugs, self::PLAYER_MANAGE_SLUGS); } return array_values(array_unique($slugs)); } /** * 平台「代理」系统角色模板(出现在「平台角色管理」列表,供手动分配或作站点 pivot 回退)。 * * @return list */ public static function platformAgentRoleTemplateSlugs(): array { return self::defaultOwnerSlugsWithoutProfile(); } /** 确保存在 slug=agent 的平台系统角色,并同步模板权限。 */ public static function ensurePlatformAgentRole(): AdminRole { $role = AdminRole::query()->updateOrCreate( [ 'slug' => 'agent', 'scope_type' => AdminRole::SCOPE_SYSTEM, ], [ 'code' => 'agent', 'name' => '代理', 'description' => '经营代理默认权限模板(与线路内 agent_owner 默认包一致)', 'status' => 1, 'is_system' => true, 'sort_order' => 50, 'owner_agent_id' => null, 'delegated_from_role_id' => null, ], ); $role->syncLegacyPermissionSlugs(self::platformAgentRoleTemplateSlugs()); return $role->fresh() ?? $role; } }