Search code examples
controllerstrapikoactx

How to migrate my 'Is Owner' Policy from v3 to Strapi v4


I used to have a 'Is Owner' policy which worked like a charm. Since the new Strapi v4 update it stopped working...

I highly think this code is outdated because it cannot find the function 'strapi.query'..

module.exports = {
  async find(ctx) {
    const { user } = await ctx.state;
    const entities = strapi.query("connection").find({ links: user.id });
    return entities;
  },
}; // BIND LINK TO USER

also my controllers/connections.js which works below, the problem is the code above!

const { createCoreRouter } = require("@strapi/strapi").factories;

module.exports = createCoreRouter("api::connection.connection", {
  config: {
    create: {
      policies: [
        // pass a policy implementation directly
        (policyContext, config, { strapi }) => {
          policyContext.request.body.data.links = policyContext.state.user.id;
        },
      ],
    },
  },
});

How can i update my code so i will have the same function and fetch only the users todos.. ('connection' is my collection name & 'links' is my relation to a user)

Thank you in advance!


Solution

  • Answered my own question with:

    This lets you create a 'todo' and manage in a protected route (without other people seeing your posts)

    const { createCoreController } = require("@strapi/strapi").factories;
    
    module.exports = createCoreController(
      "api::connection.connection",
      ({ strapi }) => ({
        async find(ctx) {
          const { user } = ctx.state;
    
          const entities = await strapi.db
            .query("api::connection.connection")
            .findMany({
              where: {
                links: user,
              },
              populate: true,
            });
    
          return entities;
        },
      })
    );