Skip to content
Docs
Roles

Assign role to user

Assign role to user

zones.users.roles.assign(struser_id, RoleAssignParams**kwargs) -> RoleAssignment
POST/zones/{zoneId}/users/{userId}/roles

Assigns a role to the user. Provide exactly one of role_id or role_identifier; when role_identifier is used, owner_type is required to disambiguate roles that share an identifier across owner types (and must be omitted with role_id). An optional (scope_type, scope_id) pair scopes the grant; only platform roles on the org zone may carry a scope, and a zone scope must reference a different zone in the same organization.

ParametersExpand Collapse
zone_id: str
user_id: str
owner_type: Optional[Literal["platform", "customer"]]

Owner type of the role to assign. Required with role_identifier (an identifier is unique only per owner type); must be omitted with role_id.

Accepts one of the following:
"platform"
"customer"
role_id: Optional[str]

ID of the role to assign. Provide exactly one of role_id or role_identifier; owner_type must be omitted when role_id is used.

role_identifier: Optional[str]

Role identifier: a lowercase slug (letters and digits separated by single hyphens or underscores), unique per owner type within a zone. Role identifiers surface in policy evaluation, so the slug restriction keeps them unambiguous in policy text.

minLength1
maxLength255
scope_id: Optional[str]

The ID of the resource to scope the grant to. Provide together with scope_type, or omit both for an unscoped assignment. When scope_type is zone, this must reference a different zone in the same organization.

minLength1
scope_type: Optional[str]

The kind of resource to scope the grant to (e.g. zone). Provide together with scope_id, or omit both for an unscoped assignment (applies to the owning zone itself). Only platform roles on the org zone may carry a scope.

minLength1
ReturnsExpand Collapse
class RoleAssignment:

Represents a role assigned to a principal within a zone

id: str

Unique identifier of the role assignment

created_at: datetime

Entity creation timestamp

formatdate-time
principal_id: str

ID of the principal the role is assigned to (a user, application, or group ID).

principal_type: str

The kind of principal the role is assigned to: user, application, or group. A role assigned to a group is inherited by that group's members.

role_id: str

ID of the assigned role

role_identifier: str

Role identifier: a lowercase slug (letters and digits separated by single hyphens or underscores), unique per owner type within a zone. Role identifiers surface in policy evaluation, so the slug restriction keeps them unambiguous in policy text.

minLength1
maxLength255
role_owner_type: Literal["platform", "customer"]

Owner type of the assigned role. Disambiguates roles that share an identifier across owner types.

Accepts one of the following:
"platform"
"customer"
updated_at: datetime

Entity update timestamp

formatdate-time
zone_id: str

Zone this assignment belongs to

scope_id: Optional[str]

The ID of the scoped resource. Null when the assignment is unscoped.

scope_type: Optional[str]

The kind of resource this grant is scoped to (e.g. zone). Null when the assignment is unscoped (applies to the owning zone itself).

Assign role to user

import os
from keycardai_api import KeycardAPI

client = KeycardAPI(
    api_key=os.environ.get("KEYCARD_API_API_KEY"),  # This is the default and can be omitted
)
role_assignment = client.zones.users.roles.assign(
    user_id="userId",
    zone_id="zoneId",
)
print(role_assignment.id)
{
  "id": "id",
  "created_at": "2019-12-27T18:11:19.117Z",
  "principal_id": "principal_id",
  "principal_type": "principal_type",
  "role_id": "role_id",
  "role_identifier": "role_identifier",
  "role_owner_type": "platform",
  "updated_at": "2019-12-27T18:11:19.117Z",
  "zone_id": "zone_id",
  "scope_id": "scope_id",
  "scope_type": "scope_type"
}
Returns Examples
{
  "id": "id",
  "created_at": "2019-12-27T18:11:19.117Z",
  "principal_id": "principal_id",
  "principal_type": "principal_type",
  "role_id": "role_id",
  "role_identifier": "role_identifier",
  "role_owner_type": "platform",
  "updated_at": "2019-12-27T18:11:19.117Z",
  "zone_id": "zone_id",
  "scope_id": "scope_id",
  "scope_type": "scope_type"
}