diff --git a/apps/cli/src/commands/config.command.ts b/apps/cli/src/platform/commands/config.command.ts similarity index 70% rename from apps/cli/src/commands/config.command.ts rename to apps/cli/src/platform/commands/config.command.ts index eb6559443da..cd94e8af9c4 100644 --- a/apps/cli/src/commands/config.command.ts +++ b/apps/cli/src/platform/commands/config.command.ts @@ -1,17 +1,21 @@ import { OptionValues } from "commander"; import { firstValueFrom } from "rxjs"; +import { AccountService } from "@bitwarden/common/auth/abstractions/account.service"; import { EnvironmentService, Region, } from "@bitwarden/common/platform/abstractions/environment.service"; -import { Response } from "../models/response"; -import { MessageResponse } from "../models/response/message.response"; -import { StringResponse } from "../models/response/string.response"; +import { Response } from "../../models/response"; +import { MessageResponse } from "../../models/response/message.response"; +import { StringResponse } from "../../models/response/string.response"; export class ConfigCommand { - constructor(private environmentService: EnvironmentService) {} + constructor( + private environmentService: EnvironmentService, + private accountService: AccountService, + ) {} async run(setting: string, value: string, options: OptionValues): Promise { setting = setting.toLowerCase(); @@ -40,6 +44,12 @@ export class ConfigCommand { return Response.success(stringRes); } + // The server config cannot be updated while a user is actively logged in to the current server + const activeAccount = await firstValueFrom(this.accountService.activeAccount$); + if (activeAccount) { + return Response.error("Logout required before server config update."); + } + url = url === "null" || url === "bitwarden.com" || url === "https://bitwarden.com" ? null : url; await this.environmentService.setEnvironment(Region.SelfHosted, { base: url, diff --git a/apps/cli/src/program.ts b/apps/cli/src/program.ts index 37c838b6646..51c4b39e988 100644 --- a/apps/cli/src/program.ts +++ b/apps/cli/src/program.ts @@ -10,12 +10,12 @@ import { LogoutCommand } from "./auth/commands/logout.command"; import { UnlockCommand } from "./auth/commands/unlock.command"; import { BaseProgram } from "./base-program"; import { CompletionCommand } from "./commands/completion.command"; -import { ConfigCommand } from "./commands/config.command"; import { EncodeCommand } from "./commands/encode.command"; import { StatusCommand } from "./commands/status.command"; import { UpdateCommand } from "./commands/update.command"; import { Response } from "./models/response"; import { MessageResponse } from "./models/response/message.response"; +import { ConfigCommand } from "./platform/commands/config.command"; import { GenerateCommand } from "./tools/generate.command"; import { CliUtils } from "./utils"; import { SyncCommand } from "./vault/sync.command"; @@ -403,7 +403,10 @@ export class Program extends BaseProgram { writeLn("", true); }) .action(async (setting, value, options) => { - const command = new ConfigCommand(this.serviceContainer.environmentService); + const command = new ConfigCommand( + this.serviceContainer.environmentService, + this.serviceContainer.accountService, + ); const response = await command.run(setting, value, options); this.processResponse(response); });