CRecordUserDbCreateUser

Create a new user.

This message belongs to the session manager API.

{
  "id": "546b4672-5612-4069-810d-8dbdfe8ffcb0",
  "name": "USER_DB_CREATE_USER",
  "description": "Create a new user.",
  "slots": [
    {
      "key": "SessionToken",
      "name": "SESSION_TOKEN",
      "direction": "REQUEST",
      "mandatory": "true",
      "type": "UUID",
      "description": "The session token."
    },
    {
      "key": "userId",
      "name": "USER_ID",
      "direction": "REQUEST",
      "mandatory": "true",
      "type": "STRING",
      "description": "The user id."
    },
    {
      "key": "realName",
      "name": "REAL_NAME",
      "direction": "REQUEST",
      "mandatory": "true",
      "type": "STRING",
      "description": "The user's real name."
    },
    {
      "key": "email",
      "name": "EMAIL",
      "direction": "REQUEST",
      "mandatory": "true",
      "type": "STRING",
      "description": "The user's email address."
    },
    {
      "key": "pw",
      "name": "PASSWORD",
      "direction": "REQUEST",
      "mandatory": "false",
      "type": "STRING",
      "description": "The password (SH384 hash, base64 encoded)."
    }
  ]
}

Example of use of the class CRecordUserDbCreateUser (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_CreateUser. In addition, the user ID, the users real name, the email address and a password hash (SHA384, base64 encoded) are required. You can use a utility method of the kernel: CUtilPassword.hashPassword().

private void createUser(@NotNull final UUID aToken,
                        @NotNull final String aUserId,
                        @NotNull final String aRealName,
                        @NotNull final String aEmail,
                        final char[] aPassword) throws CException
{
    final String passwordHash = CUtilPassword.hashPassword(aPassword);

    final CEnvelope env = CEnvelope.forMicroService(SESSION_MICROSERVICE_ID);

    final CRecord record = CRecordUserDbCreateUser.create();
    CRecordUserDbCreateUser.setSessionToken(record,
                                            aToken);
    CRecordUserDbCreateUser.setUserId(record,
                                      aUserId);
    CRecordUserDbCreateUser.setRealName(record,
                                        aRealName);
    CRecordUserDbCreateUser.setEmail(record,
                                     aEmail);
    CRecordUserDbCreateUser.setPassword(record,
                                        passwordHash);

    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(CRecordUserDbCreateUser.ID,
                  this::asyncCreateUser);
private boolean asyncCreateUser(@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