protected void executeInternal()

in ddm-bpm-extension/src/main/java/com/epam/digital/data/platform/bpms/extension/delegate/connector/keycloak/officer/KeycloakCreateOfficerUserDelegate.java [82:118]


  protected void executeInternal(DelegateExecution execution) throws Exception {
    userNameResponseVariable.on(execution).set("");

    var fullName = fullNameVariable.from(execution).getOrThrow();
    validateSystemAttribute(fullName, FULL_NAME_REGEX, ATTRIBUTE_FULL_NAME);
    var edrpou = edrpouVariable.from(execution).getOrThrow();
    validateSystemAttribute(edrpou, EDRPOU_REGEX, ATTRIBUTE_EDRPOU);
    var drfo = drfoVariable.from(execution).getOrThrow();
    validateSystemAttribute(drfo, DRFO_REGEX, ATTRIBUTE_DRFO);
    var attributes = attributesVariable.from(execution).getOptional().orElse(Maps.newHashMap());
    validateCustomAttributes(attributes);
    addAttributeIfDefined(attributes, ATTRIBUTE_FULL_NAME, fullName);
    addAttributeIfDefined(attributes, ATTRIBUTE_EDRPOU, edrpou);
    addAttributeIfDefined(attributes, ATTRIBUTE_DRFO, drfo);
    checkUserExistenceByAttributes(idmService, fullName, edrpou, drfo);
    var userName = createUsername(fullName, edrpou, drfo);

    var userRepresentation = new UserRepresentation();
    userRepresentation.setUsername(userName);
    userRepresentation.setFirstName(getFirstNameFromFullName(fullName));
    userRepresentation.setLastName(getLastNameFromFullName(fullName));
    userRepresentation.setEnabled(Boolean.TRUE);
    userRepresentation.setAttributes(mapAttributeValuesToList(attributes));
    //The Keycloak API currently ignores roles when creating a user.
    userRepresentation.setRealmRoles(List.of(KeycloakPlatformRole.OFFICER.getName()));

    // Need to add roles after creating the user.
    var officerRoleRepresentation = idmService.getRoleRepresentations().stream()
        .filter(role -> KeycloakPlatformRole.OFFICER.getName().equals(role.getName()))
        .findFirst()
        .map(Collections::singletonList)
        .orElse(Lists.emptyList());

    idmService.createUserRepresentation(userRepresentation, officerRoleRepresentation);

    userNameResponseVariable.on(execution).set(userName);
  }