@SuppressWarnings("unchecked") protected void addMemberOf(Fixture fixture, LdapTemplate template, String user) throws NamingException { List<String> dns = template.search( "", fixture.config.usernameFilter(user).encode(), new ContextMapper() { public Object mapFromContext(Object arg0) { DirContextAdapter ctx = (DirContextAdapter) arg0; return ctx.getNameInNamespace(); } }); assertEquals(dns.toString(), 1, dns.size()); DistinguishedName name = new DistinguishedName(dns.get(0)); DistinguishedName root = new DistinguishedName( template.getContextSource().getReadOnlyContext().getNameInNamespace()); // Build a relative name for (int i = 0; i < root.size(); i++) { name.removeFirst(); } DirContextOperations context = template.lookupContext(name); context.setAttributeValues("memberOf", new Object[] {"foo"}); template.modifyAttributes(context); }
/** * 하위 부서 존재여부를 확인한다. * * @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; }
@Before public void setUp() throws Exception { // Bind to the directory LdapContextSource contextSource = new LdapContextSource(); contextSource.setUrl("ldap://127.0.0.1:" + PORT); contextSource.setUserDn(""); contextSource.setPassword(""); contextSource.setPooled(false); contextSource.afterPropertiesSet(); // Create the Sprint LDAP template LdapTemplate template = new LdapTemplate(contextSource); // Clear out any old data - and load the test data LdapTestUtils.cleanAndSetup( template.getContextSource(), baseName, new ClassPathResource("testdata.ldif")); }
@Test public void testAuthenticate() { DirContext ctxt = ldapTemplate.getContextSource().getContext("some.person1", "password"); assertThat(ctxt).isNotNull(); }