javax.naming.directory.ModificationItem Java Examples

The following examples show how to use javax.naming.directory.ModificationItem. 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: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 7 votes vote down vote up
@Test
public void testRemoveOneOfSeveralDnAttributeSyntacticallyEqual() throws NamingException {
    BasicAttributes attributes = new BasicAttributes();
    BasicAttribute attribute = new BasicAttribute("uniqueMember", "cn=john doe,OU=company");
    attribute.add("cn=jane doe, ou=company");
    attributes.put(attribute);

    DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
    tested.setUpdateMode(true);

    tested.removeAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=john doe, ou=company"));
    ModificationItem[] modificationItems = tested.getModificationItems();
    assertThat(modificationItems.length).isEqualTo(1);

    ModificationItem modificationItem = modificationItems[0];
    assertThat(modificationItem.getModificationOp()).isEqualTo(DirContext.REMOVE_ATTRIBUTE);
    assertThat(modificationItem.getAttribute().getID()).isEqualTo("uniqueMember");
    assertThat(modificationItem.getAttribute().get()).isEqualTo("cn=john doe,OU=company");
}
 
Example #2
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testChangeAttribute() throws Exception {
	final Attributes fixtureAttrs = new BasicAttributes();
	fixtureAttrs.put(new BasicAttribute("abc", "123"));
	class TestableDirContextAdapter extends DirContextAdapter {
		public TestableDirContextAdapter() {
			super(fixtureAttrs, null);
			setUpdateMode(true);
		}
	}
	tested = new TestableDirContextAdapter();
	tested.setAttributeValue("abc", "234"); // change

	ModificationItem[] mods = tested.getModificationItems();
	assertThat(mods.length).isEqualTo(1);
	assertThat(mods[0].getModificationOp()).isEqualTo(DirContext.REPLACE_ATTRIBUTE);
	Attribute attr = mods[0].getAttribute();
	assertThat((String) attr.getID()).isEqualTo("abc");
	assertThat((String) attr.get()).isEqualTo("234");
}
 
Example #3
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddAttributeValueInUpdateModeAttributeWithOtherValueExistsInUpdAttrs()
		throws NamingException {
	tested.setUpdateMode(true);
	tested.setAttributeValue("abc", "321");

	// Perform test
	tested.addAttributeValue("abc", "123");

	Attributes attrs = tested.getAttributes();
	assertThat(attrs.get("abc")).isNull();

	ModificationItem[] modificationItems = tested.getModificationItems();
	assertThat(modificationItems.length).isEqualTo(1);
	Attribute attribute = modificationItems[0].getAttribute();
	assertThat(attribute.getID()).isEqualTo("abc");
	assertThat(attribute.get(0)).isEqualTo("321");
	assertThat(attribute.get(1)).isEqualTo("123");
}
 
Example #4
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveMultiAttribute() throws Exception {
	final Attributes fixtureAttrs = new BasicAttributes();
	Attribute abc = new BasicAttribute("abc");
	abc.add("123");
	abc.add("456");
	fixtureAttrs.put(abc);
	class TestableDirContextAdapter extends DirContextAdapter {
		public TestableDirContextAdapter() {
			super(fixtureAttrs, null);
			setUpdateMode(true);
		}
	}
	tested = new TestableDirContextAdapter();

	tested.setUpdateMode(true);
	tested.setAttributeValues("abc", new String[] {});

	ModificationItem[] mods = tested.getModificationItems();
	assertThat(mods.length).isEqualTo(1);
	assertThat(mods[0].getModificationOp()).isEqualTo(DirContext.REMOVE_ATTRIBUTE);
	Attribute attr = mods[0].getAttribute();
	assertThat((String) attr.getID()).isEqualTo("abc");
	assertThat(attr.size()).isEqualTo(0);
}
 
