Manage Access On Digital iTwins

Introduction

This tutorial will take you through the process of creating roles, adding team members, and retrieving team members for a given iTwin.

By the end of this walk-through, you will be able to utilize the API endpoints in order to create roles, add & assign permissions to the role, invite users to an iTwin, and retrieve the list of iTwin members.

Info

Skill level:

Basic

Duration:

20 minutes

Prerequisites

This tutorial assumes that you already have:

  • A tool, such as Postman that can be used to execute HTTP calls. These calls can also be made using the Try it out button in the API documentation.
  • If a user is affiliated with an Organization, the user must be an Organization Administrator to create an iTwin. An Organization Administrator must have at least one of the following roles assigned in User Management: Account Administrator, Co-Administrator, or CONNECT Services Administrator. For more information about User Management please visit our Bentley Communities Licensing, Cloud, and Web Services wiki page.

1. Register an Application

You will need to register an application to use the iTwin Platform APIs. You can use the Register button to automatically create your first single page application (SPA). This will allow you to configure Authorization Code Flow for your SPA application and get the correct access token.

Once generated, you will be shown a few lines of code under the button.

  • IMJS_AUTH_CLIENT_CLIENT_ID - this is the unique identifier for your application. Displayed on application details page as Client ID.
  • IMJS_AUTH_CLIENT_REDIRECT_URI - specifies where users are redirected after they have chosen whether or not to authenticate your app. Displayed on application details page as one of Redirect URIs.
  • IMJS_AUTH_CLIENT_LOGOUT_URI - specifies where users can be returned to after logging out. Displayed on application details page as one of Post logout redirect URIs.
  • IMJS_AUTH_CLIENT_SCOPES - list of accesses granted to the application. Displayed on application details page as Scopes.

Or optionally: Register and configure your application manually following instructions in Register and modify an Application tutorial. Make sure that your application is associated with iTwins API and has iTwins:modify and iTwins:read scopes enabled.

Requires you to sign in. Will automatically generate a Single page application (SPA) that is required to complete this tutorial. You will be able to manage your SPA from your My apps page.

2. Get a token

Before you can make a request to the APIs, a user token is needed. There are several ways to get it.

Follow this article to implement Authorization code workflow in your application.

Here you can use Client ID generated from previous registration step.
  1. Go here
  2. Click Try it out button.
  3. Under the Authorization section, select authorizationCode from the dropdown.
  4. After the sign in popup closes, the Authorization header with token value should be visible beneath the HTTP request dropdown.
  5. Copy & paste the Authorization value for this tutorial.
Use user token to replace JWT_TOKEN dynamic parameter in the next steps.

3. Create iTwin Role

The Create iTwin role endpoint is used to create iTwin roles. After a role is created, permissions can be assigned.

The iTwin id from an existing iTwin (see create and query iTwins guide) is required to create a role. A display name and description are required in the POST body.

The POST call will return a new role instance. The returned role id along with the iTwin id will be used to add permissions in the next step.

Request Syntax


HTTP
POST https://api.bentley.com/accesscontrol/itwins/{id}/roles HTTP/1.1

Request Headers


HTTP
Accept: application/vnd.bentley.itwin-platform.v1+json
Content-Type: application/json
Authorization: Bearer JWT_TOKEN

Request Body


JSON
{
  "displayName":"iTwin Administrator",
  "description":"A description of iTwin Administrator"
}

Response Headers


HTTP
HTTP/1.1 201 Created
content-length: 203
content-type: application/json
date: Wed, 30 Jun 2021 17:26:10 GMT
request-context: appId=cid-v1:7a353d36-9a8b-423e-965e-9d7f51324584
x-correlation-id: 74e263d7-ded7-45a5-8347-ca1f98ffab86
x-itwinplatform-media-type: bentley.itwin-platform.v1
x-itwinplatform-region: East US

Response Body


JSON
{
  "role":{
    "id":"14b391cf-ce9e-43d3-8fee-c49e57c2680d",
    "displayName":"iTwin Administrator",
    "description":"A description of iTwin Administrator",
    "permissions":[]
  }
}

4. Update iTwins Role Using the iTwin Id and Role Id

To assign permissions, the Role needs to be updated. iTwin id and role id are required. The POST body should contain list of permissions.

The response will contain the role details along with the list of permissions assigned.

Request Syntax


HTTP
PATCH https://api.bentley.com/accesscontrol/itwins/{id}/roles/{roleId} HTTP/1.1

Request Headers


HTTP
Accept: application/vnd.bentley.itwin-platform.v1+json
Content-Type: application/json
Authorization: Bearer JWT_TOKEN

Request Body


JSON
{
      "displayName": "A new Role display name",
      "description": "A new Role description",
      "permissions":[
        "administration_invite_member",
        "administration_manage_roles",
        "administration_remove_member"
    ]
}

Response Headers


HTTP
HTTP/1.1 200 OK
content-length: 324
content-type: application/json
date: Wed, 30 Jun 2021 18:17:39 GMT
request-context: appId=cid-v1:7a353d36-9a8b-423e-965e-9d7f51324584
x-correlation-id: f16d2765-4a9c-40a4-995e-bd7c6b8b8d4a
x-itwinplatform-media-type: bentley.itwin-platform.v1
x-itwinplatform-region: East US

Response Body


