Exemplo n.º 1
0
        /// <summary>
        /// Runs the rules
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">args</param>
        private void Run(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (!(e.Argument is RulesRunnerDoWorkArgs args))
                {
                    e.Result = null;
                    throw new ArgumentException("RulesRunner expects arguments of type RulesRunnerDoWorkArgs.");
                }

                var stopwatch = DateTime.Now;

                // clear out our duplicate tracking each run
                DuplicateStore.Reset();

                // create the connection manager and bubble up any messages
                var connections = new ConnectionManager();
                connections.OnStatusUpdate += (string message) => { this.OnStatusUpdate?.Invoke(message); };

                // used to collect the results as we process rule collections
                var results = new RulesRunnerResult();

                connections.WithConnections((LdapConnection connection, string distinguishedName) =>
                {
                    if (this.CancellationPending)
                    {
                        e.Result = null;
                        return;
                    }

                    // we try and create a key from the queried directory and fail to random guid
                    var servers    = ((LdapDirectoryIdentifier)connection.Directory).Servers;
                    var identifier = servers.Length > 0 ? servers.First() : Guid.NewGuid().ToString("D");

                    // get the rule collection to run
                    var ruleCollection = this.GetRuleCollection(distinguishedName);

                    // runs the rule collection
                    var result = this.ExecuteRuleCollection(ruleCollection, connection);

                    // handle the cancel case
                    if (result == null && this.CancellationPending)
                    {
                        e.Result = null;
                        return;
                    }

                    // add our result to the list
                    results.Add(identifier, result);
                });

                // we pass back the collection of all results for processing into the UI grid
                e.Result = results;
            }
            catch (Exception err)
            {
                this.OnStatusUpdate?.Invoke("Error in RulesRunner: " + err.Message);
                e.Result = err;
            }
            finally
            {
                DuplicateStore.Reset();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Runs the rules
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">args</param>
        private void Run(object sender, DoWorkEventArgs e)
        {
            try
            {
                if (!(e.Argument is RulesRunnerDoWorkArgs args))
                {
                    e.Result = null;
                    throw new ArgumentException("RulesRunner expects arguments of type RulesRunnerDoWorkArgs.");
                }

                var stopwatch = DateTime.Now;

                // clear out our duplicate tracking each run
                DuplicateStore.Reset();

                // create the connection manager and bubble up any messages
                var connections = new ConnectionManager();
                connections.OnStatusUpdate += (string message) => { this.OnStatusUpdate?.Invoke(message); };

                // used to collect the results as we process rule collections
                var results = new RulesRunnerResult();

                connections.WithConnections((LdapConnection connection, string distinguishedName) =>
                {
                    if (this.CancellationPending)
                    {
                        e.Result = null;
                        return;
                    }

                    // we try and create a key from the queried directory and fail to random guid
                    var servers    = ((LdapDirectoryIdentifier)connection.Directory).Servers;
                    var identifier = servers.Length > 0 ? servers.First() : Guid.NewGuid().ToString("D");

                    // get the rule collection to run
                    var ruleCollection = this.GetRuleCollection(distinguishedName);

                    // if connecting to Global Catalog Server, make sure the collection's analyzed attributes are replicated.
                    if (SettingsManager.Instance.Port == Constants.GlobalCatalogPort)
                    {
                        var schemaResult = this.ExecuteRuleCollectionSchemaCheck(ruleCollection, connection);

                        // handle the cancel case
                        if (schemaResult == null && this.CancellationPending)
                        {
                            e.Result = null;
                            return;
                        }

                        // display errors
                        if (schemaResult.Count > 0)
                        {
                            DialogResult dialogResult = DialogResult.None;

                            // Show message box on main thread.
                            FormApp.Instance.Invoke(new Action(() =>
                            {
                                var message  = string.Format(StringLiterals.SchemaWarningMessage, Environment.NewLine + string.Join(Environment.NewLine, schemaResult.ToArray()));
                                dialogResult = MessageBox.Show(FormApp.Instance, message, StringLiterals.SchemaWarningTitle, MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                            }));

                            if (dialogResult == DialogResult.No)
                            {
                                return;
                            }
                        }
                    }

                    // runs the rule collection
                    var result = this.ExecuteRuleCollection(ruleCollection, connection);

                    // handle the cancel case
                    if (result == null && this.CancellationPending)
                    {
                        e.Result = null;
                        return;
                    }

                    // add our result to the list
                    results.Add(identifier, result);
                });

                // we pass back the collection of all results for processing into the UI grid
                e.Result = results;
            }
            catch (Exception err)
            {
                this.OnStatusUpdate?.Invoke("Error in RulesRunner: " + err.Message);
                e.Result = err;
            }
            finally
            {
                DuplicateStore.Reset();
            }
        }