private boolean isUserNameWithAllowedDomainName(String userName, UserRealm realm)
      throws IdentityException {
    int index;
    index = userName.indexOf("/");

    // Check whether we have a secondary UserStoreManager setup.
    if (index > 0) {
      // Using the short-circuit. User name comes with the domain name.
      try {
        return !realm
            .getRealmConfiguration()
            .isRestrictedDomainForSlefSignUp(userName.substring(0, index));
      } catch (UserStoreException e) {
        throw new IdentityException(e.getMessage(), e);
      }
    }

    return true;
  }
  private void addUser(
      String userName,
      String password,
      Map<String, String> claimList,
      String profileName,
      UserRealm realm)
      throws IdentityException {
    UserStoreManager admin = null;
    Permission permission = null;
    try {
      // get config from tenant registry
      TenantRegistrationConfig tenantConfig =
          getTenantSignUpConfig(realm.getUserStoreManager().getTenantId());
      // set tenant config specific sign up domain
      if (tenantConfig != null && !"".equals(tenantConfig.getSignUpDomain())) {
        int index = userName.indexOf(UserCoreConstants.DOMAIN_SEPARATOR);
        if (index > 0) {
          userName =
              tenantConfig.getSignUpDomain().toUpperCase()
                  + UserCoreConstants.DOMAIN_SEPARATOR
                  + userName.substring(index + 1);
        } else {
          userName =
              tenantConfig.getSignUpDomain().toUpperCase()
                  + UserCoreConstants.DOMAIN_SEPARATOR
                  + userName;
        }
      }

      // add user to the relevant user store

      admin = realm.getUserStoreManager();
      if (!isUserNameWithAllowedDomainName(userName, realm)) {
        throw new IdentityException("Domain does not permit self registration");
      }
      // add user
      admin.addUser(userName, password, null, claimList, profileName);

      // after adding the user, assign specif roles
      List<String> roleNamesArr = getRoleName(userName, tenantConfig);
      if (claimList.get(SelfRegistrationConstants.SIGN_UP_ROLE_CLAIM_URI) != null) {
        // check is a user role is specified as a claim by the client, if so add it to the roles
        // list
        if (tenantConfig != null) {
          roleNamesArr.add(
              tenantConfig.getSignUpDomain().toUpperCase()
                  + UserCoreConstants.DOMAIN_SEPARATOR
                  + claimList.get(SelfRegistrationConstants.SIGN_UP_ROLE_CLAIM_URI));
        } else {
          roleNamesArr.add(
              UserCoreConstants.INTERNAL_DOMAIN
                  + UserCoreConstants.DOMAIN_SEPARATOR
                  + claimList.get(SelfRegistrationConstants.SIGN_UP_ROLE_CLAIM_URI));
        }
      }
      String[] identityRoleNames = roleNamesArr.toArray(new String[roleNamesArr.size()]);

      for (int i = 0; i < identityRoleNames.length; i++) {
        // if this is the first time a user signs up, needs to create role
        doAddUser(i, admin, identityRoleNames, userName, permission);
      }

    } catch (UserStoreException e) {
      throw new IdentityException(
          "Error occurred while adding user : "******". " + e.getMessage(), e);
    }
  }