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.util;
 
 import static org.connid.bundles.ad.ADConnector.UACCONTROL_ATTR;
 import static org.connid.bundles.ad.ADConnector.UF_ACCOUNTDISABLE;
 import static org.identityconnectors.common.CollectionUtil.newCaseInsensitiveSet;
 import static org.identityconnectors.common.CollectionUtil.newSet;
 
 import java.util.List;
 import java.util.Set;
 
 public class ADUtilities {
 
     private final Log LOG = Log.getLog(ADUtilities.class);
 
     private ADConnection connection;
 
     private GroupHelper groupHelper;
 
     public ADUtilities(final ADConnection connection) {
         this. = connection;
          = new GroupHelper(connection);
     }
 
     public Set<StringgetAttributesToGet(final String[] attributesToGetfinal ObjectClass oclass) {
         final Set<Stringresult;
 
         if (attributesToGet != null) {
             result = CollectionUtil.newCaseInsensitiveSet();
             result.addAll(Arrays.asList(attributesToGet));
             removeNonReadableAttributes(resultoclass);
             result.add(.);
         } else {
             // This should include Name.NAME.
             result = getAttributesReturnedByDefault(oclass);
         }
 
         // Uid is required to build a ConnectorObject.
         result.add(.);
 
         if (oclass == .) {
             // AD specific, for checking wether a user is enabled or not
             result.add();
         }
 
         // Our password is marked as readable because of sync().
         // We really can't return it from search.
        if (result.contains(.)) {
            .warn("Reading passwords not supported");
        }
        return result;
    }
    private void removeNonReadableAttributes(final Set<Stringattributesfinal ObjectClass oclass) {
        // Since the groups attributes are fake attributes, we don't want to
        // send them to LdapSchemaMapping. This, for example, avoid an 
        // (unlikely) conflict with a custom attribute defined in the server
        // schema.
        boolean ldapGroups = attributes.remove(.);
        boolean posixGroups = attributes.remove(.);
        .getSchemaMapping().removeNonReadableAttributes(oclassattributes);
        if (ldapGroups) {
            attributes.add(.);
        }
        if (posixGroups) {
            attributes.add(.);
        }
    }
    public static Set<StringgetAttributesReturnedByDefault(final LdapConnection connfinal ObjectClass oclass) {
        if (oclass.equals(.)) {
            return newSet(.);
        }
        final Set<Stringresult = newCaseInsensitiveSet();
        final ObjectClassInfo oci = conn.getSchemaMapping().schema().findObjectClassInfo(oclass.getObjectClassValue());
        if (oci != null) {
            for (AttributeInfo info : oci.getAttributeInfo()) {
                if (info.isReturnedByDefault()) {
                    result.add(info.getName());
                }
            }
        }
        return result;
    }
    public Set<StringgetLdapAttributesToGet(final Set<StringattrsToGetfinal ObjectClass oclass) {
        final Set<StringcleanAttrsToGet = newCaseInsensitiveSet();
        cleanAttrsToGet.addAll(attrsToGet);
        cleanAttrsToGet.remove(.);
        boolean posixGroups = cleanAttrsToGet.remove(.);
        final Set<Stringresult = .getSchemaMapping().getLdapAttributes(oclasscleanAttrsToGettrue);
        if (posixGroups) {
            result.add(GroupHelper.getPosixRefAttribute());
        }
        return result;
    }
            final String baseDN,
            final SearchResult result,
            final Collection<StringattrsToGet,
            final ObjectClass oclass)
            throws NamingException {
        return createConnectorObject(baseDNresult.getAttributes(), attrsToGetoclass);
    }
            final String baseDN,
            final Attributes profile,
            final Collection<StringattrsToGet,
            final ObjectClass oclass)
            throws NamingException {
        final LdapEntry entry = LdapEntry.create(baseDNprofile);
        final ConnectorObjectBuilder builder = new ConnectorObjectBuilder();
        builder.setObjectClass(oclass);
        builder.setUid(.getSchemaMapping().createUid(oclassentry));
        builder.setName(.getSchemaMapping().createName(oclassentry));
        for (String attributeName : attrsToGet) {
            Attribute attribute = null;
            if (LdapConstants.isLdapGroups(attributeName)) {
                final List<StringldapGroups = .getLdapGroups(entry.getDN().toString());
                attribute = AttributeBuilder.build(.ldapGroups);
            } else if (LdapConstants.isPosixGroups(attributeName)) {
                final Set<StringposixRefAttrs =
                        LdapUtil.getStringAttrValues(entry.getAttributes(), GroupHelper.getPosixRefAttribute());
                final List<StringposixGroups = .getPosixGroups(posixRefAttrs);
                attribute = AttributeBuilder.build(.posixGroups);
            } else if (..is(attributeName) && oclass == .) {
                // IMPORTANT!!! Return empty guarded string
                attribute = AttributeBuilder.build(attributeNamenew GuardedString());
            } else if (.equals(attributeName) && oclass == .) {
                try {
                    final String status =
                            profile.get() == null || profile.get().get() == null
                            ? null : profile.get().get().toString();
                    if (.isOk()) {
                        .ok("User Account Control: {0}"status);
                    }
                    // enabled if UF_ACCOUNTDISABLE is not included (0x00002)
                    if (status == null || Integer.parseInt(
                            profile.get().get().toString())
                            % 16 != ) {
                        attribute = AttributeBuilder.buildEnabled(true);
                    } else {
                        attribute = AttributeBuilder.buildEnabled(false);
                    }
                } catch (NamingException e) {
                    .error(e"While fetching " + );
                }
            } else {
                if (profile.get(attributeName) != null) {
                    attribute = .getSchemaMapping().createAttribute(oclassattributeNameentryfalse);
                }
            }
            // Avoid attribute adding in case of attribute name not found
            if (attribute != null) {
                builder.addAttribute(attribute);
            }
        }
        return builder.build();
    }
            final String baseDN,
            final Uid uid,
            final Attributes profile,
            final ObjectClass oclass)
            throws NamingException {
        final LdapEntry entry = LdapEntry.create(baseDNprofile);
        final ConnectorObjectBuilder builder = new ConnectorObjectBuilder();
        builder.setObjectClass(oclass);
        builder.setUid(uid);
        builder.setName("fake-dn");
        builder.addAttributes(Collections.<Attribute>emptySet());
        return builder.build();
    }

    
