Search in sources :

Example 1 with ModificationType

use of org.forgerock.opendj.ldap.ModificationType in project OpenAM by OpenRock.

the class DJLDAPv3Repo method modifyGroupMembership.

/**
     * Modifies group membership data in the directory. In case the memberOf attribute is configured, this will also
     * iterate through all the user entries and modify those as well. Otherwise this will only modify the uniquemember
     * attribute on the group entry based on the operation.
     *
     * @param groupDN The DN of the group.
     * @param memberDNs The DNs of the group members.
     * @param operation Whether the members needs to be added or removed from the group. Use {@link IdRepo#ADDMEMBER}
     * or {@link IdRepo#REMOVEMEMBER}.
     * @throws IdRepoException If there was an error while modifying the membership data.
     */
private void modifyGroupMembership(String groupDN, Set<String> memberDNs, int operation) throws IdRepoException {
    ModifyRequest modifyRequest = LDAPRequests.newModifyRequest(groupDN);
    Attribute attr = new LinkedAttribute(uniqueMemberAttr, memberDNs);
    ModificationType modType;
    if (ADDMEMBER == operation) {
        modType = ModificationType.ADD;
    } else {
        modType = ModificationType.DELETE;
    }
    modifyRequest.addModification(new Modification(modType, attr));
    Connection conn = null;
    try {
        conn = connectionFactory.getConnection();
        conn.modify(modifyRequest);
        if (memberOfAttr != null) {
            for (String member : memberDNs) {
                ModifyRequest userMod = LDAPRequests.newModifyRequest(member);
                userMod.addModification(modType, memberOfAttr, groupDN);
                conn.modify(userMod);
            }
        }
    } catch (LdapException ere) {
        DEBUG.error("An error occurred while trying to modify group membership. Name: " + groupDN + " memberDNs: " + memberDNs + " Operation: " + modType, ere);
        handleErrorResult(ere);
    } finally {
        IOUtils.closeIfNotNull(conn);
    }
}
Also used : Modification(org.forgerock.opendj.ldap.Modification) Attribute(org.forgerock.opendj.ldap.Attribute) LinkedAttribute(org.forgerock.opendj.ldap.LinkedAttribute) ModificationType(org.forgerock.opendj.ldap.ModificationType) Connection(org.forgerock.opendj.ldap.Connection) ModifyRequest(org.forgerock.opendj.ldap.requests.ModifyRequest) ByteString(org.forgerock.opendj.ldap.ByteString) LdapException(org.forgerock.opendj.ldap.LdapException) LinkedAttribute(org.forgerock.opendj.ldap.LinkedAttribute)

Example 2 with ModificationType

use of org.forgerock.opendj.ldap.ModificationType in project OpenAM by OpenRock.

the class SMSEmbeddedLdapObject method copyModItemsToLDAPModList.

// Method to covert JNDI ModificationItems to LDAPModificationSet
private static List copyModItemsToLDAPModList(ModificationItem[] mods) throws SMSException {
    if ((mods == null) || (mods.length == 0)) {
        return null;
    }
    List<LDAPModification> modList = new ArrayList<>(mods.length);
    try {
        for (ModificationItem mod : mods) {
            Attribute dAttr = mod.getAttribute();
            String attrName = dAttr.getID();
            List<String> values = new ArrayList<>();
            for (NamingEnumeration ne = dAttr.getAll(); ne.hasMore(); ) {
                values.add((String) ne.next());
            }
            ModificationType modType = null;
            switch(mod.getModificationOp()) {
                case DirContext.ADD_ATTRIBUTE:
                    modType = ModificationType.ADD;
                    break;
                case DirContext.REPLACE_ATTRIBUTE:
                    modType = ModificationType.REPLACE;
                    break;
                case DirContext.REMOVE_ATTRIBUTE:
                    modType = ModificationType.DELETE;
                    break;
            }
            if (modType != null) {
                modList.add(new LDAPModification(modType, new LDAPAttribute(attrName, values)));
            }
        }
    } catch (NamingException nne) {
        throw (new SMSException(nne, "sms-cannot-copy-fromModItemToModSet"));
    }
    return (modList);
}
Also used : ModificationItem(javax.naming.directory.ModificationItem) LDAPAttribute(org.opends.server.protocols.ldap.LDAPAttribute) Attribute(javax.naming.directory.Attribute) LDAPAttribute(org.opends.server.protocols.ldap.LDAPAttribute) ModificationType(org.forgerock.opendj.ldap.ModificationType) SMSException(com.sun.identity.sm.SMSException) LDAPModification(org.opends.server.protocols.ldap.LDAPModification) ArrayList(java.util.ArrayList) NamingEnumeration(javax.naming.NamingEnumeration) NamingException(javax.naming.NamingException)

Aggregations

ModificationType (org.forgerock.opendj.ldap.ModificationType)2 SMSException (com.sun.identity.sm.SMSException)1 ArrayList (java.util.ArrayList)1 NamingEnumeration (javax.naming.NamingEnumeration)1 NamingException (javax.naming.NamingException)1 Attribute (javax.naming.directory.Attribute)1 ModificationItem (javax.naming.directory.ModificationItem)1 Attribute (org.forgerock.opendj.ldap.Attribute)1 ByteString (org.forgerock.opendj.ldap.ByteString)1 Connection (org.forgerock.opendj.ldap.Connection)1 LdapException (org.forgerock.opendj.ldap.LdapException)1 LinkedAttribute (org.forgerock.opendj.ldap.LinkedAttribute)1 Modification (org.forgerock.opendj.ldap.Modification)1 ModifyRequest (org.forgerock.opendj.ldap.requests.ModifyRequest)1 LDAPAttribute (org.opends.server.protocols.ldap.LDAPAttribute)1 LDAPModification (org.opends.server.protocols.ldap.LDAPModification)1