private void _wizard_Finish(object sender, EventArgs e)
        {
            try
            {
                if (!controlConfigurePolicyVulnerabilities1.OKToSave())
                {
                    DialogResult = DialogResult.None;
                    return;
                }

                List <RegisteredServer> serversToAdd    = null;
                List <RegisteredServer> serversToRemove = null;
                string dynamicSelection = null;
                bool   isDynamic;
                controlPolicyAddServers1.GetServers(true, out serversToAdd, out serversToRemove, out dynamicSelection, out isDynamic);

                int policyId = -1;
                policyId = Policy.AddPolicy(PolicyName, PolicyDescription, isDynamic, dynamicSelection, _policyInterview.InterviewName, _policyInterview.GetInterviewText());
                if (policyId != -1)
                {
                    // Notify controller that a new server was added.
                    Program.gController.SignalRefreshPoliciesEvent(0);

                    // Now add the metrics to the policy
                    m_policy = Policy.GetPolicy(policyId);
                    controlConfigurePolicyVulnerabilities1.SaveMetricChanges(m_policy);
                    m_policy.SavePolicyToRepository(Program.gController.Repository.ConnectionString);

                    // Now add the selected servers to the policy
                    foreach (RegisteredServer rServer in serversToAdd)
                    {
                        RegisteredServer.AddRegisteredServerToPolicy(rServer.RegisteredServerId, m_policy.PolicyId, m_policy.AssessmentId);
                    }
                }
            }
            catch (Exception ex)
            {
                string title = "Error Creating Policy";
                string msg   =
                    string.Format("Failed to Create policy {0} error message: {1}", textBox_PolicyName.Text,
                                  ex.Message);
                logX.loggerX.Error(msg);
                MsgBox.ShowError(title, msg);
            }
        }
        private void button_OK_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(textBox_PolicyName.Text.Trim()))
                {
                    ultraTabControl1.SelectedTab = ultraTabControl1.Tabs[(int)FormTabs.General];
                    textBox_PolicyName.Focus();
                    MsgBox.ShowError(ERRORTITLE_CREATE, ERRORMSGNAMEEMPTY);
                    DialogResult = DialogResult.None;
                    return;
                }

                if (m_policy.IsPolicy)
                {
                    // Is Policy name already used
                    Policy p = Program.gController.Repository.GetPolicy(textBox_PolicyName.Text);
                    if (p != null && p.PolicyId != m_policy.PolicyId)
                    {
                        ultraTabControl1.SelectedTab = ultraTabControl1.Tabs[(int)FormTabs.General];
                        textBox_PolicyName.Focus();
                        MsgBox.ShowError(ERRORTITLE_CREATE,
                                         string.Format(ERRORMSGDUP, textBox_PolicyName.Text));
                        DialogResult = DialogResult.None;
                        return;
                    }
                }
                else
                {
                    if (string.IsNullOrEmpty(textBox_PolicyName.Text))
                    {
                        ultraTabControl1.SelectedTab = ultraTabControl1.Tabs[(int)FormTabs.General];
                        textBox_PolicyName.Focus();
                        MsgBox.ShowError(ERRORTITLE_CREATE, ERRORMSGNAMEEMPTY);
                        DialogResult = DialogResult.None;
                        return;
                    }

                    Policy p = Program.gController.Repository.GetPolicy(m_policy.PolicyId);
                    if (p.HasAssessment(textBox_PolicyName.Text))
                    {
                        Policy a = p.GetAssessment(textBox_PolicyName.Text);
                        if (a.AssessmentId != m_policy.AssessmentId)
                        {
                            MsgBox.ShowError(ERRORTITLE_UPDATE,
                                             string.Format(ERRORMSGDUP, textBox_PolicyName.Text, p.PolicyName));
                            DialogResult = DialogResult.None;

                            Cursor = Cursors.Default;
                            return;
                        }
                    }
                }
                if (!controlConfigurePolicyVulnerabilities1.OKToSave())
                {
                    ultraTabControl1.SelectedTab = ultraTabControl1.Tabs[(int)FormTabs.SecurityChecks];
                    DialogResult = DialogResult.None;
                    return;
                }
                if (controlConfigurePolicyVulnerabilities1.NumSecurityChecks == 0)
                {
                    if (DialogResult.No == MsgBox.ShowWarningConfirm(WARNINGTITLE, ErrorMsgs.NoPolicyMetricsMsg))
                    {
                        ultraTabControl1.SelectedTab = ultraTabControl1.Tabs[(int)FormTabs.SecurityChecks];
                        DialogResult = DialogResult.None;
                        return;
                    }
                }
                if (controlPolicyAddServers1.NumServers == 0)
                {
                    if (DialogResult.No == MsgBox.ShowWarningConfirm(WARNINGTITLE, ErrorMsgs.NoPolicyServersMsg))
                    {
                        ultraTabControl1.SelectedTab = ultraTabControl1.Tabs[(int)FormTabs.Servers];
                        DialogResult = DialogResult.None;
                        return;
                    }
                }

                List <RegisteredServer> serversToAdd    = null;
                List <RegisteredServer> serversToRemove = null;
                string dynamicSelection = null;
                bool   isDynamic;
                controlPolicyAddServers1.GetServers(m_isCreateNew, out serversToAdd, out serversToRemove,
                                                    out dynamicSelection, out isDynamic);

                int policyId = -1;
                if (m_isCreateNew)
                {
                    policyId =
                        Policy.AddPolicy(textBox_PolicyName.Text, textBox_Description.Text, isDynamic, dynamicSelection, _policyInterview.InterviewName, _policyInterview.GetInterviewText());
                    if (policyId != -1)
                    {
                        m_policy = Policy.GetPolicy(policyId);
                        controlConfigurePolicyVulnerabilities1.SaveMetricChanges(m_policy);
                        m_policy.SavePolicyToRepository(Program.gController.Repository.ConnectionString);
                    }
                }
                else
                {
                    policyId = m_policy.PolicyId;
                    controlConfigurePolicyVulnerabilities1.SaveMetricChanges(m_policy);
                    if (m_policy.IsAssessment)
                    {
                        m_policy.AssessmentName        = textBox_PolicyName.Text;
                        m_policy.AssessmentDescription = textBox_Description.Text;
                        m_policy.AssessmentNotes       = _textBox_Notes.Text;
                    }
                    else
                    {
                        m_policy.SetPolicyName(textBox_PolicyName.Text);
                        m_policy.PolicyDescription = textBox_Description.Text;
                    }
                    m_policy.IsDynamic        = isDynamic;
                    m_policy.DynamicSelection = dynamicSelection;
                    m_policy.InterviewName    = _policyInterview.InterviewName;
                    m_policy.InterviewText    = _policyInterview.GetInterviewText();

                    m_policy.SavePolicyToRepository(Program.gController.Repository.ConnectionString);
                }

                //list should be empty if dynamic
                if (!isDynamic)
                {
                    foreach (RegisteredServer rServer in serversToRemove)
                    {
                        RegisteredServer.RemoveRegisteredServerFromPolicy(rServer.RegisteredServerId, m_policy.PolicyId, m_policy.AssessmentId);
                    }

                    foreach (RegisteredServer rServer in serversToAdd)
                    {
                        RegisteredServer.AddRegisteredServerToPolicy(rServer.RegisteredServerId, policyId, m_policy.AssessmentId);
                    }
                }

                Program.gController.SignalRefreshPoliciesEvent(0);
            }
            catch (Exception ex)
            {
                string title = ERRORTITLE_UPDATE;
                string msg   =
                    string.Format(ERRORMSGFMT, textBox_PolicyName.Text, ex.Message);
                if (m_isCreateNew)
                {
                    title = ERRORTITLE_CREATE;
                    msg   =
                        string.Format(ERRORMSGFMT, textBox_PolicyName.Text, ex.Message);
                }
                logX.loggerX.Error(msg);
                MsgBox.ShowError(title, msg);
            }
        }
