将角色关联到用户 Microsoft Dynamics CRM (Rest API)
Associate Role to a User Microsoft Dynamics CRM (Rest API)
我有一个用例需要创建一个角色,在 crm 实例中创建一个用户
并将角色关联到用户。
我探索了 api 创建用户和创建角色。
下面是代码:
private void createUser(IntegrationUserDTO integrationUserDTO, STSDto stsDetails, CRMAuthContext crmAuthContext)
throws IntegrationsException {
Map<String, Object> requestBody = new HashMap<>();
URI uri = new MSCRMHttpDelegate().odataUriBuilder(crmAuthContext.getCrmApiUrl())
.appendEntitySetSegment("systemusers").build();
HttpPost httpPost = new HttpPost(uri.toString());
httpPost.setHeader("Authorization", "Bearer " + crmAuthContext.getAccessToken());
httpPost.setHeader("Accept", MediaType.APPLICATION_JSON);
httpPost.setHeader("OData-MaxVersion", "4.0");
httpPost.setHeader("OData-Version", "4.0");
httpPost.setHeader("Content-Type", "application/json");
requestBody.put("accessmode", "4");
requestBody.put("applicationid", UUID.fromString(stsDetails.getClientId()));
requestBody.put("firstname", integrationUserDTO.getUsername());
requestBody.put("lastname", integrationUserDTO.getSecretToken());
requestBody.put("internalemailaddress", integrationUserDTO.getExtraParams());
requestBody.put("isintegrationuser", true);
MSCRMUser user = getBusinessUnitId(crmAuthContext);
if (StringUtils.isNoneBlank(user.getBusinessUnitId())) {
requestBody.put("businessunitid@odata.bind",
"/businessunits(" + UUID.fromString(user.getBusinessUnitId()) + ")");
}
if (StringUtils.isNoneBlank(user.getOrganizationId())) {
requestBody.put("organizationid", UUID.fromString(user.getOrganizationId()));
}
try {
httpPost.setEntity(new StringEntity(
new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(requestBody)));
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
if (response.getStatusLine().getStatusCode() >= 400) {
log.info("error in adding privileges to role at microsoft instance =");
throw new IntegrationsException(IntegrationsErrorCode.CRM_UNAUTHORIZED_ACCESS);
}
}
} catch (Exception e) {
throw new IntegrationsException(IntegrationsErrorCode.INTERNAL_ERROR, e);
}
}
private void createRole(IntegrationUserDTO integrationUserDTO, STSDto stsDetails, CRMAuthContext crmAuthContext)
throws IntegrationsException {
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("name", ROLE_NAME);
MSCRMUser user = getBusinessUnitId(crmAuthContext);
if (StringUtils.isNoneBlank(user.getBusinessUnitId())) {
requestBody.put("businessunitid@odata.bind",
"/businessunits(" + UUID.fromString(user.getBusinessUnitId()) + ")");
}
if (StringUtils.isNoneBlank(user.getOrganizationId())) {
requestBody.put("organizationid", UUID.fromString(user.getOrganizationId()));
}
URI uri = new MSCRMHttpDelegate().odataUriBuilder(crmAuthContext.getCrmApiUrl()).appendEntitySetSegment("roles")
.build();
HttpPost httpPost = new HttpPost(uri.toString());
httpPost.setHeader("Authorization", "Bearer " + crmAuthContext.getAccessToken());
httpPost.setHeader("Accept", MediaType.APPLICATION_JSON);
httpPost.setHeader("OData-MaxVersion", "4.0");
httpPost.setHeader("OData-Version", "4.0");
httpPost.setHeader("Content-Type", "application/json");
try {
httpPost.setEntity(new StringEntity(
new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(requestBody)));
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
if (response.getStatusLine().getStatusCode() >= 400) {
log.info("error in adding privileges to role at microsoft instance =");
throw new IntegrationsException(IntegrationsErrorCode.CRM_UNAUTHORIZED_ACCESS);
}
}
} catch (Exception e) {
throw new IntegrationsException(IntegrationsErrorCode.INTERNAL_ERROR, e);
}
}
我找不到任何 Rest API 来将用户与角色相关联。
我看过 soap API's,但我没有看到任何 rest APIs。我在 Dynamics CRM 文档中进行了探索,我没有看到与实体的角色关联相关的任何内容。有人知道将角色与用户相关联的其余部分 api 吗?
您可以使用 Web API 向 associate 具有给定角色的用户发送请求。
用户和角色之间的关系称为systemuserroles_association。因此,您应该发送以下格式的请求:
POST [Organization URI]/api/data/v9.0/systemusers(00000000-0000-0000-0000-000000000002)/systemuserroles_association/$ref HTTP/1.1
Content-Type: application/json
Accept: application/json
OData-MaxVersion: 4.0
OData-Version: 4.0
{
"@odata.id":"[Organization URI]/api/data/v9.0/roles(00000000-0000-0000-0000-000000000001)"
}
对其他人来说很容易
使用邮递员可以发送以下两种格式的请求:
我有一个用例需要创建一个角色,在 crm 实例中创建一个用户 并将角色关联到用户。
我探索了 api 创建用户和创建角色。
下面是代码:
private void createUser(IntegrationUserDTO integrationUserDTO, STSDto stsDetails, CRMAuthContext crmAuthContext)
throws IntegrationsException {
Map<String, Object> requestBody = new HashMap<>();
URI uri = new MSCRMHttpDelegate().odataUriBuilder(crmAuthContext.getCrmApiUrl())
.appendEntitySetSegment("systemusers").build();
HttpPost httpPost = new HttpPost(uri.toString());
httpPost.setHeader("Authorization", "Bearer " + crmAuthContext.getAccessToken());
httpPost.setHeader("Accept", MediaType.APPLICATION_JSON);
httpPost.setHeader("OData-MaxVersion", "4.0");
httpPost.setHeader("OData-Version", "4.0");
httpPost.setHeader("Content-Type", "application/json");
requestBody.put("accessmode", "4");
requestBody.put("applicationid", UUID.fromString(stsDetails.getClientId()));
requestBody.put("firstname", integrationUserDTO.getUsername());
requestBody.put("lastname", integrationUserDTO.getSecretToken());
requestBody.put("internalemailaddress", integrationUserDTO.getExtraParams());
requestBody.put("isintegrationuser", true);
MSCRMUser user = getBusinessUnitId(crmAuthContext);
if (StringUtils.isNoneBlank(user.getBusinessUnitId())) {
requestBody.put("businessunitid@odata.bind",
"/businessunits(" + UUID.fromString(user.getBusinessUnitId()) + ")");
}
if (StringUtils.isNoneBlank(user.getOrganizationId())) {
requestBody.put("organizationid", UUID.fromString(user.getOrganizationId()));
}
try {
httpPost.setEntity(new StringEntity(
new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(requestBody)));
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
if (response.getStatusLine().getStatusCode() >= 400) {
log.info("error in adding privileges to role at microsoft instance =");
throw new IntegrationsException(IntegrationsErrorCode.CRM_UNAUTHORIZED_ACCESS);
}
}
} catch (Exception e) {
throw new IntegrationsException(IntegrationsErrorCode.INTERNAL_ERROR, e);
}
}
private void createRole(IntegrationUserDTO integrationUserDTO, STSDto stsDetails, CRMAuthContext crmAuthContext)
throws IntegrationsException {
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("name", ROLE_NAME);
MSCRMUser user = getBusinessUnitId(crmAuthContext);
if (StringUtils.isNoneBlank(user.getBusinessUnitId())) {
requestBody.put("businessunitid@odata.bind",
"/businessunits(" + UUID.fromString(user.getBusinessUnitId()) + ")");
}
if (StringUtils.isNoneBlank(user.getOrganizationId())) {
requestBody.put("organizationid", UUID.fromString(user.getOrganizationId()));
}
URI uri = new MSCRMHttpDelegate().odataUriBuilder(crmAuthContext.getCrmApiUrl()).appendEntitySetSegment("roles")
.build();
HttpPost httpPost = new HttpPost(uri.toString());
httpPost.setHeader("Authorization", "Bearer " + crmAuthContext.getAccessToken());
httpPost.setHeader("Accept", MediaType.APPLICATION_JSON);
httpPost.setHeader("OData-MaxVersion", "4.0");
httpPost.setHeader("OData-Version", "4.0");
httpPost.setHeader("Content-Type", "application/json");
try {
httpPost.setEntity(new StringEntity(
new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create().toJson(requestBody)));
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
if (response.getStatusLine().getStatusCode() >= 400) {
log.info("error in adding privileges to role at microsoft instance =");
throw new IntegrationsException(IntegrationsErrorCode.CRM_UNAUTHORIZED_ACCESS);
}
}
} catch (Exception e) {
throw new IntegrationsException(IntegrationsErrorCode.INTERNAL_ERROR, e);
}
}
我找不到任何 Rest API 来将用户与角色相关联。 我看过 soap API's,但我没有看到任何 rest APIs。我在 Dynamics CRM 文档中进行了探索,我没有看到与实体的角色关联相关的任何内容。有人知道将角色与用户相关联的其余部分 api 吗?
您可以使用 Web API 向 associate 具有给定角色的用户发送请求。
用户和角色之间的关系称为systemuserroles_association。因此,您应该发送以下格式的请求:
POST [Organization URI]/api/data/v9.0/systemusers(00000000-0000-0000-0000-000000000002)/systemuserroles_association/$ref HTTP/1.1
Content-Type: application/json
Accept: application/json
OData-MaxVersion: 4.0
OData-Version: 4.0
{
"@odata.id":"[Organization URI]/api/data/v9.0/roles(00000000-0000-0000-0000-000000000001)"
}
对其他人来说很容易
使用邮递员可以发送以下两种格式的请求: