Skip to content

Commit

Permalink
Merge pull request #4356 from sbwalker/dev
Browse files Browse the repository at this point in the history
fix #4353 - add defensive logic when sending notifications and improve performance
  • Loading branch information
sbwalker authored Jun 26, 2024
2 parents 08213ae + 03f081f commit 84b560e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
20 changes: 11 additions & 9 deletions Oqtane.Server/Infrastructure/LogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,19 +203,21 @@ private void SendNotification(Log log)
}
if (Enum.Parse<LogLevel>(log.Level) >= notifylevel)
{
var subject = $"Site {log.Level} Notification";
string body = $"Log Message: {log.Message}";

var alias = _tenantManager.GetAlias();
foreach (var userrole in _userRoles.GetUserRoles(log.SiteId.Value))
if (alias != null)
{
if (userrole.Role.Name == RoleNames.Host)
{
var subject = $"{alias.Name} Site {log.Level} Notification";
var url = $"{_accessor.HttpContext.Request.Scheme}://{alias.Name}/admin/log?id={log.LogId}";
string body = $"Log Message: {log.Message}<br /><br />Please visit {url} for more information";
var notification = new Notification(log.SiteId.Value, userrole.User, subject, body);
_notifications.AddNotification(notification);
}
subject = $"{alias.Name} Site {log.Level} Notification";
body = $"Log Message: {log.Message}<br /><br />Please visit {alias.Protocol}://{alias.Name}/admin/log?id={log.LogId} for more information";
}

foreach (var userrole in _userRoles.GetUserRoles(RoleNames.Host, log.SiteId.Value))
{
var notification = new Notification(log.SiteId.Value, userrole.User, subject, body);
_notifications.AddNotification(notification);
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Oqtane.Server/Repository/Interfaces/IUserRoleRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public interface IUserRoleRepository
{
IEnumerable<UserRole> GetUserRoles(int siteId);
IEnumerable<UserRole> GetUserRoles(int userId, int siteId);
IEnumerable<UserRole> GetUserRoles(string roleName, int siteId);
UserRole AddUserRole(UserRole userRole);
UserRole UpdateUserRole(UserRole userRole);
UserRole GetUserRole(int userRoleId);
Expand Down
15 changes: 12 additions & 3 deletions Oqtane.Server/Repository/UserRoleRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,25 @@ public IEnumerable<UserRole> GetUserRoles(int siteId)
return db.UserRole
.Include(item => item.Role) // eager load roles
.Include(item => item.User) // eager load users
.Where(item => item.Role.SiteId == siteId || item.Role.SiteId == null).ToList();
.Where(item => item.Role.SiteId == siteId || item.Role.SiteId == null || siteId == -1).ToList();
}

public IEnumerable<UserRole> GetUserRoles(int userId, int siteId)
{
using var db = _dbContextFactory.CreateDbContext();
return db.UserRole.Where(item => item.UserId == userId)
return db.UserRole
.Include(item => item.Role) // eager load roles
.Include(item => item.User) // eager load users
.Where(item => item.Role.SiteId == siteId || item.Role.SiteId == null || siteId == -1).ToList();
.Where(item => (item.Role.SiteId == siteId || item.Role.SiteId == null || siteId == -1) && item.UserId == userId).ToList();
}

public IEnumerable<UserRole> GetUserRoles(string roleName, int siteId)
{
using var db = _dbContextFactory.CreateDbContext();
return db.UserRole
.Include(item => item.Role) // eager load roles
.Include(item => item.User) // eager load users
.Where(item => (item.Role.SiteId == siteId || item.Role.SiteId == null || siteId == -1) && item.Role.Name == roleName).ToList();
}

public UserRole AddUserRole(UserRole userRole)
Expand Down
4 changes: 2 additions & 2 deletions Oqtane.Shared/Models/Alias.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ public string SiteKey
}

/// <summary>
/// Protocol for the request from which the alias was resolved (ie. http:// or https:// )
/// Protocol for the request from which the alias was resolved (ie. http or https )
/// </summary>
[NotMapped]
public string Protocol { get; set; }
public string Protocol { get; set; } = "https"; // default value

/// <summary>
/// Base Url for static resources (note that this will only be set for remote clients)
Expand Down

0 comments on commit 84b560e

Please sign in to comment.