@Transactional
  public synchronized UserEntity createUser(String userName, String localUserName, String displayName, Boolean active) throws AmbariException {

    String validatedUserName = UserName.fromString(userName).toString();
    String validatedDisplayName = (StringUtils.isEmpty(displayName))
        ? validatedUserName
        : UserName.fromString(displayName).toString();
    String validatedLocalUserName = (StringUtils.isEmpty(localUserName))
        ? validatedUserName
        : UserName.fromString(localUserName).toString();

    // Ensure that the user does not already exist
    if (userDAO.findUserByName(validatedUserName) != null) {
      throw new AmbariException("User already exists");
    }

    // Create the PrincipalEntity - needed for assigning privileges/roles
    PrincipalTypeEntity principalTypeEntity = principalTypeDAO.findById(PrincipalTypeEntity.USER_PRINCIPAL_TYPE);
    if (principalTypeEntity == null) {
      principalTypeEntity = new PrincipalTypeEntity();
      principalTypeEntity.setId(PrincipalTypeEntity.USER_PRINCIPAL_TYPE);
      principalTypeEntity.setName(PrincipalTypeEntity.USER_PRINCIPAL_TYPE_NAME);
      principalTypeDAO.create(principalTypeEntity);
    }
    PrincipalEntity principalEntity = new PrincipalEntity();
    principalEntity.setPrincipalType(principalTypeEntity);
    principalDAO.create(principalEntity);

    // Create the new UserEntity Record
    UserEntity userEntity = new UserEntity();
    userEntity.setUserName(validatedUserName);
    userEntity.setDisplayName(validatedDisplayName);
    userEntity.setLocalUsername(validatedLocalUserName);

    userEntity.setPrincipal(principalEntity);
    if (active != null) {
      userEntity.setActive(active);
    }

    userDAO.create(userEntity);

    // execute user initialization hook if required ()
    executeUserHook(validatedUserName);

    return userEntity;
  }
Creates new, user with provided userName, local username, and display name.

@param userName      user name
@param localUserName the local username to use; if <code>null</code> or empty, userName will be used
@param displayName   the name to display for presentation; if <code>null</code> or empty, userName will be used
@param active        is user active
@return the new UserEntity
@throws AmbariException if user already exists
ambari-server/src/main/java/org/apache/ambari/server/security/authorization/Users.java