TeamCity REST API: Add user to group?

Could someone please provide an example of what the REST API call would be used to add a user to a userGroup? 

Either type of example would work for our use case:

  • Add a user to an existing group during the process of creating the user. 
  • Add an existing user to an existing group.

There was a previous post alluding to the fact that the first option was possible however it did not provide an example of how it was possible. 

6 comments
Comment actions Permalink

Hi Michael,

 

The users element has the "groups" property. It includes the groups it belongs to. If you pass the groups when creating the user with the mandatory "KEY" parameter the user will automatically be added to the groups.

Adding an existing user to a group, it should be simple. You can post to the "app/rest/users/<user locator>/groups" to add a user to a group. The endpoint will accept a "group" object, which will be an instance of the groups returned by requesting a GET to the same endpoint.

1
Comment actions Permalink

I'm having to assign a user to a group programmatically and as I was doing my research, I came across this thread. I understand the URL to use and POST as HTTP method but I'm not sure about "group" object. So I would need to pass the "group" object as body the to POST request right? I'm guessing it's some kind of JSON data but I'm not sure how to form it. Could you please help me out on this one? Is there any example I could take a look at?

0
Comment actions Permalink

You'll need the group key for the target group. You can find your group keys here: http://TEAMCITY_SERVER/httpAuth/app/rest/userGroups

Below is an example using curl:

curl -v -u USERNAME:PASSWORD --request POST --header "Content-Type: application/xml" http://TEAMCITY_SERVER/httpAuth/app/rest/users/username:USERNAME/groups --data "<group key='GROUP_KEY'/>"
0
Comment actions Permalink

Hello, Eric. Thank you very much for your help.

I was able to successfully add an existing user to an existing group with a few caveats.

  1. I wanted to do it with JSON but I got HTTP 415 because it's not supported, so I used application/xml based on your suggestion.
  2. CSRF was enabled on the server I was working on, so added Origin key and its value to the HTTP header.

After that, all is good. 

Again, thank you very much for your help! :)

0
Comment actions Permalink

Hi,
is there similar way to 

curl -v -u USERNAME:PASSWORD --request POST --header "Content-Type: application/xml" http://TEAMCITY_SERVER/httpAuth/app/rest/users/username:USERNAME/groups --data "<group key='GROUP_KEY'/>"

how to remove a user from a group?

 

0
Comment actions Permalink

Sure, you can remove a user from a group with REST API using something like this:

curl -v -u USERNAME:PASSWORD --request DELETE http://TEAMCITY_SERVER/httpAuth/app/rest/users/username:USERNAME/groups/key:GROUP_KEY
0

Please sign in to leave a comment.