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.
Take a look at the components section. You should always specify the ID of the component that you want to update. Otherwise, it will be deleted and recreated. So the ID of the new component will change.
Strapi is a headless CMS that allows you to easily build customizable backend services. You can integrate Strapi applications with any type of frontend and can deploy the application to the cloud. This tutorial guides you through the process of creating a simple React-native to-do app with Strapi as your backend.
Strapi Releases v4 of Headless CMS Strapi's headless posture also enables it to power front-end frameworks such as React, Gatsby, Vue. js, Next. js, and other static site generators. It also supports multiple databases as well as both REST and GraphQL APIs.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With