chore: update config when admin changes their name #1298
@ -1,5 +1,6 @@
|
||||
import { RequestHandler } from "express";
|
||||
import { getAccountForRequest, isNameTaken } from "@/src/services/loginService";
|
||||
import { getAccountForRequest, isAdministrator, isNameTaken } from "@/src/services/loginService";
|
||||
import { config, saveConfig } from "@/src/services/configService";
|
||||
|
||||
export const renameAccountController: RequestHandler = async (req, res) => {
|
||||
const account = await getAccountForRequest(req);
|
||||
@ -7,8 +8,18 @@ export const renameAccountController: RequestHandler = async (req, res) => {
|
||||
if (await isNameTaken(req.query.newname)) {
|
||||
res.status(409).json("Name already in use");
|
||||
} else {
|
||||
if (isAdministrator(account)) {
|
||||
for (let i = 0; i != config.administratorNames!.length; ++i) {
|
||||
if (config.administratorNames![i] == account.DisplayName) {
|
||||
config.administratorNames![i] = req.query.newname;
|
||||
}
|
||||
}
|
||||
await saveConfig();
|
||||
}
|
||||
|
||||
account.DisplayName = req.query.newname;
|
||||
await account.save();
|
||||
|
||||
res.end();
|
||||
}
|
||||
} else {
|
||||
|
10
src/index.ts
10
src/index.ts
@ -19,9 +19,13 @@ import mongoose from "mongoose";
|
||||
return "<BIGINT>" + this.toString() + "</BIGINT>";
|
||||
};
|
||||
const og_stringify = JSON.stringify;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-member-access
|
||||
(JSON as any).stringify = (obj: any): string => {
|
||||
return og_stringify(obj).split(`"<BIGINT>`).join(``).split(`</BIGINT>"`).join(``);
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
JSON.stringify = (obj: any, replacer?: any, space?: string | number): string => {
|
||||
return og_stringify(obj, replacer as string[], space)
|
||||
.split(`"<BIGINT>`)
|
||||
.join(``)
|
||||
.split(`</BIGINT>"`)
|
||||
.join(``);
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,7 @@ interface IConfig {
|
||||
httpsPort?: number;
|
||||
myIrcAddresses?: string[];
|
||||
NRS?: string[];
|
||||
administratorNames?: string[] | string;
|
||||
administratorNames?: string[];
|
||||
autoCreateAccount?: boolean;
|
||||
skipTutorial?: boolean;
|
||||
skipAllDialogue?: boolean;
|
||||
@ -83,10 +83,15 @@ export const updateConfig = async (data: string): Promise<void> => {
|
||||
Object.assign(config, JSON.parse(data));
|
||||
};
|
||||
|
||||
export const saveConfig = async (): Promise<void> => {
|
||||
amnesia = true;
|
||||
await fsPromises.writeFile(configPath, JSON.stringify(config, null, 2));
|
||||
};
|
||||
|
||||
export const validateConfig = (): void => {
|
||||
if (typeof config.administratorNames == "string") {
|
||||
logger.warn(
|
||||
`"administratorNames" should be an array; please add square brackets: ["${config.administratorNames}"]`
|
||||
);
|
||||
logger.info(`Updating config.json to make administratorNames an array.`);
|
||||
config.administratorNames = [config.administratorNames];
|
||||
void saveConfig();
|
||||
}
|
||||
};
|
||||
|
@ -92,13 +92,7 @@ export const getAccountIdForRequest = async (req: Request): Promise<string> => {
|
||||
};
|
||||
|
||||
export const isAdministrator = (account: TAccountDocument): boolean => {
|
||||
if (!config.administratorNames) {
|
||||
return false;
|
||||
}
|
||||
if (typeof config.administratorNames == "string") {
|
||||
return config.administratorNames == account.DisplayName;
|
||||
}
|
||||
return !!config.administratorNames.find(x => x == account.DisplayName);
|
||||
return !!config.administratorNames?.find(x => x == account.DisplayName);
|
||||
};
|
||||
|
||||
const platform_magics = [753, 639, 247, 37, 60];
|
||||
|
Loading…
x
Reference in New Issue
Block a user