Example #5
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testChangeMultiAttribute_RemoveTwoValues() throws Exception {
	final Attributes fixtureAttrs = new BasicAttributes();
	Attribute multi = new BasicAttribute("abc");
	multi.add("123");
	multi.add("qwe");
	multi.add("rty");
	fixtureAttrs.put(multi);
	class TestableDirContextAdapter extends DirContextAdapter {
		public TestableDirContextAdapter() {
			super(fixtureAttrs, null);
			setUpdateMode(true);
		}
	}
	tested = new TestableDirContextAdapter();
	assertThat(tested.isUpdateMode()).isTrue();
	tested.setAttributeValues("abc", new String[] { "123" });

	ModificationItem[] modificationItems = tested.getModificationItems();
	assertThat(modificationItems.length).isEqualTo(1);
    assertThat(modificationItems[0].getModificationOp()).isEqualTo(DirContext.REMOVE_ATTRIBUTE);
	assertThat(modificationItems[0].getAttribute().get(0)).isEqualTo("qwe");
	assertThat(modificationItems[0].getAttribute().get(1)).isEqualTo("rty");
}
 
Example #6
Source File: LdapManager.java    From fess with Apache License 2.0 6 votes vote down vote up
public boolean changePassword(final String username, final String password) {
    if (!fessConfig.isLdapAdminEnabled(username)) {
        return false;
    }

    final Supplier<Hashtable<String, String>> adminEnv = this::createAdminEnv;
    final String userDN = fessConfig.getLdapAdminUserSecurityPrincipal(username);
    search(fessConfig.getLdapAdminUserBaseDn(), fessConfig.getLdapAdminUserFilter(username), null, adminEnv, result -> {
        if (!result.isEmpty()) {
            final List<ModificationItem> modifyList = new ArrayList<>();
            modifyReplaceEntry(modifyList, "userPassword", password);
            modify(userDN, modifyList, adminEnv);
        } else {
            throw new LdapOperationException("User is not found: " + username);
        }
    });
    return true;
}
 
Example #7
Source File: ModifyAttributesOperationRecorderTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCompensatingModificationItem_RemoveFullExistingAttribute()
        throws NamingException {
    BasicAttribute attribute = new BasicAttribute("someattr");
    attribute.add("value1");
    attribute.add("value2");
    Attributes attributes = new BasicAttributes();
    attributes.put(attribute);

    ModificationItem originalItem = new ModificationItem(
            DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("someattr"));

    // Perform test
    ModificationItem result = tested.getCompensatingModificationItem(
            attributes, originalItem);

    // Verify result
    assertThat(result.getModificationOp()).isEqualTo(DirContext.ADD_ATTRIBUTE);
    Attribute resultAttribute = result.getAttribute();
    assertThat(resultAttribute.getID()).isEqualTo("someattr");
    Object object = resultAttribute.get(0);
    assertThat(object).isEqualTo("value1");
    assertThat(resultAttribute.get(1)).isEqualTo("value2");
}
 
Example #8
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateWithIdSpecified() throws NamingException {
    when(contextSourceMock.getReadOnlyContext()).thenReturn(dirContextMock);
    when(contextSourceMock.getReadWriteContext()).thenReturn(dirContextMock);
    LdapName expectedName = LdapUtils.newLdapName("ou=someOu");

    ModificationItem[] expectedModificationItems = new ModificationItem[0];
    DirContextOperations ctxMock = mock(DirContextOperations.class);
    when(ctxMock.getDn()).thenReturn(expectedName);
    when(ctxMock.isUpdateMode()).thenReturn(true);
    when(ctxMock.getModificationItems()).thenReturn(expectedModificationItems);

    Object expectedObject = new Object();
    when(odmMock.getId(expectedObject)).thenReturn(expectedName);
    when(odmMock.getCalculatedId(expectedObject)).thenReturn(null);

    when(dirContextMock.lookup(expectedName)).thenReturn(ctxMock);

    tested.update(expectedObject);

    verify(odmMock, never()).setId(expectedObject, expectedName);
    verify(odmMock).mapToLdapDataEntry(expectedObject, ctxMock);
    verify(dirContextMock).modifyAttributes(expectedName, expectedModificationItems);

    verify(dirContextMock, times(2)).close();
}
 
