Skip to content

Retrieving Users By Role

Description

All users have different roles assigned to them, those can be default system roles assigned by the system and also custom roles. By invoking this operation you can find users by a custom role; the result of this operation is an array of BackendlessUser objects found by a specific role.  This operation works only with custom roles, otherwise an error message will be returned in response.

Method

Backendless.UserService.findByRole(roleName, loadRoles, query): Promise<Backendless.User[]>;

where

Argument                Description
roleName is a string name of a particular role. String value.
loadRoles Optional parameter. When set to true, each user in a returned value will have a list of roles associated with that user account. Boolean value.
query Optional parameter. Must be an instance of Backendless.DataQueryBuilder. The returned list of users will satisfy the query.

Refer to the General Object Retrieval API topic to learn more about the queryobject.

Return Value

An array containing multiple BackendlessUser objects.

Example

The example below retrieves all users associated with the user role "TrialUser".

Retrieve Users By Specific Role

const result = await Backendless.UserService.findByRole("TrialUser")
console.log(result)

Retrieve Users By Specific Role And Get All Their Roles

const result = await Backendless.UserService.findByRole("TrialUser", true)

console.log(result)
/*
[
  {
    ___class: 'Users',
    name    : 'nick-test-role',
    objectId: '14F0E678-3C6C-4C8F-8B80-4714E1A7A94A',
    roles   : [
      'TrialUser',
      'testRole1'
    ]
  }
]
*/

Retrieve Users Matching a Query By Specific Role

const result = await Backendless.UserService.findByRole("TrialUser", true, { where: 'age > 18' })

console.log(result)
/*
[
  {
    ___class: 'Users',
    age     : 20,
    name    : 'nick-test-role',
    objectId: '14F0E678-3C6C-4C8F-8B80-4714E1A7A94A',
    roles   : [
      'testRoleName',
      'testRole1'
    ]
  }
]
*/

Retrieve Users Matching a Query By Specific Role 2

const query = Backendless.DataQueryBuilder.create()
query.setWhereClause('age > 18')

Backendless.UserService.findByRole("TrialUser", true, query)

Codeless Reference

user_service_codeless_get_users_by_role

where:

Argument                Description
role name A string name of a particular role.
load roles Optional parameter. When set to true, this operation returns a list containing all security roles associated with every returned user.
relations Optional parameter. Sets an array of related columns names. Objects from the related columns are included into the response.
properties Optional parameter. Sets the names of the properties which values to include into the response.
relations depth Optional parameter. Sets the number of "levels" in the hierarchy of related objects to include into the response.
page size Sets the page size - which is the number of objects to return in the response. Maximum value is 100.
page offset Optional parameter. Sets the offset - an index in the server-side storage from where the data objects should be retrieved.

Returns a list containing multiple BackendlessUser objects.

Consider the following records in the Users data table:

user_service_example_get_users_by_role_new_1

Suppose the Alice and Charlie records both have the "TrialUser" role assigned to them; the example below retrieves users by role "TrialUser".

user_service_example_get_users_by_role_new_2

The result of this operation is a list containing two objects, representing the Alice and Charlie records in the data table. These records have the "TrialUser" role assigned to them:

user_service_example_get_users_by_role_new_3

For more details about the related data tables refer to the Relations Overview topic.

Sometimes you may want to retrieve additional data from the related table using the relations property. This argument expects a list containing column(s) that reference records in the related data table.

Consider the following records in the Users data table.  Note that the data column is related to the UserData table where additional user information is stored. You need to use relation values  stored in the data column in your requests to retrieve additional information from the UserData table.


user_service_codeless_example_get_users_by_role_1

Consider the UserData table below, it is the custom data table where additional information is stored. Every single record in this data table is related(linked) to another record stored in the Users data table presented above.

user_service_codeless_example_get_users_by_role_4

In the context of this example the email column represents the identity. The UserData table contains information stored in the age, address and phone columns.

The example below obtains users from the Users data table that have the "TrialUser" role assigned to them. Pay attention how additional user information is obtained from the custom table called UserData: the relations parameter expects the column name that has relations to the UserData table; In this case the name of the column that has relations is data.

To limit the number of columns returned in the response, you must pass a list to the properties parameter, containing column names that you want to have in the response (e.g. email, name).

user_service_codeless_example_get_users_by_role_2

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

As you can see the operation has returned two BackendlessUser objects containing user information.  In the "userData" property you can find an object containing additional(related) user information returned from the UserData table.