public boolean userExists(String username) { DistinguishedName dn = usernameMapper.buildDn(username); try { Object obj = template.lookup(dn); if (obj instanceof Context) { LdapUtils.closeContext((Context) obj); } return true; } catch (org.springframework.ldap.NameNotFoundException e) { return false; } }
private boolean validateDnExist(LdapTemplateContextSource ldapTemplateContextSource, String fullDn){ try { String dn = fullDn.replace("," + ldapTemplateContextSource.getLdapContextSource().getBaseLdapPathAsString(), ""); Object result = ldapTemplateContextSource.getLdapTemplate().lookup(dn, new AbstractContextMapper<Object>() { @Override protected Object doMapFromContext(DirContextOperations ctx) { Attributes group = ctx.getAttributes(); return group; } }); return result != null; }catch (Exception e){ logger.warn(String.format("validateDnExist[%s] fail", fullDn), e); return false; } }
/** * {@inheritDoc} */ @Override public DirContextOperations lookupContext(Name dn) { return (DirContextOperations) lookup(dn); }
/** * {@inheritDoc} */ @Override public DirContextOperations lookupContext(Name dn) { return (DirContextOperations) lookup(dn); }
/** * {@inheritDoc} */ @Override public DirContextOperations lookupContext(String dn) { return (DirContextOperations) lookup(dn); }
public boolean userExists(String username) { DistinguishedName dn = usernameMapper.buildDn(username); try { Object obj = template.lookup(dn); if (obj instanceof Context) { LdapUtils.closeContext((Context) obj); } return true; } catch(org.springframework.ldap.NameNotFoundException e) { return false; } }
private void verifyDeleted(Name dn) { try { tested.lookup(dn); fail("Expected entry '" + dn + "' to be non-existent"); } catch (NameNotFoundException expected) { // expected } }
private void verifyCleanup() { try { tested.lookup(DN); fail("NameNotFoundException expected"); } catch (NameNotFoundException expected) { assertThat(true).isTrue(); } } }
private void verifyReboundCorrectData() { DirContextAdapter result = (DirContextAdapter) tested.lookup(DN); assertThat(result.getStringAttribute("cn")).isEqualTo("Some Person4"); assertThat(result.getStringAttribute("sn")).isEqualTo("Person4.Changed"); }
private void verifyBoundCorrectData() { DirContextAdapter result = (DirContextAdapter) tested.lookup(PERSON4_DN); assertThat(result.getStringAttribute("cn")).isEqualTo("Some Person4"); assertThat(result.getStringAttribute("sn")).isEqualTo("Person4"); assertThat(result.getStringAttribute("description")).isEqualTo("Some other description"); } }
private void verifyBoundCorrectData() { DirContextAdapter result = (DirContextAdapter) tested.lookup(NEWDN); assertThat(result.getStringAttribute("cn")).isEqualTo("Some Person6"); assertThat(result.getStringAttribute("sn")).isEqualTo("Person6"); assertThat(result.getStringAttribute("description")).isEqualTo("Some description"); } }
/** * This method depends on a DirObjectFactory ( * {@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) * being set in the ContextSource. */ @Test public void testLookupContextRoot() { DirContextAdapter result = (DirContextAdapter) tested.lookup(""); assertThat(result.getDn().toString()).isEqualTo(""); assertThat(result.getNameInNamespace()).isEqualTo(base); }
@Test public void testLookup_ContextMapper() throws Exception { expectGetReadOnlyContext(); Object transformed = new Object(); Object expected = new Object(); when(dirContextMock.lookup(nameMock)).thenReturn(expected); when(contextMapperMock.mapFromContext(expected)).thenReturn(transformed); Object actual = tested.lookup(nameMock, contextMapperMock); verify(dirContextMock).close(); assertThat(actual).isSameAs(transformed); }
@Test public void testLookup_String_AttributesMapper() throws Exception { expectGetReadOnlyContext(); BasicAttributes expectedAttributes = new BasicAttributes(); when(dirContextMock.getAttributes(DEFAULT_BASE_STRING)).thenReturn(expectedAttributes); Object expected = new Object(); when(attributesMapperMock.mapFromAttributes(expectedAttributes)).thenReturn(expected); Object actual = tested .lookup(DEFAULT_BASE_STRING, attributesMapperMock); verify(dirContextMock).close(); assertThat(actual).isSameAs(expected); }
/** * This method depends on a DirObjectFactory ( * {@link org.springframework.ldap.core.support.DefaultDirObjectFactory}) * being set in the ContextSource. */ @Test public void testLookup_Plain() { DirContextAdapter result = (DirContextAdapter) tested.lookup("cn=Some Person2, ou=company1,ou=Sweden"); assertThat(result.getStringAttribute("cn")).isEqualTo("Some Person2"); assertThat(result.getStringAttribute("sn")).isEqualTo("Person2"); assertThat(result.getStringAttribute("description")).isEqualTo("Sweden, Company1, Some Person2"); }
@Test public void testLookup() throws Exception { expectGetReadOnlyContext(); Object expected = new Object(); when(dirContextMock.lookup(nameMock)).thenReturn(expected); Object actual = tested.lookup(nameMock); verify(dirContextMock).close(); assertThat(actual).isSameAs(expected); }
@Test public void testCreate() { dummyDao.create("Sweden", "company1", "some testperson", "testperson", "some description"); log.debug("Verifying result"); String expectedDn = "cn=some testperson, ou=company1, ou=Sweden"; Object ldapResult = ldapTemplate.lookup(expectedDn); assertThat(ldapResult).isNotNull(); ldapTemplate.unbind(expectedDn); }
@Test public void testLookup_AttributesMapper_LdapName() { AttributesMapper mapper = new PersonAttributesMapper(); Person person = (Person) tested.lookup(LdapUtils.newLdapName("cn=Some Person2, ou=company1,ou=Sweden"), mapper); assertThat(person.getFullname()).isEqualTo("Some Person2"); assertThat(person.getLastname()).isEqualTo("Person2"); assertThat(person.getDescription()).isEqualTo("Sweden, Company1, Some Person2"); }
public void update(OrgPerson person) { String dn = prepareDn(person); DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn); ctx.setAttributeValue("sn", person.getLastname()); ctx.setAttributeValue("description", person.getDescription()); ldapTemplate.modifyAttributes(ctx); this.getHibernateTemplate().saveOrUpdate(person); }