Example #9
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddAttributeValueInUpdateModeAttributeWithSameValueExistsInUpdAttrs()
		throws NamingException {
	tested.setUpdateMode(true);
	tested.setAttributeValue("abc", "123");

	// Perform test
	tested.addAttributeValue("abc", "123");

	Attributes attrs = tested.getAttributes();
	assertThat(attrs.get("abc")).isNull();

	ModificationItem[] modificationItems = tested.getModificationItems();
	assertThat(modificationItems.length).isEqualTo(1);
	Attribute attribute = modificationItems[0].getAttribute();
	assertThat(attribute.size()).isEqualTo(1);
	assertThat(attribute.getID()).isEqualTo("abc");
	assertThat(attribute.get()).isEqualTo("123");
}
 
Example #10
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * Convert a list of ModificationItemImpl to a list of 
 *
 * @param modificationImpls
 * @param atRegistry
 * @return
 * @throws LdapException
 */
public static List<Modification> convertToServerModification( List<ModificationItem> modificationItems,
    SchemaManager schemaManager ) throws LdapException
{
    if ( modificationItems != null )
    {
        List<Modification> modifications = new ArrayList<Modification>( modificationItems.size() );

        for ( ModificationItem modificationItem : modificationItems )
        {
            AttributeType attributeType = schemaManager.lookupAttributeTypeRegistry( modificationItem
                .getAttribute().getID() );
            modifications.add( toServerModification( modificationItem, attributeType ) );
        }

        return modifications;
    }
    else
    {
        return null;
    }
}
 
Example #11
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testModifyAttributes_NamingException() throws Exception {
	expectGetReadWriteContext();

	ModificationItem[] mods = new ModificationItem[0];

       javax.naming.LimitExceededException ne = new javax.naming.LimitExceededException();
       doThrow(ne).when(dirContextMock).modifyAttributes(nameMock, mods);

	try {
		tested.modifyAttributes(nameMock, mods);
		fail("LimitExceededException expected");
	}
	catch (LimitExceededException expected) {
		assertThat(true).isTrue();
	}

       verify(dirContextMock).close();
}
 
Example #12
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveDnAttributeSyntacticallyEqual() throws NamingException {
    BasicAttributes attributes = new BasicAttributes();
    attributes.put("uniqueMember", "cn=john doe,OU=company");

    DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
    tested.setUpdateMode(true);

    tested.removeAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=john doe, ou=company"));
    ModificationItem[] modificationItems = tested.getModificationItems();
    assertThat(modificationItems.length).isEqualTo(1);

    ModificationItem modificationItem = modificationItems[0];
    assertThat(modificationItem.getModificationOp()).isEqualTo(DirContext.REMOVE_ATTRIBUTE);
    assertThat(modificationItem.getAttribute().getID()).isEqualTo("uniqueMember");
}
 
Example #13
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testChangeMultiAttributeOrderDoesMatter() throws Exception {
	final Attributes fixtureAttrs = new BasicAttributes();
	Attribute multi = new BasicAttribute("abc");
	multi.add("123");
	multi.add("qwe");
	fixtureAttrs.put(multi);
	class TestableDirContextAdapter extends DirContextAdapter {
		public TestableDirContextAdapter() {
			super(fixtureAttrs, null);
			setUpdateMode(true);
		}
	}
	tested = new TestableDirContextAdapter();
	assertThat(tested.isUpdateMode()).isTrue();
	tested.setAttributeValues("abc", new String[] { "qwe", "123" }, true);

	// change
	ModificationItem[] mods = tested.getModificationItems();
	assertThat(mods.length).isEqualTo(1);
	assertThat(mods[0].getModificationOp()).isEqualTo(DirContext.REPLACE_ATTRIBUTE);
	Attribute attr = mods[0].getAttribute();
	assertThat(attr.get(0)).isEqualTo("qwe");
	assertThat(attr.get(1)).isEqualTo("123");
}
 
