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.     
   * 
   * 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 
  * http://IdentityConnectors.dev.java.net/legal/license.txt
  * 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 identityconnectors/legal/license.txt.
  * 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.ad.crud;
 
 import static org.connid.ad.ADConnector.*;
 
 import static org.identityconnectors.common.CollectionUtil.isEmpty;
 import static org.identityconnectors.common.CollectionUtil.newSet;
 import static org.identityconnectors.common.CollectionUtil.nullAsEmpty;
 import static org.identityconnectors.ldap.LdapUtil.checkedListByFilter;
 import static org.identityconnectors.ldap.LdapUtil.quietCreateLdapName;
 
 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;
 
     public ADUpdate(
             final ADConnection connfinal ObjectClass oclassfinal Uid uid) {
 
         super(conn);
         this. = oclass;
         this. = uid;
         this. = conn;
     }
 
     public Uid update(final Set<Attributeattrs) {
         final String filter =
                 .getConfiguration().getUidAttribute()
                 + "="
                 + .getUidValue();
 
         final ConnectorObject obj = LdapSearches.findObject(
                 ,
                 ,
                 LdapFilter.forNativeFilter(filter),
                 );
 
        String entryDN = obj.getName().getNameValue();
        final PosixGroupMember posixMember = new PosixGroupMember(entryDN);
        // Extract the Name attribute if any, to be used to rename the entry later.
        Set<AttributeupdateAttrs = attrs;
        final Name newName = (Name) AttributeUtil.find(.attrs);
        String newEntryDN = null;
        if (newName != null) {
            updateAttrs = newSet(attrs);
            updateAttrs.remove(newName);
            newEntryDN = .getSchemaMapping().getEntryDN(newName);
        }
        final List<StringldapGroups = getStringListValue(updateAttrs,
                .);
        final List<StringposixGroups = getStringListValue(updateAttrs,
                .);
        final Pair<AttributesADGuardedPasswordAttributeattrToModify =
                getAttributesToModify(objupdateAttrs);
        final Attributes ldapAttrs = attrToModify.first;
        // If we are removing all POSIX ref attributes, check they are not used
        // in POSIX groups. Note it is OK to update the POSIX ref attribute instead of
        // removing them -- we will update the groups to refer to the new attributes.
        final Set<StringnewPosixRefAttrs = getAttributeValues(
                GroupHelper.getPosixRefAttribute(),
                quietCreateLdapName(newEntryDN != null ? newEntryDN : entryDN),
                ldapAttrs);
        if (newPosixRefAttrs != null && newPosixRefAttrs.isEmpty()) {
            checkRemovedPosixRefAttrs(posixMember.getPosixRefAttributes(),
                    posixMember.getPosixGroupMemberships());
        }
        // Update the attributes.
        modifyAttributes(entryDNattrToModify.);
        // Rename the entry if needed.
        String oldEntryDN = null;
        if (newName != null) {
            if (newPosixRefAttrs != null && .getConfiguration().
                    isMaintainPosixGroupMembership() || posixGroups != null) {
                posixMember.getPosixRefAttributes();
            }
            oldEntryDN = entryDN;
            entryDN = .getSchemaMapping().rename(oldEntryDNnewName);
        }
        // Update the LDAP groups.
        final Modification<GroupMembershipldapGroupMod =
                new Modification<GroupMembership>();
        if (oldEntryDN != null && .getConfiguration().
                isMaintainLdapGroupMembership()) {
            final Set<GroupMembershipmembers =
                    .getLdapGroupMemberships(oldEntryDN);
            ldapGroupMod.removeAll(members);
            for (GroupMembership member : members) {
                ldapGroupMod.add(new GroupMembership(entryDN,
                        member.getGroupDN()));
            }
        }
        if (ldapGroups != null) {
            final Set<GroupMembershipmembers =
                    .getLdapGroupMemberships(entryDN);
            ldapGroupMod.removeAll(members);
            ldapGroupMod.clearAdded(); // Since we will be replacing with the new groups.
            for (String ldapGroup : ldapGroups) {
                ldapGroupMod.add(new GroupMembership(entryDNldapGroup));
            }
        }
        .modifyLdapGroupMemberships(ldapGroupMod);
        // Update the POSIX groups.
        final Modification<GroupMembershipposixGroupMod =
                new Modification<GroupMembership>();
        if (newPosixRefAttrs != null && .getConfiguration().
                isMaintainPosixGroupMembership()) {
            final Set<StringremovedPosixRefAttrs =
                    new HashSet<String>(posixMember.getPosixRefAttributes());
            removedPosixRefAttrs.removeAll(newPosixRefAttrs);
            final Set<GroupMembershipmembers =
                    posixMember.getPosixGroupMembershipsByAttrs(
                    removedPosixRefAttrs);
            posixGroupMod.removeAll(members);
            if (!members.isEmpty()) {
                for (GroupMembership member : members) {
                    posixGroupMod.add(new GroupMembership(
                            getFirstPosixRefAttr(entryDNnewPosixRefAttrs),
                            member.getGroupDN()));
                }
            }
        }
        if (posixGroups != null) {
            final Set<GroupMembershipmembers =
                    posixMember.getPosixGroupMemberships();
            posixGroupMod.removeAll(members);
            // Since we will be replacing with the new groups.
            posixGroupMod.clearAdded();
            if (!posixGroups.isEmpty()) {
                for (String posixGroup : posixGroups) {
                    posixGroupMod.add(new GroupMembership(
                            getFirstPosixRefAttr(entryDNnewPosixRefAttrs),
                            posixGroup));
                }
            }
        }
        .modifyPosixGroupMemberships(posixGroupMod);
        return .getSchemaMapping().createUid(entryDN);
    }
    public Uid addAttributeValues(Set<Attributeattrs) {
        final String filter =
                .getConfiguration().getUidAttribute()
                + "="
                + .getUidValue();
        final ConnectorObject obj = LdapSearches.findObject(
                ,
                ,
                LdapFilter.forNativeFilter(filter),
                );
        String entryDN = obj.getName().getNameValue();
        final PosixGroupMember posixMember = new PosixGroupMember(entryDN);
        final Pair<AttributesADGuardedPasswordAttributeattrsToModify =
                getAttributesToModify(objattrs);
        modifyAttributes(entryDNattrsToModify.);
        List<StringldapGroups = getStringListValue(attrs,
                .);
        if (!isEmpty(ldapGroups)) {
            .addLdapGroupMemberships(entryDNldapGroups);
        }
        List<StringposixGroups = getStringListValue(attrs,
                .);
        if (!isEmpty(posixGroups)) {
            Set<StringposixRefAttrs = posixMember.getPosixRefAttributes();
            String posixRefAttr = getFirstPosixRefAttr(entryDNposixRefAttrs);
            .addPosixGroupMemberships(posixRefAttrposixGroups);
        }
        return ;
    }
    public Uid removeAttributeValues(Set<Attributeattrs) {
        final String filter =
                .getConfiguration().getUidAttribute()
                + "="
                + .getUidValue();
        final ConnectorObject obj = LdapSearches.findObject(
                ,
                ,
                LdapFilter.forNativeFilter(filter),
                );
        String entryDN = obj.getName().getNameValue();
        final PosixGroupMember posixMember = new PosixGroupMember(entryDN);
        final Pair<AttributesADGuardedPasswordAttributeattrsToModify =
                getAttributesToModify(objattrs);
        Attributes ldapAttrs = attrsToModify.first;
        Set<StringremovedPosixRefAttrs = getAttributeValues(GroupHelper.
                getPosixRefAttribute(), nullldapAttrs);
        if (!isEmpty(removedPosixRefAttrs)) {
            checkRemovedPosixRefAttrs(removedPosixRefAttrsposixMember.
                    getPosixGroupMemberships());
        }
        modifyAttributes(entryDNattrsToModify.);
        List<StringldapGroups = getStringListValue(attrs,
                .);
        if (!isEmpty(ldapGroups)) {
            .removeLdapGroupMemberships(entryDNldapGroups);
        }
        List<StringposixGroups = getStringListValue(attrs,
                .);
        if (!isEmpty(posixGroups)) {
            Set<GroupMembershipmembers = posixMember.
                    getPosixGroupMembershipsByGroups(posixGroups);
            .removePosixGroupMemberships(members);
        }
        return ;
    }
    private void checkRemovedPosixRefAttrs(
            final Set<StringremovedPosixRefAttrs,
            final Set<GroupMembershipmemberships) {
        for (GroupMembership membership : memberships) {
            if (removedPosixRefAttrs.contains(membership.getMemberRef())) {
                throw new ConnectorException(.format(
                        "cannotRemoveBecausePosixMember", GroupHelper.
                        getPosixRefAttribute()));
            }
        }
    }
            final ConnectorObject objfinal Set<Attributeattrs) {
        final BasicAttributes ldapAttrs = new BasicAttributes();
        ADGuardedPasswordAttribute pwdAttr = null;
        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 (LdapConstants.isLdapGroups(attr.getName())) {
                // Handled elsewhere.
            } else if (LdapConstants.isPosixGroups(attr.getName())) {
                // Handled elsewhere.
            } else if (attr.is(.)) {
                pwdAttr = ADGuardedPasswordAttribute.create(
                        .getConfiguration().getPasswordAttribute(), attr);
            } else if (attr.is(.)) {
                final Attribute uac =
                        obj.getAttributeByName();
                int 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 += ;
                    }
                }
                ldapAttr = .getSchemaMapping().encodeAttribute(
                        ,
                        AttributeBuilder.build(
                        , Integer.toString(uacValue)));
            } else {
                ldapAttr = .getSchemaMapping().encodeAttribute(
                        ,
                        attr);
            }
            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);
                }
            }
        }
        return new Pair<AttributesADGuardedPasswordAttribute>(
                ldapAttrspwdAttr);
    }
    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(
                    entryDN,
                    modItems.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) {
            return checkedListByFilter(nullAsEmpty(attr.getValue()),
                    String.class);
        }
        return null;
    }
New to GrepCode? Check out our FAQ X