예제 #1
0
파일: LDAPFilter.cs 프로젝트: telefrek/ldap
        /// <summary>
        /// Tries to combine two substring filters
        /// </summary>
        /// <param name="filter">The original substring filter</param>
        /// <param name="addition">The second substring filter to add</param>
        /// <returns>A new filter, if the combination is valid</returns>
        public static LDAPFilter TryCombine(this LDAPFilter filter, LDAPFilter addition)
        {
            if (filter == null)
            {
                throw new ArgumentNullException("filter was null");
            }
            if (addition == null)
            {
                throw new ArgumentNullException("addition was null");
            }

            // Validate the filter type
            if (filter.FilterType == LDAPFilterType.Substring && addition.FilterType == filter.FilterType)
            {
                // Validate they both have substrings
                if (filter.Substrings.Count > 0 && addition.Substrings.Count > 0)
                {
                    if (filter.Substrings.Count(s => s.SubstringType == LDAPSubstringType.Initial) +
                        filter.Substrings.Count(s => s.SubstringType == LDAPSubstringType.Initial) > 1)
                    {
                        return(null);
                    }

                    if (filter.Substrings.Count(s => s.SubstringType == LDAPSubstringType.Final) +
                        filter.Substrings.Count(s => s.SubstringType == LDAPSubstringType.Final) > 1)
                    {
                        return(null);
                    }

                    var combined = new LDAPFilter
                    {
                        FilterType = LDAPFilterType.Substring
                    };

                    foreach (var sub in filter.Substrings)
                    {
                        combined.Substrings.Add(sub);
                    }

                    foreach (var sub in addition.Substrings)
                    {
                        combined.Substrings.Add(sub);
                    }

                    return(combined);
                }
            }

            return(null);
        }
예제 #2
0
        /// <summary>
        /// Stub for now
        /// </summary>
        /// <param name="dn"></param>
        /// <param name="scope"></param>
        /// <param name="aliasing"></param>
        /// <param name="filter"></param>
        /// <param name="attributes"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task <LDAPResult> TrySearch(string dn, LDAPScope scope, LDAPAliasDereferencing aliasing, LDAPFilter filter, string[] attributes, CancellationToken token)
        {
            _log.LogInformation("Searching for {0}", dn);
            var op = new SearchRequest {
                DistinguishedName = dn, Scope = scope, Aliasing = aliasing, Filter = filter, Attributes = attributes
            };
            var objList = new List <LDAPObject>();
            var result  = new LDAPResult
            {
                Objects     = objList,
                IsStreaming = false,
            };

            foreach (var msg in await _connection.TryQueueOperation(op, token))
            {
                _log.LogInformation("Received {0}", msg.Operation);
                switch (msg.Operation)
                {
                case ProtocolOp.SEARCH_RESPONSE:
                    var sResponse = msg as SearchResponse;
                    _log.LogInformation("Found {0}", sResponse.DistinguishedName);
                    if (!string.IsNullOrWhiteSpace(sResponse.DistinguishedName))
                    {
                        objList.Add(new LDAPObject {
                            DistinguishedName = sResponse.DistinguishedName, Attributes = new List <LDAPAttribute>(sResponse.Attributes)
                        });
                    }
                    break;

                case ProtocolOp.SEARCH_RESULT:
                    var sResult = msg as SearchResult;
                    result.ResultCode    = (LDAPResultCode)sResult.ResultCode;
                    result.WasSuccessful = sResult.ResultCode == 0;
                    break;
                }
            }

            return(result);
        }
예제 #3
0
 /// <summary>
 /// Stub for now
 /// </summary>
 /// <param name="dn"></param>
 /// <param name="scope"></param>
 /// <param name="aliasing"></param>
 /// <param name="filter"></param>
 /// <param name="token"></param>
 /// <returns></returns>
 public async Task <LDAPResult> TrySearch(string dn, LDAPScope scope, LDAPAliasDereferencing aliasing, LDAPFilter filter, CancellationToken token) =>
 await TrySearch(dn, scope, aliasing, filter, null, token);