Example #14
Source File: LdapUserDao.java    From projectforge-webapp with GNU General Public License v3.0 6 votes vote down vote up
public void deactivateUser(final DirContext ctx, final LdapUser user) throws NamingException
{
  log.info("Deactivate user: " + buildDn(null, user));
  final List<ModificationItem> modificationItems = new ArrayList<ModificationItem>();
  modificationItems.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("userPassword", null)));
  modificationItems.add(new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("mail", DEACTIVATED_MAIL)));
  buildDn(null, user);
  modify(ctx, user, modificationItems);
  final String ou = user.getOrganizationalUnit();
  if (ou.startsWith(DEACTIVATED_SUB_CONTEXT2) == false) {
    // Move user to the sub-context "deactivated".
    final String newOu = LdapUtils.getOu(DEACTIVATED_SUB_CONTEXT, getOuBase());
    move(ctx, user, newOu);
    user.setOrganizationalUnit(newOu);
  }
}
 
Example #15
Source File: ModifyAttributesOperationRecorderTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetCompensatingModificationItem_AddNonExistingAttribute()
        throws NamingException {
    Attributes attributes = new BasicAttributes();

    BasicAttribute modificationAttribute = new BasicAttribute("someattr");
    modificationAttribute.add("newvalue1");
    modificationAttribute.add("newvalue2");
    ModificationItem originalItem = new ModificationItem(
            DirContext.ADD_ATTRIBUTE, modificationAttribute);

    // Perform test
    ModificationItem result = tested.getCompensatingModificationItem(
            attributes, originalItem);

    // Verify result
    assertThat(result.getModificationOp()).isEqualTo(DirContext.REMOVE_ATTRIBUTE);
    Attribute resultAttribute = result.getAttribute();
    assertThat(resultAttribute.getID()).isEqualTo("someattr");
    assertThat(resultAttribute.size()).isEqualTo(0);
}
 
Example #16
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 #17
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateWithIdCalculated() throws NamingException {
    when(contextSourceMock.getReadOnlyContext()).thenReturn(dirContextMock);
    when(contextSourceMock.getReadWriteContext()).thenReturn(dirContextMock);
    LdapName expectedName = LdapUtils.newLdapName("ou=someOu");

    ModificationItem[] expectedModificationItems = new ModificationItem[0];
    DirContextOperations ctxMock = mock(DirContextOperations.class);
    when(ctxMock.getDn()).thenReturn(expectedName);
    when(ctxMock.isUpdateMode()).thenReturn(true);
    when(ctxMock.getModificationItems()).thenReturn(expectedModificationItems);

    Object expectedObject = new Object();
    when(odmMock.getId(expectedObject)).thenReturn(null);
    when(odmMock.getCalculatedId(expectedObject)).thenReturn(expectedName);

    when(dirContextMock.lookup(expectedName)).thenReturn(ctxMock);

    tested.update(expectedObject);

    verify(odmMock).setId(expectedObject, expectedName);
    verify(odmMock).mapToLdapDataEntry(expectedObject, ctxMock);
    verify(dirContextMock).modifyAttributes(expectedName, expectedModificationItems);

    verify(dirContextMock, times(2)).close();
}
 
Example #18
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveAttributeValueInUpdateModeOtherAndSameValueExistsInUpdatedAttrs()
		throws NamingException {
	tested.setUpdateMode(true);
	tested.setAttributeValues("abc", new String[] { "321", "123" });

	// Perform test
	tested.removeAttributeValue("abc", "123");

	assertThat(tested.getAttributes().get("abc")).isNull();

	ModificationItem[] modificationItems = tested.getModificationItems();
	assertThat(modificationItems.length).isEqualTo(1);
	Attribute modificationAttribute = modificationItems[0].getAttribute();
	assertThat(modificationAttribute.getID()).isEqualTo("abc");
	assertThat(modificationAttribute.size()).isEqualTo(1);
}
 
