Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaned up compile warnings #4167

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 46 additions & 8 deletions CSETWebApi/CSETWebCore.AutoResponder/EmailHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading.Tasks;
using System.Net.Mail;
using System.Net;
using NLog;

namespace CSETWebCore.AutoResponder
{
Expand All @@ -22,6 +23,11 @@ public EmailHelper(IConfiguration configuration, IResourceHelper resourceHelper)
this._resourceHelper = resourceHelper;

}


/// <summary>
///
/// </summary>
public void SendFollowUp(string email, string firstName, string lastName)
{
string templateFile = @"App_Data\assessmentFollowUpTemplate_CF.html";
Expand All @@ -34,14 +40,27 @@ public void SendFollowUp(string email, string firstName, string lastName)
message.Subject = "CSET Assessment Follow-up for Cybersecure Florida";
message.Body = bodyHtml;
message.To.Add(new MailAddress(email));
message.From = new MailAddress(
emailConfig.FirstOrDefault(x => x.Key == "Email:SenderEmail").Value,
emailConfig.FirstOrDefault(x => x.Key == "Email:SenderDisplayName").Value);

var configFromEmail = emailConfig.FirstOrDefault(x => x.Key == "Email:SenderEmail");
if (configFromEmail.Value == null)
{
// Don't send email because we don't have a "From:" address configured
LogManager.GetCurrentClassLogger().Error("No configuration for Email:SenderEmail");
return;
}

message.From = new MailAddress(configFromEmail.Value,
emailConfig.FirstOrDefault(x => x.Key == "Email:SenderDisplayName").Value);

message.IsBodyHtml = true;

SendMail(message);
}


/// <summary>
///
/// </summary>
public void SendWeekly(string attachmentPath, string email, string firstName, string lastName)
{
string templateFile = @"App_Data\weeklyTemplate_CF.html";
Expand All @@ -54,16 +73,25 @@ public void SendWeekly(string attachmentPath, string email, string firstName, st
message.Subject = "New Weekly Contacts for Cybersecure Florida";
message.Body = bodyHtml;
message.To.Add(new MailAddress(email));
message.From = new MailAddress(
emailConfig.FirstOrDefault(x => x.Key == "Email:SenderEmail").Value,
emailConfig.FirstOrDefault(x => x.Key == "Email:SenderDisplayName").Value);

var configFromEmail = emailConfig.FirstOrDefault(x => x.Key == "Email:SenderEmail");
if (configFromEmail.Value == null)
{
// Don't send email because we don't have a "From:" address configured
LogManager.GetCurrentClassLogger().Error("No configuration for Email:SenderEmail");
return;
}

message.From = new MailAddress(configFromEmail.Value,
emailConfig.FirstOrDefault(x => x.Key == "Email:SenderDisplayName").Value);

message.IsBodyHtml = true;
message.Attachments.Add(new Attachment(attachmentPath));

SendMail(message);
}


// <summary>
/// Sends a MailMessage using the configured SmtpClient.
/// See class level note (top of the file)
Expand All @@ -81,11 +109,21 @@ public void SendMail(MailMessage mail)
string footerCF = _resourceHelper.GetEmbeddedResource(@"App_Data\EmailFooter_CF.html");
mail.Body = mail.Body.Replace("{{email-footer-CF}}", footerCF);

var configSmtpHost = emailConfig.FirstOrDefault(x => x.Key == "Email:SmtpHost");

// if no mail host defined, don't send the email
if (configSmtpHost.Value == null)
{
LogManager.GetCurrentClassLogger().Error("No configuration for Email:SmtpHost");
return;
}


SmtpClient client = new SmtpClient
{
DeliveryMethod = SmtpDeliveryMethod.Network,
Host = emailConfig.FirstOrDefault(x => x.Key == "Email:SmtpHost").Value,
Port = int.Parse(emailConfig.FirstOrDefault(x => x.Key == "Email:SmtpPort").Value),
Host = configSmtpHost.Value,
Port = int.Parse(emailConfig.FirstOrDefault(x => x.Key == "Email:SmtpPort").Value ?? "25"),
UseDefaultCredentials = false
};

Expand Down
17 changes: 10 additions & 7 deletions CSETWebApi/CSETWebCore.AutoResponder/ExcelProcess.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
ο»Ώusing CSETWebCore.DataLayer.Model;
using NLog;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;

Expand Down Expand Up @@ -43,6 +44,7 @@
return clist.ToList();
}


