Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) 2011 ConnId (connid-dev@googlegroups.com) Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.connid.bundles.ad.crud;
 
 import static org.connid.bundles.ad.ADConnector.UACCONTROL_ATTR;
 import static org.connid.bundles.ad.ADConnector.UF_ACCOUNTDISABLE;
 import static org.connid.bundles.ad.ADConnector.UF_NORMAL_ACCOUNT;
 import static org.connid.bundles.ldap.commons.LdapUtil.checkedListByFilter;
 import static org.identityconnectors.common.CollectionUtil.isEmpty;
 import static org.identityconnectors.common.CollectionUtil.nullAsEmpty;
 
 import java.util.List;
 import java.util.Set;
 
 public class ADCreate extends LdapModifyOperation {
 
     private static final Log LOG = Log.getLog(ADConnection.class);
 
     private final ObjectClass oclass;
 
     private final Set<Attributeattrs;
 
     @SuppressWarnings("FieldNameHidesFieldInSuperclass")
     private final ADConnection conn;
 
     public ADCreate(
             final ADConnection conn,
             final ObjectClass oclass,
             final Set<Attributeattrs,
             final OperationOptions options) {
         super(conn);
 
         this. = oclass;
         this. = attrs;
         this. = conn;
     }
 
     public Uid create() {
         try {
             return executeImpl();
         } catch (NamingException e) {
             throw new ConnectorException(e);
         }
     }
 
     private Uid executeImpl() throws NamingException {
 
         // -------------------------------------------------
         // Retrieve DN
         // -------------------------------------------------
         final Name nameAttr = AttributeUtil.getNameFromAttributes();
 
         if (nameAttr == null) {
             throw new IllegalArgumentException("No Name attribute provided in the attributes");
         }
 
         final Attribute cnAttr = AttributeUtil.find(.);
         if (cnAttr != null) {
             .remove(cnAttr);
         }
 
         final ADUtilities utils = new ADUtilities();
 
         Name name;
        if (utils.isDN(nameAttr.getNameValue())) {
            name = nameAttr;
        } else {
            Uid uidAttr = AttributeUtil.getUidAttribute();
            if (uidAttr == null && StringUtil.isNotBlank(nameAttr.getNameValue())) {
                uidAttr = new Uid(nameAttr.getNameValue());
                .add(uidAttr);
            }
            name = new Name(utils.getDN(nameAttrcnAttr));
        }
        // -------------------------------------------------
        List<StringldapGroups = null;
        ADGuardedPasswordAttribute pwdAttr = null;
        final BasicAttributes adAttrs = new BasicAttributes(true);
        int uacValue = -1;
        Boolean uccp = null;
        for (Attribute attr : ) {
            if (attr.is(.)) {
                // Handled already.
            } else if (attr.is(.)) {
                final List<Objectvalue = attr.getValue();
                if (value != null && !value.isEmpty()) {
                    uccp = (Booleanvalue.get(0);
                }
            } else if (attr.is(.)) {
                final List<Objectvalue = attr.getValue();
                if (value != null && !value.isEmpty() && (Booleanvalue.get(0)) {
                    adAttrs.put(
                            new BasicAttribute(..));
                }
            } else if (attr.is(.)) {
                final List<Objectvalue = attr.getValue();
                if (value != null && !value.isEmpty() && (Booleanvalue.get(0)) {
                    adAttrs.put(
                            new BasicAttribute(..));
                }
            } else if (LdapConstants.isLdapGroups(attr.getName())) {
                ldapGroups = checkedListByFilter(nullAsEmpty(attr.getValue()), String.class);
            } 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) {
                if (attr.getValue() == null
                        || attr.getValue().isEmpty()
                        || Boolean.parseBoolean(attr.getValue().get(0).toString())) {
                    uacValue = ;
                } else {
                    uacValue =  + ;
                }
            } else {
                javax.naming.directory.Attribute ldapAttr = .getSchemaMapping().encodeAttribute(attr);
                // Do not send empty attributes. 
                if (ldapAttr != null && ldapAttr.size() > 0) {
                    adAttrs.put(ldapAttr);
                }
            }
        }
        final String pwdAttrName = .getConfiguration().getPasswordAttribute();
        if (.is(.)) {
            if (pwdAttr != null) {
                pwdAttr.access(new Accessor() {
                    @Override
                    public void access(BasicAttribute attr) {
                        try {
                            if (attr.get() != null && !attr.get().toString().isEmpty()) {
                                adAttrs.put(attr);
                            }
                        } catch (NamingException e) {
                            .error(e"Error retrieving password value");
                        }
                    }
                });
            }
            if (adAttrs.get(pwdAttrName) != null) {
                adAttrs.put("userAccountControl", Integer.toString(uacValue));
            } else {
                adAttrs.put("userAccountControl", Integer.toString( + ));
            }
        }
        final String entryDN = .getSchemaMapping().create(nameadAttrs);
        if (uccp != null) {
            // ---------------------------------
            // Change ntSecurityDescriptor
            // ---------------------------------
            .getInitialContext().modifyAttributes(entryDNnew ModificationItem[] {
                new ModificationItem(.utils.userCannotChangePassword(entryDNuccp)) });
            // ---------------------------------
        }
        if (!isEmpty(ldapGroups)) {
            .addLdapGroupMemberships(entryDNldapGroups);
        }
        return .getSchemaMapping().createUid(entryDN);
    }
New to GrepCode? Check out our FAQ X