bootstrap: enable create permission for public role

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2020-11-10 15:21:38 +01:00
parent 2a042320d9
commit ff05fd4125
No known key found for this signature in database
GPG Key ID: 4EF064D0E6D63020
2 changed files with 37 additions and 34 deletions

View File

@ -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();
}
};

View File

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