diff --git a/backend/config/functions/bootstrap.js b/backend/config/functions/bootstrap.js index 3bbb0a4..3b1dd3c 100644 --- a/backend/config/functions/bootstrap.js +++ b/backend/config/functions/bootstrap.js @@ -10,38 +10,44 @@ * See more details here: https://strapi.io/documentation/v3.x/concepts/configurations.html#bootstrap */ -async function bootstrap_admin() { - 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, - } +const findPublicRole = async () => { + const result = await strapi + .query("role", "users-permissions") + .findOne({ type: "public" }); + return result; +}; - const admins = await strapi.query('user', 'admin').find({ _limit: 1 }) - if (admins.length) { - console.error(`Admin user already exists: ${admins[0].email}`) - return - } +const setDefaultPermissions = async () => { + const role = await findPublicRole(); + const permissions = await strapi + .query("permission", "users-permissions") + .find({ type: "application", role: role.id, action: "create" }); + console.dir(permissions) + 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.') +}; - try { - // in the admin under services you can find the super admin role - const superAdminRole = await strapi.admin.services.role.getSuperAdmin() - // Hash password before storing in the database - console.dir(superAdminRole) - 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) - } catch (error) { - console.error(error) - } -} +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; +}; module.exports = async () => { - await bootstrap_admin() -} + const shouldSetDefaultPermissions = await isFirstRun(); + if (shouldSetDefaultPermissions) { + console.log('SETUP') + await setDefaultPermissions(); + } +}; diff --git a/env.example b/env.example index 6a69bc9..ba6226f 100644 --- a/env.example +++ b/env.example @@ -1,6 +1,3 @@ -ADMIN_USER=admin -ADMIN_PASS=status1! -ADMIN_EMAIL=admin@example.org DATABASE_NAME=status-affliate DATABASE_USERNAME=status DATABASE_PASSWORD=status1!