Example usage for javax.naming.directory SearchControls ONELEVEL_SCOPE

List of usage examples for javax.naming.directory SearchControls ONELEVEL_SCOPE

Introduction

In this page you can find the example usage for javax.naming.directory SearchControls ONELEVEL_SCOPE.

Prototype

int ONELEVEL_SCOPE

To view the source code for javax.naming.directory SearchControls ONELEVEL_SCOPE.

Click Source Link

Document

Search one level of the named context.

Usage

From source file:egovframework.com.ext.ldapumt.service.impl.DeptManageLdapDAO.java

/**
 * DN?  ?? /*from   w  ww  .  ja v a  2  s .  c  om*/
 * @param dn  ?? Distinguished Name
 * @return
 * @throws Exception
 */
public List<Object> selectDeptManageSubList(String dn) throws Exception {
    List<Object> ucorgList = null;
    String filter = "objectclass=ucorg2";

    try {
        ucorgList = ldapTemplate.search(dn, filter, SearchControls.ONELEVEL_SCOPE,
                new ObjectMapper<UcorgVO>(UcorgVO.class));
    } catch (NameNotFoundException e) {
    }

    return ucorgList;
}

From source file:com.hs.mail.security.login.JndiLoginModule.java

@SuppressWarnings("unchecked")
protected boolean authenticate(String username, String password) throws Exception {
    DirContext context = null;//from  w  w w .  j a v a 2  s . c  om
    try {
        context = open();
        searchFilterFormat.format(new String[] { username });
        SearchControls constraints = new SearchControls();
        constraints.setSearchScope(subtree ? SearchControls.SUBTREE_SCOPE : SearchControls.ONELEVEL_SCOPE);
        if (returnAttribute != null) {
            String[] attribs = StringUtils.split(returnAttribute, ",");
            constraints.setReturningAttributes(attribs);
        }
        NamingEnumeration ne = context.search(base, searchFilter, constraints);
        if (ne == null || !ne.hasMore()) {
            return false;
        }
        SearchResult sr = (SearchResult) ne.next();
        if (ne.hasMore()) {
            // Ignore for now
        }
        // Check the credentials by binding to server
        if (bindUser(context, sr.getNameInNamespace(), password)) {
            return true;
        } else {
            return true;
        }
    } catch (NamingException e) {
        close(context);
        return false;
    }
}

From source file:egovframework.com.ext.ldapumt.service.impl.DeptManageLdapDAO.java

/**
 *    ?./*from   w  w w.ja v  a2 s  .  c  o m*/
 * @param vo  vo
 */
public boolean hasChildren(String dn) throws NamingException {
    ContextSource contextSource = ldapTemplate.getContextSource();
    DirContext ctx = contextSource.getReadOnlyContext();

    String filter = "objectclass=*";
    SearchControls control = new SearchControls();
    control.setSearchScope(SearchControls.ONELEVEL_SCOPE);

    NamingEnumeration<SearchResult> n = ctx.search(dn, filter, control);

    if (n != null && n.hasMore()) {
        return true;
    }

    return false;
}

From source file:com.orangeleap.common.security.OrangeLeapLdapUserSearch.java

/**
 * If true then searches the entire subtree as identified by context, if false (the default) then only searches the level identified by the context.
 *
 * @param searchSubtree true the underlying search controls should be set to SearchControls.SUBTREE_SCOPE rather than SearchControls.ONELEVEL_SCOPE.
 *///from   w w  w .ja v  a2 s.c om
public void setSearchSubtree(boolean searchSubtree) {
    searchControls.setSearchScope(searchSubtree ? SearchControls.SUBTREE_SCOPE : SearchControls.ONELEVEL_SCOPE);
}

From source file:org.apache.archiva.redback.common.ldap.role.TestLdapRoleMapper.java

private void assertExist(DirContext context, String dn, String attribute, String value) throws NamingException {
    SearchControls ctls = new SearchControls();

    ctls.setDerefLinkFlag(true);//from  w w  w  .j  av  a  2s. c  o  m
    ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
    ctls.setReturningAttributes(new String[] { "*" });

    BasicAttributes matchingAttributes = new BasicAttributes();
    matchingAttributes.put(attribute, value);
    BasicAttribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("inetOrgPerson");
    matchingAttributes.put(objectClass);

    NamingEnumeration<SearchResult> results = context.search(suffix, matchingAttributes);

    assertTrue(results.hasMoreElements());
    SearchResult result = results.nextElement();
    Attributes attrs = result.getAttributes();
    Attribute testAttr = attrs.get(attribute);
    assertEquals(value, testAttr.get());

}

