From c6ff7c79d9254baa3c6d052fbc762aa74eff6630 Mon Sep 17 00:00:00 2001 From: Ilpo Juvander Date: Mon, 12 Nov 2018 11:52:09 +0200 Subject: [PATCH 1/5] Added setting to enable/disable e-mail notify when user downloads file --- App_LocalResources/Settings.ascx.resx | 6 +++ Repository.ascx.cs | 60 +++++++++++++++++++++++---- Settings.ascx | 7 ++++ Settings.ascx.cs | 38 ++++++++++++++--- 4 files changed, 96 insertions(+), 15 deletions(-) diff --git a/App_LocalResources/Settings.ascx.resx b/App_LocalResources/Settings.ascx.resx index 73efde5..a240253 100644 --- a/App_LocalResources/Settings.ascx.resx +++ b/App_LocalResources/Settings.ascx.resx @@ -374,6 +374,12 @@ Send Email when Comment Posted + + If you want to receive an email when user downloads a file, check this box and enter the email address where you would like the notification sent + + + Send Email when user downloads file + If you want to receive an email when a new item is uploaded, check this box and enter the email address where you would like the notification sent diff --git a/Repository.ascx.cs b/Repository.ascx.cs index ea51711..1333607 100644 --- a/Repository.ascx.cs +++ b/Repository.ascx.cs @@ -42,15 +42,16 @@ using DotNetNuke.UI.WebControls; using DotNetNuke.Services.Mail; using DotNetNuke.Security.Permissions; +using DotNetNuke.Services.Exceptions; namespace DotNetNuke.Modules.Repository { public abstract class Repository : Entities.Modules.PortalModuleBase, Entities.Modules.Communications.IModuleListener { - - #region "Controls" - protected Label lblDescription; + static Instrumentation.DnnLogger log = Instrumentation.DnnLogger.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()); + #region "Controls" + protected Label lblDescription; private DataGrid withEventsField_lstObjects; protected DataGrid lstObjects { get { return withEventsField_lstObjects; } @@ -406,8 +407,48 @@ public void lstObjects_PageIndexChanged(object source, DataGridPageChangedEventA BindObjectList(); } - - private void SendCommentNotification(RepositoryInfo objRepository, TextBox txtName, TextBox txtComment) + private void SendDownloadNotification(RepositoryInfo objRepository) + { + try + { + log.ErrorFormat("Download: {0} {1}", Settings["EmailOnDownload"], Settings["EmailOnDownloadAddress"]); + // check to see if we need to send an email notification + if (string.IsNullOrEmpty(Convert.ToString(Settings["EmailOnDownload"])) == false) + { + if (bool.Parse(Settings["EmailOnDownload"].ToString()) == true) + { + string _email = Convert.ToString(Settings["EmailOnDownloadAddress"]); + if (string.IsNullOrEmpty(_email) == false) + { + // send an email + string _subject = string.Format("[{0}] New Download from Your Repository", PortalSettings.PortalName); + System.Text.StringBuilder _body = new System.Text.StringBuilder(); + _body.Append(string.Format("A new download at {0}
", System.DateTime.Now)); + _body.Append(string.Format("by {0} ({1})

", UserInfo.DisplayName, UserInfo.Email)); + _body.Append(string.Format("File {0}

", objRepository.FileName)); + _body.Append("------------------------------------------------------------
"); + _body.Append(string.Format("{0}
", this.Request.UserHostAddress)); + Mail.SendMail( + PortalSettings.Email, + _email, + "", "", + Services.Mail.MailPriority.Normal, + _subject, + Services.Mail.MailFormat.Html, + System.Text.Encoding.Default, + _body.ToString(), + "", "", "", "", ""); + } + } + } + } + catch (Exception ex) + { + Exceptions.ProcessModuleLoadException(this, ex); + } + } + + private void SendCommentNotification(RepositoryInfo objRepository, TextBox txtName, TextBox txtComment) { // check to see if we need to send an email notification if (!string.IsNullOrEmpty(Convert.ToString(Settings["EmailOnComment"]))) { @@ -504,7 +545,8 @@ public void lstObjects_ItemCommand(object source, DataGridCommandEventArgs e) string target = oRepositoryBusinessController.GetSkinAttribute(xmlDoc, "DOWNLOAD", "Target", "NEW"); oRepositoryBusinessController.DownloadFile(e.CommandArgument.ToString(), target); - break; + SendDownloadNotification(objRepository); + break; case "PostComment": objCommentsPanel = null; TextBox txtName = null; @@ -736,7 +778,8 @@ private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.Data string target = oRepositoryBusinessController.GetSkinAttribute(xmlDoc, "DOWNLOAD", "Target", "NEW"); oRepositoryBusinessController.DownloadFile(e.CommandArgument.ToString(), target); - break; + SendDownloadNotification(objRepository); + break; case "PostComment": objCommentsPanel = null; TextBox txtName = null; @@ -797,8 +840,7 @@ private void DataList1_ItemCommand(object source, System.Web.UI.WebControls.Data } - - private void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) + private void DataList1_ItemDataBound(object sender, DataListItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem) { PlaceHolder holder = (PlaceHolder)e.Item.FindControl("PlaceHolder1"); diff --git a/Settings.ascx b/Settings.ascx index 5958e1a..e46bc21 100644 --- a/Settings.ascx +++ b/Settings.ascx @@ -151,6 +151,13 @@

+ + + + +

+ + diff --git a/Settings.ascx.cs b/Settings.ascx.cs index 17cf812..662792d 100644 --- a/Settings.ascx.cs +++ b/Settings.ascx.cs @@ -302,9 +302,11 @@ protected ImageButton cmdDeleteValue { protected System.Web.UI.WebControls.CheckBox cbAllFiles; protected System.Web.UI.WebControls.CheckBox cbxIsPersonal; protected System.Web.UI.WebControls.CheckBox cbxEmailOnComment; - protected System.Web.UI.WebControls.CheckBox cbxEmailOnUpload; + protected System.Web.UI.WebControls.CheckBox cbxEmailOnDownload; + protected System.Web.UI.WebControls.CheckBox cbxEmailOnUpload; protected System.Web.UI.WebControls.TextBox txtEmailOnComment; - protected System.Web.UI.WebControls.TextBox txtEmailOnUpload; + protected System.Web.UI.WebControls.TextBox txtEmailOnDownload; + protected System.Web.UI.WebControls.TextBox txtEmailOnUpload; protected System.Web.UI.WebControls.RadioButtonList rblDataControl; protected System.Web.UI.WebControls.TextBox txtWatermark; @@ -379,7 +381,17 @@ private void Page_Load(System.Object sender, System.EventArgs e) cbxEmailOnComment.Checked = false; } - if (!string.IsNullOrEmpty(Convert.ToString(settings["AnonEditDelete"]))) { + + if (!string.IsNullOrEmpty(Convert.ToString(settings["EmailOnDownload"]))) + { + cbxEmailOnDownload.Checked = bool.Parse(settings["EmailOnDownload"].ToString()); + } + else + { + cbxEmailOnDownload.Checked = false; + } + + if (!string.IsNullOrEmpty(Convert.ToString(settings["AnonEditDelete"]))) { cbxAnonEditDelete.Checked = bool.Parse(settings["AnonEditDelete"].ToString()); } else { cbxAnonEditDelete.Checked = false; @@ -392,7 +404,17 @@ private void Page_Load(System.Object sender, System.EventArgs e) // default value } - if (!string.IsNullOrEmpty(Convert.ToString(settings["EmailOnUpload"]))) { + if (!string.IsNullOrEmpty(Convert.ToString(settings["EmailOnDownloadAddress"]))) + { + txtEmailOnDownload.Text = Convert.ToString(settings["EmailOnDownloadAddress"]); + } + else + { + txtEmailOnDownload.Text = ""; + // default value + } + + if (!string.IsNullOrEmpty(Convert.ToString(settings["EmailOnUpload"]))) { cbxEmailOnUpload.Checked = bool.Parse(settings["EmailOnUpload"].ToString()); } else { cbxEmailOnUpload.Checked = false; @@ -742,7 +764,8 @@ private void Page_Load(System.Object sender, System.EventArgs e) cbAllFiles.Text = Localization.GetString("AllowAllFiles", LocalResourceFile); cbxIsPersonal.Text = Localization.GetString("IsPersonal", LocalResourceFile); - cbxEmailOnComment.Text = Localization.GetString("EmailOnComment", LocalResourceFile); + cbxEmailOnComment.Text = Localization.GetString("EmailOnComment", LocalResourceFile); + cbxEmailOnDownload.Text = Localization.GetString("EmailOnDownload", LocalResourceFile); cbxEmailOnUpload.Text = Localization.GetString("EmailOnUpload", LocalResourceFile); cbxAnonEditDelete.Text = Localization.GetString("AnonEditDelete", LocalResourceFile); @@ -818,7 +841,10 @@ public override void UpdateSettings() objModules.UpdateModuleSetting(ModuleId, "EmailOnComment", cbxEmailOnComment.Checked.ToString()); objModules.UpdateModuleSetting(ModuleId, "EmailOnCommentAddress", txtEmailOnComment.Text); - objModules.UpdateModuleSetting(ModuleId, "EmailOnUpload", cbxEmailOnUpload.Checked.ToString()); + objModules.UpdateModuleSetting(ModuleId, "EmailOnDownload", cbxEmailOnDownload.Checked.ToString()); + objModules.UpdateModuleSetting(ModuleId, "EmailOnDownloadAddress", txtEmailOnDownload.Text); + + objModules.UpdateModuleSetting(ModuleId, "EmailOnUpload", cbxEmailOnUpload.Checked.ToString()); objModules.UpdateModuleSetting(ModuleId, "EmailOnUploadAddress", txtEmailOnUpload.Text); objModules.UpdateModuleSetting(ModuleId, "AnonEditDelete", cbxAnonEditDelete.Checked.ToString()); From 8b14be8d4c651ca1ac0ae37168b77130089ae925 Mon Sep 17 00:00:00 2001 From: Ilpo Juvander Date: Mon, 12 Nov 2018 12:03:55 +0200 Subject: [PATCH 2/5] removed not needed logging --- Repository.ascx.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Repository.ascx.cs b/Repository.ascx.cs index 1333607..528005c 100644 --- a/Repository.ascx.cs +++ b/Repository.ascx.cs @@ -411,7 +411,6 @@ private void SendDownloadNotification(RepositoryInfo objRepository) { try { - log.ErrorFormat("Download: {0} {1}", Settings["EmailOnDownload"], Settings["EmailOnDownloadAddress"]); // check to see if we need to send an email notification if (string.IsNullOrEmpty(Convert.ToString(Settings["EmailOnDownload"])) == false) { From b5fb0fad2dcb9de64c8d69c3d747bfec9fe834ca Mon Sep 17 00:00:00 2001 From: Ilpo Juvander Date: Wed, 14 Nov 2018 08:26:36 +0200 Subject: [PATCH 3/5] changed bool.Parse to bool.TryParse --- Repository.ascx.cs | 49 +++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/Repository.ascx.cs b/Repository.ascx.cs index 528005c..67280d2 100644 --- a/Repository.ascx.cs +++ b/Repository.ascx.cs @@ -411,33 +411,32 @@ private void SendDownloadNotification(RepositoryInfo objRepository) { try { - // check to see if we need to send an email notification - if (string.IsNullOrEmpty(Convert.ToString(Settings["EmailOnDownload"])) == false) + bool emailOnDownload = false; + // check to see if we need to send an email notification + if (bool.TryParse(Settings["EmailOnDownload"].ToString(), out emailOnDownload) == true && + emailOnDownload == true) { - if (bool.Parse(Settings["EmailOnDownload"].ToString()) == true) + string _email = Convert.ToString(Settings["EmailOnDownloadAddress"]); + if (string.IsNullOrEmpty(_email) == false) { - string _email = Convert.ToString(Settings["EmailOnDownloadAddress"]); - if (string.IsNullOrEmpty(_email) == false) - { - // send an email - string _subject = string.Format("[{0}] New Download from Your Repository", PortalSettings.PortalName); - System.Text.StringBuilder _body = new System.Text.StringBuilder(); - _body.Append(string.Format("A new download at {0}
", System.DateTime.Now)); - _body.Append(string.Format("by {0} ({1})

", UserInfo.DisplayName, UserInfo.Email)); - _body.Append(string.Format("File {0}

", objRepository.FileName)); - _body.Append("------------------------------------------------------------
"); - _body.Append(string.Format("{0}
", this.Request.UserHostAddress)); - Mail.SendMail( - PortalSettings.Email, - _email, - "", "", - Services.Mail.MailPriority.Normal, - _subject, - Services.Mail.MailFormat.Html, - System.Text.Encoding.Default, - _body.ToString(), - "", "", "", "", ""); - } + // send an email + string _subject = string.Format("[{0}] New Download from Your Repository", PortalSettings.PortalName); + System.Text.StringBuilder _body = new System.Text.StringBuilder(); + _body.Append(string.Format("A new download at {0}
", System.DateTime.Now)); + _body.Append(string.Format("by {0} ({1})

", UserInfo.DisplayName, UserInfo.Email)); + _body.Append(string.Format("File {0}

", objRepository.FileName)); + _body.Append("------------------------------------------------------------
"); + _body.Append(string.Format("{0}
", this.Request.UserHostAddress)); + Mail.SendMail( + PortalSettings.Email, + _email, + "", "", + Services.Mail.MailPriority.Normal, + _subject, + Services.Mail.MailFormat.Html, + System.Text.Encoding.Default, + _body.ToString(), + "", "", "", "", ""); } } } From ae681d49393de8371780ed3f04f1e9cbf46adeb1 Mon Sep 17 00:00:00 2001 From: Ilpo Juvander Date: Wed, 14 Nov 2018 08:47:26 +0200 Subject: [PATCH 4/5] Another bool.Parse to bool.TryParse --- Settings.ascx.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Settings.ascx.cs b/Settings.ascx.cs index 662792d..ad5edb3 100644 --- a/Settings.ascx.cs +++ b/Settings.ascx.cs @@ -381,10 +381,10 @@ private void Page_Load(System.Object sender, System.EventArgs e) cbxEmailOnComment.Checked = false; } - - if (!string.IsNullOrEmpty(Convert.ToString(settings["EmailOnDownload"]))) + bool emailOnDownload = false; + if (bool.TryParse(settings["EmailOnDownload"].ToString(), out emailOnDownload) == true) { - cbxEmailOnDownload.Checked = bool.Parse(settings["EmailOnDownload"].ToString()); + cbxEmailOnDownload.Checked = emailOnDownload; } else { From d7cd9c8f8af921c9606edb9069f65ee875762e09 Mon Sep 17 00:00:00 2001 From: Daniel Valadas Date: Thu, 16 Jan 2020 15:07:47 -0500 Subject: [PATCH 5/5] Added null check ? adds a safe null check to prevent a failure if the setting does not exist yet --- Settings.ascx.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Settings.ascx.cs b/Settings.ascx.cs index ad5edb3..6292b98 100644 --- a/Settings.ascx.cs +++ b/Settings.ascx.cs @@ -382,7 +382,7 @@ private void Page_Load(System.Object sender, System.EventArgs e) } bool emailOnDownload = false; - if (bool.TryParse(settings["EmailOnDownload"].ToString(), out emailOnDownload) == true) + if (bool.TryParse(settings["EmailOnDownload"]?.ToString(), out emailOnDownload) == true) { cbxEmailOnDownload.Checked = emailOnDownload; }