Solved

Can you add or change the Custom role though an API?


  • Getting inSpired
  • 1 reply posted

I need to change the Custom Role for a large group of users, I like to do this through a scripted API call.

No it looks like that the Custom Role is not a field that you can use to fetch users and you also can’t add or change that filed.

 

Or am I doing something wrong and do I need to read better :)

iMac-van-Ron:python ron$ curl -H 'Authorization: Bearer <TOKEN>' -X PUT https://api2-eu-west-1.almostinsided.com/user/{UserID}/role/'Extreme Hero'
This action is only available for fields:email,oracle_sso_id,token_sso_id,openidconnect_sso_id,facebook_sso_id,janrain_sso_id,saml_sso_id,linkedin_sso_id,username,oauth2_sso_id,avatar

 

/Ron

 

 

icon

Best answer by daniel.boon 14 September 2020, 14:39

@RonH @mstone 

It’s now possible to remove custom user roles via API :) https://api2-eu-west-1.insided.com/docs/user/#operation/revokeRole

View original

13 replies

Userlevel 6
Badge +1

Hey @RonH!

It’s not currently possible to update the custom user roles of a user via API, or to remove a custom user role from a user via API.

However, it is possible to add custom user roles via API: https://api2-eu-west-1.insided.com/docs/user/#operation/addRole. You can achieve this by passing the custom role name into the user role object.

Hey Daniel,

Thanks for the reply.

I tried that and it give me a “Done” but nothing changes in the user settings

 

iMac-van-Ron:~ ron$ curl -H 'Authorization: Bearer <TOKEN>' -X POST https://api2-eu-west-1.almostinsided.com/user/{userID}/role -d {"data":{-"user_role":["Extreme Hero"]}}

#Done

In the user data I only see:

"roles":[{"auth_item":{"name":"roles.registered","main":true}}]

and not what I expected:

"roles":[{"auth_item":{"name":"roles.registered","main":true}},{"auth_item":{"name":"Extreme Hero","main":false}}]

 

/Ron

 

Userlevel 3
Badge +1

@daniel.boon any plans for allowing for the removal of a custom role via API? Use case being: user x was a customer, but is not longer a customer and as a result should be removed from the ‘customer’ custom role. 

Userlevel 5
Badge +1

Hey @RonH hmm yep your call looks ok - i think there could be something going on here with this specific end point unfortunately as im also unable to update the user role via API on one of our test environments too. I’ve opened a technical investigation internally into this which im sure will be immediately picked up, once I have some more information regarding it i’ll let you guys know!

Userlevel 6
Badge +1

@daniel.boon any plans for allowing for the removal of a custom role via API? Use case being: user x was a customer, but is not longer a customer and as a result should be removed from the ‘customer’ custom role. 

We have no concrete plans for this at the moment @mstone, but it makes complete sense :)

Userlevel 5
Badge +1

Hi @RonH I was wondering if can you try this again? Can you just check your payload looks like this:

{
"data": {
"user_role": [
"tester"
]
}

Example uri: POST /user/329/role

Also can you just ensure that the ‘Content-Type’ in the header is set to: ‘application/json’ else the endpoint will not know what to do with the payload else :slight_smile:

Let me know how it goes!

Userlevel 6
Badge +1

@RonH @mstone 

It’s now possible to remove custom user roles via API :) https://api2-eu-west-1.insided.com/docs/user/#operation/revokeRole

Hi

@daniel.boon I tried to use 

https://api2-eu-west-1.insided.com/docs/user/#operation/revokeRole

 

DELETE https://api2-eu-west-1.almostinsided.com/user/22005/role/Summit

and got response 404

 

roleName “Summit” and userId obtained from fetch user request and also exists in UI. Adding role works.

 

Can you help?

Userlevel 6
Badge +1

Hi

@daniel.boon I tried to use 

https://api2-eu-west-1.insided.com/docs/user/#operation/revokeRole

 

DELETE https://api2-eu-west-1.almostinsided.com/user/22005/role/Summit

and got response 404

 

roleName “Summit” and userId obtained from fetch user request and also exists in UI. Adding role works.

 

Can you help?

Just to close the loop for anyone else who was interested: revoking roles should now be working as expected via the endpoint

Userlevel 2
Badge +1

@daniel.boon - are we able to revoke roles with spaces? For example “Supported User” is one of our roles. I am able to apply the role with the POST command, but cannot delete the role. I have tested with single-word roles and my code works in that case. 

Userlevel 6
Badge +1

I think I know the solution there @alexandra.culler .

Depending on the language your code is in, you may need to use a placeholder such as putting the Supported User reference in either double quotes or doing it as something like Supported%20User as %20 is a placeholder for space. There’s usually a way of doing it somehow! It could also be a bug though.

Userlevel 2
Badge +1

Thanks @Blastoise186 but I have tried a few different methods like the one you suggest. We used quotation marks and apostrophes. We used spaces, %20, “-”, and “+”. None of these have worked so far :(.

Userlevel 6
Badge +1

Hey @alexandra.culler - it seems that support believe this may be a bug (thanks for raising it to them!). Support will keep you up to date on how to solve this :).

Reply