Skip to content

User Information

As an application developer, you can use UserRoles API to retrieve username, user-id, and roles associated with the user. SAP Cloud Platform Mobile Services allows administrators to assign various roles to a user, which can be used to build flexible UIs for mobile application based on the roles.

UserRoles.load gets information about the current user which includes username, user-id, and roles of the current user from Mobile Services. The user-id and the username of the user are always available. Roles are available only if the access control is enabled in the mobile services cockpit. This method must be invoked from the UI thread. The callback methods are invoked on the UI thread.

'SettingsParameters' class provides all the necessary information about the server URL and app-id. The authentication provider mechanism associated with the application security configuration in the cockpit provides the context for the user whose user information is retrieved.

Sample Usage

The following sample code shows how to use this API.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
SettingsParameters settingsParameters = new SettingsParameters(hostUrl, APP_ID,  "deviceId", "1.0");
UserRoles roles = new UserRoles(okHttpClient, settingsParameters);

roles.load(new UserRoles.CallbackListener() {

     @Override
     public void onSuccess(UserInfo o) {
        /*
        Here goes the code for processing successful response.
        UserInfo contains the user-id, username, and user-roles
        UserInfo is a parcelable object so, if you wish, this can
        be passed through bundle to different activities 
        */
        Log.i("User Name", o.getUserName());
        Log.i("User Id", o.getId());
        String[] roles = o.getRoles();
        Log.i("UserInfo: ", "User has the following Roles");
        for (int i = 0; i < roles.length; i++) {
            Log.i("Role Name", roles[i]);
        }
     }

     @Override
     public void onError(Throwable result) {
        //Handle error here...
        if (result instanceof HttpException) {
            //HttpException type com.sap.cloud.mobile.foundation.networking.HttpException
            HttpException ne = (HttpException) result;
            Log.e("Http Exception: ", ne.message() + ", with error code: " + ne.code());
        } else {
            Log.e("Exception occurred: ", result.getMessage());
         }
     }
  }
);
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
val settingsParameters = SettingsParameters(hostUrl, APP_ID, "deviceId", "1.0")
val roles = UserRoles(okHttpClient, settingsParameters)

roles.load(object : UserRoles.CallbackListener {

    override fun onSuccess(o: UserInfo) {
        /*
        Here goes the code for processing successful response.
        UserInfo contains the user-id, username, and user-roles
        UserInfo is a parcelable object so, if you wish, this can
        be passed through bundle to different activities
        */
        Log.i("User Name", o.userName)
        Log.i("User Id", o.id)
        val roles = o.roles
        Log.i("UserInfo: ", "User has the following Roles")
        for (i in roles.indices) {
            Log.i("Role Name", roles[i])
        }
    }

    override fun onError(result: Throwable) {
        //Handle error here...
        if (result is HttpException) {
            //HttpException type com.sap.cloud.mobile.foundation.networking.HttpException
            Log.e("Http Exception: ", result.message() + ", with error code: " + result.code())
        } else {
            Log.e("Exception occurred: ", result.message)
        }
    }
})