LANCommander/LANCommander.Server/UI/Pages/FirstTimeSetup/Administrator.razor

153 lines
4.7 KiB
Text

@page "/FirstTimeSetup/Administrator"
@using LANCommander.Server.Services.Exceptions
@layout FirstTimeSetupLayout
@inject SetupService SetupService
@inject RoleService RoleService
@inject UserService UserService
@inject NavigationManager NavigationManager
@inject IMessageService MessageService
@inject ILogger<Administrator> Logger
<PageTitle>Create Administrator User - First Time Setup</PageTitle>
<Form Model="@Model" Loading="Loading" OnFinish="CreateAdministratorUser" Layout="FormLayout.Vertical">
<FormItem>
To get started, you will need to create an administrator user.
</FormItem>
<FormItem Label="Username">
<Input @bind-Value="@context.Username" />
</FormItem>
<FormItem Label="Password">
<InputPassword @bind-Value="@context.Password" />
</FormItem>
<FormItem Label="Confirm Password">
<InputPassword @bind-Value="@context.PasswordConfirm" />
</FormItem>
<FormItem>
<GridRow Justify="RowJustify.End" Style="margin-top: 16px;">
<GridCol>
<Button Type="ButtonType.Primary" HtmlType="submit">
Create
</Button>
</GridCol>
</GridRow>
</FormItem>
</Form>
@code {
[CascadingParameter] FirstTimeSetupLayout Layout { get; set; }
FirstTimeSetupModel Model = new();
bool Loading;
protected override async Task OnInitializedAsync()
{
await Layout.ChangeCurrentStep(FirstTimeSetupStep.Administrator);
var isSetupInitialized = await SetupService.IsSetupInitialized();
if (isSetupInitialized)
NavigationManager.NavigateTo("/");
}
async Task CreateAdministratorUser()
{
Loading = true;
StateHasChanged();
await Task.Yield();
if (Model.Password != Model.PasswordConfirm)
{
MessageService.Error("Passwords do not match");
Loading = false;
StateHasChanged();
await Task.Yield();
return;
}
try
{
var role = await RoleService.GetAsync(RoleService.AdministratorRoleName);
try
{
if (role == null)
role = await RoleService.AddAsync(new Role
{
Name = RoleService.AdministratorRoleName,
});
}
catch (AddRoleException ex)
{
foreach (var error in ex.IdentityResult.Errors)
{
MessageService.Error(error.Description);
}
Loading = false;
StateHasChanged();
await Task.Yield();
return;
}
var administrators = await RoleService.GetUsersAsync(RoleService.AdministratorRoleName);
if (administrators != null && administrators.Any())
NavigationManager.NavigateTo("/");
else
{
var user = Activator.CreateInstance<User>();
user.Approved = true;
user.ApprovedOn = DateTime.UtcNow;
user.UserName = Model.Username;
try
{
await UserService.AddAsync(user);
await UserService.ChangePassword(user.UserName, Model.Password);
await UserService.AddToRolesAsync(user.UserName, [RoleService.AdministratorRoleName]);
Logger?.LogInformation("Administrator created a new account with password.");
await MessageService.Success("Setup completed! Redirecting...", 3);
Loading = false;
NavigationManager.NavigateTo("/");
}
catch (UserRegistrationException ex)
{
foreach (var error in ex.IdentityResult.Errors)
{
MessageService.Error(error.Description);
}
Loading = false;
StateHasChanged();
await Task.Yield();
}
catch (AddRoleException ex)
{
foreach (var error in ex.IdentityResult.Errors)
{
MessageService.Error(error.Description);
}
Loading = false;
StateHasChanged();
await Task.Yield();
}
}
}
catch (Exception ex)
{
Logger?.LogError(ex, "Could not create administrator");
MessageService.Error($"Could not create administrator: {ex.Message}", 10);
}
Loading = false;
}
}