org.springframework.ldap.core.DirContextAdapter Java Examples

The following examples show how to use org.springframework.ldap.core.DirContextAdapter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LdapTemplateModifyITest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
/**
 * Test written originally to verify that duplicates are allowed on ordered
 * attributes, but had to be changed since Apache DS seems to disallow
 * duplicates even for ordered attributes.
 */
@Test
public void testModifyAttributes_MultiValueAddDuplicateToOrdered() {
	BasicAttribute attr = new BasicAttribute("description", "Some other description", true); // ordered
	attr.add("Another description");
	// Commented out duplicate to make test work for Apache DS
	// attr.add("Some description");
	ModificationItem[] mods = new ModificationItem[1];
	mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, attr);

	tested.modifyAttributes(PERSON4_DN, mods);

	DirContextAdapter result = (DirContextAdapter) tested.lookup(PERSON4_DN);
       List<String> attributes = Arrays.asList(result.getStringAttributes("description"));
       assertThat(attributes).hasSize(3);
       assertThat(attributes.contains("Some other description")).isTrue();
       assertThat(attributes.contains("Another description")).isTrue();
       assertThat(attributes.contains("Some description")).isTrue();
}
 
Example #2
Source File: LdapAuthRepositoryCustomImpl.java    From Spring-5.0-Projects with MIT License 6 votes vote down vote up
@Override
public void createByBindOperation(LdapAuthUser ldapAuthUser) {
	
	DirContextOperations ctx = new DirContextAdapter();
	ctx.setAttributeValues("objectclass", new String[] {"top", "person", "organizationalPerson","inetOrgPerson"});
	ctx.setAttributeValue("cn", ldapAuthUser.getFirstName());
	ctx.setAttributeValue("sn", ldapAuthUser.getSurName());
	ctx.setAttributeValue("uid", ldapAuthUser.getUserName());
	ctx.setAttributeValue("userPassword", ldapAuthUser.getPassword());
	
	Name dn = LdapNameBuilder.newInstance()
		      .add("ou=users")
		      .add("uid=bpatel")
		      .build();
	
	ctx.setDn(dn);
	ldapTemplate.bind(ctx);
	
}
 
Example #3
Source File: UsersTest.java    From secure-data-service with Apache License 2.0 6 votes vote down vote up
@Test
public void testAttributeExtractionCommas() {
    String desc = "tenant=myTenantId,edOrg=myEdorgId";
    PersonContextMapper mapper = new PersonContextMapper();
    DirContextAdapter context = Mockito.mock(DirContextAdapter.class);
    Mockito.when(context.getStringAttribute("cn")).thenReturn("Full Name");
    Mockito.when(context.getStringAttribute("description")).thenReturn(desc);
    User user = (User) mapper.mapFromContext(context);

    assertEquals("Full Name", user.getAttributes().get("userName"));
    assertEquals("myTenantId", user.getAttributes().get("tenant"));
    assertEquals("myEdorgId", user.getAttributes().get("edOrg"));
    assertEquals(null, user.getAttributes().get("vendor"));
    assertEquals(null, user.getAttributes().get("givenName"));
    assertEquals(null, user.getAttributes().get("sn"));
    assertEquals(6, user.getAttributes().size());
}
 
Example #4
Source File: IncrementalAttributeMapperITest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
private void createUser(String username) throws UnsupportedEncodingException {
    DirContextAdapter ctx = new DirContextAdapter(new DistinguishedName(OU_DN).append("cn", username));

    ctx.addAttributeValue("objectclass", "top");
    ctx.addAttributeValue("objectclass", "person");
    ctx.addAttributeValue("objectclass", "organizationalPerson");
    ctx.addAttributeValue("objectclass", "user");

    ctx.setAttributeValue("givenName", username);
    ctx.setAttributeValue("userPrincipalName", username + "@example.com");
    ctx.setAttributeValue("cn", username);
    ctx.setAttributeValue("description", "Dummy user");
    ctx.setAttributeValue("sAMAccountName", username.toUpperCase() + "." + username.toUpperCase());
    ctx.setAttributeValue("userAccountControl", "512");

    String newQuotedPassword = "\"" + DEFAULT_PASSWORD + "\"";
    ctx.setAttributeValue("unicodePwd", newQuotedPassword.getBytes("UTF-16LE"));

    ldapTemplate.bind(ctx);
}
 
