Hi I’m trying to update a controller so that users can only see items created by them or their organizations.
I’m unable to set any values for created_by or modified_by via controller. The ORM appears to strip out and ignore these fields which is super frustrating because there’s zero documentation on how to use this feature.
I created a model hook for “afterCreate” to see what the Admin UI was doing, and I am seemingly passing it the same data but it just won’t work from my controller.
Here’s an example of what data is present in the payload I’m passing to strapi.model.create() and the a console.log from the beforeCreate() function to show that the unhelpful ORM is taking off my created_by fields because “reasons”??!!
Super frustrated with this, but I realize its just my lack of understanding and inability to read the docs thats blocking me. There HAS to be a way to do this… I’m just not finding it.
So I have sort of a hacky way to accomplish what I want.
To save anybody who is facing this issue the time / struggle… Strapi just doesn’t let you do what I am trying to do above here. Don’t research it… you’ll just waste your time and get angry. You can’t use the built in created_by field so stop trying.
The solution is to add your own createdByeditedBy or editedAt fields to the model and update them manually in every controller action that touches your records. Then in your controller you can look at the ctx.state.user to find IDs for assignment that will work. Then update your list/find controller actions to ignore any URL params, and set the createdBy from the ctx.state.user.id value you can grab.
This is by far the worst part of Strapi I’ve encountered yet. Its the type of thing that will cause people to be discouraged from using the platform because what I’d consider basic functionality is totally lacking. I am trying to channel my frustration into constructive criticism… I hope someone reads these posts and updates the holes in the documentation or writes a blog post about “how to deal with the fact that your admin users and API users are two different sets”.
I really… REALLY wish there was a way to undo the separation of admin users and web users. Especially with the great ACL admin UI that Strapi has it takes a great feature and completely ruins it.
Just wanted to come in and say that honestly I agree with you, we are planning to make the users-permissions plugin optional in the v4 and it won’t be installed by default however I personally believe that we could do better on this.
On that note, those specific fields are only used by the admin plugin as I assume you figured out and they aren’t polymorphic nor are they configurable without heavy modification to the strapi-admin package. Your solution is the correct one, I believe in the v4 we might be changing their name but I would need to confirm.