bootstrap: create super admin user from env variables

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2020-11-12 16:58:20 +01:00
parent ff05fd4125
commit 1083416f98
No known key found for this signature in database
GPG Key ID: 4EF064D0E6D63020
2 changed files with 65 additions and 17 deletions

View File

@ -10,44 +10,88 @@
* See more details here: https://strapi.io/documentation/v3.x/concepts/configurations.html#bootstrap
*/
const createSuperAdminUser = async () => {
const params = {
username: process.env.ADMIN_USER,
password: process.env.ADMIN_PASS,
firstname: process.env.ADMIN_USER,
lastname: process.env.ADMIN_USER,
email: process.env.ADMIN_EMAIL,
blocked: false,
isActive: true,
}
if (params.username == undefined || params.password == undefined) {
console.warn('Unable to create admin user, check your environment variables.')
return
}
const admins = await strapi.query('user', 'admin').find({ _limit: 1 })
if (admins.length) {
console.error(`Admin user already exists: ${admins[0].email}`)
return
}
try {
let superAdminRole = await strapi.query('role', 'admin').findOne(
{ code: 'strapi-super-admin' }
)
/* At first startup the role might not exist yet */
if (!superAdminRole) {
superAdminRole = await strapi.query('role', 'admin').create({
name: 'Super Admin',
code: 'strapi-super-admin',
description: 'Super Admins can access and manage all features and settings.',
})
}
// Hash password before storing in the database
params.roles = [superAdminRole.id]
params.password = await strapi.admin.services.auth.hashPassword(params.password)
// Create admin account
const admin = await strapi.query('user', 'admin').create({...params})
console.info('Admin account created:', admin.email)
} catch (error) {
console.error(error)
}
}
const findPublicRole = async () => {
const result = await strapi
.query("role", "users-permissions")
.findOne({ type: "public" });
return result;
};
.findOne({ type: "public" })
return result
}
const setDefaultPermissions = async () => {
const role = await findPublicRole();
const role = await findPublicRole()
const permissions = await strapi
.query("permission", "users-permissions")
.find({ type: "application", role: role.id, action: "create" });
console.dir(permissions)
.find({ type: "application", role: role.id, action: "create" })
await Promise.all(
permissions.map(p =>
strapi
.query("permission", "users-permissions")
.update({ id: p.id }, { enabled: true })
)
);
)
console.info('Added create permissions for Public role.')
};
}
const isFirstRun = async () => {
const pluginStore = strapi.store({
environment: strapi.config.environment,
type: "type",
name: "setup"
});
const initHasRun = await pluginStore.get({ key: "initHasRun" });
await pluginStore.set({ key: "initHasRun", value: true });
return !initHasRun;
};
})
const initHasRun = await pluginStore.get({ key: "initHasRun" })
await pluginStore.set({ key: "initHasRun", value: true })
return !initHasRun
}
module.exports = async () => {
const shouldSetDefaultPermissions = await isFirstRun();
const shouldSetDefaultPermissions = await isFirstRun()
if (shouldSetDefaultPermissions) {
console.log('SETUP')
await setDefaultPermissions();
await setDefaultPermissions()
await createSuperAdminUser()
}
};
}

View File

@ -1,3 +1,7 @@
BASE_URL=http://localhost:1337
ADMIN_USER=admin
ADMIN_PASS=status1!
ADMIN_EMAIL=admin@example.org
DATABASE_NAME=status-affliate
DATABASE_USERNAME=status
DATABASE_PASSWORD=status1!