Skip to content

Retrieving User Roles By User ID

Description

This operation can be used to retrieve custom and system roles assigned to a user.

This functionality can be used only in CloudCode (Java, JS or Codeless), the reason for this restriction is that a malicious use of this API can easily compromise application's security. As a result, this API must be used from a controlled environment.

Method

Non-Blocking API

public void Backendless.UserService.getUserRoles( String userId, AsyncCallback<List<String>> responder )

Blocking API

public List<String> Backendless.UserService.getUserRoles( String userId )

where:

Argument                Description
userId Mandatory string argument representing a unique objectId of a record in the Users data table. String value.

Return Value

Returns a collection of user roles, found by corresponding user ID.

Example

The example below retrieves all roles associated with the objectId: "4D584E4D-05A3-4AC4-90C7-B80D1584E7AD".

Non-Blocking API

Backendless.UserService.getUserRoles(
        "4D584E4D-05A3-4AC4-90C7-B80D1584E7AD",
        new AsyncCallback<List<String>>()
        {
          @Override
          public void handleResponse( List<String> roleNames )
          {
            // logic for processing retrieved role names
          }

          @Override
          public void handleFault( BackendlessFault backendlessFault )
          {
            // exception handling logic
          }
        }
);

Blocking API

List<String> roleNames = Backendless.UserService.getUserRoles( "4D584E4D-05A3-4AC4-90C7-B80D1584E7AD" );

Codeless Reference

user_service_codeless_get_user_roles_cloud

where:

Argument                Description
user id Unique identifier of the user account to retrieve all roles assigned to the user.

Important

This Codeless block is only available in CloudCode due to the security reasons. If this operation is exposed to users, then your application can be compromised.

Returns a list containing all roles associated with the user.

Consider the following record in the Users data table:

user_service_codeless_example_get_user_roles

The example below retrieves all roles of the user "alice@yourmail.com" associated with the user id(objectId): "``4D584E4D-05A3-4AC4-90C7-B80D1584E7AD``".

user_service_codeless_example_get_user_roles_cloud

The output will look as shown below after the Codeless logic runs:

user_service_codeless_example_get_user_roles_example_3