Java Code Examples for org.springframework.ldap.core.DirContextAdapter#setAttributeValue()

The following examples show how to use org.springframework.ldap.core.DirContextAdapter#setAttributeValue() . 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: LdapTemplateNoBaseSuffixITest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testBindAndUnbind_Plain() {
	DirContextAdapter adapter = new DirContextAdapter();
	adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
	adapter.setAttributeValue("cn", "Some Person4");
	adapter.setAttributeValue("sn", "Person4");
	tested.bind("cn=Some Person4, ou=company1, ou=Sweden," + base, adapter, null);

	DirContextAdapter result = (DirContextAdapter) tested
			.lookup("cn=Some Person4, ou=company1, ou=Sweden," + base);

	assertThat(result.getStringAttribute("cn")).isEqualTo("Some Person4");
	assertThat(result.getStringAttribute("sn")).isEqualTo("Person4");
	assertThat(result.getDn()).isEqualTo(LdapUtils.newLdapName("cn=Some Person4,ou=company1,ou=Sweden," + base));

	tested.unbind("cn=Some Person4,ou=company1,ou=Sweden," + base);
	try {
		tested.lookup("cn=Some Person4, ou=company1, ou=Sweden," + base);
		fail("NameNotFoundException expected");
	}
	catch (NameNotFoundException expected) {
		assertThat(true).isTrue();
	}
}
 
Example 2
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 3
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 4
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 5
Source File: PersonDaoImpl.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private void mapToContext(Person person, DirContextAdapter context) {
	context.setAttributeValues("objectclass", new String[] { "top", "person" });
	context.setAttributeValue("cn", person.getFullName());
	context.setAttributeValue("sn", person.getLastName());
	context.setAttributeValue("description", person.getDescription());
	context.setAttributeValue("telephoneNumber", person.getPhone());
}
 
Example 6
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 7
Source File: SimpleLdapTemplateITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testBindAndUnbindName() {
	DirContextAdapter adapter = new DirContextAdapter();
	adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
	adapter.setAttributeValue("cn", "Some Person4");
	adapter.setAttributeValue("sn", "Person4");

	ldapTemplate.bind(DN, adapter, null);
	verifyBoundCorrectData();
	ldapTemplate.unbind(DN);
	verifyCleanup();
}
 
Example 8
Source File: DummyDaoLdapAndHibernateImpl.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
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);

}
 
Example 9
Source File: SimpleLdapTemplateITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testBindAndUnbind() {
	DirContextAdapter adapter = new DirContextAdapter();
	adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
	adapter.setAttributeValue("cn", "Some Person4");
	adapter.setAttributeValue("sn", "Person4");

	ldapTemplate.bind(DN_STRING, adapter, null);
	verifyBoundCorrectData();
	ldapTemplate.unbind(DN_STRING);
	verifyCleanup();
}
 
Example 10
Source File: SimpleLdapTemplateITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testBindAndUnbindWithDirContextAdapter() {
	DirContextAdapter adapter = new DirContextAdapter(DN);
	adapter.setAttributeValues("objectclass", new String[] { "top", "person" });
	adapter.setAttributeValue("cn", "Some Person4");
	adapter.setAttributeValue("sn", "Person4");

	ldapTemplate.bind(adapter);
	verifyBoundCorrectData();
	ldapTemplate.unbind(DN);
	verifyCleanup();
}
 
Example 11
Source File: LdapTemplateBindUnbindITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testBindAndUnbindWithDirContextAdapterUsingLdapName() {
	DirContextAdapter adapter = new DirContextAdapter();
	adapter.setAttributeValues("objectclass", new String[] { "top",
			"person" });
	adapter.setAttributeValue("cn", "Some Person4");
	adapter.setAttributeValue("sn", "Person4");

	tested.bind(LdapUtils.newLdapName(DN), adapter, null);
	verifyBoundCorrectData();
	tested.unbind(LdapUtils.newLdapName(DN));
	verifyCleanup();
}
 
Example 12
Source File: LdapClient.java    From tutorials with MIT License 5 votes vote down vote up
public void create(final String username, final String password) {
    Name dn = LdapNameBuilder
      .newInstance()
      .add("ou", "users")
      .add("cn", username)
      .build();
    DirContextAdapter context = new DirContextAdapter(dn);

    context.setAttributeValues("objectclass", new String[]{"top", "person", "organizationalPerson", "inetOrgPerson"});
    context.setAttributeValue("cn", username);
    context.setAttributeValue("sn", username);
    context.setAttributeValue("userPassword", digestSHA(password));

    ldapTemplate.bind(context);
}
 
Example 13
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 14
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 15
Source File: IncrementalAttributeMapperITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private void createOu() {
    DirContextAdapter ctx = new DirContextAdapter(OU_DN);

    ctx.addAttributeValue("objectClass", "top");
    ctx.addAttributeValue("objectClass", "organizationalUnit");

    ctx.setAttributeValue("ou", "dummy");
    ctx.setAttributeValue("description", "dummy description");

    ldapTemplate.bind(ctx);
}
 
Example 16
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 17
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 18
Source File: LdapTemplateBindUnbindITest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testBindAndUnbindWithDirContextAdapterOnly() {
	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();
	tested.unbind(DN);
	verifyCleanup();
}
 
Example 19
Source File: LdapServiceImpl.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
private DirContextAdapter createUserContext(String realm, User user) {
    DirContextAdapter context = new DirContextAdapter(buildUserDN(realm, user));
    boolean isCreate = true;
    mapUserToContext(context, user, isCreate);
    context.setAttributeValue("cn", user.getCn());
    return context;
}
 
Example 20
Source File: UserService.java    From secure-data-service with Apache License 2.0 5 votes vote down vote up
private void mapUserToContext(DirContextAdapter context, User user) {
    context.setAttributeValues("objectclass", new String[] { "inetOrgPerson", "posixAccount", "top" });
    context.setAttributeValue("givenName", user.getAttributes().get("givenName"));
    context.setAttributeValue("sn", user.getAttributes().get("sn"));
    context.setAttributeValue("uid", user.getAttributes().get("uid"));
    context.setAttributeValue("uidNumber", user.getAttributes().get("uidNumber"));
    context.setAttributeValue("gidNumber", user.getAttributes().get("gidNumber"));
    context.setAttributeValue("cn", user.getAttributes().get("userName"));
    context.setAttributeValue("mail", user.getAttributes().get("mail"));
    context.setAttributeValue("homeDirectory", user.getAttributes().get("homeDirectory"));

    context.setAttributeValue("gecos", user.getAttributes().get("resetKey"));
    context.setAttributeValue("userPassword", user.getAttributes().get("userPassword"));

    String description = "";
    if (user.getAttributes().get("tenant") != null) {
        description += "tenant=" + user.getAttributes().get("tenant");
    }
    if (user.getAttributes().get("edOrg") != null) {
        description += ",edOrg=" + user.getAttributes().get("edOrg");
    }
    if (!"".equals(description)) {
        context.setAttributeValue("description", "tenant=" + user.getAttributes().get("tenant") + "," + "edOrg="
                + user.getAttributes().get("edOrg"));
    }

    if (user.getAttributes().containsKey("employeeNumber")) {
        context.setAttributeValue("employeeNumber", user.getAttributes().get("employeeNumber"));
    }
}