From source file:org.apache.archiva.redback.users.ldap.LdapUserManagerTest.java

private void assertExist(DirContext context, String dn, String attribute, String value) throws NamingException {
    SearchControls ctls = new SearchControls();

    ctls.setDerefLinkFlag(true);/*w w w  . jav a2s  .co m*/
    ctls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
    ctls.setReturningAttributes(new String[] { "*" });

    BasicAttributes matchingAttributes = new BasicAttributes();
    matchingAttributes.put(attribute, value);
    BasicAttribute objectClass = new BasicAttribute("objectClass");
    objectClass.add("inetOrgPerson");
    matchingAttributes.put(objectClass);

    NamingEnumeration<SearchResult> results = context.search(suffix, matchingAttributes);
    // NamingEnumeration<SearchResult> results = context.search( suffix, "(" + attribute + "=" + value + ")", ctls
    // );

    assertTrue(results.hasMoreElements());
    SearchResult result = results.nextElement();
    Attributes attrs = result.getAttributes();
    Attribute testAttr = attrs.get(attribute);
    assertEquals(value, testAttr.get());

}

From source file:com.orangeleap.common.security.OrangeLeapLdapAuthoritiesPopulator.java

/**
 * If set to true, a subtree scope search will be performed. If false a single-level search is used.
 *
 * @param searchSubtree set to true to enable searching of the entire tree below the <tt>groupSearchBase</tt>.
 *///from  w w  w.  j a  va 2 s .c o  m
public void setSearchSubtree(boolean searchSubtree) {
    int searchScope = searchSubtree ? SearchControls.SUBTREE_SCOPE : SearchControls.ONELEVEL_SCOPE;
    searchControls.setSearchScope(searchScope);
}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static LDAPUser getIfValidUser(String cn, String password) {
    LDAPUser user = null;/*www .  ja v a2  s .c  o m*/
    NamingEnumeration results = null;
    DirContext ctx = null;
    try {
        ctx = getAuthContext(cn, password);
        SearchControls controls = new SearchControls();
        String retAttrs[] = { "cn", "sn", "givenName", "title", "registeredAddress", "mail", "memberOf",
                "createTimestamp" };
        controls.setReturningAttributes(retAttrs);
        controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        results = ctx.search(rb.getString("peopleRoot"), "(cn=" + cn + ")", controls);
        if (results.hasMore()) {
            SearchResult searchResult = (SearchResult) results.next();
            Attributes attributes = searchResult.getAttributes();
            user = new LDAPUser();

            if (attributes.get("cn") != null)
                user.setUsername((String) attributes.get("cn").get());
            if (attributes.get("sn") != null)
                user.setSurname((String) attributes.get("sn").get());
            if (attributes.get("givenName") != null)
                user.setGivenname((String) attributes.get("givenName").get());
            if (attributes.get("title") != null)
                user.setTitle((String) attributes.get("title").get());
            if (attributes.get("registeredAddress") != null)
                user.setPreferredMail((String) attributes.get("registeredAddress").get(0));
            if (attributes.get("mail") != null) {
                String mails = "";
                for (int i = 0; i < attributes.get("mail").size(); i++) {
                    if (i != 0)
                        mails = mails + ", ";
                    mails = mails + (String) attributes.get("mail").get(i);
                }
                user.setAdditionalMails(mails);
            }
            if (attributes.get("memberOf") != null) {
                for (int i = 0; i < attributes.get("memberOf").size(); i++) {
                    user.addGroup((String) attributes.get("memberOf").get(i));
                }
            }
            if (attributes.get("createTimestamp") != null) {
                String time = (String) attributes.get("createTimestamp").get();
                DateFormat ldapData = new SimpleDateFormat("yyyyMMddHHmmss");
                user.setCreationTime(ldapData.parse(time));
            }

        }
    } catch (NameNotFoundException ex) {
        _log.error(ex);
    } catch (NamingException e) {
        _log.error(e);
    } catch (ParseException ex) {
        _log.error(ex);
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }

    return user;
}

From source file:de.fiz.ddb.aas.auxiliaryoperations.ThreadOrganisationSetApprove.java