Example #19
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoChangeMultiAttributeOrderDoesNotMatter() throws Exception {
	final Attributes fixtureAttrs = new BasicAttributes();
	Attribute multi = new BasicAttribute("abc");
	multi.add("123");
	multi.add("qwe");
	fixtureAttrs.put(multi);
	class TestableDirContextAdapter extends DirContextAdapter {
		public TestableDirContextAdapter() {
			super(fixtureAttrs, null);
			setUpdateMode(true);
		}
	}
	tested = new TestableDirContextAdapter();
	tested.setAttributeValues("abc", new String[] { "qwe", "123" });

	ModificationItem[] mods = tested.getModificationItems();
	assertThat(mods.length).isEqualTo(0);
	String[] modNames = tested.getNamesOfModifiedAttributes();
	assertThat(modNames.length).isEqualTo(0);
}
 
Example #20
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testChangeMultiAttribute_AddAndRemoveValue() throws Exception {
	final Attributes fixtureAttrs = new BasicAttributes();
	Attribute multi = new BasicAttribute("abc");
	multi.add("123");
	multi.add("qwe");
	multi.add("rty");
	multi.add("uio");
	fixtureAttrs.put(multi);
	class TestableDirContextAdapter extends DirContextAdapter {
		public TestableDirContextAdapter() {
			super(fixtureAttrs, null);
			setUpdateMode(true);
		}
	}
	tested = new TestableDirContextAdapter();
	assertThat(tested.isUpdateMode()).isTrue();
	tested.setAttributeValues("abc", new String[] { "123", "qwe", "klytt",
			"kalle" });

	ModificationItem[] modificationItems = tested.getModificationItems();
	assertThat(modificationItems.length).isEqualTo(2);

	Attribute modifiedAttribute = modificationItems[0].getAttribute();
    assertThat(modificationItems[0].getModificationOp()).isEqualTo(DirContext.REMOVE_ATTRIBUTE);
	assertThat(modifiedAttribute.getID()).isEqualTo("abc");
	assertThat(modifiedAttribute.size()).isEqualTo(2);
	assertThat(modifiedAttribute.get(0)).isEqualTo("rty");
	assertThat(modifiedAttribute.get(1)).isEqualTo("uio");

    assertThat(modificationItems[1].getModificationOp()).isEqualTo(DirContext.ADD_ATTRIBUTE);
	modifiedAttribute = modificationItems[1].getAttribute();
	assertThat(modifiedAttribute.getID()).isEqualTo("abc");
	assertThat(modifiedAttribute.size()).isEqualTo(2);
	assertThat(modifiedAttribute.get(0)).isEqualTo("klytt");
	assertThat(modifiedAttribute.get(1)).isEqualTo("kalle");
}
 
Example #21
Source File: RetryingDirContext.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Override
public void modifyAttributes(final Name name, final ModificationItem[] mods)
        throws NamingException {
    new LoggingRetryHandler(DEFAULT_EXCEPTION_CLASSES, this, getSchedule(), getMaxRetries()) {

        @Override
        public Object operation() throws NamingException {
            ((DirContext) getDelegate()).modifyAttributes(name, mods);
            return null;
        }
    }.perform();
}
 
Example #22
Source File: ModifyAttributesOperationExecutorTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testPerformOperation() {
	ModificationItem[] expectedCompensatingItems = new ModificationItem[0];
	ModificationItem[] expectedActualItems = new ModificationItem[0];

	Name expectedDn = LdapUtils.newLdapName("cn=john doe");

	ModifyAttributesOperationExecutor tested = new ModifyAttributesOperationExecutor(ldapOperationsMock,
			expectedDn, expectedActualItems, expectedCompensatingItems);

	// Perform test
	tested.performOperation();

	verify(ldapOperationsMock).modifyAttributes(expectedDn, expectedActualItems);
}
 
