<span id="配置角色继承"></span>

# Configure Role Inheritance

Add or remove a parent role for a child role in a workspace and inspect the inheritance graph to verify permission propagation.

(sdk-ai-studio-role-inheritance-flow)=
<span id="任务流程"></span>

## Task flow

```text
Workspace IAM handle
         │
         ▼
1. Read current lifecycle versions of the child and parent roles
         │
         ▼
2. Submit request to add (or remove) the parent role
         │
         ▼
3. Read role inheritance graph ──▶ Verify directed edges (child -> parent)
```

(sdk-ai-studio-role-inheritance-prepare)=
<span id="准备"></span>

## Prerequisites

1. Complete the endpoint, personal access token, and workspace setup in [Use the SDK](../../../quickstart/ai-studio/sdk.md).
2. Ensure the current identity has role management permission in the target workspace.
3. Determine the child role ID and parent role ID; list the workspace role directory as shown in [Manage Roles and Object Permissions](管理角色和权限.md#sdk-ai-studio-iam-s1).
4. Generate a unique request ID for each mutation and retain it to check status if a network timeout leaves the outcome uncertain.

(sdk-ai-studio-role-inheritance-add)=
<span id="添加继承关系"></span>

## Add role inheritance

Read the current permission views of both the child role and parent role to obtain their expected lifecycle versions, submit the inheritance addition request, and read back the inheritance graph. Directed edges point from child role to parent role, and the server validates lifecycle states and rejects cycles.

:::::{tab-set}
:sync-group: sdk-language

::::{tab-item} Python
:sync: python

```python
import os
import uuid

import moi_product_sdk as sdk

client = sdk.new_with_personal_access_token(
    os.environ["PRODUCT_API_BASE_URL"],
    os.environ["PRODUCT_API_KEY"],
)
iam = client.iam(os.environ["WORKSPACE_ID"])
child_id = os.environ["CHILD_ROLE_ID"]
parent_id = os.environ["PARENT_ROLE_ID"]

child = iam.role_permission_view(child_id)
parent = iam.role_permission_view(parent_id)

added = iam.add_role_inheritance(
    child_id,
    sdk.IAMRoleInheritanceMutationRequest(
        parent_role_id=parent_id,
        expected_child_lifecycle_version=child.expected_role_lifecycle_version,
        expected_parent_lifecycle_version=parent.expected_role_lifecycle_version,
        request_id=str(uuid.uuid4()),
    ),
)
print("Added edge:", added.child_role_id, "->", added.parent_role_id)

graph = iam.role_inheritance_graph(child_id)
for edge in graph.edges:
    print(edge)
```

::::

::::{tab-item} Go
:sync: go

```go
package main

import (
	"context"
	"fmt"
	"os"
	"time"

	sdk "github.com/matrixorigin/matrixflow/sdk/go-sdk"
)

func main() {
	ctx := context.Background()
	client, err := sdk.NewWithPersonalAccessToken(
		os.Getenv("PRODUCT_API_BASE_URL"),
		os.Getenv("PRODUCT_API_KEY"),
	)
	if err != nil {
		panic(err)
	}
	iam := client.IAM(os.Getenv("WORKSPACE_ID"))
	childID := os.Getenv("CHILD_ROLE_ID")
	parentID := os.Getenv("PARENT_ROLE_ID")

	child, err := iam.RolePermissionView(ctx, childID)
	if err != nil {
		panic(err)
	}
	parent, err := iam.RolePermissionView(ctx, parentID)
	if err != nil {
		panic(err)
	}

	added, err := iam.AddRoleInheritance(ctx, childID, &sdk.IAMRoleInheritanceMutationRequest{
		ParentRoleId:                   parentID,
		ExpectedChildLifecycleVersion:  child.GetExpectedRoleLifecycleVersion(),
		ExpectedParentLifecycleVersion: parent.GetExpectedRoleLifecycleVersion(),
		RequestId:                      fmt.Sprintf("role-inherit-add-%d", time.Now().UnixNano()),
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("Added edge:", added.GetChildRoleId(), "->", added.GetParentRoleId())

	graph, err := iam.RoleInheritanceGraph(ctx, childID)
	if err != nil {
		panic(err)
	}
	for _, edge := range graph.GetEdges() {
		fmt.Println(edge)
	}
}
```

::::

:::::

(sdk-ai-studio-role-inheritance-remove)=
<span id="移除继承关系"></span>

## Remove role inheritance

To detach an existing parent role from a child role, re-read the latest lifecycle versions of both roles, submit the removal request, and inspect the inheritance graph to confirm the edge has been removed. On a version conflict, fetch updated role versions and review concurrent changes before retrying.

:::::{tab-set}
:sync-group: sdk-language

::::{tab-item} Python
:sync: python

```python
child = iam.role_permission_view(child_id)
parent = iam.role_permission_view(parent_id)

removed = iam.remove_role_inheritance(
    child_id,
    sdk.IAMRoleInheritanceMutationRequest(
        parent_role_id=parent_id,
        expected_child_lifecycle_version=child.expected_role_lifecycle_version,
        expected_parent_lifecycle_version=parent.expected_role_lifecycle_version,
        request_id=str(uuid.uuid4()),
    ),
)
print("Removed edge:", removed.child_role_id, "->", removed.parent_role_id)
```

::::

::::{tab-item} Go
:sync: go

```go
child, err = iam.RolePermissionView(ctx, childID)
if err != nil {
	panic(err)
}
parent, err = iam.RolePermissionView(ctx, parentID)
if err != nil {
	panic(err)
}

removed, err := iam.RemoveRoleInheritance(ctx, childID, &sdk.IAMRoleInheritanceMutationRequest{
	ParentRoleId:                   parentID,
	ExpectedChildLifecycleVersion:  child.GetExpectedRoleLifecycleVersion(),
	ExpectedParentLifecycleVersion: parent.GetExpectedRoleLifecycleVersion(),
	RequestId:                      fmt.Sprintf("role-inherit-rm-%d", time.Now().UnixNano()),
})
if err != nil {
	panic(err)
}
fmt.Println("Removed edge:", removed.GetChildRoleId(), "->", removed.GetParentRoleId())
```

::::

:::::

(sdk-ai-studio-role-inheritance-next)=
<span id="下一步"></span>

## Next steps

- [Manage Roles and Object Permissions](管理角色和权限.md)
- [Delete Roles and Resolve Dependencies](删除角色并处理依赖.md)
