Example #1
0
        /// <summary>
        /// Try to add a record to the directory
        /// </summary>
        /// <param name="obj">The LDAP Entity to add</param>
        /// <param name="add"></param>
        /// <param name="remove"></param>
        /// <param name="update"></param>
        /// <param name="token"></param>
        /// <returns>True if successful</returns>
        public async Task <LDAPResult> TryModify(LDAPObject obj, ICollection <LDAPAttribute> add, ICollection <LDAPAttribute> remove, ICollection <LDAPAttribute> update, CancellationToken token)
        {
            var op = new ModifyRequest {
                DistinguishedName = obj.DistinguishedName
            };

            if (add != null && add.Count > 0)
            {
                op.Added = add.ToArray();
            }

            if (remove != null && remove.Count > 0)
            {
                op.Removed = remove.ToArray();
            }

            if (update != null && update.Count > 0)
            {
                op.Modified = update.ToArray();
            }

            var objList = new List <LDAPObject>();
            var result  = new LDAPResult
            {
                Objects     = objList,
                IsStreaming = false,
            };

            foreach (var msg in await _connection.TryQueueOperation(op, token))
            {
                result.ResultCode    = (LDAPResultCode)msg.ResultCode;
                result.WasSuccessful = msg.ResultCode == 0;

                // Modify the attributes
                if (result.WasSuccessful)
                {
                    foreach (var attr in add ?? new List <LDAPAttribute>())
                    {
                        obj.Attributes.Add(attr);
                    }

                    foreach (var attr in update ?? new List <LDAPAttribute>())
                    {
                        obj.Attributes.RemoveAll((p) => p.Description.Equals(attr.Description));
                        obj.Attributes.Add(attr);
                    }

                    foreach (var attr in remove ?? new List <LDAPAttribute>())
                    {
                        obj.Attributes.RemoveAll((p) => p.Description.Equals(attr.Description));
                    }
                }

                objList.Add(obj);
                break;
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// Try to remove a record from the directory
        /// </summary>
        /// <param name="obj">The entity to remove</param>
        /// <param name="token"></param>
        /// <returns>True if successful</returns>
        public async Task <LDAPResult> TryRemove(LDAPObject obj, CancellationToken token)
        {
            var op = new DeleteRequest {
                DistinguishedName = obj.DistinguishedName
            };
            var objList = new List <LDAPObject>();
            var result  = new LDAPResult
            {
                Objects     = objList,
                IsStreaming = false,
            };

            foreach (var msg in await _connection.TryQueueOperation(op, token))
            {
                result.ResultCode    = (LDAPResultCode)msg.ResultCode;
                result.WasSuccessful = msg.ResultCode == 0;
                objList.Add(obj);
                break;
            }

            return(result);
        }
Example #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="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);
        }