improve: handle config.administratorNames being a string (#658)
This commit is contained in:
		
							parent
							
								
									f0eea818f9
								
							
						
					
					
						commit
						02f4d0e821
					
				@ -6,11 +6,12 @@ import http from "http";
 | 
			
		||||
import https from "https";
 | 
			
		||||
import fs from "node:fs";
 | 
			
		||||
import { app } from "./app";
 | 
			
		||||
import { config } from "./services/configService";
 | 
			
		||||
import { config, validateConfig } from "./services/configService";
 | 
			
		||||
import { connectDatabase } from "@/src/services/mongoService";
 | 
			
		||||
import { registerLogFileCreationListener } from "@/src/utils/logger";
 | 
			
		||||
 | 
			
		||||
registerLogFileCreationListener();
 | 
			
		||||
validateConfig();
 | 
			
		||||
 | 
			
		||||
void (async (): Promise<void> => {
 | 
			
		||||
    await connectDatabase();
 | 
			
		||||
 | 
			
		||||
@ -22,6 +22,7 @@ fs.watchFile(configPath, () => {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        Object.assign(config, JSON.parse(fs.readFileSync(configPath, "utf-8")));
 | 
			
		||||
        validateConfig();
 | 
			
		||||
    }
 | 
			
		||||
});
 | 
			
		||||
 | 
			
		||||
@ -32,7 +33,7 @@ interface IConfig {
 | 
			
		||||
    httpPort?: number;
 | 
			
		||||
    httpsPort?: number;
 | 
			
		||||
    myIrcAddresses?: string[];
 | 
			
		||||
    administratorNames?: string[];
 | 
			
		||||
    administratorNames?: string[] | string;
 | 
			
		||||
    autoCreateAccount?: boolean;
 | 
			
		||||
    skipTutorial?: boolean;
 | 
			
		||||
    skipAllDialogue?: boolean;
 | 
			
		||||
@ -61,3 +62,11 @@ export const updateConfig = async (data: string): Promise<void> => {
 | 
			
		||||
    await fsPromises.writeFile(configPath, data);
 | 
			
		||||
    Object.assign(config, JSON.parse(data));
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export const validateConfig = (): void => {
 | 
			
		||||
    if (typeof config.administratorNames == "string") {
 | 
			
		||||
        logger.warn(
 | 
			
		||||
            `"administratorNames" should be an array; please add square brackets: ["${config.administratorNames}"]`
 | 
			
		||||
        );
 | 
			
		||||
    }
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
@ -75,5 +75,11 @@ export const getAccountIdForRequest = async (req: Request): Promise<string> => {
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
export const isAdministrator = (account: TAccountDocument): boolean => {
 | 
			
		||||
    return !!config.administratorNames?.find(x => x == account.DisplayName);
 | 
			
		||||
    if (!config.administratorNames) {
 | 
			
		||||
        return false;
 | 
			
		||||
    }
 | 
			
		||||
    if (typeof config.administratorNames == "string") {
 | 
			
		||||
        return config.administratorNames == account.DisplayName;
 | 
			
		||||
    }
 | 
			
		||||
    return !!config.administratorNames.find(x => x == account.DisplayName);
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user