跳到主要内容

AccountHelper

AccountHelper 提供账户管理相关的能力,包括身份(Profile)和角色(Role)的分配与管理,以及人员信息的查询。

概述​

在 Chameleon 平台中,一个用户可以拥有多个身份(如"部门经理"、"项目经理")和多个角色(如"管理员"、"普通员工")。AccountHelper 提供了对这些关系的完整 CRUD 操作。

API 方法​

当前用户属性​

// 当前登录人的组织编码(未登录则使用默认组织)
string CurrentUserOrganization { get; }

// 当前登录人的身份列表(一个人可有多个身份)
string[] CurrentUserProfiles { get; }

// 是否正式人员
bool IsFormalUser { get; }

使用示例:

// 获取当前用户的组织
var org = sdk.AccountHelper.CurrentUserOrganization;

// 获取当前用户的所有身份
var profiles = sdk.AccountHelper.CurrentUserProfiles;

// 判断是否正式人员
if (sdk.AccountHelper.IsFormalUser)
{
// 正式人员的业务逻辑...
}

身份管理​

AssignProfileAsync — 分配身份​

为指定人员分配身份。

Task AssignProfileAsync(AssignProfileRequest request);

示例:

await sdk.AccountHelper.AssignProfileAsync(new AssignProfileRequest
{
ProfileId = "manager-profile-id",
UserIds = new[] { "user-001", "user-002" }
});

RemoveProfileAsync — 移除身份​

移除指定人员的身份。

Task RemoveProfileAsync(AssignProfileRequest request);

示例:

await sdk.AccountHelper.RemoveProfileAsync(new AssignProfileRequest
{
ProfileId = "manager-profile-id",
UserIds = new[] { "user-001" }
});

角色管理​

AssignRoleAsync — 分配角色​

为指定人员分配角色。

Task AssignRoleAsync(AssignRoleRequest request);

示例:

await sdk.AccountHelper.AssignRoleAsync(new AssignRoleRequest
{
RoleId = "admin-role-id",
UserIds = new[] { "user-001" }
});

RemoveRoleAsync — 移除角色​

移除指定人员的角色。

Task RemoveRoleAsync(AssignRoleRequest request);

人员查询​

GetUserIdsByProfileIdsAsync — 根据身份查人员​

根据身份 ID 集合获取对应的人员信息 ID 集合。

Task<string[]> GetUserIdsByProfileIdsAsync(GetUserIdsByProfileIdsRequest request);

示例:

// 查找具有"经理"身份的所有人员
var userIds = await sdk.AccountHelper.GetUserIdsByProfileIdsAsync(
new GetUserIdsByProfileIdsRequest
{
ProfileIds = new[] { "manager-profile-id", "director-profile-id" }
}
);

GetUserIdsByRoleIdsAsync — 根据角色查人员​

根据角色 ID 集合获取对应的人员信息 ID 集合。

Task<string[]> GetUserIdsByRoleIdsAsync(GetUserIdsByRoleIdsRequest request);

GetUserProfileIdsAsync — 查询人员的身份​

获取指定人员具有的身份 ID 集合。

// 查询单个人员
Task<GetUsersProfileIdsResponse> GetUserProfileIdsAsync(string userId);

// 批量查询
Task<List<GetUsersProfileIdsResponse>> GetUsersProfileIdsAsync(GetUsersProfileIdsRequest request);

示例:

// 查询某用户拥有的所有身份
var profiles = await sdk.AccountHelper.GetUserProfileIdsAsync("user-001");
// profiles.ProfileIds 包含该用户的所有身份 ID

GetUserRoleIdsAsync — 查询人员的角色​

获取指定人员具有的角色 ID 集合。

// 查询单个人员
Task<GetUsersRoleIdsResponse> GetUserRoleIdsAsync(string userId);

// 批量查询
Task<List<GetUsersRoleIdsResponse>> GetUsersRoleIdsAsync(GetUsersRoleIdsRequest request);

典型场景​

新员工入职自动分配身份​

public async Task<object> ExecuteAsync(ComponentContext context, ChameleonSDK sdk)
{
// 新增数据后,为新员工分配默认身份
var userId = context.CurrentEntity.Id;

await sdk.AccountHelper.AssignProfileAsync(new AssignProfileRequest
{
ProfileId = "employee-profile-id",
UserIds = new[] { userId }
});

return new { success = true };
}

根据身份查找相关人员​

// 查找所有具有"财务审核"身份的人员,用于审批流转
var approvers = await sdk.AccountHelper.GetUserIdsByProfileIdsAsync(
new GetUserIdsByProfileIdsRequest
{
ProfileIds = new[] { "finance-reviewer-profile-id" }
}
);

相关文档​