Example #1
0
        public void AddModification_Modification_Adds()
        {
            //prepare
            var mod = new DirectoryAttributeModification {
                Name = "Test"
            };

            //act
            _attributes.AddModification(mod);

            //assert
            _attributes.GetChangedAttributes().Should().Contain(mod);
        }
        /// <summary>
        /// Adds the entry to the directory.
        /// </summary>
        /// <param name="connection">The connection to the directory.</param>
        /// <param name="entry">The entry to add.</param>
        /// <param name="log">The log for query information. Defaults to null.</param>
        /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param>
        /// <param name="listeners">The event listeners to be notified.</param>
        /// <param name="resultProcessing">How the async results are processed</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="connection"/> or <paramref name="entry"/>> is null.
        /// </exception>
        /// <exception cref="DirectoryOperationException">Thrown if the add was not successful.</exception>
        /// <exception cref="LdapException">Thrown if the operation fails.</exception>
        public static async Task AddAsync(this LdapConnection connection, IDirectoryAttributes entry, ILinqToLdapLogger log = null,
                                          DirectoryControl[] controls = null, IEnumerable <IAddEventListener> listeners = null, PartialResultProcessing resultProcessing = LdapConfiguration.DefaultAsyncResultProcessing)
        {
            string distinguishedName = null;

            try
            {
                if (connection == null)
                {
                    throw new ArgumentNullException("connection");
                }
                if (entry == null)
                {
                    throw new ArgumentNullException("entry");
                }
                distinguishedName = entry.DistinguishedName;

                if (distinguishedName.IsNullOrEmpty())
                {
                    throw new ArgumentException("entry.DistinguishedName is invalid.");
                }

                var request = new AddRequest(distinguishedName, entry.GetChangedAttributes().Where(da => da.Count > 0).ToArray());
                if (controls != null)
                {
                    request.Controls.AddRange(controls);
                }

                if (listeners != null)
                {
                    var args = new ListenerPreArgs <object, AddRequest>(entry, request, connection);
                    foreach (var eventListener in listeners.OfType <IPreAddEventListener>())
                    {
                        eventListener.Notify(args);
                    }
                }

                if (log != null && log.TraceEnabled)
                {
                    log.Trace(request.ToLogString());
                }

                AddResponse response = null;
#if NET45
                await Task.Factory.FromAsync(
                    (callback, state) =>
                {
                    return(connection.BeginSendRequest(request, resultProcessing, callback, state));
                },
                    (asyncresult) =>
                {
                    response = (AddResponse)connection.EndSendRequest(asyncresult);
                    response.AssertSuccess();
                },
                    null
                    ).ConfigureAwait(false);
#else
                response = await Task.Run(() => connection.SendRequest(request) as AddResponse).ConfigureAwait(false);

                response.AssertSuccess();
#endif

                if (listeners != null)
                {
                    var args = new ListenerPostArgs <object, AddRequest, AddResponse>(entry, request, response, connection);
                    foreach (var eventListener in listeners.OfType <IPostAddEventListener>())
                    {
                        eventListener.Notify(args);
                    }
                }
            }
            catch (Exception ex)
            {
                if (log != null)
                {
                    log.Error(ex, string.Format("An error occurred while trying to add '{0}'.", distinguishedName));
                }
                throw;
            }
        }
Example #3
0
        /// <summary>
        /// Adds the entry to the directory.
        /// </summary>
        /// <param name="connection">The connection to the directory.</param>
        /// <param name="entry">The entry to add.</param>
        /// <param name="log">The log for query information. Defaults to null.</param>
        /// <param name="controls">Any <see cref="DirectoryControl"/>s to be sent with the request</param>
        /// <param name="listeners">The event listeners to be notified.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="connection"/> or <paramref name="entry"/>> is null.
        /// </exception>
        /// <exception cref="DirectoryOperationException">Thrown if the add was not successful.</exception>
        /// <exception cref="LdapException">Thrown if the operation fails.</exception>
        public static void Add(this LdapConnection connection, IDirectoryAttributes entry, ILinqToLdapLogger log = null,
                               DirectoryControl[] controls = null, IEnumerable <IAddEventListener> listeners = null)
        {
            string distinguishedName = null;

            try
            {
                if (connection == null)
                {
                    throw new ArgumentNullException("connection");
                }
                if (entry == null)
                {
                    throw new ArgumentNullException("entry");
                }
                distinguishedName = entry.DistinguishedName;

                if (distinguishedName.IsNullOrEmpty())
                {
                    throw new ArgumentException("entry.DistinguishedName is invalid.");
                }

                var request = new AddRequest(distinguishedName, entry.GetChangedAttributes().Where(da => da.Count > 0).ToArray());
                if (controls != null)
                {
                    request.Controls.AddRange(controls);
                }

                if (listeners != null)
                {
                    var args = new ListenerPreArgs <object, AddRequest>(entry, request, connection);
                    foreach (var eventListener in listeners.OfType <IPreAddEventListener>())
                    {
                        eventListener.Notify(args);
                    }
                }

                if (log != null && log.TraceEnabled)
                {
                    log.Trace(request.ToLogString());
                }

                var response = connection.SendRequest(request) as AddResponse;
                response.AssertSuccess();

                if (listeners != null)
                {
                    var args = new ListenerPostArgs <object, AddRequest, AddResponse>(entry, request, response, connection);
                    foreach (var eventListener in listeners.OfType <IPostAddEventListener>())
                    {
                        eventListener.Notify(args);
                    }
                }
            }
            catch (Exception ex)
            {
                if (log != null)
                {
                    log.Error(ex, string.Format("An error occurred while trying to add '{0}'.", distinguishedName));
                }
                throw;
            }
        }