private void copyingToLicensedOrgs(Organisation pOrganisation)
        throws AASUnauthorizedException, ExecutionException {
    LOG.log(Level.INFO, "copyingToLicensedOrgs.Organisation.getOIDs(): "
            + (pOrganisation != null ? pOrganisation.getOIDs() : "NULL"));
    if ((pOrganisation == null) || !ConstEnumOrgStatus.approved.equals(pOrganisation.getStatus())) {
        LOG.log(Level.INFO,//from  w  ww .j ava  2 s  . c  o  m
                "Return: not in Status: " + (pOrganisation != null ? pOrganisation.getStatus() : "NULL"));
        return;
    }

    ThreadOrganisationCreate threadOrganisationCreate;
    ThreadOrganisationRead threadOrgOnWorkDirRead;

    try {
        threadOrganisationCreate = new ThreadOrganisationCreate(pOrganisation, _performer);
        threadOrganisationCreate.setAddToLicensedOrgs(true);
        threadOrganisationCreate.call();
        // -- find all sub-organizations
        Set<OIDs> vAllSubOrgIdsOnWorkDir = this.getAllSubOrgIds(false, pOrganisation.getOIDs(),
                SearchControls.ONELEVEL_SCOPE, this._performer);
        // -- all sub-organizations must be deleted:
        for (OIDs vOIDs : vAllSubOrgIdsOnWorkDir) {
            try {
                threadOrgOnWorkDirRead = new ThreadOrganisationRead(vOIDs, this._performer);
                this.copyingToLicensedOrgs(threadOrgOnWorkDirRead.call());
            } catch (NameNotFoundException ex) {
                LOG.log(Level.WARNING,
                        "copyingToLicensedOrgs.NameNotFoundException: ex '" + ex.getMessage() + "'");
            } catch (AASUnauthorizedException ex) {
                LOG.log(Level.WARNING, "copyingToLicensedOrgs.AASUnauthorizedException: ex: '" + ex.getMessage()
                        + "', Performer" + _performer);
            } catch (ExecutionException ex) {
                LOG.log(Level.WARNING,
                        "copyingToLicensedOrgs.ExecutionException: oid = '" + pOrganisation.getOIDs() + "'");
            }
        }
    } catch (NameAlreadyBoundException ex) {
        LOG.log(Level.WARNING,
                "copyingToLicensedOrgs.NameAlreadyBoundException: oid = '" + pOrganisation.getOIDs() + "'");
    } catch (AASUnauthorizedException ex) {
        LOG.log(Level.WARNING, "copyingToLicensedOrgs.AASUnauthorizedException: oid = '"
                + pOrganisation.getOIDs() + "', Performer" + _performer);
    } catch (ExecutionException ex) {
        LOG.log(Level.WARNING,
                "copyingToLicensedOrgs.ExecutionException: oid = '" + pOrganisation.getOIDs() + "'");
    }
}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static LDAPUser getUser(String cn) {
    LDAPUser user = null;//from w  w  w .  jav  a 2 s  .  c  om
    NamingEnumeration results = null;
    DirContext ctx = null;
    try {
        ctx = getContext();
        SearchControls controls = new SearchControls();
        String retAttrs[] = { "cn", "sn", "givenName", "title", "registeredAddress", "mail", "memberOf",
                "createTimestamp" };
        controls.setReturningAttributes(retAttrs);
        controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        results = ctx.search(rb.getString("peopleRoot"), "(cn=" + cn + ")", controls);
        if (results.hasMore()) {
            SearchResult searchResult = (SearchResult) results.next();
            Attributes attributes = searchResult.getAttributes();
            user = new LDAPUser();

            if (attributes.get("cn") != null)
                user.setUsername((String) attributes.get("cn").get());
            if (attributes.get("sn") != null)
                user.setSurname((String) attributes.get("sn").get());
            if (attributes.get("givenName") != null)
                user.setGivenname((String) attributes.get("givenName").get());
            if (attributes.get("title") != null)
                user.setTitle((String) attributes.get("title").get());
            if (attributes.get("registeredAddress") != null)
                user.setPreferredMail((String) attributes.get("registeredAddress").get(0));
            if (attributes.get("mail") != null) {
                String mails = "";
                for (int i = 0; i < attributes.get("mail").size(); i++) {
                    if (i != 0)
                        mails = mails + ", ";
                    mails = mails + (String) attributes.get("mail").get(i);
                }
                user.setAdditionalMails(mails);
            }
            if (attributes.get("memberOf") != null) {
                for (int i = 0; i < attributes.get("memberOf").size(); i++) {
                    user.addGroup((String) attributes.get("memberOf").get(i));
                }
            }

            if (attributes.get("createTimestamp") != null) {
                String time = (String) attributes.get("createTimestamp").get();
                DateFormat ldapData = new SimpleDateFormat("yyyyMMddHHmmss");
                user.setCreationTime(ldapData.parse(time));
            }

        }
    } catch (NameNotFoundException ex) {
        _log.error(ex);
    } catch (NamingException e) {
        _log.error(e);
    } catch (ParseException ex) {
        _log.error(ex);
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }

    return user;
}