6

In Strapi every user-defined collection type has a default services which allow to create/find/update/etc. on the corresponding Model. For Example the below code in a Strapi controller will update a bill collection type with the given data:

await strapi.services.bill.update({id}, {verified: true, receipt_number})

But there is no service for User built-in collection type. I need to change the user's role by a custom controller.

1 Answers1

16

OK. I found the solution. We could modify or create a custom services and get inspired by this core services implementation here: https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-services

for my case I have added this method to a custom service:

updateUserRole: async (user_id, role_id) => {
return await strapi.query('user', 'users-permissions').update({id: user_id}, {role: role_id});}

now I could access this service like this:

await strapi.services.customServiceName.updateUserRole(user_id, new_role)
Joe Sadoski
  • 586
  • 2
  • 7
  • 22