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