Ejemplo n.º 1
0
        public bool Execute([NotNull] IIdentity identity)
        {
            bool result = false;

            if (identity is IThreatEvent threatEvent)
            {
                using (var dialog = new ThreatEventScenarioCreationDialog(threatEvent))
                {
                    result = dialog.ShowDialog(Form.ActiveForm) == DialogResult.OK;
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        public void ExecuteCustomAction([NotNull] IActionDefinition action)
        {
            string text    = null;
            bool   warning = false;

            try
            {
                switch (action.Name)
                {
                case "AddScenario":
                    if (_properties.Item is IThreatEvent threatEvent)
                    {
                        using (var dialog = new ThreatEventScenarioCreationDialog(threatEvent))
                        {
                            if (dialog.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                            {
                                text = "Scenario creation";
                            }
                        }
                    }
                    break;

                case "AddMitigation":
                    if (_properties.Item is IThreatEvent threatEvent2)
                    {
                        using (var dialog = new ThreatEventMitigationSelectionDialog(threatEvent2))
                        {
                            if (dialog.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                            {
                                text = "Mitigation creation";
                            }
                        }
                    }
                    break;

                case "RemoveScenario":
                    var selected = _currentRow?.GridPanel?.SelectedCells?.OfType <GridCell>()
                                   .Select(x => x.GridRow)
                                   .Where(x => x.Tag is IThreatEventScenario)
                                   .Distinct()
                                   .ToArray();

                    if (_currentRow != null)
                    {
                        if ((selected?.Length ?? 0) > 1)
                        {
                            var outcome = MessageBox.Show(Form.ActiveForm,
                                                          $"You have selected {selected.Length} Scenarios. Do you want to remove them all?\nPlease click 'Yes' to remove all selected Scenarios,\nNo to remove only the last one you selected, '{_currentRow.Tag?.ToString()}'.\nPress Cancel to abort.",
                                                          "Remove Scenarios", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning,
                                                          MessageBoxDefaultButton.Button3);
                            switch (outcome)
                            {
                            case DialogResult.Yes:
                                bool removed = true;
                                foreach (var row in selected)
                                {
                                    bool r = false;
                                    if (row.Tag is IThreatEventScenario s)
                                    {
                                        r = s.ThreatEvent.RemoveScenario(s.Id);
                                    }

                                    removed &= r;

                                    if (r && row == _currentRow)
                                    {
                                        _properties.Item = null;
                                        _currentRow      = null;
                                    }
                                }

                                if (removed)
                                {
                                    text = "Remove Scenarios";
                                }
                                else
                                {
                                    warning = true;
                                    text    = "One or more Scenarios cannot be removed.";
                                }

                                break;

                            case DialogResult.No:
                                if (_currentRow != null && _currentRow.Tag is IThreatEventScenario s2)
                                {
                                    if (s2.ThreatEvent.RemoveScenario(s2.Id))
                                    {
                                        _properties.Item = null;
                                        _currentRow      = null;
                                        text             = "Remove Scenario";
                                    }
                                    else
                                    {
                                        warning = true;
                                        text    = "The Scenario cannot be removed.";
                                    }
                                }

                                break;
                            }
                        }
                        else if (_currentRow?.Tag is IThreatEventScenario scenario &&
                                 MessageBox.Show(Form.ActiveForm,
                                                 $"You are about to remove scenario '{scenario.Name}' from the current Threat Event. Are you sure?",
                                                 "Remove Scenario", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                                 MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        {
                            if (scenario.ThreatEvent.RemoveScenario(scenario.Id))
                            {
                                text             = "Remove Scenario";
                                _properties.Item = null;
                            }
                            else
                            {
                                warning = true;
                                text    = "The Scenario cannot be removed.";
                            }
                        }
                    }
                    break;

                case "RemoveMitigation":
                    var selected2 = _currentRow?.GridPanel?.SelectedCells?.OfType <GridCell>()
                                    .Select(x => x.GridRow)
                                    .Where(x => x.Tag is IThreatEventMitigation)
                                    .Distinct()
                                    .ToArray();

                    if (_currentRow != null)
                    {
                        if ((selected2?.Length ?? 0) > 1)
                        {
                            var name    = (_currentRow.Tag as IThreatEventMitigation)?.Mitigation.Name;
                            var outcome = MessageBox.Show(Form.ActiveForm,
                                                          $"You have selected {selected2.Length} Mitigation associations. Do you want to remove them all?\nPlease click 'Yes' to remove all selected Mitigation associations,\nNo to remove only the last one you selected, '{name}'.\nPress Cancel to abort.",
                                                          "Remove Mitigation association", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning,
                                                          MessageBoxDefaultButton.Button3);
                            switch (outcome)
                            {
                            case DialogResult.Yes:
                                bool removed = true;
                                foreach (var row in selected2)
                                {
                                    bool r = false;
                                    if (row.Tag is IThreatEventMitigation m)
                                    {
                                        r = m.ThreatEvent.RemoveMitigation(m.MitigationId);
                                    }

                                    removed &= r;

                                    if (r && row == _currentRow)
                                    {
                                        _properties.Item = null;
                                        _currentRow      = null;
                                    }
                                }

                                if (removed)
                                {
                                    text = "Remove Mitigation association";
                                }
                                else
                                {
                                    warning = true;
                                    text    = "One or more Mitigation associations cannot be removed.";
                                }

                                break;

                            case DialogResult.No:
                                if (_currentRow != null && _currentRow.Tag is IThreatEventMitigation m2)
                                {
                                    if (m2.ThreatEvent.RemoveMitigation(m2.MitigationId))
                                    {
                                        _properties.Item = null;
                                        _currentRow      = null;
                                        text             = "Remove Mitigation association";
                                    }
                                    else
                                    {
                                        warning = true;
                                        text    = "The Mitigation association cannot be removed.";
                                    }
                                }

                                break;
                            }
                        }
                        else if (_currentRow?.Tag is IThreatEventMitigation mitigation &&
                                 MessageBox.Show(Form.ActiveForm,
                                                 $"You are about to remove mitigation '{mitigation.Mitigation.Name}' from the current Threat Event. Are you sure?",
                                                 "Remove Mitigation association", MessageBoxButtons.YesNo, MessageBoxIcon.Warning,
                                                 MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                        {
                            if (mitigation.ThreatEvent.RemoveMitigation(mitigation.MitigationId))
                            {
                                text             = "Remove Mitigation association";
                                _properties.Item = null;
                            }
                            else
                            {
                                warning = true;
                                text    = "The Mitigation association cannot be removed.";
                            }
                        }
                    }
                    break;

                case "OpenAllNodes":
                    try
                    {
                        _loading = true;
                        _grid.PrimaryGrid.ExpandAll(10);
                        Application.DoEvents();
                    }
                    finally
                    {
                        _loading = false;
                    }
                    break;

                case "OpenFirstLevel":
                    try
                    {
                        _loading = true;
                        _grid.PrimaryGrid.ExpandAll(0);
                        Application.DoEvents();
                    }
                    finally
                    {
                        _loading = false;
                    }
                    break;

                case "OpenBranch":
                    try
                    {
                        _loading = true;
                        if (_currentRow != null)
                        {
                            _currentRow.ExpandAll(10);
                            _currentRow.Expanded = true;
                        }
                    }
                    finally
                    {
                        _loading = false;
                    }
                    break;

                case "Collapse":
                    try
                    {
                        _loading = true;
                        _grid.PrimaryGrid.CollapseAll();
                    }
                    finally
                    {
                        _loading = false;
                    }
                    break;

                case "Refresh":
                    LoadModel();
                    break;
                }

                if (warning)
                {
                    ShowWarning?.Invoke(text);
                }
                else if (text != null)
                {
                    ShowMessage?.Invoke($"{text} has been executed successfully.");
                }
            }
            catch
            {
                ShowWarning?.Invoke($"An error occurred during the execution of the action.");
                throw;
            }
        }