简体   繁体   English

将角色与用户关联 Microsoft Dynamics CRM (Rest API)

[英]Associate Role to a User Microsoft Dynamics CRM (Rest API)

I have use case where I need to create a role, create a user in crm instance and associate role to user.我有一个用例,我需要创建一个角色,在 crm 实例中创建一个用户并将角色关联到用户。

I have explored api to create user and create role.我已经探索了 api 来创建用户和创建角色。

Below is the code :下面是代码:

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);
        }
    }

I'm unable to find any Rest API to associate a user to a role.我找不到任何 Rest API 来将用户与角色相关联。 I have seen soap API's but I didn't see any rest APIs.我见过soap API,但没有看到任何rest API。 I have explored in Dynamics CRM docs I have not seen anything related to role association to the entity.我在 Dynamics CRM 文档中进行了探索,但没有看到与实体的角色关联相关的任何内容。 Do any one know any rest api to associate role to user?有没有人知道将角色与用户相关联的任何休息 api?

You can use the Web API to send a request to associate the user with a given role.您可以使用 Web API 发送将用户与给定角色关联的请求。

The relationship between user and role is called systemuserroles_association .用户和角色之间的关系称为systemuserroles_association You should thus send a request of the following format:因此,您应该发送以下格式的请求:

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)"  
}  

Does this work for multiple roles? 这对多个角色有用吗?

{
"@odata.id":"[Organization URI]/api/data/v9.0/roles( [**roleId1**] )"  
"@odata.id":"[Organization URI]/api/data/v9.0/roles( [**roleId2**] )"  
"@odata.id":"[Organization URI]/api/data/v9.0/roles( [**roleId3**] )"  
"@odata.id":"[Organization URI]/api/data/v9.0/roles( [**roleId4**] )"  
"@odata.id":"[Organization URI]/api/data/v9.0/roles( [**roleId5**] )"  
"@odata.id":"[Organization URI]/api/data/v9.0/roles( [**roleId6**] )"  
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 How to Create Case in Microsoft Dynamics CRM 2016 (Version 8) using webservices or REST API in JAVA or C#? - How to Create Case in Microsoft Dynamics CRM 2016 (Version 8) using webservices or REST API in JAVA or C#? 尝试通过java中的web api连接Microsoft dynamics crm - trying to connect Microsoft dynamics crm by web api in java 将 java 连接到 Microsoft Dynamics crm - connecting java to microsoft dynamics crm 401- 使用 REST API Dynamics CRM 和来自 Spring Boot 应用程序的 Azure AD 的未经授权的身份验证 - 401- Unauthorized authentication using REST API Dynamics CRM with Azure AD from a Spring Boot app 进行POST呼叫以验证Microsoft Dynamics CRM时出现证书错误 - Certificate error while making a POST call for authenticating microsoft dynamics crm Android Microsoft Dynamics CRM adal4j登录问题 - Android Microsoft dynamics CRM adal4j Login Issue 从JAVA在线调用Microsoft Dynamics CRM 2011 - Calling Microsoft Dynamics CRM 2011 online from JAVA 使用Java连接到Microsoft Dynamics CRM内部部署Web服务? - Connecting to Microsoft Dynamics CRM on-premise web service with Java? 如何通过REST API关联两个实体 - How to associate two entities via REST API 通过Java API激活和取消激活Dynamics CRM中的实体 - Activating and Deactivating Entities in Dynamics CRM from Java API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM