commit
892d9c1ecf
|
@ -48,26 +48,12 @@
|
||||||
|
|
||||||
private async Task Send()
|
private async Task Send()
|
||||||
{
|
{
|
||||||
var notification = new Notification();
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var user = await UserService.GetUserAsync(username, PageState.Site.SiteId);
|
var user = await UserService.GetUserAsync(username, PageState.Site.SiteId);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
{
|
{
|
||||||
notification.SiteId = PageState.Site.SiteId;
|
var notification = new Notification(PageState.Site.SiteId, PageState.User, user, subject, body, null);
|
||||||
notification.FromUserId = PageState.User.UserId;
|
|
||||||
notification.FromDisplayName = PageState.User.DisplayName;
|
|
||||||
notification.FromEmail = PageState.User.Email;
|
|
||||||
notification.ToUserId = user.UserId;
|
|
||||||
notification.ToDisplayName = user.DisplayName;
|
|
||||||
notification.ToEmail = user.Email;
|
|
||||||
notification.Subject = subject;
|
|
||||||
notification.Body = body;
|
|
||||||
notification.ParentId = null;
|
|
||||||
notification.CreatedOn = DateTime.UtcNow;
|
|
||||||
notification.IsDelivered = false;
|
|
||||||
notification.DeliveredOn = null;
|
|
||||||
notification.SendOn = DateTime.UtcNow;
|
|
||||||
notification = await NotificationService.AddNotificationAsync(notification);
|
notification = await NotificationService.AddNotificationAsync(notification);
|
||||||
await logger.LogInformation("Notification Created {Notification}", notification);
|
await logger.LogInformation("Notification Created {Notification}", notification);
|
||||||
NavigationManager.NavigateTo(NavigateUrl());
|
NavigationManager.NavigateTo(NavigateUrl());
|
||||||
|
@ -79,7 +65,7 @@
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
await logger.LogError(ex, "Error Adding Notification {Notification} {Error}", notification, ex.Message);
|
await logger.LogError(ex, "Error Adding Notification {Error}", ex.Message);
|
||||||
AddModuleMessage(Localizer["Error Adding Notification"], MessageType.Error);
|
AddModuleMessage(Localizer["Error Adding Notification"], MessageType.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -176,26 +176,12 @@
|
||||||
|
|
||||||
private async Task Send()
|
private async Task Send()
|
||||||
{
|
{
|
||||||
var notification = new Notification();
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var user = await UserService.GetUserAsync(username, PageState.Site.SiteId);
|
var user = await UserService.GetUserAsync(username, PageState.Site.SiteId);
|
||||||
if (user != null)
|
if (user != null)
|
||||||
{
|
{
|
||||||
notification.SiteId = PageState.Site.SiteId;
|
var notification = new Notification(PageState.Site.SiteId, PageState.User, user, subject, body, notificationid);
|
||||||
notification.FromUserId = PageState.User.UserId;
|
|
||||||
notification.FromDisplayName = PageState.User.DisplayName;
|
|
||||||
notification.FromEmail = PageState.User.Email;
|
|
||||||
notification.ToUserId = user.UserId;
|
|
||||||
notification.ToDisplayName = user.DisplayName;
|
|
||||||
notification.ToEmail = user.Email;
|
|
||||||
notification.Subject = subject;
|
|
||||||
notification.Body = body;
|
|
||||||
notification.ParentId = notificationid;
|
|
||||||
notification.CreatedOn = DateTime.UtcNow;
|
|
||||||
notification.IsDelivered = false;
|
|
||||||
notification.DeliveredOn = null;
|
|
||||||
notification.SendOn = DateTime.UtcNow;
|
|
||||||
notification = await NotificationService.AddNotificationAsync(notification);
|
notification = await NotificationService.AddNotificationAsync(notification);
|
||||||
await logger.LogInformation("Notification Created {Notification}", notification);
|
await logger.LogInformation("Notification Created {Notification}", notification);
|
||||||
NavigationManager.NavigateTo(NavigateUrl());
|
NavigationManager.NavigateTo(NavigateUrl());
|
||||||
|
@ -207,7 +193,7 @@
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
await logger.LogError(ex, "Error Adding Notification {Notification} {Error}", notification, ex.Message);
|
await logger.LogError(ex, "Error Adding Notification {Error}", ex.Message);
|
||||||
AddModuleMessage(Localizer["Error Adding Notification"], MessageType.Error);
|
AddModuleMessage(Localizer["Error Adding Notification"], MessageType.Error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Microsoft.AspNetCore.Authentication;
|
using Microsoft.AspNetCore.Authentication;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
@ -146,20 +146,10 @@ namespace Oqtane.Controllers
|
||||||
newUser = _users.AddUser(user);
|
newUser = _users.AddUser(user);
|
||||||
if (!verified)
|
if (!verified)
|
||||||
{
|
{
|
||||||
Notification notification = new Notification();
|
|
||||||
notification.SiteId = user.SiteId;
|
|
||||||
notification.FromUserId = null;
|
|
||||||
notification.ToUserId = newUser.UserId;
|
|
||||||
notification.ToEmail = newUser.Email;
|
|
||||||
notification.Subject = "User Account Verification";
|
|
||||||
string token = await _identityUserManager.GenerateEmailConfirmationTokenAsync(identityuser);
|
string token = await _identityUserManager.GenerateEmailConfirmationTokenAsync(identityuser);
|
||||||
string url = HttpContext.Request.Scheme + "://" + _tenants.GetAlias().Name + "/login?name=" + user.Username + "&token=" + WebUtility.UrlEncode(token);
|
string url = HttpContext.Request.Scheme + "://" + _tenants.GetAlias().Name + "/login?name=" + user.Username + "&token=" + WebUtility.UrlEncode(token);
|
||||||
notification.Body = "Dear " + user.DisplayName + ",\n\nIn Order To Complete The Registration Of Your User Account Please Click The Link Displayed Below:\n\n" + url + "\n\nThank You!";
|
string body = "Dear " + user.DisplayName + ",\n\nIn Order To Complete The Registration Of Your User Account Please Click The Link Displayed Below:\n\n" + url + "\n\nThank You!";
|
||||||
notification.ParentId = null;
|
var notification = new Notification(user.SiteId, null, newUser, "User Account Verification", body, null);
|
||||||
notification.CreatedOn = DateTime.UtcNow;
|
|
||||||
notification.IsDelivered = false;
|
|
||||||
notification.DeliveredOn = null;
|
|
||||||
notification.SendOn = DateTime.UtcNow;
|
|
||||||
_notifications.AddNotification(notification);
|
_notifications.AddNotification(notification);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -379,20 +369,10 @@ namespace Oqtane.Controllers
|
||||||
IdentityUser identityuser = await _identityUserManager.FindByNameAsync(user.Username);
|
IdentityUser identityuser = await _identityUserManager.FindByNameAsync(user.Username);
|
||||||
if (identityuser != null)
|
if (identityuser != null)
|
||||||
{
|
{
|
||||||
Notification notification = new Notification();
|
|
||||||
notification.SiteId = user.SiteId;
|
|
||||||
notification.FromUserId = null;
|
|
||||||
notification.ToUserId = user.UserId;
|
|
||||||
notification.ToEmail = "";
|
|
||||||
notification.Subject = "User Password Reset";
|
|
||||||
string token = await _identityUserManager.GeneratePasswordResetTokenAsync(identityuser);
|
string token = await _identityUserManager.GeneratePasswordResetTokenAsync(identityuser);
|
||||||
string url = HttpContext.Request.Scheme + "://" + _tenants.GetAlias().Name + "/reset?name=" + user.Username + "&token=" + WebUtility.UrlEncode(token);
|
string url = HttpContext.Request.Scheme + "://" + _tenants.GetAlias().Name + "/reset?name=" + user.Username + "&token=" + WebUtility.UrlEncode(token);
|
||||||
notification.Body = "Dear " + user.DisplayName + ",\n\nPlease Click The Link Displayed Below To Reset Your Password:\n\n" + url + "\n\nThank You!";
|
string body = "Dear " + user.DisplayName + ",\n\nPlease Click The Link Displayed Below To Reset Your Password:\n\n" + url + "\n\nThank You!";
|
||||||
notification.ParentId = null;
|
var notification = new Notification(user.SiteId, null, user, "User Password Reset", body, null);
|
||||||
notification.CreatedOn = DateTime.UtcNow;
|
|
||||||
notification.IsDelivered = false;
|
|
||||||
notification.DeliveredOn = null;
|
|
||||||
notification.SendOn = DateTime.UtcNow;
|
|
||||||
_notifications.AddNotification(notification);
|
_notifications.AddNotification(notification);
|
||||||
_logger.Log(LogLevel.Information, this, LogFunction.Security, "Password Reset Notification Sent For {Username}", user.Username);
|
_logger.Log(LogLevel.Information, this, LogFunction.Security, "Password Reset Notification Sent For {Username}", user.Username);
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,16 +22,18 @@ namespace Oqtane.Infrastructure
|
||||||
IsEnabled = false;
|
IsEnabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// job is executed for each tenant in installation
|
||||||
public override string ExecuteJob(IServiceProvider provider)
|
public override string ExecuteJob(IServiceProvider provider)
|
||||||
{
|
{
|
||||||
string log = "";
|
string log = "";
|
||||||
|
|
||||||
// get services
|
// get services
|
||||||
var siteRepository = provider.GetRequiredService<ISiteRepository>();
|
var siteRepository = provider.GetRequiredService<ISiteRepository>();
|
||||||
|
var userRepository = provider.GetRequiredService<IUserRepository>();
|
||||||
var settingRepository = provider.GetRequiredService<ISettingRepository>();
|
var settingRepository = provider.GetRequiredService<ISettingRepository>();
|
||||||
var notificationRepository = provider.GetRequiredService<INotificationRepository>();
|
var notificationRepository = provider.GetRequiredService<INotificationRepository>();
|
||||||
|
|
||||||
// iterate through sites for this tenant
|
// iterate through sites for current tenant
|
||||||
List<Site> sites = siteRepository.GetSites().ToList();
|
List<Site> sites = siteRepository.GetSites().ToList();
|
||||||
foreach (Site site in sites)
|
foreach (Site site in sites)
|
||||||
{
|
{
|
||||||
|
@ -59,49 +61,79 @@ namespace Oqtane.Infrastructure
|
||||||
client.Credentials = new NetworkCredential(settings["SMTPUsername"], settings["SMTPPassword"]);
|
client.Credentials = new NetworkCredential(settings["SMTPUsername"], settings["SMTPPassword"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// iterate through notifications
|
// iterate through undelivered notifications
|
||||||
int sent = 0;
|
int sent = 0;
|
||||||
List<Notification> notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList();
|
List<Notification> notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList();
|
||||||
foreach (Notification notification in notifications)
|
foreach (Notification notification in notifications)
|
||||||
{
|
{
|
||||||
MailMessage mailMessage = new MailMessage();
|
// get sender and receiver information if not provided
|
||||||
mailMessage.From = new MailAddress(settings["SMTPSender"], site.Name);
|
if ((string.IsNullOrEmpty(notification.FromEmail) || string.IsNullOrEmpty(notification.FromDisplayName)) && notification.FromUserId != null)
|
||||||
mailMessage.Subject = notification.Subject;
|
|
||||||
if (notification.FromUserId != null)
|
|
||||||
{
|
{
|
||||||
mailMessage.Body = "From: " + notification.FromDisplayName + "<" + notification.FromEmail + ">" + "\n";
|
var user = userRepository.GetUser(notification.FromUserId.Value);
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
notification.FromEmail = (string.IsNullOrEmpty(notification.FromEmail)) ? user.Email : notification.FromEmail;
|
||||||
|
notification.FromDisplayName = (string.IsNullOrEmpty(notification.FromDisplayName)) ? user.DisplayName : notification.FromDisplayName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
if ((string.IsNullOrEmpty(notification.ToEmail) || string.IsNullOrEmpty(notification.ToDisplayName)) && notification.ToUserId != null)
|
||||||
{
|
{
|
||||||
mailMessage.Body = "From: " + site.Name + "\n";
|
var user = userRepository.GetUser(notification.ToUserId.Value);
|
||||||
|
if (user != null)
|
||||||
|
{
|
||||||
|
notification.ToEmail = (string.IsNullOrEmpty(notification.ToEmail)) ? user.Email : notification.ToEmail;
|
||||||
|
notification.ToDisplayName = (string.IsNullOrEmpty(notification.ToDisplayName)) ? user.DisplayName : notification.ToDisplayName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
mailMessage.Body += "Sent: " + notification.CreatedOn + "\n";
|
|
||||||
if (notification.ToUserId != null)
|
|
||||||
{
|
|
||||||
mailMessage.To.Add(new MailAddress(notification.ToEmail, notification.ToDisplayName));
|
|
||||||
mailMessage.Body += "To: " + notification.ToDisplayName + "<" + notification.ToEmail + ">" + "\n";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
mailMessage.To.Add(new MailAddress(notification.ToEmail));
|
|
||||||
mailMessage.Body += "To: " + notification.ToEmail + "\n";
|
|
||||||
}
|
|
||||||
mailMessage.Body += "Subject: " + notification.Subject + "\n\n";
|
|
||||||
mailMessage.Body += notification.Body;
|
|
||||||
|
|
||||||
// send mail
|
// validate recipient
|
||||||
try
|
if (string.IsNullOrEmpty(notification.ToEmail))
|
||||||
{
|
{
|
||||||
client.Send(mailMessage);
|
log += "Recipient Missing For NotificationId: " + notification.NotificationId + "<br />";
|
||||||
sent = sent++;
|
notification.IsDeleted = true;
|
||||||
notification.IsDelivered = true;
|
|
||||||
notification.DeliveredOn = DateTime.UtcNow;
|
|
||||||
notificationRepository.UpdateNotification(notification);
|
notificationRepository.UpdateNotification(notification);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
else
|
||||||
{
|
{
|
||||||
// error
|
MailMessage mailMessage = new MailMessage();
|
||||||
log += ex.Message + "<br />";
|
mailMessage.From = new MailAddress(settings["SMTPSender"], site.Name);
|
||||||
|
mailMessage.Subject = notification.Subject;
|
||||||
|
if (!string.IsNullOrEmpty(notification.FromEmail) && !string.IsNullOrEmpty(notification.FromDisplayName))
|
||||||
|
{
|
||||||
|
mailMessage.Body = "From: " + notification.FromDisplayName + "<" + notification.FromEmail + ">" + "\n";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mailMessage.Body = "From: " + site.Name + "\n";
|
||||||
|
}
|
||||||
|
mailMessage.Body += "Sent: " + notification.CreatedOn + "\n";
|
||||||
|
if (!string.IsNullOrEmpty(notification.ToEmail) && !string.IsNullOrEmpty(notification.ToDisplayName))
|
||||||
|
{
|
||||||
|
mailMessage.To.Add(new MailAddress(notification.ToEmail, notification.ToDisplayName));
|
||||||
|
mailMessage.Body += "To: " + notification.ToDisplayName + "<" + notification.ToEmail + ">" + "\n";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
mailMessage.To.Add(new MailAddress(notification.ToEmail));
|
||||||
|
mailMessage.Body += "To: " + notification.ToEmail + "\n";
|
||||||
|
}
|
||||||
|
mailMessage.Body += "Subject: " + notification.Subject + "\n\n";
|
||||||
|
mailMessage.Body += notification.Body;
|
||||||
|
|
||||||
|
// send mail
|
||||||
|
try
|
||||||
|
{
|
||||||
|
client.Send(mailMessage);
|
||||||
|
sent = sent++;
|
||||||
|
notification.IsDelivered = true;
|
||||||
|
notification.DeliveredOn = DateTime.UtcNow;
|
||||||
|
notificationRepository.UpdateNotification(notification);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
// error
|
||||||
|
log += ex.Message + "<br />";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log += "Notifications Delivered: " + sent + "<br />";
|
log += "Notifications Delivered: " + sent + "<br />";
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Oqtane.Models;
|
using Oqtane.Models;
|
||||||
|
@ -20,7 +20,7 @@ namespace Oqtane.Repository
|
||||||
{
|
{
|
||||||
return _db.Notification
|
return _db.Notification
|
||||||
.Where(item => item.SiteId == siteId)
|
.Where(item => item.SiteId == siteId)
|
||||||
.Where(item => item.IsDelivered == false)
|
.Where(item => item.IsDelivered == false && item.IsDeleted == false)
|
||||||
.Where(item => item.SendOn == null || item.SendOn < System.DateTime.UtcNow)
|
.Where(item => item.SendOn == null || item.SendOn < System.DateTime.UtcNow)
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.ComponentModel.DataAnnotations.Schema;
|
using System.ComponentModel.DataAnnotations.Schema;
|
||||||
|
|
||||||
namespace Oqtane.Models
|
namespace Oqtane.Models
|
||||||
|
@ -23,6 +23,50 @@ namespace Oqtane.Models
|
||||||
public DateTime? DeletedOn { get; set; }
|
public DateTime? DeletedOn { get; set; }
|
||||||
public bool IsDeleted { get; set; }
|
public bool IsDeleted { get; set; }
|
||||||
public DateTime? SendOn { get; set; }
|
public DateTime? SendOn { get; set; }
|
||||||
|
|
||||||
|
public Notification() {}
|
||||||
|
|
||||||
|
public Notification(int siteId, User from, User to, string subject, string body, int? parentId)
|
||||||
|
{
|
||||||
|
SiteId = siteId;
|
||||||
|
if (from != null)
|
||||||
|
{
|
||||||
|
FromUserId = from.UserId;
|
||||||
|
FromDisplayName = from.DisplayName;
|
||||||
|
FromEmail = from.Email;
|
||||||
|
}
|
||||||
|
if (to != null)
|
||||||
|
{
|
||||||
|
ToUserId = to.UserId;
|
||||||
|
ToDisplayName = to.DisplayName;
|
||||||
|
ToEmail = to.Email;
|
||||||
|
}
|
||||||
|
Subject = subject;
|
||||||
|
Body = body;
|
||||||
|
ParentId = parentId;
|
||||||
|
CreatedOn = DateTime.UtcNow;
|
||||||
|
IsDelivered = false;
|
||||||
|
DeliveredOn = null;
|
||||||
|
SendOn = DateTime.UtcNow;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Notification(int siteId, string fromDisplayName, string fromEmail, string toDisplayName, string toEmail, string subject, string body)
|
||||||
|
{
|
||||||
|
SiteId = siteId;
|
||||||
|
FromUserId = null;
|
||||||
|
FromDisplayName = fromDisplayName;
|
||||||
|
FromEmail = fromEmail;
|
||||||
|
ToUserId = null;
|
||||||
|
ToDisplayName = toDisplayName;
|
||||||
|
ToEmail = toEmail;
|
||||||
|
Subject = subject;
|
||||||
|
Body = body;
|
||||||
|
ParentId = null;
|
||||||
|
CreatedOn = DateTime.UtcNow;
|
||||||
|
IsDelivered = false;
|
||||||
|
DeliveredOn = null;
|
||||||
|
SendOn = DateTime.UtcNow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user