I want to allow permission for public role by default

System Information
  • Strapi Version: 4
  • Operating System: Mac
  • Database: Posgres
  • Node Version:
  • NPM Version:
  • Yarn Version:

After digging it for few hours I figured out that the code for allowing default permission for public role will somewhat look like this:

async bootstrap({ strapi }) {
    const findPublicRole = async () => {
      const publicRole = await strapi
        .query("plugin::users-permissions.role")
        .findOne({
          where: {
            type: "public",
          },
        });
      console.log(publicRole);
      return publicRole;
    };

    const setDefaultPermissions = async () => {
      const role = await findPublicRole();
      const permissions = await strapi
        .query("plugin::users-permissions.permission")
        .findMany({ type: "public", role: role.id });
      console.log(permissions);

      await Promise.all(
        permissions.map((p) =>
          strapi.query("plugin::users-permissions.permission").update({
            where: { id: p.id },
            data: {
              enabled: true,
            },
          })
        )
      );
    };

    await setDefaultPermissions();
  },

But the problem here is the code isn’t setting all the permissions. I want to set all permissions by default while bootstrapping.


In the image below I want to allow all (inshort tick all the checkboxes)

what am I doing wrong with my code? Please help.

Hi Midha, I created a plugin to help simplify this problem: strapi-plugin-public-permissions - npm.

Hopefully it helps solve your problem!

If you’d rather code the solution yourself, for the plugin I ended up using strapi.db.connection to gain access to knex (the underlying ORM which Strapi uses to interact with the database). You can see the approach I took here.

What about the Authenticated Permissions can your plugin deal with this?