How to assign a user to a Group
Task
Assign a user to an individual Group
Find the Endpoint
Go to the API Reference section of this documentation and to the controller called Public User Group.
There you will find an endpoint called ‘/api/usergroup/create/member/group/{groupkey}’
If you click on the endpoint it will display more information.
This endpoint assigns a to a group.
Code
Here is an example code in Python (3.6.8) of how to assign a user to a group.
Note: This doesn’t include getting an Access Token to be able to communicate with the Bracken API. Please add the necessary code to get an Access Token.
# Declare your Groupkey
groupkey = YOUR_GROUPKEY
# using the endpoint to assign a user to a group
urlAssignUserToAGroup = 'https://api.brackenlearning.com/api/usergroup/create/member/group/{0}'.format(groupkey)
response = s.post(urlAssignUserToAGroup, json={
"userkey": 0,
"access": 0
})
print(response)
print(response.json())
The part to focus on for this tutorial is this line.
# using the endpoint to assign a user to a group
urlAssignUserToAGroup = 'https://api.brackenlearning.com/api/usergroup/create/member/group/{0}'.format(groupkey)
Which is where we use the endpoint that we found in the API Reference document to assign a user to a group. The groupkey
we pass as a parameter is the identifier of the group we are assigning the user to.
We assign a user to a group by posting our information to the endpoint.
response = s.post(urlAssignUserToAGroup, json={
"userkey": 0,
"access": 0
})
In the above code we can see mock information that we are passing as a JSON body to the ‘/api/usergroup/create/member/group/{groupkey}’ endpoint. If we look at our user again they are now a new member of that group.
Important to note
The userkey
is the unique identifier for a specific user and that is how we determine which user will be assigned to the group.
The access
variable is a set of group permissions that are defined as flags and allow for the user to have various permissions within the group. Please see Group Based Permissions for more information.
The parameter {groupkey} references the key of a particular group and only that group, unlike a multicode.
Response
The [200] response for this call will look like the example below. Please note the access
will default 0 for the Group Based Permissions unless set otherwise.
{
"groupkey": 0,
"title": "Usergroup.title ",
"userkey": 0,
"username": "Test-User",
"firstname": "Test",
"lastname": "User",
"created": "2020-11-24T23:17:53.703Z",
"access": 0,
"role": "string"
}