System Information
- Strapi Version: 4.1.9
- Operating System: WSL 2
- Database: Maria
- Node Version: 14.17.6
Hello everyone,
I’m a bit confused since version 4 of Strapi and mainly on one particular point. It is about assigning the ownership of an entity to the connected user.
In Strapi 3, it was enough to do something like this:
entity.owner = ctx.state.user.id;
And naturally, the entity is created, having as owner, the connected user.
In Strapi 4, I went with something like this:
async create(ctx) {
ctx.request.body.data = { ...ctx.request.body.data, owner: ctx.state.user.id }
const response = await super.create(ctx);
return response;
}
The problem here is that my entity is created without this owner. The only way to make it work is to open the findUsers route to the public role. Now, this is a bit of a shame, as it will force me to secure this method that I don’t want to open!
So I did this:
async create(ctx) {
ctx.request.body.data = { ...ctx.request.body.data, user: ctx.state.user.id }
const entry = await strapi.entityService.create('api::transaction.transaction', {
data: ctx.request.body.data,
});
return entry;
}
This option works, but I would prefer not to use it for several reasons:
- The return format is not standard
- I no longer use the CoreController service
Do you have a solution and an explanation?
Thank you in advance.