CRecordUserDbCreateRole

Create a role in the user database.

This message belongs to the session manager API.

{
  "id": "8c254063-959a-4cb3-8736-242c1a59ad69",
  "name": "USER_DB_CREATE_ROLE",
  "description": "Create a role.",
  "slots": [
    {
      "key": "SessionToken",
      "name": "SESSION_TOKEN",
      "direction": "REQUEST",
      "mandatory": "true",
      "type": "UUID",
      "description": "The session token."
    },
    {
      "key": "role",
      "name": "ROLE_RECORD",
      "direction": "REQUEST",
      "mandatory": "true",
      "type": "RECORD",
      "description": "The role record."
    }
  ]
}

Example of use of the class CRecordUserDbCreateRole (after generating)

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_CreateRole. In addition, the role id, the role description and a protected flag are required. The protected flag is used only by system roles. It prevents the deletion of the new role.

private void createRole(@NotNull final UUID aToken,
                        @NotNull final String aRoleId,
                        @NotNull final String aDescription,
                        final boolean aIsProtected) throws CException
{
    final CEnvelope env = CEnvelope.forMicroService(SESSION_MICROSERVICE_ID);

    final CRecord record = CRecordUserDbCreateRole.create();
    CRecordUserDbCreateRole.setSessionToken(record,
                                            aToken);
    // create role record:
    final CRecord recordRole = CRecordUserDbRoleRecord.create();
    CRecordUserDbRoleRecord.setRoleId(recordRole,
                                      aRoleId);
    CRecordUserDbRoleRecord.setDescription(recordRole,
                                           aDescription);
    CRecordUserDbRoleRecord.setIsProtected(recordRole,
                                           aIsProtected);
    // createdBy and timeCreated are set by the user database

    // add role record:
    CRecordUserDbCreateRole.setRoleRecord(record,
                                          recordRole);

    sendRequest(env,
                record);
}

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(CRecordUserDbCreateRole.ID,
                  this::asyncCreateRole);
private boolean asyncCreateRole(@NotNull final CEnvelope aEnvelope,
                                @NotNull final CRecord aRecord)
{
    if (aEnvelope.isAnswer())
    {
        final int resultCode = aEnvelope.getResultCode();
        if (resultCode == CResultCode.SUCCESS)
        {
            // ...
        }
        return true;
    }
    return false;
}

nyssr.net - Innovative Distributed System