public void validateBeforeAdding(
      LoginMethodDto loginMethodDto, String username, String password) {
    try {
      ldapHelper.authenticateLDAPUser(
          loginMethodDto.getUrl(), loginMethodDto.getId(), username, password);
    } catch (InternalAuthenticationServiceException ex) {
      throw new AppException(
          HttpStatus.UNAUTHORIZED.value(),
          "The LDAP server " + loginMethodDto.getUrl() + " is unauthorized.",
          null);
    }

    if (loginMethodRepository.findByPriority(loginMethodDto.getPriority()) != null) {
      throw new AppException(
          HttpStatus.CONFLICT.value(),
          "The loginMethod.priority " + loginMethodDto.getPriority() + " already exists.",
          null,
          null);
    }

    LoginMethodEntity loginMethod = loginMethodRepository.findByUrl(loginMethodDto.getUrl());

    if (loginMethod != null) {
      throw new AppException(
          HttpStatus.CONFLICT.value(),
          "The loginMethod.url " + loginMethodDto.getUrl() + " has already existed.",
          "The login method [id = "
              + loginMethod.getId()
              + "] also has url "
              + loginMethodDto.getUrl(),
          null);
    }
  }
Example #2
0
  public Collection<LDAPSearchResult> search(final LDAPFilter filter) {
    Assert.notNull(filter, "filter");
    try {
      if ((this.connectOnSearch) && (!this.isConnected())) {
        this.connect();
      }
      Assert.notNull(this.context, "context");
      SearchControls controls = new SearchControls();
      controls.setReturningAttributes(filter.getAttributes());
      controls.setCountLimit(filter.getLimit());
      controls.setTimeLimit(0);
      controls.setSearchScope(SearchControls.SUBTREE_SCOPE);

      NamingEnumeration<SearchResult> results =
          this.context.search(filter.getBaseName(), filter.getFilter(), controls);

      Collection<LDAPSearchResult> c = LDAPHelper.toCollection(results);

      if (this.connectOnSearch) {
        this.disconnect();
      }

      return c;
    } catch (NamingException e) {
      throw new LDAPException(e);
    }
  }