CRecordUserDbAddRolesToUser

Assign roles to a user.

This message belongs to the Session Manager API.
The Session Manager is part of the NY_Session2PlugIn plugin.

{
  "id": "e43a6fa3-5421-4d6c-866a-6f69358e9c2a",
  "name": "USER_DB_ADD_ROLES_TO_USER",
  "description": "Add some roles to a user.",
  "slots": [
    {
      "key": "1",
      "name": "USER_ID",
      "direction": "REQUEST",
      "mandatory": "true",
      "type": "STRING",
      "description": "The user id."
    },
    {
      "key": "2",
      "name": "ROLES",
      "direction": "REQUEST",
      "mandatory": "true",
      "type": "STRING_ARRAY",
      "description": "The roles."
    }
  ]
}

Usage

Sending the request

You need the microservice ID of the session manager:

public static final IId SESSION_MICROSERVICE_ID = CIdFactory.fromObject("ccf168c1-f18b-4229-85f9-24461a19ee6a");

The own SessionToken is needed to verify the authorization for the change. You need the permission NY_AddRoleToUser. In addition, the user ID and the list of roles are required.

private void assignRolesToUser(final byte[] aToken,
                               @NotNull final String aUserId,
                               @NotNull final String[] aRoles) throws CException
{
    final CEnvelope env = CEnvelope.forMicroService(SESSION_MICROSERVICE_ID);
    env.setSessionToken(aToken);

    final CRecord record = CRecordUserDbAddRolesToUser.create();
    CRecordUserDbAddRolesToUser.setUserId(record,
                                          aUserId);
    CRecordUserDbAddRolesToUser.setRoles(record,
                                         aRoles);

    sendRequest(env,
                record);
}

Dealing with the response

To catch the response of the request, we need a message handler. We add it in the constructor of the message handler registry.

// constructor:
addMessageHandler(CRecordUserDbAddRolesToUser.ID,
                  this::asyncAddRolesToUser);
private boolean asyncAddRolesToUser(@NotNull final CEnvelope aEnvelope,
                                    @NotNull final CRecord aRecord)
{
    if (aEnvelope.isAnswer())
    {
        final int resultCode = aEnvelope.getResultCode();
        if (resultCode == CResultCode.SUCCESS)
        {
            // ...
        }
        return true;
    }
    return false;
}