Get the roles assigned to a user with the Go SDK
Permit.Api.Users.GetAssignedRoles() returns one page of the role assignments that a user holds in one tenant. This reference is for Go developers who read a user's roles from backend code.
Users.GetAssignedRoles signature
func (u *Users) GetAssignedRoles(ctx context.Context, userKey string, tenantKey string, page int, perPage int) ([]models.RoleAssignmentRead, error)
Users.GetAssignedRoles parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx | context.Context | Yes | The context of the request. |
userKey | string | Yes | The key of the user whose role assignments you want. |
tenantKey | string | Yes | The key of the tenant to read role assignments from. Use default for the default tenant. |
page | int | Yes | The page number to fetch, starting at 1. |
perPage | int | Yes | The number of role assignments per page, from 1 to 100. |
Example: get a user's roles with Users.GetAssignedRoles
The example uses a client named Permit, created with permit.NewPermit() as shown in Check permissions with the Go SDK, and a ctx of type context.Context. The call fetches the first 10 role assignments of user-key in the default tenant:
roleAssignments, err := Permit.Api.Users.GetAssignedRoles(ctx, "user-key", "default", 1, 10)
Users.GetAssignedRoles return value and errors
On success, Users.GetAssignedRoles returns a []models.RoleAssignmentRead. Each item describes one role assignment, including the role and the tenant. If the user has no role assignments in the tenant, the slice is empty.
Users.GetAssignedRoles checks the pagination values before it calls the Permit API. If page is less than 1, or perPage is less than 1 or greater than 100, err holds an errors.PermitError with the ErrorCode PaginationError.
If the Permit API call fails, err holds an errors.PermitError from the github.com/permitio/permit-golang/pkg/errors package. Its StatusCode field has the HTTP status, and its ErrorCode field has one of these codes:
ErrorCode | Cause |
|---|---|
UnprocessableEntityError | HTTP 422: an argument failed validation. |
Unauthorized, ForbiddenAccess | HTTP 401 or 403: the API key is invalid or has no access to the environment. |
UnexpectedError | A server error (HTTP 5xx) or a network error. |