private string CreateACopyOfTheFile()
{
string excelFile = @"Survey contacts.xlsx";
Expand All @@ -57,9 +59,9 @@

private void BuildContactsSheet(string excelFile, List<ContactData> data, string password)
{
Excel.Application excel = null;
Excel.Workbook workbook = null;
Excel.Worksheet worksheet = null;
Excel.Application? excel = null;
Excel.Workbook? workbook = null;
Excel.Worksheet? worksheet = null;
try
{
excel = new Excel.Application { Visible = false, DisplayAlerts = false };
Expand All @@ -84,13 +86,14 @@
catch (Exception ex) // Or System.Runtime.InteropServices.COMException
{
// Handle it or log or do nothing
LogManager.GetCurrentClassLogger().Error(ex);
}
finally
{
// Close Book and Excel and release COM Object
workbook?.Close(0);
excel?.Quit();
Marshal.ReleaseComObject(excel);

Check warning on line 96 in CSETWebApi/CSETWebCore.AutoResponder/ExcelProcess.cs

View workflow job for this annotation

GitHub Actions / .NET Analysis

Possible null reference argument for parameter 'o' in 'int Marshal.ReleaseComObject(object o)'.

Check warning on line 96 in CSETWebApi/CSETWebCore.AutoResponder/ExcelProcess.cs

View workflow job for this annotation

GitHub Actions / .NET Analysis

This call site is reachable on all platforms. 'Marshal.ReleaseComObject(object)' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)

Check warning on line 96 in CSETWebApi/CSETWebCore.AutoResponder/ExcelProcess.cs

View workflow job for this annotation

GitHub Actions / .NET Analysis

Possible null reference argument for parameter 'o' in 'int Marshal.ReleaseComObject(object o)'.

Check warning on line 96 in CSETWebApi/CSETWebCore.AutoResponder/ExcelProcess.cs

View workflow job for this annotation

GitHub Actions / .NET Analysis

This call site is reachable on all platforms. 'Marshal.ReleaseComObject(object)' is only supported on: 'windows'. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1416)
}
}
}
Expand All @@ -101,9 +104,9 @@
{
}