JSON
{
  "role":{
    "id":"10a2d277-ea41-4e81-b309-f7a9aea09d51",
    "displayName":"iTwin Administrator",
    "description":"A description of iTwin Administrator",
    "permissions":[
      "administration_invite_member",
      "administration_manage_roles",
      "administration_remove_member"
    ]
  }
}

5. Query for Roles using the iTwin Id

iTwin Id can be used to retrieve a list of iTwin roles.

The api will return a list of roles for a given iTwin.

Request Syntax


HTTP
GET https://api.bentley.com/accesscontrol/itwins/{id}/roles HTTP/1.1

Request Headers


HTTP
Accept: application/vnd.bentley.itwin-platform.v1+json
Authorization: Bearer JWT_TOKEN

Response Headers


HTTP
HTTP/1.1 200 OK 
content-type: application/json
date: Mon, 29 Aug 2022 19:24:14 GMT
request-context: appId=cid-v1:7a353d36-9a8b-423e-965e-9d7f51324584
vary: Accept-Encoding
x-correlation-id: 260678e5-0244-413d-bc60-f6dac19fd450
x-itwinplatform-media-type: bentley.itwin-platform.v1
x-itwinplatform-region: East US
x-rate-limit-limit: 1s
x-rate-limit-remaining: 49
x-rate-limit-reset: 2022-08-29T19:24:15.3864257Z

Response Body


JSON
{
    "roles": [{
        "id": "5ba9eab1-9597-4f20-b360-4d79b9af511d",
        "displayName": "iTwin Administrator",
        "description": "The iTwin Administration Role",
        "permissions": [],
        "assignedDateTime": "0001-01-01T00:00:00"
    }, {
        "id": "fe117fcd-7cf0-4f95-829f-b4cc065dc6ed",
        "displayName": "iTwin Administrator 3",
        "description": "The iTwin Administration Role 3",
        "permissions": [],
        "assignedDateTime": "0001-01-01T00:00:00"
    }]
}

6. Add iTwin Team Member

The Add iTwin team member endpoint is used to add team members to a given iTwin.

iTwin id is a required parameter. User email and the role id are required in the POST body.

The POST call will only return the headers.

Request Syntax


HTTP
POST https://api.bentley.com/accesscontrol/itwins/{id}/members HTTP/1.1

Request Headers


HTTP
Accept: application/vnd.bentley.itwin-platform.v1+json
Content-Type: application/json
Authorization: Bearer JWT_TOKEN

Request Body


JSON
{
  "email":"John.Doe@bentley.com",
  "roleId": "5abbfcef-0eab-472a-b5f5-5c5a43df34b1"
}

Response Headers


HTTP
HTTP/1.1 201 Created
cache-control: must-revalidate, no-cache, private
content-encoding: gzip
content-type: application/json
date: Wed, 30 Jun 2021 19:30:40 GMT
mas-request-id: 744155a0-f492-44a9-b78d-3a8c516f53ee
mas-server: Bentley-WSG/4.0.12,Bentley-WebAPI/2.9
pragma: no-cache
request-context: appId=cid-v1:7a353d36-9a8b-423e-965e-9d7f51324584
vary: Accept-Encoding
x-correlation-id: d1999ea1-62bd-4e84-8e5d-1a8aac040de4
x-itwinplatform-media-type: bentley.itwin-platform.v1
x-itwinplatform-region: East US

Response Body


JSON
NO CONTENT

7. Query for Team Members using the iTwin Id

The Id query parameter can be used to retrieve a list of team members using the iTwin Id.

The api will return a list of team members for the given iTwin.

Request Syntax


HTTP
GET https://api.bentley.com/accesscontrol/itwins/{id}/members HTTP/1.1

Request Headers


HTTP
Accept: application/vnd.bentley.itwin-platform.v1+json
Authorization: Bearer JWT_TOKEN

Response Headers


HTTP
HTTP/1.1 200 OK 
content-length: 480
content-type: application/json
date: Wed, 30 Jun 2021 19:46:24 GMT
request-context: appId=cid-v1:7a353d36-9a8b-423e-965e-9d7f51324584
x-correlation-id: 7a199ff9-317f-4d4f-ba82-70ba25f31d32
x-itwinplatform-media-type: bentley.itwin-platform.v1
x-itwinplatform-region: East US

Response Body


JSON
{
    "members": [{
        "id": "56c58c0c-28a9-4cd2-b50b-102e33b657f2",
        "email": "Sam.Lanza@bentley.com",
        "givenName": "Sam",
        "surname": "Lanza",
        "organization": "Bentley Systems Inc",
        "roles": [{
            "id": "74da182b-57cb-41f7-9d3c-612f484ebb07",
            "displayName": "iTwin owner",
            "assignedDateTime": "2021-09-14T19:41:29.913"
        }]
    }, {
        "id": "08a2ede0-fc7d-4f52-996e-e1047feb27e9",
        "email": "John.Doe@bentley.com",
        "givenName": "John",
        "surname": "Doe",
        "organization": "Bentley Systems Inc",
        "roles": [{
            "id": "0af949d5-af90-4eed-84e0-d4d860f723d0",
            "displayName": "iTwin Administrator",
            "assignedDateTime": "2022-08-29T18:05:19.537846"
        }]
    }],
  "_links":{
    "self":{
      "href":"https://api.bentley.com/accesscontrol/efa557b7-ecd7-4a60-9589-2acce11f6cc9/members?$top=100&$skip=0"
    }
  }
}

More resources that you may like

Tutorial for creating and querying iTwins.