From 3a53fa0a3cc5064f6009cbbdda2cab5489f26853 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Thu, 16 Nov 2023 00:48:06 +0100 Subject: [PATCH] Done some todo's and removed old TL tags --- Moonlight/App/Configuration/ConfigV1.cs | 8 +++++ .../App/Services/Community/PostService.cs | 2 -- Moonlight/App/Services/MailService.cs | 2 +- .../App/Services/Store/StoreAdminService.cs | 8 +++-- .../App/Services/Utils/ConnectionService.cs | 34 +++++++++++++++++++ Moonlight/Program.cs | 1 + .../Shared/Components/Forms/SmartForm.razor | 2 +- .../Partials/SoftErrorHandler.razor | 2 +- Moonlight/Shared/Layouts/MainLayout.razor | 5 +-- .../Shared/Views/Admin/Tickets/Index.razor | 8 ++--- Moonlight/Shared/Views/Store/Order.razor | 5 +-- 11 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 Moonlight/App/Services/Utils/ConnectionService.cs diff --git a/Moonlight/App/Configuration/ConfigV1.cs b/Moonlight/App/Configuration/ConfigV1.cs index aecc2cef..1cbd38e8 100644 --- a/Moonlight/App/Configuration/ConfigV1.cs +++ b/Moonlight/App/Configuration/ConfigV1.cs @@ -32,6 +32,10 @@ public class ConfigV1 [JsonProperty("EnableEmailVerify")] [Description("This will users force to verify their email address if they havent already")] public bool EnableEmailVerify { get; set; } = false; + + [JsonProperty("EnableReverseProxyMode")] + [Description("Enable this option if you are using a reverse proxy to access moonlight. This will configure some parts of moonlight to act correctly like the ip detection")] + public bool EnableReverseProxyMode { get; set; } = false; } public class DatabaseData @@ -69,5 +73,9 @@ public class ConfigV1 [JsonProperty("Password")] public string Password { get; set; } = "s3cr3t"; [JsonProperty("UseSsl")] public bool UseSsl { get; set; } = true; + + [JsonProperty("SenderName")] + [Description("This will be shown as the system emails sender name in apps like gmail")] + public string SenderName { get; set; } = "Moonlight System"; } } \ No newline at end of file diff --git a/Moonlight/App/Services/Community/PostService.cs b/Moonlight/App/Services/Community/PostService.cs index f29b94ac..0e1f56b2 100644 --- a/Moonlight/App/Services/Community/PostService.cs +++ b/Moonlight/App/Services/Community/PostService.cs @@ -116,8 +116,6 @@ public class PostService if(await CheckTextForBadWords(content)) throw new DisplayException("Bad word detected. Please follow the community rules"); - //TODO: Swear word filter - var comment = new PostComment() { Author = user, diff --git a/Moonlight/App/Services/MailService.cs b/Moonlight/App/Services/MailService.cs index f1842482..b04c7fbd 100644 --- a/Moonlight/App/Services/MailService.cs +++ b/Moonlight/App/Services/MailService.cs @@ -28,7 +28,7 @@ public class MailService var message = new MimeMessage(); message.From.Add(new MailboxAddress( - "Moonlight System", //TODO: Replace with config option + config.SenderName, config.Email )); diff --git a/Moonlight/App/Services/Store/StoreAdminService.cs b/Moonlight/App/Services/Store/StoreAdminService.cs index 5ebbb6b5..61648a51 100644 --- a/Moonlight/App/Services/Store/StoreAdminService.cs +++ b/Moonlight/App/Services/Store/StoreAdminService.cs @@ -11,16 +11,19 @@ public class StoreAdminService { private readonly Repository ProductRepository; private readonly Repository CategoryRepository; + private readonly Repository ServiceRepository; private readonly ServiceService ServiceService; public StoreAdminService( Repository productRepository, Repository categoryRepository, - ServiceService serviceService) + ServiceService serviceService, + Repository serviceRepository) { ProductRepository = productRepository; CategoryRepository = categoryRepository; ServiceService = serviceService; + ServiceRepository = serviceRepository; } public Task AddCategory(string name, string description, string slug) @@ -96,7 +99,8 @@ public class StoreAdminService public Task DeleteProduct(Product product) { - //TODO: Implement checks if services with that product id exist + if (ServiceRepository.Get().Any(x => x.Product.Id == product.Id)) + throw new DisplayException("Product cannot be deleted as services related to this products exist. Delete the services first"); ProductRepository.Delete(product); diff --git a/Moonlight/App/Services/Utils/ConnectionService.cs b/Moonlight/App/Services/Utils/ConnectionService.cs new file mode 100644 index 00000000..9f505913 --- /dev/null +++ b/Moonlight/App/Services/Utils/ConnectionService.cs @@ -0,0 +1,34 @@ +using Moonlight.App.Helpers; + +namespace Moonlight.App.Services.Utils; + +public class ConnectionService +{ + private readonly IHttpContextAccessor ContextAccessor; + private readonly ConfigService ConfigService; + + public ConnectionService(IHttpContextAccessor contextAccessor, ConfigService configService) + { + ContextAccessor = contextAccessor; + ConfigService = configService; + } + + public Task GetIpAddress() + { + if (ContextAccessor.HttpContext == null) + return Task.FromResult("N/A (Missing http context)"); + + var request = ContextAccessor.HttpContext.Request; + + if (request.Headers.ContainsKey("X-Real-IP")) + { + if(ConfigService.Get().Security.EnableReverseProxyMode) + return Task.FromResult(request.Headers["X-Real-IP"].ToString()); + + Logger.Warn($"Detected an ip mask attempt by using a fake X-Real-IP header. Fake IP: {request.Headers["X-Real-IP"]}. Real IP: {ContextAccessor.HttpContext.Connection.RemoteIpAddress}"); + return Task.FromResult(ContextAccessor.HttpContext.Connection.RemoteIpAddress?.ToString() ?? "N/A (Remote IP missing)"); + } + + return Task.FromResult(ContextAccessor.HttpContext.Connection.RemoteIpAddress?.ToString() ?? "N/A (Remote IP missing)"); + } +} \ No newline at end of file diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index 580ca593..62fe67a8 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -50,6 +50,7 @@ builder.Services.AddScoped(typeof(Repository<>)); // Services / Utils builder.Services.AddScoped(); +builder.Services.AddScoped(); // Services / Interop builder.Services.AddScoped(); diff --git a/Moonlight/Shared/Components/Forms/SmartForm.razor b/Moonlight/Shared/Components/Forms/SmartForm.razor index cb52711c..e71c4030 100644 --- a/Moonlight/Shared/Components/Forms/SmartForm.razor +++ b/Moonlight/Shared/Components/Forms/SmartForm.razor @@ -18,7 +18,7 @@
@foreach (var msg in ErrorMessages) { - @(msg) + @(msg)
}
diff --git a/Moonlight/Shared/Components/Partials/SoftErrorHandler.razor b/Moonlight/Shared/Components/Partials/SoftErrorHandler.razor index 61db135f..4668a9ea 100644 --- a/Moonlight/Shared/Components/Partials/SoftErrorHandler.razor +++ b/Moonlight/Shared/Components/Partials/SoftErrorHandler.razor @@ -8,7 +8,7 @@ @if (Crashed) { - if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development" || IdentityService.Permissions[Permission.AdminViewExceptions]) // TODO: Add check for admin perms to show exceptions to admins + if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development" || IdentityService.Permissions[Permission.AdminViewExceptions]) { if (Exception != null) { diff --git a/Moonlight/Shared/Layouts/MainLayout.razor b/Moonlight/Shared/Layouts/MainLayout.razor index cf4f018b..993bdd0c 100644 --- a/Moonlight/Shared/Layouts/MainLayout.razor +++ b/Moonlight/Shared/Layouts/MainLayout.razor @@ -3,6 +3,7 @@ @using Moonlight.App.Models.Enums @using Moonlight.Shared.Components.Auth @using Moonlight.App.Event +@using Moonlight.App.Services.Utils @inherits LayoutComponentBase @implements IDisposable @@ -12,7 +13,7 @@ @inject IdentityService IdentityService @inject SessionService SessionService @inject NavigationManager Navigation -@inject IJSRuntime JsRuntime +@inject ConnectionService ConnectionService @{ var url = new Uri(Navigation.Uri); @@ -140,7 +141,7 @@ else MySession = new() { - //Ip = ConnectionService.GetIp(), TODO: Implement + Ip = await ConnectionService.GetIpAddress(), Url = Navigation.Uri, User = IdentityService.CurrentUserNullable }; diff --git a/Moonlight/Shared/Views/Admin/Tickets/Index.razor b/Moonlight/Shared/Views/Admin/Tickets/Index.razor index d83d7a40..fbe67296 100644 --- a/Moonlight/Shared/Views/Admin/Tickets/Index.razor +++ b/Moonlight/Shared/Views/Admin/Tickets/Index.razor @@ -24,7 +24,7 @@
- Total Tickets + Total Tickets
@(TotalTicketsCount) @@ -45,7 +45,7 @@
- Pending tickets + Pending tickets
@(PendingTicketsCount) @@ -66,7 +66,7 @@
- Closed tickets + Closed tickets
@(ClosedTicketsCount) @@ -87,7 +87,7 @@
- Ticket overview + Ticket overview
diff --git a/Moonlight/Shared/Views/Store/Order.razor b/Moonlight/Shared/Views/Store/Order.razor index a21da6e5..3a1f6474 100644 --- a/Moonlight/Shared/Views/Store/Order.razor +++ b/Moonlight/Shared/Views/Store/Order.razor @@ -11,16 +11,13 @@ @inject IdentityService IdentityService @inject AlertService AlertService @inject NavigationManager Navigation -@inject ServiceService ServiceService @inject Repository ProductRepository @inject Repository CouponRepository @if (SelectedProduct == null) { - @* -TODO: Add 404 here -*@ + } else {