Fix analyzer issues, update docker compose (#113)

This commit is contained in:
Leendert de Borst
2024-07-22 23:04:08 +02:00
parent b2e344c523
commit d65db96447
15 changed files with 80 additions and 41 deletions

View File

@@ -3,7 +3,7 @@ services:
image: aliasvault-admin
build:
context: .
dockerfile: src/AliasVault.Admin2/Dockerfile
dockerfile: src/AliasVault.Admin/Dockerfile
ports:
- "8080:8082"
volumes:

View File

@@ -65,7 +65,6 @@
return;
}
// await SignInManager.RefreshSignInAsync(user);
Logger.LogInformation("User changed their password successfully.");
GlobalNotificationService.AddSuccessMessage("Your password has been changed.", true);

View File

@@ -52,12 +52,10 @@
var setPhoneResult = await UserManager.SetPhoneNumberAsync(UserService.User(), Input.PhoneNumber);
if (!setPhoneResult.Succeeded)
{
// TODO: show error that phone number could not be set, use proper form validation?
GlobalNotificationService.AddErrorMessage("Phone number could not be set", true);
}
}
// await SignInManager.RefreshSignInAsync(UserService.User());
GlobalNotificationService.AddSuccessMessage("Your profile has been updated", true);
}

View File

@@ -70,17 +70,16 @@ else
{
if (firstRender)
{
await LoadAliasesAsync();
await LoadEmailsAsync();
}
}
private async Task LoadAliasesAsync()
private async Task LoadEmailsAsync()
{
IsLoading = true;
StateHasChanged();
// Load the aliases from the database.
var user = UserService.User();
// Load the emails from the database.
EmailList = await DbContext.Emails
.OrderByDescending(x => x.DateSystem)
.ToListAsync();

View File

@@ -124,4 +124,4 @@ using (var scope = app.Services.CreateScope())
await StartupTasks.SetAdminUser(scope.ServiceProvider);
}
app.Run();
await app.RunAsync();

View File

