From 1191e0694d57ad1aa961a6b2205cc63935f80d1d Mon Sep 17 00:00:00 2001 From: Mia Winter Date: Fri, 29 Mar 2024 19:51:26 +0100 Subject: [PATCH] Added Customization for generated ID length using `JSI_CUSTOMIZATION__GENERATEDIDLENGTH` --- Components/Pages/Urls.razor | 10 ++++++++-- Components/Partials/LoginPartial.razor | 4 ++-- Model/Customization.cs | 5 +++++ Program.cs | 1 + Properties/launchSettings.json | 3 ++- 5 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 Model/Customization.cs diff --git a/Components/Pages/Urls.razor b/Components/Pages/Urls.razor index 138e66c..7c7a62e 100644 --- a/Components/Pages/Urls.razor +++ b/Components/Pages/Urls.razor @@ -5,9 +5,12 @@ @using System.Text.RegularExpressions @using System.Web @using Humanizer +@using JustShortIt.Model @using Microsoft.AspNetCore.WebUtilities +@using Microsoft.Extensions.Options @attribute [Authorize] +@inject IOptions CustomizationOptions @inject IDistributedCache Db @inject IConfiguration Configuration @inject NavigationManager Navigation @@ -152,11 +155,14 @@ MessageType = MessageComponent.AlertType.Success; } - private static string GenerateNewId() { + private string GenerateNewId() { string base64Guid = Regex.Replace( Convert.ToBase64String(Guid.NewGuid().ToByteArray()), "[/+=]", ""); - return base64Guid[..6]; + + int length = Math.Min(Math.Max(CustomizationOptions.Value.GeneratedIdLength, 2), 16); + + return base64Guid[..length]; } #region Models diff --git a/Components/Partials/LoginPartial.razor b/Components/Partials/LoginPartial.razor index 4b5658c..0a2feec 100644 --- a/Components/Partials/LoginPartial.razor +++ b/Components/Partials/LoginPartial.razor @@ -1,9 +1,9 @@  @(context.User.Identity?.Name ?? "err_username_unknown") - Logout + Logout - Login + Login diff --git a/Model/Customization.cs b/Model/Customization.cs new file mode 100644 index 0000000..6905ce4 --- /dev/null +++ b/Model/Customization.cs @@ -0,0 +1,5 @@ +namespace JustShortIt.Model; + +public class Customization { + public int GeneratedIdLength { get; set; } = 6; +} \ No newline at end of file diff --git a/Program.cs b/Program.cs index e3e6e6d..da8c596 100644 --- a/Program.cs +++ b/Program.cs @@ -15,6 +15,7 @@ var redisConnection = builder.Configuration.GetSection("Redis").Get(); var user = builder.Configuration.GetSection("Account").Get(); string? baseUrl = builder.Configuration.GetValue("BaseUrl"); +builder.Services.Configure(builder.Configuration.GetSection(nameof(Customization))); #if DEBUG baseUrl = "http://localhost/"; diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json index 25632c8..e2323fc 100644 --- a/Properties/launchSettings.json +++ b/Properties/launchSettings.json @@ -20,7 +20,8 @@ "commandName": "Docker", "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", "environmentVariables": { - "ASPNETCORE_HTTP_PORTS": "8080" + "ASPNETCORE_HTTP_PORTS": "8080", + "JSI_CUSTOMIZATION__GENERATEDIDLENGTH": "12" }, "publishAllPorts": true }