My IsOwner global policy version

I implemented a middleware to assign author of any content.

./src/middlewares/assign-owner.js

'use strict';
/**
 * `assign-owner` middleware.
 */
module.exports = (config, { strapi }) => {
  return async (ctx, next) => {
    // it is required that user must be authenticated
    ctx.request.body.data.author = ctx.state.user.id
    await next();
  };
};

We can put these config in routes of any content type:

./src/api/article/routes/article.js

'use strict';
/**
 * article router.
 */
const { createCoreRouter } = require('@strapi/strapi').factories;
module.exports = createCoreRouter('api::article.article', {
    config: {
      create: {
        middleware: ['global::assign-owner']
      },
      update: {
        policies: ['global::is-owner']
      },
      delete: {
        policies: ['global::is-owner']
      }
    }
  });

So the article controller don’t need to do anything.

4 Likes