TonyDeplanque / strapi-plugin-migrations

Init or update your data easly with a version controller.
25 stars 14 forks source link

Using plugin::users-permissions.role fails #5

Open HSorensen opened 2 years ago

HSorensen commented 2 years ago

I am trying to set the find and findOne permissions for the public role for some new content types using the migration plugin.

If I run the script below using the strap console the commands works as intended, but when migration plugin runs the script, the strapi.entityService.findMany(...) returns an empty array [].

Not sure what is causing this, but I guess it is related to if the user-permissions is ready or not at the time the migrations plugin is running.

// Based on https://forum.strapi.io/t/set-permissions-programmatically-6294/1774/10
module.exports = async () => {
    const r1 = await strapi.entityService.findMany('plugin::users-permissions.role', {filters: {type:'public'}})

    const _public = await strapi
        .service("plugin::users-permissions.role")
        .findOne(r1[0].id);

    for (const permission of Object.keys(_public.permissions)) {
        if (permission.startsWith("api::content-type")) {
            for (const controller of Object.keys(
                _public.permissions[permission].controllers
            )) {
                _public.permissions[permission]
                             .controllers[controller]
                             .find.enabled = true;
                _public.permissions[permission]
                            .controllers[controller]
                            .findOne.enabled = true;
            }
        }
    }

    await strapi
    .service("plugin::users-permissions.role")
    .updateRole(_public.id, _public)
}
saintego commented 2 years ago

I have similar issue. I'm trying to create user with custom role and it screw creating other roles by user-permission, because my role is created first. Can we run it somehow after user-permission is creating it's roles? Edit: I solved it by running migration manually in index.js > bootstrap