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
| Parameter | Type | Required | Description |
|---|---|---|---|
ctx | context.Context | Yes | The context of the request. |
userKey | string | Yes | The key of the user who gets the role. |
roleKey | string | Yes | The key of the resource role to assign, as defined on the resource type. |
tenantKey | string | Yes | The key of the tenant that the resource instance belongs to. |
resourceInstance | string | Yes | The 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:
ErrorCode | Cause |
|---|---|
NotFound | HTTP 404: the Permit API can't find an object that the call references. |
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. |