@Test public void testUpdateAndRenameWithException() { String dn = "cn=Some Person2,ou=company1,ou=Sweden"; String newDn = "cn=Some Person2,ou=company2,ou=Sweden"; try { // Perform test dummyDao.updateAndRenameWithException(dn, newDn, "Updated description"); fail("DummyException expected"); } catch (DummyException expected) { assertTrue(true); } // Verify that entry was not moved. try { ldapTemplate.lookup(newDn); fail("NameNotFoundException expected"); } catch (NameNotFoundException expected) { assertTrue(true); } // Verify that original entry was not updated. Object object = ldapTemplate.lookup( dn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Sweden, Company1, Some Person2", attributes.get("description").get()); return new Object(); } }); assertNotNull(object); }
@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); }
@Test public void testMyLdifFileWasLoaded() throws Exception { LdapTemplate t = new LdapTemplate(); LdapContextSource s = new LdapContextSource(); s.setPassword(LdapConfiguration.DEFAULT_PASSWORD); s.setUserDn(LdapConfiguration.DEFAULT_BIND_DN); s.setUrl(String.format("ldap://*****:*****@SuppressWarnings("unchecked") List<String> dns = t.search( "", filter.encode(), new ContextMapper() { public Object mapFromContext(Object ctx) { DirContextAdapter context = (DirContextAdapter) ctx; return context.getDn().toString(); } }); assertEquals(2, dns.size()); assertTrue(dns.contains("dc=root")); assertTrue(dns.contains("dc=child,dc=root")); }
@Override public void update(Person person) { Name dn = buildDn(person); DirContextAdapter context = (DirContextAdapter) ldapTemplate.lookup(dn); mapToContext(person, context); ldapTemplate.modifyAttributes(dn, context.getModificationItems()); }
@After public void tearDown() { try { ltmpl.unbind("ou=users", true); } catch (NamingException e) { } try { ltmpl.unbind("ou=roles", true); } catch (NamingException e) { } }
@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); assertNotNull(ldapResult); ldapTemplate.unbind(expectedDn); }
@Test public void testStartsUpWithMyEntries() throws Exception { LdapTemplate t = ldapTemplate( LdapConfiguration.DEFAULT_BIND_DN, LdapConfiguration.DEFAULT_PASSWORD, LdapConfiguration.DEFAULT_PORT); final List<String> dns = t.search("", OU_PRESENT, DN_MAPPER); assertThat("My OU should have been returned", dns, hasItems("ou=Groups,dc=root")); }
public void removeGroup(Group group) throws InternalErrorException { List<String> uniqueUsersIds = new ArrayList<String>(); uniqueUsersIds = this.getAllUniqueMembersInGroup(group.getId(), group.getVoId()); for (String s : uniqueUsersIds) { Attribute memberOf = new BasicAttribute( "memberOf", "perunGroupId=" + group.getId() + ",perunVoId=" + group.getVoId() + "," + ldapProperties.getLdapBase()); ModificationItem memberOfItem = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, memberOf); this.updateUserWithUserId(s, new ModificationItem[] {memberOfItem}); } try { ldapTemplate.unbind( getGroupDN(String.valueOf(group.getVoId()), String.valueOf(group.getId()))); log.debug( "Entry deleted from LDAP: Group {} from Vo with ID=" + group.getVoId() + ".", group); } catch (NameNotFoundException e) { throw new InternalErrorException(e); } }
/** * 등록된 부서의 상세정보를 조회한다. * * @param vo 부서 Vo * @return deptManageVO 부서 Vo * @param bannerVO */ public UcorgVO selectDeptManage(UcorgVO vo) throws Exception { final ContainerCriteria criteria = query().where("objectclass").is("ucorg2"); @SuppressWarnings("unchecked") Map<String, Object> introspected = new BeanMap(vo); for (String key : introspected.keySet()) { if (key.equals("dn") || key.equals("class") || introspected.get(key) == null || introspected.get(key).equals("")) continue; ContainerCriteria c = query().where(key).is(String.valueOf(introspected.get(key))); criteria.and(c); } List<Object> list = null; try { list = ldapTemplate.search(criteria, new ObjectMapper<UcorgVO>(UcorgVO.class)); } catch (Exception e) { e.printStackTrace(); } return (UcorgVO) list.get(0); }
@Test public void testSearch_Plain() { CountNameClassPairCallbackHandler handler = new CountNameClassPairCallbackHandler(); tested.search(base, "(objectclass=person)", handler); assertThat(handler.getNoOfRows()).isEqualTo(5); }
@SuppressWarnings("unchecked") @Override public List<Person> searchForPersons(String keyword) { String filter = "(&(samAccountType=805306368)(|(sn=" + keyword + "*)(givenname=" + keyword + "*)(mail=" + keyword + "*)))"; logger.debug("filter:" + filter); List<Person> persons = (List<Person>) ldapTemplate.search("", filter, new PersonAttributesMapper()); persons.remove(null); if (logger.isTraceEnabled()) { for (Person p : persons) { if (p != null) { logger.trace("username: "******"; sap:" + p.getSap()); } } } return persons; }
@Test public void testUpdateWithException() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; try { dummyDao.updateWithException(dn, "Some Person", "Updated Person", "Updated description"); fail("DummyException expected"); } catch (DummyException expected) { assertTrue(true); } log.debug("Verifying result"); Object ldapResult = ldapTemplate.lookup( dn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Person", attributes.get("sn").get()); assertEquals("Sweden, Company1, Some Person", attributes.get("description").get()); return new Object(); } }); assertNotNull(ldapResult); }
@Override public void create(Person person) { Name dn = buildDn(person); DirContextAdapter context = new DirContextAdapter(dn); mapToContext(person, context); ldapTemplate.bind(dn, context, null); }
@Test public void testModifyAttributesWithException() { String dn = "cn=Some Person,ou=company1,ou=Sweden"; try { // Perform test dummyDao.modifyAttributesWithException(dn, "Updated lastname", "Updated description"); fail("DummyException expected"); } catch (DummyException expected) { assertTrue(true); } // Verify result - check that the operation was properly rolled back Object result = ldapTemplate.lookup( dn, new AttributesMapper() { public Object mapFromAttributes(Attributes attributes) throws NamingException { assertEquals("Person", attributes.get("sn").get()); assertEquals("Sweden, Company1, Some Person", attributes.get("description").get()); return new Object(); } }); assertNotNull(result); }
public List<ContactAcctDTO> findMailContactAcct(String contactCN) { AndFilter andFilter = new AndFilter(); andFilter.and(new EqualsFilter("cn", contactCN)); andFilter.and(new EqualsFilter("cn", "Mail.Location")); return ldapTemplate.search( "cn=" + contactCN, andFilter.encode(), new ContactAcctAttributeMapper()); }
public void createResource(Resource resource) throws InternalErrorException { // Create a set of attributes Attributes attributes = new BasicAttributes(); // Create the objectclass to add Attribute objClasses = new BasicAttribute("objectClass"); objClasses.add("top"); objClasses.add("perunResource"); // Add attributes attributes.put(objClasses); attributes.put("cn", resource.getName()); attributes.put("perunResourceId", String.valueOf(resource.getId())); attributes.put("perunFacilityId", String.valueOf(resource.getFacilityId())); attributes.put("perunVoId", String.valueOf(resource.getVoId())); if (resource.getDescription() != null && !resource.getDescription().isEmpty()) attributes.put("description", resource.getDescription()); // Create the entry try { ldapTemplate.bind( getResourceDN(String.valueOf(resource.getVoId()), String.valueOf(resource.getId())), null, attributes); log.debug( "New entry created in LDAP: Resource {} in Vo with Id=" + resource.getVoId() + " and Facility with ID=" + resource.getFacilityId() + ".", resource); } catch (NameNotFoundException e) { throw new InternalErrorException(e); } }
/** * This method depends on a DirObjectFactory ( {@link * org.springframework.ldap.core.support.DefaultDirObjectFactory}) being set in the ContextSource. */ @Test public void testThatPlainLookupWorksWithSpring25() { DirContextOperations result = tested.lookupContext("cn=Some Person2, ou=company1,c=Sweden"); assertEquals("Some Person2", result.getStringAttribute("cn")); assertEquals("Person2", result.getStringAttribute("sn")); assertEquals("Sweden, Company1, Some Person2", result.getStringAttribute("description")); }
public void deleteVo(Vo vo) throws InternalErrorException { try { ldapTemplate.unbind(getVoDNByVoId(String.valueOf(vo.getId()))); log.debug("Entry deleted from LDAP: Vo {}.", vo); } catch (NameNotFoundException e) { throw new InternalErrorException(e); } }
public void deleteUser(User user) throws InternalErrorException { try { ldapTemplate.unbind(getUserDN(String.valueOf(user.getId()))); log.debug("Entry deleted from LDAP: User {}.", user); } catch (NameNotFoundException e) { throw new InternalErrorException(e); } }
@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")); }
public Attributes getAllUsersAttributes(User user) { Object o = ldapTemplate.lookup( getUserDN(String.valueOf(user.getId())), new UserAttributesContextMapper()); Attributes attrs = null; if (o != null) attrs = (Attributes) o; return attrs; }
/* (non-Javadoc) * @see org.springframework.ldap.odm.core.OdmManager#search(java.lang.Class, javax.naming.Name, java.lang.String, javax.naming.directory.SearchControls) */ public <T> List<T> search(Class<T> managedClass, Name base, String filter, SearchControls scope) { Filter searchFilter = null; if (StringUtils.hasText(filter)) { searchFilter = new HardcodedFilter(filter); } return ldapTemplate.find(base, searchFilter, scope, managedClass); }
/** * ouCode를 활용하여 하위 부서를 조 * * @param ouCode * @return * @throws Exception */ public List<Object> selectDeptManageSubListByOuCode(String ouCode) throws Exception { ContainerCriteria criteria = query().where("objectclass").is("ucorg2").and("parentoucode").is(ouCode); List<Object> list = ldapTemplate.search(criteria, new ObjectMapper<UcorgVO>(UcorgVO.class)); return list; }
/** Fonction servant à vider entièrement le serveur ldap */ public void deleteAll() { OperatorDao operatorDao = this.simpleConnection(); List all = operatorDao.findAll("sn"); for (int i = 0; i < all.size(); i++) { Name dn = buildDn(all.get(i).toString()); ldapTemplate.unbind(dn); } operatorDao.disconnection(); }
/** * Update a given field of a contact in LDAP. * * @param contactDTO */ public void updateContact(ContactDTO contactDTO) { Attributes personAttributes = new BasicAttributes(); BasicAttribute personBasicAttribute = new BasicAttribute("objectclass"); personBasicAttribute.add("person"); personAttributes.put(personBasicAttribute); personAttributes.put("cn", contactDTO.getCommonName()); personAttributes.put("sn", contactDTO.getSurname()); personAttributes.put("description", contactDTO.getDescription()); ldapTemplate.rebind("cn=" + contactDTO.getCommonName(), null, personAttributes); }
/** * Return a specific certification for a particular contact. * * @param contactCN * @param acctCN * @return */ public List<ContactCertDTO> findContactCert(String contactCN, String certCN) { // Create filter AndFilter andFilter = new AndFilter(); andFilter.and(new EqualsFilter("objectclass", "document")); andFilter.and(new EqualsFilter("cn", certCN)); return ldapTemplate.search( "cn=" + contactCN, andFilter.encode(), new ContactCertAttributeMapper()); }
@Override public List<String> getAllPersonNames() { return ldapTemplate.search( query().attributes("cn").where("objectclass").is("person"), new AttributesMapper<String>() { public String mapFromAttributes(Attributes attrs) throws NamingException { return attrs.get("cn").get().toString(); } }); }
@Override public Person getLdapPersonOnlyBySAP(String sap) { Person p = null; String filter = "(employeenumber=" + sap + ")"; List<?> persons = ldapTemplate.search("", filter, new PersonAttributesMapperForLdapOnly()); if (!persons.isEmpty()) { p = (Person) persons.get(0); } return p; }
public boolean userExist(User user) { Object o = null; try { o = ldapTemplate.lookup( getUserDN(String.valueOf(user.getId())), new UserPerunUserIdContextMapper()); } catch (NameNotFoundException ex) { return false; } return true; }
/** * Outil de recherche de contact, celui-ci renvoit tous les contacts. * * @param param : critère demandé. * @return : Liste du critère demandé de tous les utilisateurs. */ @Override public List findAll(final String param) { String base = Settings.getString(Settings.SYSTEM.section, Settings.SYSTEM.base_user); return ldapTemplate.search( base, "(objectClass=Person)", new AttributesMapper() { public Object mapFromAttributes(Attributes attrs) throws NamingException { return attrs.get("sn").get(); } }); }