XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFESpaceNinjaServer

A simple server for a small space ninja game

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFESpaceNinjaServer

chore: avoid doing authentication twice with getGuildForRequest (#3505)

Closes #3491 Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/3505 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com>

518a9616
Sainan <63328889+Sainan@users.noreply.github.com>
提交于

代码差异

11 个文件 +25 -41
Modified src/controllers/api/cancelGuildAdvertisementController.ts +2 -4
@@ -1,14 +1,12 @@
1 1 import { GuildAd } from "../../models/guildModel.ts";
2 import { getGuildForRequestEx, hasGuildPermission } from "../../services/guildService.ts";
3 import { getInventory } from "../../services/inventoryService.ts";
2 import { getGuildForRequest, hasGuildPermission } from "../../services/guildService.ts";
4 3 import { getAccountIdForRequest } from "../../services/loginService.ts";
5 4 import { GuildPermission } from "../../types/guildTypes.ts";
6 5 import type { RequestHandler } from "express";
7 6
8 7 export const cancelGuildAdvertisementController: RequestHandler = async (req, res) => {
9 8 const accountId = await getAccountIdForRequest(req);
10 const inventory = await getInventory(accountId, "GuildId");
11 const guild = await getGuildForRequestEx(req, inventory);
9 const guild = await getGuildForRequest(req, accountId);
12 10 if (!(await hasGuildPermission(guild, accountId, GuildPermission.Advertiser))) {
13 11 res.status(400).end();
14 12 return;
Modified src/controllers/api/changeDojoSpawnRoomController.ts +2 -4
@@ -1,13 +1,11 @@
1 1 import type { RequestHandler } from "express";
2 2 //import type { IOidWithLegacySupport } from "../../types/commonTypes.ts";
3 3 import { getAccountForRequest } from "../../services/loginService.ts";
4 import { getInventory } from "../../services/inventoryService.ts";
5 import { getDojoClient, getGuildForRequestEx } from "../../services/guildService.ts";
4 import { getDojoClient, getGuildForRequest } from "../../services/guildService.ts";
6 5
7 6 export const changeDojoSpawnRoomController: RequestHandler = async (req, res) => {
8 7 const account = await getAccountForRequest(req);
9 const inventory = await getInventory(account._id, "GuildId");
10 const guild = await getGuildForRequestEx(req, inventory);
8 const guild = await getGuildForRequest(req, account._id);
11 9 if (guild.DojoComponents.length != 1) {
12 10 res.status(400).end();
13 11 }
Modified src/controllers/api/changeGuildRankController.ts +1 -1
@@ -13,7 +13,7 @@ export const changeGuildRankController: RequestHandler = async (req, res) => {
13 13 accountId: account._id,
14 14 guildId: req.query.guildId as string
15 15 }))!;
16 const guild = await getGuildForRequest(req);
16 const guild = await getGuildForRequest(req, account._id);
17 17 const target = (await GuildMember.findOne({
18 18 guildId: req.query.guildId as string,
19 19 accountId: req.query.targetId as string
Modified src/controllers/api/createGuildDojoController.ts +2 -4
@@ -1,15 +1,13 @@
1 1 import type { RequestHandler } from "express";
2 2 //import type { IOidWithLegacySupport } from "../../types/commonTypes.ts";
3 3 import { Types } from "mongoose";
4 import { getDojoClient, getGuildForRequestEx } from "../../services/guildService.ts";
4 import { getDojoClient, getGuildForRequest } from "../../services/guildService.ts";
5 5 import { getJSONfromString } from "../../helpers/stringHelpers.ts";
6 6 import { getAccountForRequest } from "../../services/loginService.ts";
7 import { getInventory } from "../../services/inventoryService.ts";
8 7
9 8 export const createGuildDojoController: RequestHandler = async (req, res) => {
10 9 const account = await getAccountForRequest(req);
11 const inventory = await getInventory(account._id, "GuildId");
12 const guild = await getGuildForRequestEx(req, inventory);
10 const guild = await getGuildForRequest(req, account._id);
13 11
14 12 if (guild.DojoComponents.length == 0) {
15 13 const payload = getJSONfromString<ICreateGuildDojoRequest>(String(req.body));
Modified src/controllers/api/customizeGuildRanksController.ts +1 -1
@@ -6,7 +6,7 @@ import type { RequestHandler } from "express";
6 6
7 7 export const customizeGuildRanksController: RequestHandler = async (req, res) => {
8 8 const accountId = await getAccountIdForRequest(req);
9 const guild = await getGuildForRequest(req);
9 const guild = await getGuildForRequest(req, accountId);
10 10 const payload = JSON.parse(String(req.body)) as ICustomizeGuildRanksRequest;
11 11 if (!(await hasGuildPermission(guild, accountId, GuildPermission.Ruler))) {
12 12 res.status(400).json("Invalid permission");
Modified src/controllers/api/removeFromGuildController.ts +2 -2
@@ -16,14 +16,14 @@ import type { RequestHandler, Response } from "express";
16 16
17 17 export const removeFromGuildGetController: RequestHandler = async (req, res) => {
18 18 const account = await getAccountForRequest(req);
19 const guild = await getGuildForRequest(req);
19 const guild = await getGuildForRequest(req, account._id);
20 20 const userName = req.query.userName as string;
21 21 await processRemoveFromGuildRequest(account, guild, { userName }, res);
22 22 };
23 23
24 24 export const removeFromGuildPostController: RequestHandler = async (req, res) => {
25 25 const account = await getAccountForRequest(req);
26 const guild = await getGuildForRequest(req);
26 const guild = await getGuildForRequest(req, account._id);
27 27 const payload = JSON.parse(String(req.body)) as IRemoveFromGuildRequest;
28 28 await processRemoveFromGuildRequest(account, guild, payload, res);
29 29 };
Modified src/controllers/api/setDojoComponentMessageController.ts +2 -4
@@ -1,12 +1,10 @@
1 1 import type { RequestHandler } from "express";
2 import { getDojoClient, getGuildForRequestEx } from "../../services/guildService.ts";
2 import { getDojoClient, getGuildForRequest } from "../../services/guildService.ts";
3 3 import { getAccountForRequest } from "../../services/loginService.ts";
4 import { getInventory } from "../../services/inventoryService.ts";
5 4
6 5 export const setDojoComponentMessageController: RequestHandler = async (req, res) => {
7 6 const account = await getAccountForRequest(req);
8 const inventory = await getInventory(account._id, "GuildId");
9 const guild = await getGuildForRequestEx(req, inventory);
7 const guild = await getGuildForRequest(req, account._id);
10 8 // At this point, we know that a member of the guild is making this request. Assuming they are allowed to change the message.
11 9 const component = guild.DojoComponents.id(req.query.componentId as string)!;
12 10 const payload = JSON.parse(String(req.body)) as SetDojoComponentMessageRequest;
Modified src/controllers/custom/addVaultTypeCountController.ts +2 -4
@@ -1,7 +1,6 @@
1 1 import { getAccountIdForRequest } from "../../services/loginService.ts";
2 import { getInventory } from "../../services/inventoryService.ts";
3 2 import type { RequestHandler } from "express";
4 import { getGuildForRequestEx, hasGuildPermission } from "../../services/guildService.ts";
3 import { getGuildForRequest, hasGuildPermission } from "../../services/guildService.ts";
5 4 import { GuildPermission } from "../../types/guildTypes.ts";
6 5 import type { ITypeCount } from "../../types/commonTypes.ts";
7 6
@@ -11,8 +10,7 @@ export const addVaultTypeCountController: RequestHandler = async (req, res) => {
11 10 vaultType: keyof typeof vaultConfig;
12 11 items: ITypeCount[];
13 12 };
14 const inventory = await getInventory(accountId, "GuildId");
15 const guild = await getGuildForRequestEx(req, inventory);
13 const guild = await getGuildForRequest(req, accountId);
16 14 if (!(await hasGuildPermission(guild, accountId, vaultConfig[vaultType]))) {
17 15 res.status(400).send("-1").end();
18 16 return;
Modified src/controllers/custom/setGuildCheatController.ts +2 -4
@@ -1,6 +1,5 @@
1 1 import { GuildMember } from "../../models/guildModel.ts";
2 import { getGuildForRequestEx } from "../../services/guildService.ts";
3 import { getInventory } from "../../services/inventoryService.ts";
2 import { getGuildForRequest } from "../../services/guildService.ts";
4 3 import { getAccountIdForRequest } from "../../services/loginService.ts";
5 4 import type { IGuildCheats } from "../../types/guildTypes.ts";
6 5 import type { RequestHandler } from "express";
@@ -8,8 +7,7 @@ import type { RequestHandler } from "express";
8 7 export const setGuildCheatController: RequestHandler = async (req, res) => {
9 8 const accountId = await getAccountIdForRequest(req);
10 9 const payload = req.body as ISetGuildCheatRequest;
11 const inventory = await getInventory(accountId, `GuildId`);
12 const guild = await getGuildForRequestEx(req, inventory);
10 const guild = await getGuildForRequest(req, accountId);
13 11 const member = await GuildMember.findOne({ accountId: accountId, guildId: guild._id });
14 12
15 13 if (member) {
Modified src/controllers/custom/techProjectController.ts +5 -10
@@ -1,8 +1,7 @@
1 1 import { getAccountIdForRequest } from "../../services/loginService.ts";
2 import { getInventory } from "../../services/inventoryService.ts";
3 2 import type { RequestHandler } from "express";
4 3 import {
5 getGuildForRequestEx,
4 getGuildForRequest,
6 5 setGuildTechLogState,
7 6 processFundedGuildTechProject,
8 7 scaleRequiredCount,
@@ -18,8 +17,7 @@ import { GuildMember } from "../../models/guildModel.ts";
18 17 export const addTechProjectController: RequestHandler = async (req, res) => {
19 18 const accountId = await getAccountIdForRequest(req);
20 19 const requests = req.body as ITechProjectRequest[];
21 const inventory = await getInventory(accountId, "GuildId");
22 const guild = await getGuildForRequestEx(req, inventory);
20 const guild = await getGuildForRequest(req, accountId);
23 21 if (!(await hasGuildPermission(guild, accountId, GuildPermission.Tech))) {
24 22 res.status(400).send("-1").end();
25 23 return;
@@ -53,8 +51,7 @@ export const addTechProjectController: RequestHandler = async (req, res) => {
53 51 export const removeTechProjectController: RequestHandler = async (req, res) => {
54 52 const accountId = await getAccountIdForRequest(req);
55 53 const requests = req.body as ITechProjectRequest[];
56 const inventory = await getInventory(accountId, "GuildId");
57 const guild = await getGuildForRequestEx(req, inventory);
54 const guild = await getGuildForRequest(req, accountId);
58 55 if (!(await hasGuildPermission(guild, accountId, GuildPermission.Tech))) {
59 56 res.status(400).send("-1").end();
60 57 return;
@@ -73,8 +70,7 @@ export const removeTechProjectController: RequestHandler = async (req, res) => {
73 70 export const fundTechProjectController: RequestHandler = async (req, res) => {
74 71 const accountId = await getAccountIdForRequest(req);
75 72 const requests = req.body as ITechProjectRequest[];
76 const inventory = await getInventory(accountId, "GuildId");
77 const guild = await getGuildForRequestEx(req, inventory);
73 const guild = await getGuildForRequest(req, accountId);
78 74 const guildMember = (await GuildMember.findOne(
79 75 { accountId, guildId: guild._id },
80 76 "RegularCreditsContributed MiscItemsContributed"
@@ -104,8 +100,7 @@ export const fundTechProjectController: RequestHandler = async (req, res) => {
104 100 export const completeTechProjectsController: RequestHandler = async (req, res) => {
105 101 const accountId = await getAccountIdForRequest(req);
106 102 const requests = req.body as ITechProjectRequest[];
107 const inventory = await getInventory(accountId, "GuildId");
108 const guild = await getGuildForRequestEx(req, inventory);
103 const guild = await getGuildForRequest(req, accountId);
109 104 if (!(await hasGuildPermission(guild, accountId, GuildPermission.Tech))) {
110 105 res.status(400).send("-1").end();
111 106 return;
Modified src/services/guildService.ts +4 -3
@@ -1,6 +1,5 @@
1 1 import type { Request } from "express";
2 2 import type { TAccountDocument } from "./loginService.ts";
3 import { getAccountIdForRequest } from "./loginService.ts";
4 3 import { addLevelKeys, addRecipes, combineInventoryChanges, getInventory } from "./inventoryService.ts";
5 4 import type { TGuildDatabaseDocument } from "../models/guildModel.ts";
6 5 import { Alliance, AllianceMember, Guild, GuildAd, GuildMember } from "../models/guildModel.ts";
@@ -39,8 +38,10 @@ import { addAccountDataToFriendInfo, addInventoryDataToFriendInfo } from "./frie
39 38 import type { ITypeCount } from "../types/commonTypes.ts";
40 39 import gameToBuildVersion from "../constants/gameToBuildVersion.ts";
41 40
42 export const getGuildForRequest = async (req: Request): Promise<TGuildDatabaseDocument> => {
43 const accountId = await getAccountIdForRequest(req);
41 export const getGuildForRequest = async (
42 req: Request,
43 accountId: string | Types.ObjectId
44 ): Promise<TGuildDatabaseDocument> => {
44 45 const inventory = await getInventory(accountId, "GuildId");
45 46 return await getGuildForRequestEx(req, inventory);
46 47 };