Example #5
Source File: DummyDaoLdapAndHibernateImpl.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
public void create(OrgPerson person) {
	DistinguishedName dn = new DistinguishedName();
       dn.add("ou", person.getCountry());
       dn.add("ou", person.getCompany());
       dn.add("cn", person.getFullname());

       DirContextAdapter ctx = new DirContextAdapter();
       ctx.setAttributeValues("objectclass", new String[] { "top", "person" });
       ctx.setAttributeValue("cn", person.getFullname());
       ctx.setAttributeValue("sn", person.getLastname());
       ctx.setAttributeValue("description", person.getDescription());
       ldapTemplate.bind(dn, ctx, null);
	this.getHibernateTemplate().saveOrUpdate(person);


}
 
Example #6
Source File: SupportedControlsITest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
   @Category(NoAdTest.class)
public void testExpectedControlsSupported() throws Exception {
	/**
	 * Maps the 'supportedcontrol' attribute to a string array.
	 */
	ContextMapper mapper = new ContextMapper() {

		public Object mapFromContext(Object ctx) {
			DirContextAdapter adapter = (DirContextAdapter) ctx;
			return adapter.getStringAttributes(SUPPORTED_CONTROL);
		}

	};

	String[] controls = (String[]) tested.lookup("", new String[] { SUPPORTED_CONTROL }, mapper);
	System.out.println(Arrays.toString(controls));

       HashSet<String> controlsSet = new HashSet<String>(Arrays.asList(controls));

       assertThat(controlsSet.contains("1.3.6.1.4.1.4203.1.10.1")).as("Entry Change Notification LDAPv3 control,").isTrue();
	assertThat(controlsSet.contains("1.3.6.1.4.1.4203.1.10.1")).as("Subentries Control,").isTrue();
	assertThat(controlsSet.contains("2.16.840.1.113730.3.4.2")).as("Manage DSA IT LDAPv3 control,").isTrue();
}
 
Example #7
Source File: LdapTemplateAuthenticationITest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
   @Category(NoAdTest.class)
public void testAuthenticateWithLookupOperationPerformedOnAuthenticatedContext() {
	AndFilter filter = new AndFilter();
	filter.and(new EqualsFilter("objectclass", "person")).and(new EqualsFilter("uid", "some.person3"));
	AuthenticatedLdapEntryContextCallback contextCallback = new AuthenticatedLdapEntryContextCallback() {
		public void executeWithContext(DirContext ctx, LdapEntryIdentification ldapEntryIdentification) {
			try {
				DirContextAdapter adapter = (DirContextAdapter) ctx.lookup(ldapEntryIdentification.getRelativeDn());
				assertThat(adapter.getStringAttribute("cn")).isEqualTo("Some Person3");
			}
			catch (NamingException e) {
				throw new RuntimeException("Failed to lookup " + ldapEntryIdentification.getRelativeDn(), e);
			}
		}
	};
	assertThat(tested.authenticate("", filter.toString(), "password", contextCallback)).isTrue();
}
 
Example #8
Source File: DefaultDirObjectFactoryTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
/**
 * Make sure that the base suffix is stripped off from the DN.
 * 
 * @throws Exception
 */
   @Test
public void testGetObjectInstance_BaseSet() throws Exception {
	Attributes expectedAttributes = new NameAwareAttributes();
	expectedAttributes.put("someAttribute", "someValue");

	when(contextMock2.getNameInNamespace()).thenReturn("dc=jayway, dc=se");

	DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(contextMock, LdapUtils.newLdapName(
			"ou=some unit"), contextMock2, new Hashtable(), expectedAttributes);

       verify(contextMock).close();

	assertThat(adapter.getDn().toString()).isEqualTo("ou=some unit");
	assertThat(adapter.getNameInNamespace()).isEqualTo("ou=some unit,dc=jayway,dc=se");
	assertThat(adapter.getAttributes()).isEqualTo(expectedAttributes);
}
 
Example #9
Source File: LdapTemplateBindUnbindITest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testBindAndRebindWithDirContextAdapterOnly() {
	DirContextAdapter adapter = new DirContextAdapter(LdapUtils.newLdapName(DN));
	adapter.setAttributeValues("objectclass", new String[] { "top",
			"person" });
	adapter.setAttributeValue("cn", "Some Person4");
	adapter.setAttributeValue("sn", "Person4");

	tested.bind(adapter);
	verifyBoundCorrectData();
	adapter.setAttributeValue("sn", "Person4.Changed");
	tested.rebind(adapter);
	verifyReboundCorrectData();
	tested.unbind(DN);
	verifyCleanup();
}
 
