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

fix #4353 - add defensive logic when sending notifications and improve performance #4356

Merged
merged 1 commit into from
Jun 26, 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
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