Example #23
Source File: LdapTemplateTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testModifyAttributes_String() throws Exception {
	expectGetReadWriteContext();

	ModificationItem[] mods = new ModificationItem[0];

	tested.modifyAttributes(DEFAULT_BASE_STRING, mods);

       verify(dirContextMock).modifyAttributes(DEFAULT_BASE_STRING, mods);
       verify(dirContextMock).close();
}
 
Example #24
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private ModificationItem getModificationItem(ModificationItem[] mods,
		int operation) {
	for (int i = 0; i < mods.length; i++) {
		if (mods[i].getModificationOp() == operation)
			return mods[i];
	}
	return null;
}
 
Example #25
Source File: Group2Activedirectory.java    From MaxKey with Apache License 2.0 5 votes vote down vote up
@Override
public boolean update(Groups group)  throws Exception{
	logger.info("update");
	try {
		SearchControls constraints = new SearchControls();
		constraints.setSearchScope(ldapUtils.getSearchScope());
		NamingEnumeration<SearchResult> results = ldapUtils.getConnection()
				.search(ldapUtils.getBaseDN(), "(cn="+group.getName()+")", constraints);
		String oldDn="";
		String rdn="";
		if (results == null || !results.hasMore()) {
			return create(group);
		}else{
			SearchResult sr = (SearchResult) results.next();
			oldDn =sr.getNameInNamespace();
			String[] dnSplit=oldDn.split(",");
			rdn=oldDn.substring(oldDn.indexOf(","), oldDn.length());
			
			String groupName=dnSplit[0].split("=")[1];
			if(group.getName()!=groupName){
				String newDn="cn="+group.getName()+","+rdn;
				ldapUtils.getCtx().rename(oldDn, newDn);
				ModificationItem[] modificationItems = new ModificationItem[1];
				modificationItems[0]=new ModificationItem(DirContext.REMOVE_ATTRIBUTE,new BasicAttribute("cn",groupName));
				ldapUtils.getCtx().modifyAttributes(newDn, modificationItems);
			}
		}
		
		ldapUtils.close();
	} catch (NamingException e) {
		e.printStackTrace();
	}
	return true;
}
 
Example #26
Source File: LdapGroupDao.java    From projectforge-webapp with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Used for bind and update.
 * @param person
 * @return
 * @see org.projectforge.ldap.LdapDao#getModificationItems(org.projectforge.ldap.LdapObject)
 */
@Override
protected List<ModificationItem> getModificationItems(final List<ModificationItem> list, final LdapGroup group)
{
  createAndAddModificationItems(list, "businessCategory", group.getBusinessCategory());
  createAndAddModificationItems(list, "o", group.getOrganization());
  createAndAddModificationItems(list, "description", group.getDescription());
  if (CollectionUtils.isNotEmpty(group.getMembers()) == true) {
    createAndAddModificationItems(list, "uniqueMember", group.getMembers());
  } else {
    createAndAddModificationItems(list, "uniqueMember", NONE_UNIQUE_MEMBER_ID);
  }
  final boolean modifyPosixAccount = isPosixAccountsConfigured() == true && GroupDOConverter.isPosixAccountValuesEmpty(group) == false;
  if (modifyPosixAccount == true) {
    if (group.getObjectClasses() != null) {
      final List<String> missedObjectClasses = LdapUtils.getMissedObjectClasses(getAdditionalObjectClasses(group), getObjectClass(),
          group.getObjectClasses());
      if (CollectionUtils.isNotEmpty(missedObjectClasses) == true) {
        for (final String missedObjectClass : missedObjectClasses) {
          list.add(createModificationItem(DirContext.ADD_ATTRIBUTE, "objectClass", missedObjectClass));
        }
      }
    }
  }
  if (modifyPosixAccount == true) {
    createAndAddModificationItems(list, "gidNumber", String.valueOf(group.getGidNumber()));
  }
  return list;
}
 
