返回提交历史
Modified
src/controllers/custom/renameAccountController.ts
+12
-1
Modified
src/index.ts
+7
-3
Modified
src/services/configService.ts
+9
-4
Modified
src/services/loginService.ts
+1
-7
XFEstudio/SpaceNinjaServer
chore: update config when admin changes their name (#1298)
Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/1298
cf3007b7
代码差异
4 个文件
+29
-15
@@ -1,5 +1,6 @@
1
1
import { RequestHandler } from "express";
2
import { getAccountForRequest, isNameTaken } from "@/src/services/loginService";
2
import { getAccountForRequest, isAdministrator, isNameTaken } from "@/src/services/loginService";
3
import { config, saveConfig } from "@/src/services/configService";
3
4
4
5
export const renameAccountController: RequestHandler = async (req, res) => {
5
6
const account = await getAccountForRequest(req);
@@ -7,8 +8,18 @@ export const renameAccountController: RequestHandler = async (req, res) => {
7
8
if (await isNameTaken(req.query.newname)) {
8
9
res.status(409).json("Name already in use");
9
10
} else {
11
if (isAdministrator(account)) {
12
for (let i = 0; i != config.administratorNames!.length; ++i) {
13
if (config.administratorNames![i] == account.DisplayName) {
14
config.administratorNames![i] = req.query.newname;
15
}
16
}
17
await saveConfig();
18
}
19
10
20
account.DisplayName = req.query.newname;
11
21
await account.save();
22
12
23
res.end();
13
24
}
14
25
} else {
@@ -19,9 +19,13 @@ import mongoose from "mongoose";
19
19
return "<BIGINT>" + this.toString() + "</BIGINT>";
20
20
};
21
21
const og_stringify = JSON.stringify;
22
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
23
(JSON as any).stringify = (obj: any): string => {
24
return og_stringify(obj).split(`"<BIGINT>`).join(``).split(`</BIGINT>"`).join(``);
22
// eslint-disable-next-line @typescript-eslint/no-explicit-any
23
JSON.stringify = (obj: any, replacer?: any, space?: string | number): string => {
24
return og_stringify(obj, replacer as string[], space)
25
.split(`"<BIGINT>`)
26
.join(``)
27
.split(`</BIGINT>"`)
28
.join(``);
25
29
};
26
30
}
27
31
@@ -34,7 +34,7 @@ interface IConfig {
34
34
httpsPort?: number;
35
35
myIrcAddresses?: string[];
36
36
NRS?: string[];
37
administratorNames?: string[] | string;
37
administratorNames?: string[];
38
38
autoCreateAccount?: boolean;
39
39
skipTutorial?: boolean;
40
40
skipAllDialogue?: boolean;
@@ -83,10 +83,15 @@ export const updateConfig = async (data: string): Promise<void> => {
83
83
Object.assign(config, JSON.parse(data));
84
84
};
85
85
86
export const saveConfig = async (): Promise<void> => {
87
amnesia = true;
88
await fsPromises.writeFile(configPath, JSON.stringify(config, null, 2));
89
};
90
86
91
export const validateConfig = (): void => {
87
92
if (typeof config.administratorNames == "string") {
88
logger.warn(
89
`"administratorNames" should be an array; please add square brackets: ["${config.administratorNames}"]`
90
);
93
logger.info(`Updating config.json to make administratorNames an array.`);
94
config.administratorNames = [config.administratorNames];
95
void saveConfig();
91
96
}
92
97
};
@@ -93,13 +93,7 @@ export const getAccountIdForRequest = async (req: Request): Promise<string> => {
93
93
};
94
94
95
95
export const isAdministrator = (account: TAccountDocument): boolean => {
96
if (!config.administratorNames) {
97
return false;
98
}
99
if (typeof config.administratorNames == "string") {
100
return config.administratorNames == account.DisplayName;
101
}
102
return !!config.administratorNames.find(x => x == account.DisplayName);
96
return !!config.administratorNames?.find(x => x == account.DisplayName);
103
97
};
104
98
105
99
const platform_magics = [753, 639, 247, 37, 60];