From 63c33fcfcba51cbdba5d959d5998891b5524d403 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Thu, 11 Sep 2025 14:16:01 +0100 Subject: [PATCH] Remove the ability to register accounts. --- .../Components/Account/Pages/Register.razor | 153 ------------------ .../Account/Pages/RegisterConfirmation.razor | 73 --------- Aaru.Server/Components/Layout/NavMenu.razor | 5 - Aaru.Server/Program.cs | 39 +++-- 4 files changed, 27 insertions(+), 243 deletions(-) delete mode 100644 Aaru.Server/Components/Account/Pages/Register.razor delete mode 100644 Aaru.Server/Components/Account/Pages/RegisterConfirmation.razor diff --git a/Aaru.Server/Components/Account/Pages/Register.razor b/Aaru.Server/Components/Account/Pages/Register.razor deleted file mode 100644 index 766d4304..00000000 --- a/Aaru.Server/Components/Account/Pages/Register.razor +++ /dev/null @@ -1,153 +0,0 @@ -@page "/Account/Register" -@using System.ComponentModel.DataAnnotations -@using System.Text -@using System.Text.Encodings.Web -@using Microsoft.AspNetCore.Identity -@using Microsoft.AspNetCore.WebUtilities - -@inject UserManager UserManager -@inject IUserStore UserStore -@inject SignInManager SignInManager -@inject IEmailSender EmailSender -@inject ILogger Logger -@inject NavigationManager NavigationManager -@inject IdentityRedirectManager RedirectManager - -Register - -

Register

- -
-
- - - -

Create a new account.

-
- -
- - - -
-
- - - -
-
- - - -
- -
-
-
-
-

Use another service to register.

-
- -
-
-
- -@code { - private IEnumerable? identityErrors; - - [SupplyParameterFromForm] - private InputModel Input { get; set; } = new(); - - [SupplyParameterFromQuery] - private string? ReturnUrl { get; set; } - - private string? Message => identityErrors is null ? null : $"Error: {string.Join(", ", identityErrors.Select(error => error.Description))}"; - - public async Task RegisterUser(EditContext editContext) - { - IdentityUser user = CreateUser(); - - await UserStore.SetUserNameAsync(user, Input.Email, CancellationToken.None); - IUserEmailStore emailStore = GetEmailStore(); - await emailStore.SetEmailAsync(user, Input.Email, CancellationToken.None); - IdentityResult result = await UserManager.CreateAsync(user, Input.Password); - - if(!result.Succeeded) - { - identityErrors = result.Errors; - - return; - } - - Logger.LogInformation("User created a new account with password."); - - string userId = await UserManager.GetUserIdAsync(user); - string code = await UserManager.GenerateEmailConfirmationTokenAsync(user); - code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); - - string callbackUrl = NavigationManager.GetUriWithQueryParameters(NavigationManager.ToAbsoluteUri("Account/ConfirmEmail").AbsoluteUri, - new Dictionary - { - ["userId"] = userId, - ["code"] = code, - ["returnUrl"] = ReturnUrl - }); - - await EmailSender.SendConfirmationLinkAsync(user, Input.Email, HtmlEncoder.Default.Encode(callbackUrl)); - - if(UserManager.Options.SignIn.RequireConfirmedAccount) - { - RedirectManager.RedirectTo("Account/RegisterConfirmation", - new Dictionary - { - ["email"] = Input.Email, - ["returnUrl"] = ReturnUrl - }); - } - - await SignInManager.SignInAsync(user, false); - RedirectManager.RedirectTo(ReturnUrl); - } - - private IdentityUser CreateUser() - { - try - { - return Activator.CreateInstance(); - } - catch - { - throw new InvalidOperationException($"Can't create an instance of '{nameof(IdentityUser)}'. " + $"Ensure that '{nameof(IdentityUser)}' is not an abstract class and has a parameterless constructor."); - } - } - - private IUserEmailStore GetEmailStore() - { - if(!UserManager.SupportsUserEmail) - { - throw new NotSupportedException("The default UI requires a user store with email support."); - } - - return (IUserEmailStore)UserStore; - } - - private sealed class InputModel - { - [Required] - [EmailAddress] - [Display(Name = "Email")] - public string Email { get; set; } = ""; - - [Required] - [StringLength(100, ErrorMessage = "The {0} must be at least {2} and at max {1} characters long.", MinimumLength = 6)] - [DataType(DataType.Password)] - [Display(Name = "Password")] - public string Password { get; set; } = ""; - - [DataType(DataType.Password)] - [Display(Name = "Confirm password")] - [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")] - public string ConfirmPassword { get; set; } = ""; - } - -} \ No newline at end of file diff --git a/Aaru.Server/Components/Account/Pages/RegisterConfirmation.razor b/Aaru.Server/Components/Account/Pages/RegisterConfirmation.razor deleted file mode 100644 index 0cdb84be..00000000 --- a/Aaru.Server/Components/Account/Pages/RegisterConfirmation.razor +++ /dev/null @@ -1,73 +0,0 @@ -@page "/Account/RegisterConfirmation" -@using System.Text -@using Microsoft.AspNetCore.Identity -@using Microsoft.AspNetCore.WebUtilities - -@inject UserManager UserManager -@inject IEmailSender EmailSender -@inject NavigationManager NavigationManager -@inject IdentityRedirectManager RedirectManager - -Register confirmation - -

Register confirmation

- - - -@if(emailConfirmationLink is not null) -{ -

- This app does not currently have a real email sender registered, see these docs for how to configure a real email sender. - Normally this would be emailed: Click here to confirm your account -

-} -else -{ -

Please check your email to confirm your account.

-} - -@code { - private string? emailConfirmationLink; - private string? statusMessage; - - [CascadingParameter] - private HttpContext HttpContext { get; set; } = default!; - - [SupplyParameterFromQuery] - private string? Email { get; set; } - - [SupplyParameterFromQuery] - private string? ReturnUrl { get; set; } - - protected override async Task OnInitializedAsync() - { - if(Email is null) - { - RedirectManager.RedirectTo(""); - } - - IdentityUser? user = await UserManager.FindByEmailAsync(Email); - - if(user is null) - { - HttpContext.Response.StatusCode = StatusCodes.Status404NotFound; - statusMessage = "Error finding user for unspecified email"; - } - else if(EmailSender is IdentityNoOpEmailSender) - { - // Once you add a real email sender, you should remove this code that lets you confirm the account - string userId = await UserManager.GetUserIdAsync(user); - string code = await UserManager.GenerateEmailConfirmationTokenAsync(user); - code = WebEncoders.Base64UrlEncode(Encoding.UTF8.GetBytes(code)); - - emailConfirmationLink = NavigationManager.GetUriWithQueryParameters(NavigationManager.ToAbsoluteUri("Account/ConfirmEmail").AbsoluteUri, - new Dictionary - { - ["userId"] = userId, - ["code"] = code, - ["returnUrl"] = ReturnUrl - }); - } - } - -} \ No newline at end of file diff --git a/Aaru.Server/Components/Layout/NavMenu.razor b/Aaru.Server/Components/Layout/NavMenu.razor index 0d5bfefd..e54315f1 100644 --- a/Aaru.Server/Components/Layout/NavMenu.razor +++ b/Aaru.Server/Components/Layout/NavMenu.razor @@ -52,11 +52,6 @@ -