Example #10
Source File: LdapTemplateModifyITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testModifyAttributes_MultiValueReplace() {
	BasicAttribute attr = new BasicAttribute("description", "Some other description");
	attr.add("Another description");
	ModificationItem[] mods = new ModificationItem[1];
	mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, attr);

	tested.modifyAttributes(PERSON4_DN, mods);

	DirContextAdapter result = (DirContextAdapter) tested.lookup(PERSON4_DN);
	List<String> attributes = Arrays.asList(result.getStringAttributes("description"));
	assertThat(attributes).hasSize(2);
	assertThat(attributes.contains("Some other description")).isTrue();
       assertThat(attributes.contains("Another description")).isTrue();
}
 
Example #11
Source File: LdapTemplateSearchResultITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testSearch_ContextMapper_LdapQuery_NoBase() {
    contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
    contextMapper.setExpectedValues(ALL_VALUES);
    List<DirContextAdapter> list = tested.search(query()
            .where("objectclass").is("person").and("sn").is("Person2"),
            contextMapper);
    assertThat(list).hasSize(1);
}
 
Example #12
Source File: LdapTemplateSearchResultITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testSearch_ContextMapper_LdapQuery() {
    contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
    contextMapper.setExpectedValues(ALL_VALUES);
    List<DirContextAdapter> list = tested.search(query()
            .base(BASE_NAME)
            .where("objectclass").is("person").and("sn").is("Person2"),
             contextMapper);
    assertThat(list).hasSize(1);
}
 
Example #13
Source File: LdapTemplateSearchResultITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testSearchForObject() {
	contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
	contextMapper.setExpectedValues(ALL_VALUES);
	DirContextAdapter result = (DirContextAdapter) tested
			.searchForObject(BASE_STRING, FILTER_STRING, contextMapper);
	assertThat(result).isNotNull();
}
 
Example #14
Source File: DefaultDirObjectFactoryTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetObjectInstance_ObjectNotContext() throws Exception {
	Attributes expectedAttributes = new NameAwareAttributes();
	expectedAttributes.put("someAttribute", "someValue");

	DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(new Object(), DN, null,
			new Hashtable(), expectedAttributes);

	assertThat(adapter.getDn()).isEqualTo(DN);
	assertThat(adapter.getAttributes()).isEqualTo(expectedAttributes);
}
 
Example #15
Source File: LdapTemplateRenameITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Before
public void prepareTestedInstance() throws Exception {
	DirContextAdapter adapter = new DirContextAdapter();
	adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
	adapter.setAttributeValue("cn", "Some Person6");
	adapter.setAttributeValue("sn", "Person6");
	adapter.setAttributeValue("description", "Some description");

	tested.bind(DN, adapter, null);
}
 
Example #16
Source File: LdapTemplateLookupOpenLdapITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
public void testLookup_GetNameInNamespace_Plain() {
    DirContextAdapter result = (DirContextAdapter) tested
            .lookup("cn=Some Person2, ou=company1,c=Sweden");

    assertThat(result.getDn().isEqualTo("cn=Some Person2, ou=company1, c=Sweden")
            .toString());
    assertEquals(
            "cn=Some Person2, ou=company1, c=Sweden, dc=jayway, dc=se",
            result.getNameInNamespace());
}
 
Example #17
Source File: LdapTemplateLookupITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
}
 
Example #18
Source File: DefaultDirObjectFactoryTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstructAdapterFromName_EmptyName() throws InvalidNameException {
	CompositeName name = new CompositeName();
	name.add("ldap://localhost:389");
	DefaultDirObjectFactory tested = new DefaultDirObjectFactory();
	DirContextAdapter result = tested.constructAdapterFromName(new BasicAttributes(), name, "");

	assertThat(result.getDn().toString()).isEqualTo("");
	assertThat(result.getReferralUrl().toString()).isEqualTo("ldap://localhost:389");
}
 
Example #19
Source File: LdapTemplateSearchResultITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testSearch_ContextMapper_Name() {
	contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
	contextMapper.setExpectedValues(ALL_VALUES);
       List<DirContextAdapter> list = tested.search(BASE_NAME, FILTER_STRING, contextMapper);
	assertThat(list).hasSize(1);
}
 
Example #20
Source File: LdapTemplateSearchResultNamespaceConfigITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testSearch_ContextMapper_LdapQuery_NoBase() {
    contextMapper.setExpectedAttributes(ALL_ATTRIBUTES);
    contextMapper.setExpectedValues(ALL_VALUES);
    List<DirContextAdapter> list = tested.search(query()
            .where("objectclass").is("person").and("sn").is("Person2"),
            contextMapper);
    assertThat(list).hasSize(1);
}
 
