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;
 
 
 public class DirSyncUtils {
 
     private static final Log LOG = Log.getLog(DirSyncUtils.class);
 
     public static String createDirSyncUFilter(final ADConfiguration conffinal ADUtilities utils) {
 
         final String isDeleted = String.valueOf(conf.isRetrieveDeletedUser()).toUpperCase();
 
         final StringBuilder filter = new StringBuilder();
         final StringBuilder mfilter = new StringBuilder();
         final StringBuilder ufilter = new StringBuilder();
 
         mfilter.append("(objectClass=group)");
         
         ufilter.append(utils.getMembershipSearchFilter(conf));     
         
         ufilter.insert(0, "(&(objectClass=user)").append(")");
 
         filter.append("(|").append(ufilter).append(mfilter).
                 append("(&(isDeleted=").append(isDeleted).append(")(objectClass=user)))");
 
         return filter.toString();
     }
 
     public static String createDirSyncGFilter(final ADConfiguration conf) {
 
         final StringBuilder filter = new StringBuilder();
 
         if (conf.isRetrieveDeletedGroup()) {
             filter.append("(objectClass=group)");
         } else {
             filter.append("(&(objectClass=group)(! (isDeleted=TRUE)))");
         }
 
         return filter.toString();
     }
 
     public static String createLdapUFilter(final ADConfiguration conf) {
 
         final String[] memberships = conf.getMemberships();
 
         final String isDeleted = String.valueOf(conf.isRetrieveDeletedUser()).toUpperCase();
 
         final StringBuilder filter = new StringBuilder();
         final StringBuilder mfilter = new StringBuilder();
         final StringBuilder ufilter = new StringBuilder();
 
         if (memberships != null && memberships.length > 0) {
             mfilter.append("(&(objectClass=group)(|");
             ufilter.append(conf.isMembershipsInOr() ? "(|" : "(&");
 
             for (String group : memberships) {
                 mfilter.append("(distinguishedName=").append(group).append(")");
                 ufilter.append("(memberOf=").append(group).append(")");
             }
 
             ufilter.append(")");
             mfilter.append("))");
         }
 
         ufilter.insert(0, "(&(objectClass=user)").append(")");
 
         filter.append("(|").append(ufilter).append(mfilter).
                 append("(&(isDeleted=").append(isDeleted).append(")(objectClass=user)))");
 
        return filter.toString();
    }
    private static String AddLeadingZero(int k) {
        return (k <= 0xF)
                ? "0" + Integer.toHexString(k) : Integer.toHexString(k);
    }
    public static String getGuidAsString(byte[] GUID) {
        String strGUID = "";
        String byteGUID = "";
        for (int c = 0; c < GUID.lengthc++) {
            byteGUID = byteGUID + "\\" + AddLeadingZero((intGUID[c] & 0xFF);
        }
        //convert the GUID into string format
        strGUID = strGUID + AddLeadingZero((intGUID[3] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[2] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[1] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[0] & 0xFF);
        strGUID = strGUID + "-";
        strGUID = strGUID + AddLeadingZero((intGUID[5] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[4] & 0xFF);
        strGUID = strGUID + "-";
        strGUID = strGUID + AddLeadingZero((intGUID[7] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[6] & 0xFF);
        strGUID = strGUID + "-";
        strGUID = strGUID + AddLeadingZero((intGUID[8] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[9] & 0xFF);
        strGUID = strGUID + "-";
        strGUID = strGUID + AddLeadingZero((intGUID[10] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[11] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[12] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[13] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[14] & 0xFF);
        strGUID = strGUID + AddLeadingZero((intGUID[15] & 0xFF);
        return strGUID;
    }

    
Verify custom filter (used to validate any retrieved user).

Parameters:
ctx ldap context.
dn user distinguished name.
conf connector configuration.
Returns:
TRUE if verified; FALSE otherwise.
    public static boolean verifyCustomFilter(
            final LdapContext ctx,
            final String dn,
            final ADConfiguration conf) {
        return verifyFilter(ctxdngetFilter(conf));
    }

    
Verify complete filter including the custom one. This method is used to validate users 'IN' group.

Parameters:
conf connector configuration.
Returns:
TRUE if verified; FALSE otherwise.
    public static String getUserFilter(final ADConfiguration conf) {
        final StringBuilder filter = new StringBuilder();
        filter.append("(&(").append(createLdapUFilter(conf)).append(")");
        filter.append(getFilter(conf) != null ? getFilter(conf) : "").append(")");
        return filter.toString();
    }
    public static boolean verifyFilter(
            final LdapContext ctx,
            final String dn,
            final String filter) {
        final SearchControls searchCtls = LdapInternalSearch.createDefaultSearchControls();
        searchCtls.setSearchScope(.);
        searchCtls.setReturningAttributes(new String[]{});
        boolean found = true;
        if (StringUtil.isNotBlank(filter)) {
            try {
                final NamingEnumeration res = ctx.search(dnfiltersearchCtls);
                found = res != null && res.hasMoreElements();
            } catch (NamingException ex) {
                .warn(ex"Error searching for {0}"filter);
                found = false;
            }
        }
        return found;
    }
    private static String getFilter(final AbstractConfiguration conf) {
        return ((ADConfigurationconf).getAccountSearchFilter();
    }
New to GrepCode? Check out our FAQ X