Example #27
Source File: DirContextAdapterTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testSetDnAttributeValueIdentical() {
    BasicAttributes attributes = new BasicAttributes();
    attributes.put("uniqueMember", "cn=john doe, ou=company");

    DirContextAdapter tested = new DirContextAdapter(attributes, LdapUtils.newLdapName("cn=administrators, ou=groups"));
    tested.setUpdateMode(true);

    tested.setAttributeValue("uniqueMember", LdapUtils.newLdapName("cn=john doe, ou=company"));
    ModificationItem[] modificationItems = tested.getModificationItems();
    assertThat(modificationItems.length).isEqualTo(0);
}
 
Example #28
Source File: ModifyAttributesOperationExecutorTest.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
@Test
public void testRollback() {
	ModificationItem[] expectedCompensatingItems = new ModificationItem[0];
	ModificationItem[] expectedActualItems = new ModificationItem[0];

	Name expectedDn = LdapUtils.newLdapName("cn=john doe");

	ModifyAttributesOperationExecutor tested = new ModifyAttributesOperationExecutor(ldapOperationsMock,
			expectedDn, expectedActualItems, expectedCompensatingItems);

	// Perform test
	tested.rollback();

	verify(ldapOperationsMock).modifyAttributes(expectedDn, expectedCompensatingItems);
}
 
Example #29
Source File: ServerEntryUtils.java    From MyVirtualDirectory with Apache License 2.0 5 votes vote down vote up
/**
 * Convert a ModificationItem to an instance of a ServerModification object
 *
 * @param modificationImpl the modification instance to convert
 * @param attributeType the associated attributeType
 * @return a instance of a ServerModification object
 */
private static Modification toServerModification( ModificationItem modificationImpl, AttributeType attributeType )
    throws LdapException
{
    ModificationOperation operation;

    switch ( modificationImpl.getModificationOp() )
    {
        case DirContext.REMOVE_ATTRIBUTE:
            operation = ModificationOperation.REMOVE_ATTRIBUTE;
            break;

        case DirContext.REPLACE_ATTRIBUTE:
            operation = ModificationOperation.REPLACE_ATTRIBUTE;
            break;

        case DirContext.ADD_ATTRIBUTE:
        default:
            operation = ModificationOperation.ADD_ATTRIBUTE;
            break;

    }

    Modification modification = new DefaultModification(
        operation,
        ServerEntryUtils.toServerAttribute( modificationImpl.getAttribute(), attributeType ) );

    return modification;

}
 
Example #30
Source File: Group2Ldap.java    From MaxKey with Apache License 2.0 5 votes vote down vote up
@Override
public boolean addMember(GroupMember groupMember) throws Exception {
	try {
		SearchControls constraints = new SearchControls();
		constraints.setSearchScope(ldapUtils.getSearchScope());
		NamingEnumeration<SearchResult> results = ldapUtils.getConnection()
				.search(ldapUtils.getBaseDN(), "(cn="+groupMember.getGroupName()+")", constraints);
		if (results == null || !results.hasMore()) {
			Groups  group =new Groups();
			group.setName(groupMember.getGroupName());
			create(group);
		}
		
		String uniqueMember="uid="+groupMember.getMemberName()+",dc=users,"+ldapUtils.getBaseDN();
		ModificationItem[] modificationItems = new ModificationItem[1];
		modificationItems[0]=new ModificationItem(DirContext.ADD_ATTRIBUTE,new BasicAttribute("uniqueMember",uniqueMember));
		
		String dn="cn="+groupMember.getGroupName()+",dc=groups,"+ldapUtils.getBaseDN();
		logger.debug("dn : "+dn);
		logger.debug("uniqueMember : "+uniqueMember);
		ldapUtils.getCtx().modifyAttributes(dn, modificationItems);
		ldapUtils.close();
	} catch (NamingException e) {
		e.printStackTrace();
	}
	return true;
}