Example #21
Source File: LdapTemplateModifyITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
 * Demonstrates how the DirContextAdapter can be used to automatically keep
 * track of changes of the attributes and deliver ModificationItems to use
 * in moifyAttributes().
 */
@Test
public void testModifyAttributes_DirContextAdapter() throws Exception {
	DirContextAdapter adapter = (DirContextAdapter) tested.lookup(PERSON4_DN);

	adapter.setAttributeValue("description", "Some other description");

	ModificationItem[] modificationItems = adapter.getModificationItems();
	tested.modifyAttributes(PERSON4_DN, modificationItems);

	verifyBoundCorrectData();
}
 
Example #22
Source File: DefaultDirObjectFactoryTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetObjectInstance_nullObject() throws Exception {
	Attributes expectedAttributes = new NameAwareAttributes();
	expectedAttributes.put("someAttribute", "someValue");

	DirContextAdapter adapter = (DirContextAdapter) tested.getObjectInstance(null, DN, null, new Hashtable(),
			expectedAttributes);

	assertThat(adapter.getDn()).isEqualTo(DN);
	assertThat(adapter.getAttributes()).isEqualTo(expectedAttributes);
}
 
Example #23
Source File: LdapAndJdbcDummyDaoImpl.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
public void updateAndRename(String dn, String newDn, String description) {
	DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn);
	ctx.setAttributeValue("description", description);

	ldapTemplate.modifyAttributes(ctx);
	ldapTemplate.rename(dn, newDn);
}
 
Example #24
Source File: LdapAndJdbcDummyDaoImpl.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
public void update(String dn, String fullname, String lastname, String description) {
	DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn);
	ctx.setAttributeValue("sn", lastname);
	ctx.setAttributeValue("description", description);

	ldapTemplate.modifyAttributes(ctx);
	jdbcTemplate.update("update PERSON set lastname=?, description = ? where fullname = ?", new Object[] {
			lastname, description, fullname });
}
 
Example #25
Source File: LdapTemplateSearchResultITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testSearch_SearchScope_LimitedAttrs_ContextMapper_Name() {
	contextMapper.setExpectedAttributes(CN_SN_ATTRS);
	contextMapper.setExpectedValues(CN_SN_VALUES);
	contextMapper.setAbsentAttributes(ABSENT_ATTRIBUTES);
       List<DirContextAdapter> list = tested.search(BASE_NAME, FILTER_STRING, SearchControls.SUBTREE_SCOPE, CN_SN_ATTRS, contextMapper);
	assertThat(list).hasSize(1);
}
 
Example #26
Source File: LdapDummyDaoImpl.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Override
public void createRecursivelyAndUnbindSubnode() {
    DirContextAdapter ctx = new DirContextAdapter();
    ctx.setAttributeValues("objectclass", new String[]{"top", "organizationalUnit"});
    ctx.setAttributeValue("ou", "dummy");
    ctx.setAttributeValue("description", "dummy description");

    ldapTemplate.bind("ou=dummy", ctx, null);
    ldapTemplate.bind("ou=dummy,ou=dummy", ctx, null);
    ldapTemplate.unbind("ou=dummy,ou=dummy");
    ldapTemplate.unbind("ou=dummy");
}
 
Example #27
Source File: LdapDummyDaoImpl.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
public void modifyAttributes(String dn, String lastName, String description) {
    DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn);
    ctx.setAttributeValue("sn", lastName);
    ctx.setAttributeValue("description", description);

    ldapTemplate.modifyAttributes(dn, ctx.getModificationItems());
}
 
Example #28
Source File: LdapDummyDaoImpl.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
public void updateAndRename(String dn, String newDn, String description) {
    DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn);
    ctx.setAttributeValue("description", description);

    ldapTemplate.modifyAttributes(ctx);
    ldapTemplate.rename(dn, newDn);
}
 
Example #29
Source File: LdapDummyDaoImpl.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
public void update(String dn, String fullname, String lastname,
        String description) {
    DirContextAdapter ctx = (DirContextAdapter) ldapTemplate.lookup(dn);
    ctx.setAttributeValue("sn", lastname);
    ctx.setAttributeValue("description", description);

    ldapTemplate.modifyAttributes(ctx);
}
 
Example #30
Source File: InvalidBackslashITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Before
public void prepareTestedInstance() throws Exception {
	DirContextAdapter adapter = new DirContextAdapter();
	adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
	adapter.setAttributeValue("cn", "Some\\Person6");
	adapter.setAttributeValue("sn", "Person6");
	adapter.setAttributeValue("description", "Some description");

	tested.unbind(DN);
	tested.bind(DN, adapter, null);
}