Create a DN string starting from a set attributes and a default people container. This method has to be used if __NAME__ attribute is not provided or it it is not a DN.

Parameters:
attrs set of user attributes.
defaulContainer default people container.
Returns:
distinguished name string.
    public final String getDN(final ObjectClass oclassfinal Set<Attributeattrs) {
        String cn;
        final Attribute cnAttr = AttributeUtil.find("cn"attrs);
        if (cnAttr == null || cnAttr.getValue() == null
                || cnAttr.getValue().isEmpty()
                || StringUtil.isBlank(cnAttr.getValue().get(0).toString())) {
            // Get the name attribute and consider this as the principal name.
            // Use the principal name as the CN to generate DN.
            cn = AttributeUtil.getNameFromAttributes(attrs).getNameValue();
        } else {
            // Get the common name and use this to generate the DN.
            cn = cnAttr.getValue().get(0).toString();
        }
        return "cn=" + cn + ","
                + (oclass == .
                ? ((ADConfiguration) (.getConfiguration())).getDefaultPeopleContainer()
                : ((ADConfiguration) (.getConfiguration())).getDefaultGroupContainer());
    }

    
Check if the String is an ldap DN.

Parameters:
dn string to be checked.
Returns:
TRUE if the value provided is a DN; FALSE otherwise.
    public final boolean isDN(final String dn) {
        try {
            return StringUtil.isNotBlank(dn) && new LdapName(dn) != null;
        } catch (InvalidNameException ex) {
            return false;
        }
    }
New to GrepCode? Check out our FAQ X