Skip to main content

Assign a resource role to a user with the Go SDK

Permit.Api.Users.AssignResourceRole() assigns a resource role to a user on one resource instance, such as a single document. Resource roles are the roles you define on a resource for relationship-based access control (ReBAC). This reference is for Go developers who grant instance-level permissions from backend code. To assign a role that applies across a whole tenant, use Users.AssignRole.

Users.AssignResourceRole signature

func (u *Users) AssignResourceRole(ctx context.Context, userKey string, roleKey string, tenantKey string, resourceInstance string) (*models.RoleAssignmentRead, error)

Users.AssignResourceRole parameters

ParameterTypeRequiredDescription
ctxcontext.ContextYesThe context of the request.
userKeystringYesThe key of the user who gets the role.
roleKeystringYesThe key of the resource role to assign, as defined on the resource type.
tenantKeystringYesThe key of the tenant that the resource instance belongs to.
resourceInstancestringYesThe resource instance, in the format <resource-type-key>:<instance-key>. For example, document:doc1 is the instance doc1 of the resource type document.

Example: assign a resource role with Users.AssignResourceRole

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 assigns the role role-key to the user user-key on the document doc1 in the tenant tenant-key:

resourceRoleAssignment, err := Permit.Api.Users.AssignResourceRole(ctx, "user-key", "role-key", "tenant-key", "document:doc1")

Users.AssignResourceRole return value and errors

On success, Users.AssignResourceRole returns a *models.RoleAssignmentRead that describes the new role assignment, including the resource instance.

If the 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
NotFoundHTTP 404: the Permit API can't find an object that the call references.
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.