mirror of
https://github.com/status-im/status-affiliate.git
synced 2025-02-21 09:28:21 +00:00
bootstrap: enable create permission for public role
Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
parent
2a042320d9
commit
ff05fd4125
68
backend/config/functions/bootstrap.js
vendored
68
backend/config/functions/bootstrap.js
vendored
@ -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();
|
||||
}
|
||||
};
|
||||
|
@ -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!
|
||||
|
Loading…
x
Reference in New Issue
Block a user