Add skip admin init flag (#6576)

* adds skipAdminInit flag to bootstrap

* checks for skipAdminInit flag

* update docs for skipAdminInit
This commit is contained in:
Tommaso Bartolucci
2021-06-30 15:31:59 +02:00
committed by rijkvanzanten
parent ce3a84ffcc
commit 2f1275e36b
3 changed files with 43 additions and 23 deletions

View File

@@ -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<void> {
export default async function bootstrap({ skipAdminInit }: { skipAdminInit?: boolean }): Promise<void> {
logger.info('Initializing bootstrap...');
if ((await isDatabaseAvailable()) === false) {
@@ -27,29 +28,12 @@ export default async function bootstrap(): Promise<void> {
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 });
}

View File

@@ -63,7 +63,11 @@ rolesCommand
program.command('count <collection>').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);

View File

@@ -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
```