mirror of
https://github.com/directus/directus.git
synced 2026-01-31 23:18:03 -05:00
Add skip admin init flag (#6576)
* adds skipAdminInit flag to bootstrap * checks for skipAdminInit flag * update docs for skipAdminInit
This commit is contained in:
committed by
rijkvanzanten
parent
ce3a84ffcc
commit
2f1275e36b
@@ -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 });
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user