Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
==================== DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved. Copyright 2011-2013 Tirasa. All rights reserved. The contents of this file are subject to the terms of the Common Development and Distribution License("CDDL") (the "License"). You may not use this file except in compliance with the License. You can obtain a copy of the License at https://oss.oracle.com/licenses/CDDL See the License for the specific language governing permissions and limitations under the License. When distributing the Covered Code, include this CDDL Header Notice in each file and include the License file at https://oss.oracle.com/licenses/CDDL. If applicable, add the following below this CDDL Header, with the fields enclosed by brackets [] replaced by your own identifying information: "Portions Copyrighted [year] [name of copyright owner]" ====================
 
 package org.connid.bundles.ad.crud;
 
 import static org.connid.bundles.ad.ADConnector.*;
 import static org.connid.bundles.ldap.commons.LdapUtil.checkedListByFilter;
 import static org.identityconnectors.common.CollectionUtil.isEmpty;
 import static org.identityconnectors.common.CollectionUtil.newSet;
 import static org.identityconnectors.common.CollectionUtil.nullAsEmpty;
 
 import java.util.List;
 import java.util.Set;
 
 public class ADUpdate extends LdapModifyOperation {
 
     private static final Log LOG = Log.getLog(ADUpdate.class);
 
     private final ObjectClass oclass;
 
     private Uid uid;
 
     @SuppressWarnings("FieldNameHidesFieldInSuperclass")
     private ADConnection conn;

    
Retrieve new name if specified.

Returns:
new Name if provider; null otherwise.
 
     private Name getNewName(final String entryDNfinal Set<Attributeattrs) {
         // purge CN
         final Attribute cnAttr = AttributeUtil.find(.attrs);
 
         if (cnAttr != null) {
             attrs.remove(cnAttr);
         }
 
         // retrieve new name...
 
         final Name name = AttributeUtil.getNameFromAttributes(attrs);
 
         Name newName = null;
 
         if (name != null) {
             attrs.remove(name);
            final ADUtilities utils = new ADUtilities();
            if (utils.isDN(name.getNameValue())) {
                newName = new Name(.getSchemaMapping().getEntryDN(name));
            }
        }
        if (newName == null
                && cnAttr != null) {
            final String cnValue =
                    cnAttr.getValue() == null || cnAttr.getValue().isEmpty() || cnAttr.getValue().get(0) == null
                    ? null
                    : cnAttr.getValue().get(0).toString();
            try {
                // rename if and only if Name is a DN or CN has been provided (consider that the CN can be the Name)
                final List<Rdnrdns = new ArrayList<Rdn>(new LdapName(entryDN).getRdns());
                Rdn naming = new Rdn(rdns.get(rdns.size() - 1).getType(), cnValue);
                rdns.remove(rdns.size() - 1);
                rdns.add(naming);
                newName = new Name(new LdapName(rdns).toString());
            } catch (InvalidNameException e) {
                .error("Error retrieving new DN. Ignore rename request."e);
            }
        }
        return newName;
    }
    public ADUpdate(final ADConnection connfinal ObjectClass oclassfinal Uid uid) {
        super(conn);
        this. = oclass;
        this. = uid;
        this. = conn;
    }
    public Uid update(final Set<Attributeattrs) {
        final ConnectorObject obj = getEntryToBeUpdated();
        String entryDN = obj.getName().getNameValue();
        // ---------------------------------
        // Check if entry should be renamed.
        // ---------------------------------
        final Set<AttributeattrsToBeUpdated = newSet(attrs);
        final Name newName = getNewName(entryDNattrsToBeUpdated);
        // ---------------------------------
        // ---------------------------------
        // Perform modify/rename
        // ---------------------------------
        final Pair<AttributesADGuardedPasswordAttributeattrToModify = getAttributesToModify(objattrsToBeUpdated);
        // Update the attributes.
        modifyAttributes(entryDNattrToModify.);
        // Rename the entry if needed.
        if (newName != null) {
            entryDN = .getSchemaMapping().rename(entryDNnewName);
        }
        // ---------------------------------
        // ---------------------------------
        // Perform group memberships
        // ---------------------------------
        final List<StringldapGroups = getStringListValue(attrsToBeUpdated.);
        if (ldapGroups != null) {
            final Set<StringoldMemberships = new TreeSet<String>(.);
            oldMemberships.addAll(.getLdapGroups(entryDN));
            final Set<StringnewMemberships = new TreeSet<String>(.);
            newMemberships.addAll(ldapGroups);
            // Update the LDAP groups.
            final Modification<GroupMembershipldapGroupMod = new Modification<GroupMembership>();
            if (!newMemberships.equals(oldMemberships)) {
                final Set<StringtoBeRemoved = new TreeSet<String>(.);
                toBeRemoved.addAll(oldMemberships);
                toBeRemoved.removeAll(newMemberships);
                for (String membership : toBeRemoved) {
                    ldapGroupMod.remove(new GroupMembership(entryDNmembership));
                }
                final Set<StringtoBeAdded = new TreeSet<String>(.);
                toBeAdded.addAll(newMemberships);
                toBeAdded.removeAll(oldMemberships);
                for (String membership : toBeAdded) {
                    ldapGroupMod.add(new GroupMembership(entryDNmembership));
                }
            }
            .modifyLdapGroupMemberships(ldapGroupMod);
        }
        // ---------------------------------
        return .getSchemaMapping().createUid(entryDN);
    }
    public Uid addAttributeValues(Set<Attributeattrs) {
        final ConnectorObject obj = getEntryToBeUpdated();
        final String entryDN = obj.getName().getNameValue();
        final Pair<AttributesADGuardedPasswordAttributeattrsToModify = getAttributesToModify(objattrs);
        modifyAttributes(entryDNattrsToModify.);
        List<StringldapGroups = getStringListValue(attrs.);
        if (!isEmpty(ldapGroups)) {
            .addLdapGroupMemberships(entryDNldapGroups);
        }
        return ;
    }
    public Uid removeAttributeValues(Set<Attributeattrs) {
        final ConnectorObject obj = getEntryToBeUpdated();
        final String entryDN = obj.getName().getNameValue();
        final Pair<AttributesADGuardedPasswordAttributeattrsToModify = getAttributesToModify(objattrs);
        modifyAttributes(entryDNattrsToModify.);
        List<StringldapGroups = getStringListValue(attrs.);
        if (!isEmpty(ldapGroups)) {
            .removeLdapGroupMemberships(entryDNldapGroups);
        }
        return ;
    }
            final ConnectorObject objfinal Set<Attributeattrs) {
        final BasicAttributes ldapAttrs = new BasicAttributes();
        ADGuardedPasswordAttribute pwdAttr = null;
        int uacValue = -1;
        for (Attribute attr : attrs) {
            javax.naming.directory.Attribute ldapAttr = null;
            if (attr.is(.)) {
                throw new IllegalArgumentException("Unable to modify an object's uid");
            } else if (attr.is(.)) {
                // Such a change would have been handled in update() above.
                throw new IllegalArgumentException("Unable to modify an object's name");
            } else if (attr.is(.)) {
                final List<Objectvalue = attr.getValue();
                if (value != null && !value.isEmpty() && (Booleanvalue.get(0)) {
                    ldapAttrs.put(
                            new BasicAttribute(..));
                }
            } else if (attr.is(.)) {
                final List<Objectvalue = attr.getValue();
                if (value != null && !value.isEmpty() && (Booleanvalue.get(0)) {
                    ldapAttrs.put(
                            new BasicAttribute(..));
                }
            } else if (LdapConstants.isLdapGroups(attr.getName())) {
                // Handled elsewhere.
            } else if (attr.is(.)) {
                pwdAttr = ADGuardedPasswordAttribute.create(.getConfiguration().getPasswordAttribute(), attr);
            } else if (attr.is() && .is(.)) {
                uacValue = attr.getValue() == null || attr.getValue().isEmpty()
                        ? -1
                        : Integer.parseInt(attr.getValue().get(0).toString());
            } else if (attr.is(.)
                    && .is(.)
                    && uacValue == -1) {
                final Attribute uac = obj.getAttributeByName();
                uacValue = uac != null && uac.getValue() != null && !uac.getValue().isEmpty()
                        ? Integer.parseInt(uac.getValue().get(0).toString()) : 0;
                boolean enabled = attr.getValue() == null
                        || attr.getValue().isEmpty() || Boolean.parseBoolean(attr.getValue().get(0).toString());
                if (enabled) {
                    // if not enabled yet --> enable removing 0x00002
                    if (uacValue % 16 == ) {
                        uacValue -= ;
                    }
                } else {
                    // if not disabled yet --> disable adding 0x00002
                    if (uacValue % 16 != ) {
                        uacValue += ;
                    }
                }
            } else {
                ldapAttr = .getSchemaMapping().encodeAttribute(attr);
            }
            addAttribute(ldapAttrldapAttrs);
        }
        if (uacValue != -1) {
                    , AttributeBuilder.build(, Integer.toString(uacValue))),
                    ldapAttrs);
        }
        return new Pair<AttributesADGuardedPasswordAttribute>(ldapAttrspwdAttr);
    }
    private void addAttribute(final javax.naming.directory.Attribute ldapAttrfinal BasicAttributes ldapAttrs) {
        if (ldapAttr != null) {
            final javax.naming.directory.Attribute existingAttr = ldapAttrs.get(ldapAttr.getID());
            if (existingAttr != null) {
                try {
                    NamingEnumeration<?> all = ldapAttr.getAll();
                    while (all.hasMoreElements()) {
                        existingAttr.add(all.nextElement());
                    }
                } catch (NamingException e) {
                    throw new ConnectorException(e);
                }
            } else {
                ldapAttrs.put(ldapAttr);
            }
        }
    }
    private void modifyAttributes(
            final String entryDN,
            final Pair<AttributesADGuardedPasswordAttributeattrs,
            final int modifyOp) {
        final List<ModificationItemmodItems = new ArrayList<ModificationItem>(attrs.first.size());
        NamingEnumeration<? extends javax.naming.directory.AttributeattrEnum = attrs.first.getAll();
        while (attrEnum.hasMoreElements()) {
            modItems.add(new ModificationItem(modifyOpattrEnum.nextElement()));
        }
        if (attrs.second != null) {
            attrs.second.access(new Accessor() {
                @Override
                public void access(BasicAttribute attr) {
                    try {
                        if (attr.get() != null) {
                            modItems.add(new ModificationItem(modifyOpattr));
                            modifyAttributes(entryDNmodItems);
                        }
                    } catch (NamingException e) {
                        .error(e"Error retrieving password value");
                    }
                }
            });
        }
        modifyAttributes(entryDNmodItems);
    }
    private void modifyAttributes(final String entryDNfinal List<ModificationItemmodItems) {
        try {
            .getInitialContext().modifyAttributes(entryDNmodItems.toArray(new ModificationItem[modItems.size()]));
        } catch (NamingException e) {
            throw new ConnectorException(e);
        }
    }
    private List<StringgetStringListValue(final Set<Attributeattrsfinal String attrName) {
        final Attribute attr = AttributeUtil.find(attrNameattrs);
        if (attr != null && attr.getValue() != null) {
            return checkedListByFilter(nullAsEmpty(attr.getValue()), String.class);
        }
        return null;
    }
        final String filter = .getConfiguration().getUidAttribute() + "=" + .getUidValue();
        final ConnectorObject obj = LdapSearches.findObject(
                , LdapFilter.forNativeFilter(filter), );
        if (obj == null) {
            throw new ConnectorException("Entry not found");
        }
        return obj;
    }
New to GrepCode? Check out our FAQ X