Search code examples
strapi

Strapi 4 Assign record to a An admin user


I’m using strapi v4.20.0 . And i have a couple of collections in there . I have 2 roles 1 admin and moderator .

i don’t know if this is possible . but I want to assign a record in products collection to a moderator so he can have access to it .

i have set up a relation between products and users . product has many users .

in the RBAC I set that he can see and update if he is the creator … but I want to make him see the record and update it if he’s in relation with a product …

any idea how can this be done ? or is it even possible in strapi ?


Solution

  • Yes it's possible to achieve that. You will have to create a custom condition for that. Here is the documentation: https://docs.strapi.io/dev-docs/configurations/rbac

    In my case, I created a many-way relation between Products and Admin Users. The key is "Shared_with" - so updated it accordingly to your needs.

    Then, you will have to create the custom condition.

        await strapi.admin.services.permission.conditionProvider.register({
          displayName: 'Shared with',
          name: 'shared-with',
          plugin: 'admin',
          handler: async (user) => {
            return  {
              "Shared_with": {
                $elemMatch: {
                  id: user.id
                }
              }
            }
          },
        });
      },
    

    Once it's created, you will have to apply it to your Collection Type.

    And voilà.

    Here is a video to help you going though that: https://www.loom.com/share/2490ed5def6e46fb8adacabbc80f1d2d?sid=ec166e58-7536-4ee2-85fc-afee79614834

    Hope it helps