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

System Information
  • Strapi Version: 4.0.6
  • Operating System: Mac OS
  • Database: postgres/sqlite
  • Node Version: 8.3.2
  • NPM Version: 16.13.2
  • Yarn Version: 1.22.17

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!

1 Like

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;
    },
  })
);
1 Like