Skip to main content

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

ParameterTypeRequiredDescription
ctxcontext.ContextYesThe context of the request.
userKeystringYesThe key of the user whose role assignments you want.
tenantKeystringYesThe key of the tenant to read role assignments from. Use default for the default tenant.
pageintYesThe page number to fetch, starting at 1.
perPageintYesThe 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:

ErrorCodeCause
UnprocessableEntityErrorHTTP 422: an argument failed validation.
Unauthorized, ForbiddenAccessHTTP 401 or 403: the API key is invalid or has no access to the environment.
UnexpectedErrorA server error (HTTP 5xx) or a network error.