using System.Security.Cryptography; using System.Text; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using XFEWarframe.Core.Data; namespace XFEWarframe.Core.Services; public static class EditorPermissions { private static readonly IReadOnlyDictionary Operations = new Dictionary { ["archive.export"] = "导出自己的完整存档", ["archive.import"] = "导入完整存档(含进度与规则,另需开放货币及物品权限)", ["rename"] = "修改装备与宠物名称", ["experience"] = "修改装备经验", ["forma"] = "修改极化次数", ["features"] = "修改装备特性", ["mature"] = "修改宠物成长阶段", ["remove"] = "删除库存物品", ["mod.rank"] = "修改 MOD 等级", ["quest"] = "修改系列任务进度", ["booster"] = "修改加成期限", ["evolution"] = "修改灵化进度", ["parts"] = "修改模块化部件", ["valence"] = "修改元素加成", ["shards"] = "编辑源力石词条", ["umbra"] = "编辑 Umbra 回响", ["invigoration"] = "编辑活力注入", ["ability"] = "编辑移植技能", ["exalted"] = "建立已移植技能缺失的显赫武器配装(MOD 编辑另需配装权限)", ["loadout"] = "编辑战甲与武器 MOD 配装(安装、交换、卸下与赋能)", ["riven"] = "编辑与计算紫卡", ["legacy"] = "编辑旧版 MOD", ["create.legacy"] = "创建旧版 MOD(还需添加物品权限)", ["batch"] = "批量执行已授权操作", ["create.modular"] = "组装模块化装备(还需添加物品权限)", ["create.booster"] = "添加加成(还需添加物品权限)", ["create.quest"] = "添加缺失系列任务(还需添加物品权限)", ["create.evolution"] = "添加灵化进度", ["acquire.missing"] = "补齐缺失装备与物品(还需添加物品权限)", ["acquire.mods"] = "补齐未升级 MOD(还需添加物品权限)", ["guild.settings"] = "修改氏族规则", ["guild.research"] = "编辑氏族研究", ["guild.vault"] = "编辑氏族金库物品", ["guild.currency"] = "编辑氏族金库货币(还需添加货币权限)", ["guild.member"] = "管理氏族成员", ["guild.leave"] = "退出氏族", ["guild.alliance"] = "管理联盟成员", ["unlock.intrinsics"] = "内源之力满级", ["unlock.scans"] = "解锁全部扫描", ["unlock.ship"] = "解锁飞船模块", ["unlock.captura"] = "解锁 Captura 场景", ["unlock.missions"] = "完成普通与钢铁星图", ["unlock.normalMissions"] = "完成普通星图", ["unlock.bounties"] = "解锁赏金链", ["unlock.simaris"] = "解锁 Simaris 研究", ["unlock.helminthRecipes"] = "补齐吞噬蓝图", ["unlock.mods"] = "补齐满级 MOD(还需添加物品权限)", ["unlock.badges"] = "清除新获得标记", ["unlock.focus"] = "解锁专精学派", ["unlock.helminth"] = "Helminth 满级", ["unlock.plexus"] = "Plexus 满级", ["inbox.read"] = "邮件全部已读", ["syndicate"] = "选择支持集团" }; private static readonly Lazy Schema = new(() => new JArray(Operations.Select(x => Field(x.Key, x.Value)).Concat( Assets.Json("editor/settings.json")["groups"]!["account"]!.Select(x => Field("setting." + x.Value("key"), "账号规则 · " + x.Value("label")))).Concat( ((JObject)Assets.Json("editor/import-samples.json")).Properties().Select(x => Field("template." + x.Name, "存档模板 · " + x.Name))))); private static JObject Field(string key, string label) => new() { ["key"] = key, ["label"] = label, ["type"] = "bool", ["value"] = false }; private static JObject Rules(JObject state) => state.Obj("config")["editorPermissions"] as JObject ?? new(); public static bool Enabled(JObject state) => Rules(state).Properties().Any(x => x.Value.Type == JTokenType.Boolean && x.Value.Value()); // The player-visible action set depends on these two gates as well as the editor rules. public static string PlayerVersion(JObject state, ServerOptions options) => Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes( Version(state) + ":" + PlayerPermissions.Currency(state, options) + ":" + PlayerPermissions.Items(state, options)))); private static bool Has(JObject state, string key) => Rules(state).Value(key) ?? (key == "loadout" && Rules(state).Value("exalted")); public static string Version(JObject state) => Convert.ToHexStringLower(SHA256.HashData(Encoding.UTF8.GetBytes(Rules(state).ToString(Formatting.None) + state.Obj("config")["allowPlayerItemGrant"] + state.Obj("config")["allowPlayerCurrencyGrant"]))); public static JObject Read(JObject state) { var fields = (JArray)Schema.Value.DeepClone(); foreach (var f in fields) f["value"] = Has(state, f.Value("key")!); return new JObject { ["version"] = Version(state), ["fields"] = fields }; } public static JObject Save(JObject state, JObject body) { if (!Wire.SecretEquals(body.RequiredString("version", 64), Version(state))) throw new ApiException(409, "权限配置已变化,请刷新后重新修改。"); var values = body["values"] as JObject ?? throw new ApiException(400, "缺少权限设置。"); if (values.Count == 0 || values.Properties().Any(x => x.Value.Type != JTokenType.Boolean || !Schema.Value.Any(f => f.Value("key") == x.Name))) throw new ApiException(400, "包含未知权限或无效值。"); var saved = Rules(state); foreach (var p in values.Properties()) saved[p.Name] = p.Value.DeepClone(); state.Obj("config")["editorPermissions"] = saved; return Read(state); } public static bool Allows(JObject state, ServerOptions options, string action, JObject? values = null) { if (action == "settings") return values is null ? Rules(state).Properties().Any(x => x.Name.StartsWith("setting.", StringComparison.Ordinal) && x.Value.Value()) : values.Count > 0 && values.Properties().All(x => Has(state, "setting." + x.Name)); if (action == "cleanup") return values is not null && Has(state, "setting." + values.Value("key")); if (action.StartsWith("batch.", StringComparison.Ordinal)) return Has(state, "batch") && Allows(state, options, action switch { "batch.acquire.missing" => "acquire.missing", "batch.mods.missing" => "acquire.mods", "batch.max" => "experience", "batch.mods.max" => "mod.rank", "batch.quests.complete" or "batch.quests.reset" => "quest", "batch.evolution.max" => "evolution", "batch.evolution.missing" => "create.evolution", "batch.remove" or "batch.mods.removeUnranked" or "batch.mods.removeRanked" => "remove", _ => "unknown" }); if ((action.StartsWith("guild.research.all.", StringComparison.Ordinal) || action is "guild.vault.missing" or "guild.vault.clear") && !Has(state, "batch")) return false; if (action.StartsWith("acquire.", StringComparison.Ordinal) && !PlayerPermissions.Items(state, options)) return false; if (action == "archive.import" && (!PlayerPermissions.Items(state, options) || !PlayerPermissions.Currency(state, options))) return false; if (action is "riven.create" or "riven.save") return Has(state, "riven") && (action == "riven.save" || PlayerPermissions.Items(state, options)); if (action == "unlock.mods" && !PlayerPermissions.Items(state, options)) return false; if (action.StartsWith("create.legacy.", StringComparison.Ordinal)) return Has(state, "create.legacy") && PlayerPermissions.Items(state, options); if (action.StartsWith("legacy.", StringComparison.Ordinal)) return Has(state, "legacy"); if (action == "guild.currency" && !PlayerPermissions.Currency(state, options)) return false; if ((action.StartsWith("create.modular.", StringComparison.Ordinal) || action is "create.booster" or "create.quest") && !PlayerPermissions.Items(state, options)) return false; // The legacy endpoint must obey an explicit loadout revocation as well. if (action.StartsWith("exalted.mods.", StringComparison.Ordinal)) return Has(state, "exalted") && Has(state, "loadout"); var key = action == "max" ? "experience" : action == "umbra.remove" ? "umbra" : action == "invigoration.remove" ? "invigoration" : action.StartsWith("ability.", StringComparison.Ordinal) ? "ability" : action.StartsWith("shards.", StringComparison.Ordinal) ? "shards" : action.StartsWith("loadout.", StringComparison.Ordinal) ? "loadout" : action.StartsWith("exalted.", StringComparison.Ordinal) ? "exalted" : action.StartsWith("create.modular.", StringComparison.Ordinal) ? "create.modular" : action == "guild.cleanup" ? "guild.settings" : action.StartsWith("guild.research.", StringComparison.Ordinal) ? "guild.research" : action.StartsWith("guild.vault.", StringComparison.Ordinal) ? "guild.vault" : action.StartsWith("guild.member.", StringComparison.Ordinal) ? "guild.member" : action.StartsWith("guild.alliance.", StringComparison.Ordinal) ? "guild.alliance" : action; return Schema.Value.Any(x => x.Value("key") == key) && Has(state, key); } public static void Require(JObject state, ServerOptions options, JObject body) { if (!Allows(state, options, body.RequiredString("action"), body["values"] as JObject)) throw new ApiException(403, "管理员未授权此编辑操作,或权限已撤销。"); } public static JObject Filter(JObject state, ServerOptions options, JObject view) { view["canImport"] = Allows(state, options, "archive.import"); view["canExport"] = Allows(state, options, "archive.export"); foreach (var item in view["items"] as JArray ?? []) item["canEdit"] = (item["editActions"] as JArray ?? []).Values().Any(key => key is not null && Allows(state, options, key)); foreach (var list in new[] { "actions", "batchActions", "creationActions" }) if (view[list] is JArray actions) foreach (var a in actions.Where(x => !Allows(state, options, x.Value("key")!)).ToArray()) a.Remove(); if (view["relatedEquipment"] is JArray related) foreach (var item in related.Where(x => !(x["editActions"] as JArray ?? []).Values().Any(key => key is not null && Allows(state, options, key))).ToArray()) item.Remove(); if (view["fields"] is JArray fields) foreach (var f in fields.Where(x => !Has(state, "setting." + x.Value("key"))).ToArray()) f.Remove(); if (!Has(state, "loadout")) view.Remove("modding"); foreach (var c in view.Array("categories").Where(x => x.Value("key") switch { "settings" => !Allows(state, options, "settings"), "templates" => !Rules(state).Properties().Any(p => p.Name.StartsWith("template.", StringComparison.Ordinal) && p.Value.Value()), "progress" => !Operations.Keys.Where(k => k.StartsWith("unlock.", StringComparison.Ordinal) || k is "inbox.read" or "syndicate").Any(k => Has(state, k)), string key when key.StartsWith("guild.", StringComparison.Ordinal) => !Rules(state).Properties().Any(p => p.Name.StartsWith("guild.", StringComparison.Ordinal) && p.Value.Value()), _ => false }).ToArray()) c.Remove(); return view; } }