public string FirstName { get; set; }
public string LastName { get; set; }
public string OrganizationName { get; set; }
public string Email { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? OrganizationName { get; set; }
public string? Email { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace CSETWebCore.AutoResponder.Tests
[TestClass()]
public class DailyEmailProcessWorkerTests
{
private CSETContext context;
private CSETContext? context;

[TestInitialize()]
public void Initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace CSETWebCore.Business.AssessmentIO.Export.Tests
[TestClass()]
public class AssessmentExportManagerTests
{
private CSETContext context;
private CSETContext? context;

[TestInitialize()]
public void Initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace CSETWebCore.Business.Contact.Tests
[TestClass()]
public class ConversionBusinessTests
{
private CSETContext context;
private CSETContext? context;

[TestInitialize()]
public void Initialize()
Expand All @@ -31,20 +31,27 @@ public void Initialize()

}


[TestMethod()]
public void IsEntryCFTest()
{
var db = this.context;

if (db == null)
{
throw new Exception("Database context is null");
}

IAssessmentUtil util = new TestAssessmentUtil();
ConversionBusiness conversionBusiness = new ConversionBusiness(db,util);
var mm = db.MATURITY_MODELS.FirstOrDefault();
List<int> ids = db.ASSESSMENTS.Select(x=> x.Assessment_Id).ToList();
List<CFEntry> entries = conversionBusiness.IsEntryCF(ids);
Assert.IsTrue(entries.Count > 0);

}
}


internal class TestAssessmentUtil : IAssessmentUtil
{
public void TouchAssessment(int assessmentId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace CSETWebCore.Business.GalleryParser.Tests
[TestClass()]
public class GalleryEditorTests
{
private CSETContext context;
private CSETContext? context;

[TestInitialize()]
public void Initialize()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void GetMalcolmJsonDataTest()
{
string jsonString = File.ReadAllText(file);
var malcolmData = JsonConvert.DeserializeObject<MalcolmData>(jsonString);
datalist.Add(malcolmData);
datalist.Add(malcolmData ?? new MalcolmData());
}

MalcolmBusiness tst = new MalcolmBusiness(new DataLayer.Model.CSETContext());
Expand All @@ -46,7 +46,7 @@ public void GetMalcolmJsonDataTest()
}
}
Assert.IsTrue(AllNodes.Count == nodesList.Count);
TempNode parent = null;
TempNode? parent = null;
foreach (String item in assertFile)
{
/**
Expand All @@ -58,7 +58,7 @@ public void GetMalcolmJsonDataTest()
*/
if (item.StartsWith(" "))
{
Assert.IsTrue(parent.ChildrenKeys.Contains(item.Trim()));
Assert.IsTrue(parent?.ChildrenKeys.Contains(item.Trim()));
Assert.IsTrue(nodesList.ContainsKey(item.Trim()));
}
else
Expand All @@ -80,7 +80,7 @@ public void TestMalcomTreeWalk()
{
string jsonString = File.ReadAllText(file);
var malcolmData = JsonConvert.DeserializeObject<MalcolmData>(jsonString);
datalist.Add(malcolmData);
datalist.Add(malcolmData ?? new MalcolmData());
}
MalcolmBusiness tst = new MalcolmBusiness(new DataLayer.Model.CSETContext());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@ namespace CSETWebCore.Model.Maturity
{
public class MatRelevantAnswers
{
public MatRelevantAnswers()
{
}

public ANSWER ANSWER { get; set; }
public MATURITY_QUESTIONS Mat { get; set; }
public ANSWER? ANSWER { get; set; }
public MATURITY_QUESTIONS? Mat { get; set; }

// This should have a defined getter since as a property is ascertainable
// but that getter would ping the database, and the GetMissingParents() function
Expand All @@ -45,21 +42,22 @@ public MatAnsweredQuestionDomain()
{
}

public string Title { get; set; }
public string? Title { get; set; }
public bool IsDeficient { get; set; }
public bool AreFactorQuestionsDeficient { get; set; }
public List<MaturityAnsweredQuestionsAssesment> AssessmentFactors { get; set; }
public List<MaturityAnsweredQuestionsAssesment>? AssessmentFactors { get; set; }
}

public class MaturityAnsweredQuestionsAssesment
{
public MaturityAnsweredQuestionsAssesment()
{
}
public string Title { get; set; }

public string? Title { get; set; }
public bool IsDeficient { get; set; }
public bool AreQuestionsDeficient { get; set; }
public List<MaturityAnsweredQuestionsComponent> Components { get; set; }
public List<MaturityAnsweredQuestionsComponent>? Components { get; set; }
public List<MaturityAnsweredQuestions>? Questions { get; set; }

}
Expand All @@ -68,26 +66,26 @@ public class MaturityAnsweredQuestionsComponent
public MaturityAnsweredQuestionsComponent()
{
}
public string Title { get; set; }
public string? Title { get; set; }
public bool IsDeficient { get; set; }
public List<MaturityAnsweredQuestions> Questions { get; set; }
public List<MaturityAnsweredQuestions>? Questions { get; set; }
}

public class MaturityAnsweredQuestions
{
public MaturityAnsweredQuestions()
{
}
public string Title { get; set; }
public string QuestionText { get; set; }
public string MaturityLevel { get; set; }
public string Comments { get; set; }
public string AnswerText { get; set; }
public string? Title { get; set; }
public string? QuestionText { get; set; }
public string? MaturityLevel { get; set; }
public string? Comments { get; set; }
public string? AnswerText { get; set; }
public bool MarkForReview { get; set; }
public string Comment { get; set; }
public string? Comment { get; set; }
public int MatQuestionId { get; set; }
public string FreeResponseText { get; set; }
public List<DocumentWithAnswerId> Documents { get; set; }
public string? FreeResponseText { get; set; }
public List<DocumentWithAnswerId>? Documents { get; set; }
public bool Visible { get; set; }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ public class MaturityResponse
/// <summary>
/// The name of the maturity model.
/// </summary>
public string ModelName { get; set; }
public string ModelName { get; set; } = "";

/// <summary>
/// The name of the current grouping represented in the response;
/// </summary>
public string Title { get; set; }
public string? Title { get; set; }

/// <summary>
/// Lists the display names of the maturity levels in this assessment's maturity model.
Expand All @@ -48,7 +48,7 @@ public class MaturityResponse
/// <summary>
/// If the maturity model refers to 'questions' by another name, this is that name.
/// </summary>
public string QuestionsAlias { get; set; }
public string? QuestionsAlias { get; set; }

/// <summary>
/// The top level of groupings. This will usually be Domains.
Expand Down
Loading