From 2f1275e36b33bce8a218857513a6a259f2783f10 Mon Sep 17 00:00:00 2001 From: Tommaso Bartolucci Date: Wed, 30 Jun 2021 15:31:59 +0200 Subject: [PATCH] Add skip admin init flag (#6576) * adds skipAdminInit flag to bootstrap * checks for skipAdminInit flag * update docs for skipAdminInit --- api/src/cli/commands/bootstrap/index.ts | 53 ++++++++++++++---------- api/src/cli/index.ts | 6 ++- docs/reference/command-line-interface.md | 7 ++++ 3 files changed, 43 insertions(+), 23 deletions(-) diff --git a/api/src/cli/commands/bootstrap/index.ts b/api/src/cli/commands/bootstrap/index.ts index ae9d415030..af305fcc6b 100644 --- a/api/src/cli/commands/bootstrap/index.ts +++ b/api/src/cli/commands/bootstrap/index.ts @@ -6,8 +6,9 @@ import logger from '../../../logger'; import { getSchema } from '../../../utils/get-schema'; import { RolesService, UsersService, SettingsService } from '../../../services'; import getDatabase, { isInstalled, hasDatabaseConnection } from '../../../database'; +import { SchemaOverview } from '../../../types'; -export default async function bootstrap(): Promise { +export default async function bootstrap({ skipAdminInit }: { skipAdminInit?: boolean }): Promise { logger.info('Initializing bootstrap...'); if ((await isDatabaseAvailable()) === false) { @@ -27,29 +28,12 @@ export default async function bootstrap(): Promise { const schema = await getSchema(); - logger.info('Setting up first admin role...'); - const rolesService = new RolesService({ schema }); - const role = await rolesService.createOne({ name: 'Admin', admin_access: true }); - - logger.info('Adding first admin user...'); - const usersService = new UsersService({ schema }); - - let adminEmail = env.ADMIN_EMAIL; - - if (!adminEmail) { - logger.info('No admin email provided. Defaulting to "admin@example.com"'); - adminEmail = 'admin@example.com'; + if (skipAdminInit == null) { + await createDefaultAdmin(schema); + } else { + logger.info('Skipping creation of default Admin user and role...'); } - let adminPassword = env.ADMIN_PASSWORD; - - if (!adminPassword) { - adminPassword = nanoid(12); - logger.info(`No admin password provided. Defaulting to "${adminPassword}"`); - } - - await usersService.createOne({ email: adminEmail, password: adminPassword, role }); - if (env.PROJECT_NAME && typeof env.PROJECT_NAME === 'string' && env.PROJECT_NAME.length > 0) { const settingsService = new SettingsService({ schema }); await settingsService.upsertSingleton({ project_name: env.PROJECT_NAME }); @@ -78,3 +62,28 @@ async function isDatabaseAvailable() { return false; } + +async function createDefaultAdmin(schema: SchemaOverview) { + logger.info('Setting up first admin role...'); + const rolesService = new RolesService({ schema }); + const role = await rolesService.createOne({ name: 'Admin', admin_access: true }); + + logger.info('Adding first admin user...'); + const usersService = new UsersService({ schema }); + + let adminEmail = env.ADMIN_EMAIL; + + if (!adminEmail) { + logger.info('No admin email provided. Defaulting to "admin@example.com"'); + adminEmail = 'admin@example.com'; + } + + let adminPassword = env.ADMIN_PASSWORD; + + if (!adminPassword) { + adminPassword = nanoid(12); + logger.info(`No admin password provided. Defaulting to "${adminPassword}"`); + } + + await usersService.createOne({ email: adminEmail, password: adminPassword, role }); +} diff --git a/api/src/cli/index.ts b/api/src/cli/index.ts index c2d6177d25..6e657020ef 100644 --- a/api/src/cli/index.ts +++ b/api/src/cli/index.ts @@ -63,7 +63,11 @@ rolesCommand program.command('count ').description('Count the amount of items in a given collection').action(count); -program.command('bootstrap').description('Initialize or update the database').action(bootstrap); +program + .command('bootstrap') + .description('Initialize or update the database') + .option('--skipAdminInit', 'Skips the creation of the default Admin Role and User') + .action(bootstrap); program.parseAsync(process.argv).catch((err) => { console.error(err); diff --git a/docs/reference/command-line-interface.md b/docs/reference/command-line-interface.md index fbbca43d32..6f09ce078f 100644 --- a/docs/reference/command-line-interface.md +++ b/docs/reference/command-line-interface.md @@ -40,6 +40,13 @@ information. ::: +::: tip Skip Admin User/Role + +You can pass the `--skipAdminInit` option to `bootstrap`, if you're creating your Admin role/user in another way (with a +custom migration or an external service, for example). + +::: + ### Install the Database ```