@@ -26,7 +26,7 @@ public class UserService(AliasServerDbContext dbContext, UserManager<AdminUser>
/// <summary>
/// The roles of the current user.
/// </summary>
private IList<string> _userRoles = new List<string>();
private List<string> _userRoles = [];
/// <summary>
/// Whether the current user is an admin or not.
@@ -64,7 +64,7 @@ public class UserService(AliasServerDbContext dbContext, UserManager<AdminUser>
var user = await userManager.FindByIdAsync(userId.ToString());
if (user == null)
{
throw new Exception($"User with id {userId} not found.");
throw new ArgumentException($"User with id {userId} not found.");
}
return user;
@@ -78,7 +78,7 @@ public class UserService(AliasServerDbContext dbContext, UserManager<AdminUser>
{
if (_user == null)
{
throw new Exception("Trying to access User object which is null.");
throw new ArgumentException("Trying to access User object which is null.");
}
return _user;
@@ -110,7 +110,8 @@ public class UserService(AliasServerDbContext dbContext, UserManager<AdminUser>
_user = user;
// Load all roles for current user.
_userRoles = await userManager.GetRolesAsync(User());
var roles = await userManager.GetRolesAsync(User());
_userRoles = roles.ToList();
// Define if current user is admin.
_isAdmin = _userRoles.Contains(AdminRole);

View File

@@ -58,25 +58,23 @@ public static class StartupTasks
}
else
{
// Check if the password hash is correct, if not, update it to the .env value.
if (adminUser.PasswordHash != config.AdminPasswordHash)
// Check if the password hash is different AND the password in .env file is newer than the password of user.
// If so, update the password hash of the user in the database so it matches the one in the .env file.
if (adminUser.PasswordHash != config.AdminPasswordHash && config.LastPasswordChanged > adminUser.LastPasswordChanged)
{
if (config.LastPasswordChanged > adminUser.LastPasswordChanged)
{
// The password has been changed in the .env file, update the user's password hash.
adminUser.PasswordHash = config.AdminPasswordHash;
adminUser.LastPasswordChanged = DateTime.UtcNow;
// The password has been changed in the .env file, update the user's password hash.
adminUser.PasswordHash = config.AdminPasswordHash;
adminUser.LastPasswordChanged = DateTime.UtcNow;
// Reset 2FA settings
adminUser.TwoFactorEnabled = false;
// Reset 2FA settings
adminUser.TwoFactorEnabled = false;
// Clear existing recovery codes
await userManager.GenerateNewTwoFactorRecoveryCodesAsync(adminUser, 0);
// Clear existing recovery codes
await userManager.GenerateNewTwoFactorRecoveryCodesAsync(adminUser, 0);
await userManager.UpdateAsync(adminUser);
await userManager.UpdateAsync(adminUser);
Console.WriteLine("Admin password hash updated.");
}
Console.WriteLine("Admin password hash updated.");
}
}
}

View File

@@ -40,7 +40,7 @@ function generateQrCode(id) {
element.appendChild(qrContainer);
// Initialize QRCode object
const qrcode = new QRCode(qrContainer, {
new QRCode(qrContainer, {
text: dataUrl,
width: 256,
height: 256,

View File

@@ -102,11 +102,11 @@ public class AliasServerDbContext : DbContext
/// <summary>
/// The OnModelCreating method.
/// </summary>
/// <param name="builder">ModelBuilder instance.</param>
protected override void OnModelCreating(ModelBuilder builder)
/// <param name="modelBuilder">ModelBuilder instance.</param>
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(builder);
foreach (var entity in builder.Model.GetEntityTypes())
base.OnModelCreating(modelBuilder);
foreach (var entity in modelBuilder.Model.GetEntityTypes())
{
foreach (var property in entity.GetProperties())
{
@@ -122,31 +122,31 @@ public class AliasServerDbContext : DbContext
}
// Configure AspNetIdentity tables manually.
builder.Entity<IdentityUserRole<string>>(entity =>
modelBuilder.Entity<IdentityUserRole<string>>(entity =>
{
entity.HasKey(r => new { r.UserId, r.RoleId });
entity.ToTable("UserRoles");
});
builder.Entity<IdentityUserClaim<string>>(entity =>
modelBuilder.Entity<IdentityUserClaim<string>>(entity =>
{
entity.HasKey(c => c.Id);
entity.ToTable("UserClaims");
});
builder.Entity<IdentityUserLogin<string>>(entity =>
modelBuilder.Entity<IdentityUserLogin<string>>(entity =>
{
entity.HasKey(l => new { l.LoginProvider, l.ProviderKey });
entity.ToTable("UserLogins");
});
builder.Entity<IdentityRoleClaim<string>>(entity =>
modelBuilder.Entity<IdentityRoleClaim<string>>(entity =>
{
entity.HasKey(rc => rc.Id);
entity.ToTable("RoleClaims");
});
builder.Entity<IdentityUserToken<string>>(entity =>
modelBuilder.Entity<IdentityUserToken<string>>(entity =>
{
entity.HasKey(t => new { t.UserId, t.LoginProvider, t.Name });
entity.ToTable("UserTokens");

View File

@@ -14,4 +14,20 @@ using Microsoft.AspNetCore.Identity;
/// </summary>
public class AliasVaultRole : IdentityRole
{
/// <summary>
/// Initializes a new instance of the <see cref="AliasVaultRole"/> class.
/// </summary>
public AliasVaultRole()
: base()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AliasVaultRole"/> class.
/// </summary>
/// <param name="roleName">Role name.</param>
public AliasVaultRole(string roleName)
: base(roleName)
{
}
}

View File

@@ -14,11 +14,11 @@ RUN dotnet restore "src/Utilities/InitializationCLI/InitializationCLI.csproj"
COPY . .
# Build the project
RUN dotnet build "src/Utilities/InitializationCLI/InitializationCLI.csproj" -c $BUILD_CONFIGURATION -o /app/build
RUN dotnet build "src/Utilities/InitializationCLI/InitializationCLI.csproj" -c "$BUILD_CONFIGURATION" -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "src/Utilities/InitializationCLI/InitializationCLI.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
RUN dotnet publish "src/Utilities/InitializationCLI/InitializationCLI.csproj" -c "$BUILD_CONFIGURATION" -o /app/publish /p:UseAppHost=false
FROM base AS final
WORKDIR /app

View File

@@ -7,6 +7,16 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DocumentationFile>bin\Debug\net8.0\InitializationCLI.xml</DocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<DocumentationFile>bin\Release\net8.0\InitializationCLI.xml</DocumentationFile>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<ItemGroup>
<Content Include="..\..\..\.dockerignore">
<Link>.dockerignore</Link>
@@ -18,4 +28,17 @@
<ProjectReference Include="..\..\Databases\AliasServerDb\AliasServerDb.csproj" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="..\..\stylecop.json">
<Link>stylecop.json</Link>
</AdditionalFiles>
</ItemGroup>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.556">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>

View File

@@ -1,5 +1,10 @@
// See https://aka.ms/new-console-template for more information
using System;
//-----------------------------------------------------------------------
// <copyright file="Program.cs" company="lanedirt">
// Copyright (c) lanedirt. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
// </copyright>
//-----------------------------------------------------------------------
using AliasServerDb;
using Microsoft.AspNetCore.Identity;