Exemplo n.º 3
0
        private void button_OK_Click(object sender, EventArgs e)
        {
            try
            {
                // Is Policy name already used
                Policy p = Program.gController.Repository.GetPolicy(textBox_PolicyName.Text);
                if (p != null)
                {
                    MsgBox.ShowError("Error Creating Policy",
                                     string.Format("Cannot create policy {0}. A policy with the name you specified already exist. Specify a different policy name.", textBox_PolicyName.Text));
                    DialogResult = DialogResult.None;
                    return;
                }

                int policyId = -1;
                List <RegisteredServer> serversToAdd    = new List <RegisteredServer>();
                List <RegisteredServer> serversToRemove = new List <RegisteredServer>();

                foreach (ListViewItem server in listView_AddServers.CheckedItems)
                {
                    serversToAdd.Add((RegisteredServer)server.Tag);
                }

                if (!m_isCreateNew && m_orginalServersInPolicy != null)
                {
                    foreach (RegisteredServer r in m_orginalServersInPolicy)
                    {
                        bool isStillChecked = false;
                        foreach (ListViewItem server in listView_AddServers.CheckedItems)
                        {
                            if ((RegisteredServer)server.Tag == r)
                            {
                                isStillChecked = true;
                                break;
                            }
                        }
                        if (!isStillChecked)
                        {
                            serversToRemove.Add(r);
                        }
                    }

                    foreach (RegisteredServer rServer in serversToRemove)
                    {
                        RegisteredServer.RemoveRegisteredServerFromPolicy(rServer.RegisteredServerId, m_policy.PolicyId);
                    }
                }

                if (m_isCreateNew)
                {
                    policyId = Policy.AddPolicy(textBox_PolicyName.Text, false, string.Empty);
                    if (policyId != -1)
                    {
                        m_policy = Policy.GetPolicy(policyId);
                        controlConfigurePolicyVulnerabilities1.SaveMetricChanges(m_policy);
                        m_policy.SavePolicyToRepository(Program.gController.Repository.ConnectionString);
                    }
                }
                else
                {
                    controlConfigurePolicyVulnerabilities1.SaveMetricChanges(m_policy);
                    m_policy.SetPolicyName(textBox_PolicyName.Text);
                    m_policy.SavePolicyToRepository(Program.gController.Repository.ConnectionString);
                }

                foreach (RegisteredServer rServer in serversToAdd)
                {
                    RegisteredServer.AddRegisteredServerToPolicy(rServer.RegisteredServerId, policyId);
                }
            }
            catch (Exception ex)
            {
                string title = "Error Updating Policy";
                string msg   =
                    string.Format("Failed to Update policy {0} error message: {1}", textBox_PolicyName.Text, ex.Message);
                if (m_isCreateNew)
                {
                    title = "Error Creating Policy";
                    msg   =
                        string.Format("Failed to Create policy {0} error message: {1}", textBox_PolicyName.Text,
                                      ex.Message);
                }
                logX.loggerX.Error(msg);
                MsgBox.ShowError(title, msg);
            }
        }