2026-06-12 23:48:11 -05:00
|
|
|
using AutoMapper;
|
2024-11-12 00:00:31 -06:00
|
|
|
using AutoMapper.QueryableExtensions;
|
2024-11-25 22:29:56 -06:00
|
|
|
using LANCommander.Server.Data.Enums;
|
2024-08-21 19:40:27 -05:00
|
|
|
using LANCommander.Server.Data.Models;
|
2024-10-16 18:32:48 -05:00
|
|
|
using LANCommander.Server.Services.Factories;
|
2024-10-18 19:15:05 -05:00
|
|
|
using LANCommander.Server.Services.Models;
|
2024-10-16 16:29:55 -05:00
|
|
|
using Microsoft.AspNetCore.Identity;
|
2024-10-18 19:15:05 -05:00
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-10-07 18:04:26 -05:00
|
|
|
using Microsoft.Extensions.Logging;
|
2024-10-18 19:15:05 -05:00
|
|
|
using System.Linq.Expressions;
|
2025-03-16 14:35:53 -05:00
|
|
|
using LANCommander.Server.Services.Abstractions;
|
2025-01-22 20:54:41 -06:00
|
|
|
using LANCommander.Server.Services.Exceptions;
|
2025-06-18 00:19:58 +02:00
|
|
|
using LANCommander.Server.Data;
|
2026-06-12 23:48:11 -05:00
|
|
|
using Microsoft.Extensions.Options;
|
2025-02-08 12:32:44 -06:00
|
|
|
using ZiggyCreatures.Caching.Fusion;
|
2024-08-21 19:40:27 -05:00
|
|
|
|
|
|
|
|
namespace LANCommander.Server.Services
|
|
|
|
|
{
|
2024-10-18 19:15:05 -05:00
|
|
|
public class UserService : BaseService, IBaseDatabaseService<User>
|
2024-08-21 19:40:27 -05:00
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
private readonly IdentityContextFactory _identityContextFactory;
|
2025-01-12 02:54:41 -06:00
|
|
|
private readonly CollectionService CollectionService;
|
2025-06-18 00:19:58 +02:00
|
|
|
private readonly IDbContextFactory<DatabaseContext> ContextFactory;
|
2024-11-12 00:00:31 -06:00
|
|
|
private readonly IMapper Mapper;
|
2025-02-08 12:32:44 -06:00
|
|
|
private readonly IFusionCache Cache;
|
2026-06-12 23:48:11 -05:00
|
|
|
private readonly IOptions<IdentityOptions> _identityOptions;
|
|
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
protected readonly List<Func<IQueryable<User>, IQueryable<User>>> _modifiers = new();
|
2024-08-21 19:40:27 -05:00
|
|
|
|
2024-08-28 20:33:59 -05:00
|
|
|
public UserService(
|
|
|
|
|
ILogger<UserService> logger,
|
2025-11-16 12:31:25 -06:00
|
|
|
SettingsProvider<Settings.Settings> settingsProvider,
|
2024-11-12 00:00:31 -06:00
|
|
|
IMapper mapper,
|
2025-02-08 12:32:44 -06:00
|
|
|
IFusionCache cache,
|
2025-01-12 02:54:41 -06:00
|
|
|
CollectionService collectionService,
|
2025-06-18 00:19:58 +02:00
|
|
|
IDbContextFactory<DatabaseContext> contextFactory,
|
2026-06-12 23:48:11 -05:00
|
|
|
IdentityContextFactory identityContextFactory,
|
|
|
|
|
IOptions<IdentityOptions> identityOptions) : base(logger, settingsProvider)
|
2024-08-21 19:40:27 -05:00
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
_identityContextFactory = identityContextFactory;
|
2025-01-12 02:54:41 -06:00
|
|
|
CollectionService = collectionService;
|
2025-06-18 00:19:58 +02:00
|
|
|
ContextFactory = contextFactory;
|
2024-11-12 00:00:31 -06:00
|
|
|
Mapper = mapper;
|
2025-02-09 01:23:40 -06:00
|
|
|
Cache = cache;
|
2026-06-12 23:48:11 -05:00
|
|
|
_identityOptions = identityOptions;
|
2024-08-21 19:40:27 -05:00
|
|
|
}
|
|
|
|
|
|
2025-11-16 12:31:25 -06:00
|
|
|
public void Reconfigure()
|
2025-06-21 23:16:18 +02:00
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
var options = _identityOptions.Value;
|
|
|
|
|
if (options == null)
|
2025-06-21 23:16:18 +02:00
|
|
|
return;
|
|
|
|
|
|
2025-11-16 12:31:25 -06:00
|
|
|
options.Password.RequireNonAlphanumeric = _settingsProvider.CurrentValue.Server.Authentication.PasswordRequireNonAlphanumeric;
|
|
|
|
|
options.Password.RequireLowercase = _settingsProvider.CurrentValue.Server.Authentication.PasswordRequireLowercase;
|
|
|
|
|
options.Password.RequireUppercase = _settingsProvider.CurrentValue.Server.Authentication.PasswordRequireUppercase;
|
|
|
|
|
options.Password.RequireDigit = _settingsProvider.CurrentValue.Server.Authentication.PasswordRequireDigit;
|
|
|
|
|
options.Password.RequiredLength = _settingsProvider.CurrentValue.Server.Authentication.PasswordRequiredLength;
|
2025-06-21 23:16:18 +02:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<User> GetAsync(string userName)
|
2024-08-21 19:40:27 -05:00
|
|
|
{
|
2025-03-12 19:06:32 -05:00
|
|
|
return await FirstOrDefaultAsync(u => u.UserName.ToUpper() == userName.ToUpper());
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<T> GetAsync<T>(string userName)
|
2024-11-12 00:00:31 -06:00
|
|
|
{
|
2025-03-12 19:06:32 -05:00
|
|
|
return await FirstOrDefaultAsync<T>(u => u.UserName.ToUpper() == userName.ToUpper());
|
2024-11-12 00:00:31 -06:00
|
|
|
}
|
|
|
|
|
|
2025-02-08 12:32:44 -06:00
|
|
|
public async Task<IEnumerable<Role>> GetRolesAsync(User user)
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
{
|
2025-02-08 12:32:44 -06:00
|
|
|
var roles = await Cache.GetOrSetAsync($"User/{user.Id}/Roles", async _ =>
|
2024-10-16 18:32:48 -05:00
|
|
|
{
|
2025-02-08 23:23:06 -06:00
|
|
|
try
|
|
|
|
|
{
|
2025-03-11 17:26:55 -05:00
|
|
|
user = await Query(q =>
|
|
|
|
|
{
|
|
|
|
|
return q
|
|
|
|
|
.Include(u => u.UserRoles)
|
|
|
|
|
.ThenInclude(ur => ur.Role);
|
|
|
|
|
}).FirstOrDefaultAsync(u => u.Id == user.Id);
|
2025-02-08 23:23:06 -06:00
|
|
|
|
2025-03-11 17:26:55 -05:00
|
|
|
return user.Roles;
|
2025-02-08 23:23:06 -06:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex, "Could not get roles for user {Username}", user.UserName);
|
|
|
|
|
return new List<Role>();
|
|
|
|
|
}
|
2025-02-09 01:37:31 -06:00
|
|
|
}, tags: ["User/Security", "User/Roles", $"User/{user.Id}"]);
|
2025-02-08 12:32:44 -06:00
|
|
|
|
|
|
|
|
return roles;
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
}
|
|
|
|
|
|
2025-02-08 12:32:44 -06:00
|
|
|
public async Task<bool> IsInRoleAsync(User user, string roleName)
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
{
|
2025-02-08 12:32:44 -06:00
|
|
|
var roles = await GetRolesAsync(user);
|
2026-06-12 23:48:11 -05:00
|
|
|
|
2025-02-08 12:32:44 -06:00
|
|
|
return roles.Any(r => r.Name == roleName);
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
}
|
|
|
|
|
|
2026-06-28 14:09:02 -05:00
|
|
|
public async Task<UserLimits> GetLimitsAsync(User user)
|
|
|
|
|
{
|
|
|
|
|
var roles = await GetRolesAsync(user);
|
|
|
|
|
|
|
|
|
|
var storageQuota = Resolve(user.StorageQuotaMB, roles.Select(r => r.StorageQuotaMB));
|
|
|
|
|
var downloadSpeed = Resolve(user.DownloadSpeedKBps, roles.Select(r => r.DownloadSpeedKBps));
|
|
|
|
|
var savesEnabled = Resolve(user.EnableSaves, roles.Select(r => r.EnableSaves));
|
|
|
|
|
|
|
|
|
|
return new UserLimits(storageQuota, downloadSpeed, savesEnabled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resolves an effective limit value. A per-user override (if set) takes precedence. Otherwise the most
|
|
|
|
|
/// restrictive (lowest non-zero) value across the user's roles is used. A value of 0 means unlimited, and a
|
|
|
|
|
/// null role value means the limit is not configured for that role and is ignored.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal static int Resolve(int? userOverride, IEnumerable<int?> roleValues)
|
|
|
|
|
{
|
|
|
|
|
if (userOverride.HasValue)
|
|
|
|
|
return userOverride.Value;
|
|
|
|
|
|
|
|
|
|
var restrictive = roleValues
|
|
|
|
|
.Where(v => v.HasValue && v.Value > 0)
|
|
|
|
|
.Select(v => v!.Value)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
return restrictive.Count == 0 ? 0 : restrictive.Min();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Resolves an effective boolean permission (e.g. whether saves are enabled). A per-user override (if set)
|
|
|
|
|
/// takes precedence. Otherwise the most restrictive value across the user's roles is used: if any role
|
|
|
|
|
/// explicitly disables the permission it is disabled. A null role value means the permission is not configured
|
|
|
|
|
/// for that role and is ignored; if no role configures it the permission defaults to enabled.
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal static bool Resolve(bool? userOverride, IEnumerable<bool?> roleValues)
|
|
|
|
|
{
|
|
|
|
|
if (userOverride.HasValue)
|
|
|
|
|
return userOverride.Value;
|
|
|
|
|
|
|
|
|
|
return !roleValues.Any(v => v == false);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-08 12:32:44 -06:00
|
|
|
public async Task<IEnumerable<Collection>> GetCollectionsAsync(User user)
|
2025-01-12 02:54:41 -06:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2025-02-08 12:32:44 -06:00
|
|
|
var roles = await GetRolesAsync(user);
|
2025-01-12 02:54:41 -06:00
|
|
|
var roleIds = roles.Select(r => r.Id).ToList();
|
|
|
|
|
|
2025-02-08 12:32:44 -06:00
|
|
|
if (roles.Any(r => r.Name.Equals(RoleService.AdministratorRoleName, StringComparison.OrdinalIgnoreCase)))
|
2025-01-12 02:54:41 -06:00
|
|
|
return await CollectionService.GetAsync();
|
|
|
|
|
else
|
|
|
|
|
return await CollectionService
|
2025-02-08 12:32:44 -06:00
|
|
|
.Include(c => c.Roles)
|
|
|
|
|
.GetAsync(c => c.Roles.Any(r => roleIds.Contains(r.Id)));
|
2025-01-12 02:54:41 -06:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2025-02-08 12:32:44 -06:00
|
|
|
_logger.LogError(ex, "Could not get collections for user {UserName}", user.UserName);
|
2025-01-12 02:54:41 -06:00
|
|
|
return new List<Collection>();
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-06-12 23:48:11 -05:00
|
|
|
|
2025-03-29 20:10:35 -05:00
|
|
|
public async Task<bool> ExistsAsync(Expression<Func<User, bool>> predicate)
|
|
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var context = await ContextFactory.CreateDbContextAsync();
|
|
|
|
|
return await context.Users.AnyAsync(predicate);
|
2025-03-29 20:10:35 -05:00
|
|
|
}
|
|
|
|
|
|
2025-06-18 00:19:58 +02:00
|
|
|
public Task<User> AddAsync(User user)
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
{
|
2025-06-18 00:19:58 +02:00
|
|
|
return AddAsync(user, bypassPasswordPolicy: false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<User> AddAsync(User user, bool bypassPasswordPolicy, string? password = null)
|
|
|
|
|
{
|
|
|
|
|
IdentityResult result;
|
|
|
|
|
if (bypassPasswordPolicy && !string.IsNullOrEmpty(password))
|
|
|
|
|
{
|
|
|
|
|
user.SecurityStamp = Guid.NewGuid().ToString();
|
|
|
|
|
user.ConcurrencyStamp = Guid.NewGuid().ToString();
|
2026-06-25 01:23:23 -05:00
|
|
|
user.NormalizedUserName = user.UserName.ToUpperInvariant();
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(user.Email))
|
|
|
|
|
user.NormalizedEmail = user.Email.ToUpperInvariant();
|
2025-06-18 00:19:58 +02:00
|
|
|
|
|
|
|
|
// hash & set password
|
|
|
|
|
var hasher = new PasswordHasher<User>();
|
|
|
|
|
user.PasswordHash = hasher.HashPassword(user, password);
|
|
|
|
|
|
|
|
|
|
// insert & save
|
|
|
|
|
using var context = await ContextFactory.CreateDbContextAsync();
|
|
|
|
|
context.Users!.Add(user);
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
|
|
|
|
|
result = IdentityResult.Success;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var identityContext = await _identityContextFactory.CreateAsync();
|
|
|
|
|
result = await identityContext.UserManager.CreateAsync(user);
|
2025-06-18 00:19:58 +02:00
|
|
|
}
|
|
|
|
|
|
2025-02-08 12:32:44 -06:00
|
|
|
if (result.Succeeded)
|
2026-06-12 23:48:11 -05:00
|
|
|
{
|
|
|
|
|
using var findContext = await _identityContextFactory.CreateAsync();
|
|
|
|
|
return await findContext.UserManager.FindByNameAsync(user.UserName);
|
|
|
|
|
}
|
2025-02-08 12:32:44 -06:00
|
|
|
else
|
|
|
|
|
throw new UserRegistrationException(result, "Could not create user");
|
2024-10-16 16:29:55 -05:00
|
|
|
}
|
2024-10-16 01:54:51 -05:00
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task AddToRoleAsync(string userName, string roleName)
|
2024-10-16 16:29:55 -05:00
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var identityContext = await _identityContextFactory.CreateAsync();
|
2026-06-20 23:23:29 -05:00
|
|
|
var user = await identityContext.UserManager.FindByNameAsync(userName);
|
2026-06-12 23:48:11 -05:00
|
|
|
await identityContext.UserManager.AddToRoleAsync(user, roleName);
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task AddToRolesAsync(string userName, IEnumerable<string> roleNames)
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var identityContext = await _identityContextFactory.CreateAsync();
|
2026-06-20 23:23:29 -05:00
|
|
|
var user = await identityContext.UserManager.FindByNameAsync(userName);
|
2026-06-12 23:48:11 -05:00
|
|
|
var result = await identityContext.UserManager.AddToRolesAsync(user, roleNames);
|
|
|
|
|
|
2025-02-21 23:36:52 -06:00
|
|
|
await Cache.RemoveByTagAsync(["User/Security", "User/Roles", $"User/{user.Id}", $"Library/{user.Id}"]);
|
|
|
|
|
|
2025-02-08 12:32:44 -06:00
|
|
|
if (!result.Succeeded)
|
|
|
|
|
throw new AddRoleException(result, "Could not add roles");
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-18 19:15:05 -05:00
|
|
|
public async Task RemoveFromRole(string userName, string roleName)
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var identityContext = await _identityContextFactory.CreateAsync();
|
2026-06-20 23:23:29 -05:00
|
|
|
var user = await identityContext.UserManager.FindByNameAsync(userName);
|
2026-06-12 23:48:11 -05:00
|
|
|
await identityContext.UserManager.RemoveFromRoleAsync(user, roleName);
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-18 19:15:05 -05:00
|
|
|
public async Task<bool> CheckPassword(string userName, string password)
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var identityContext = await _identityContextFactory.CreateAsync();
|
2026-06-20 23:23:29 -05:00
|
|
|
var user = await identityContext.UserManager.FindByNameAsync(userName);
|
2026-06-12 23:48:11 -05:00
|
|
|
return await identityContext.UserManager.CheckPasswordAsync(user, password);
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
}
|
|
|
|
|
|
2025-06-21 23:20:18 +02:00
|
|
|
public async Task<IdentityResult> CheckRegister(User user, string password)
|
|
|
|
|
{
|
|
|
|
|
var registerErrors = new List<IdentityError>();
|
2026-06-12 23:48:11 -05:00
|
|
|
|
|
|
|
|
using var identityContext = await _identityContextFactory.CreateAsync();
|
|
|
|
|
var userManager = identityContext.UserManager;
|
2025-06-21 23:20:18 +02:00
|
|
|
|
|
|
|
|
foreach (var validator in userManager.UserValidators ?? [])
|
|
|
|
|
{
|
|
|
|
|
var result = await validator.ValidateAsync(userManager, user);
|
|
|
|
|
if (!result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
registerErrors.AddRange(result.Errors);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
foreach (var validator in userManager.PasswordValidators ?? [])
|
|
|
|
|
{
|
|
|
|
|
var result = await validator.ValidateAsync(userManager, user, password);
|
|
|
|
|
if (!result.Succeeded)
|
|
|
|
|
{
|
|
|
|
|
registerErrors.AddRange(result.Errors);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return registerErrors.Count > 0
|
|
|
|
|
? IdentityResult.Failed(registerErrors.ToArray())
|
|
|
|
|
: IdentityResult.Success;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-18 19:15:05 -05:00
|
|
|
public async Task<IdentityResult> ChangePassword(string userName, string currentPassword, string newPassword)
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var identityContext = await _identityContextFactory.CreateAsync();
|
2026-06-20 23:23:29 -05:00
|
|
|
var user = await identityContext.UserManager.FindByNameAsync(userName);
|
2026-06-12 23:48:11 -05:00
|
|
|
var result = await identityContext.UserManager.ChangePasswordAsync(user, currentPassword, newPassword);
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
2025-02-08 12:32:44 -06:00
|
|
|
return result;
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
}
|
|
|
|
|
|
2025-06-18 00:19:58 +02:00
|
|
|
public Task<IdentityResult> ChangePassword(string userName, string newPassword)
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
{
|
2025-06-18 00:19:58 +02:00
|
|
|
return ChangePassword(userName, newPassword, bypassPolicy: false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<IdentityResult> ChangePassword(string userName, string newPassword, bool bypassPolicy)
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
{
|
2025-06-18 00:19:58 +02:00
|
|
|
IdentityResult result;
|
2024-10-18 19:15:05 -05:00
|
|
|
|
2026-06-12 23:48:11 -05:00
|
|
|
using var identityContext = await _identityContextFactory.CreateAsync();
|
2026-06-20 23:23:29 -05:00
|
|
|
var user = await identityContext.UserManager.FindByNameAsync(userName);
|
2026-06-12 23:48:11 -05:00
|
|
|
|
|
|
|
|
if (bypassPolicy && identityContext.UserManager.PasswordValidators.Any())
|
2025-06-18 00:19:58 +02:00
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
await identityContext.UserManager.RemovePasswordAsync(user);
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
2026-06-12 23:48:11 -05:00
|
|
|
result = await identityContext.UserManager.AddPasswordAsync(user, newPassword);
|
2025-06-18 00:19:58 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
var token = await identityContext.UserManager.GeneratePasswordResetTokenAsync(user);
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
|
2026-06-12 23:48:11 -05:00
|
|
|
result = await identityContext.UserManager.ResetPasswordAsync(user, token, newPassword);
|
2025-06-18 00:19:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
WIP fix for MySQL connection concurrency issues
This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL:
- The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no.
- Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory.
- A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function.
- Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml`
- The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution.
- The application can now log to Seq when using debug build
- Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future.
- Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled.
- All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation.
- Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task SignOut()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
2024-10-18 19:15:05 -05:00
|
|
|
|
2026-01-11 14:38:24 -06:00
|
|
|
public virtual async Task<bool> AnyAsync()
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
using var context = await ContextFactory.CreateDbContextAsync();
|
|
|
|
|
|
|
|
|
|
var queryable = context.Set<User>().AsQueryable();
|
|
|
|
|
|
|
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
|
|
|
|
|
|
|
|
|
return await queryable.AnyAsync();
|
|
|
|
|
}
|
|
|
|
|
catch
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<ICollection<User>> GetAsync()
|
2024-10-18 19:15:05 -05:00
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
try
|
|
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var context = await ContextFactory.CreateDbContextAsync();
|
|
|
|
|
var queryable = context.Users.AsQueryable();
|
|
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
2026-06-12 23:48:11 -05:00
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
return await queryable.ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
2024-10-18 19:15:05 -05:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<ICollection<T>> GetAsync<T>()
|
2024-11-12 00:00:31 -06:00
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
try
|
|
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var context = await ContextFactory.CreateDbContextAsync();
|
|
|
|
|
var queryable = context.Users.AsQueryable();
|
|
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
2026-06-12 23:48:11 -05:00
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
return await queryable
|
|
|
|
|
.ProjectTo<T>(Mapper.ConfigurationProvider)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
2024-11-12 00:00:31 -06:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<User> GetAsync(Guid id)
|
2024-10-18 19:15:05 -05:00
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await FirstOrDefaultAsync(u => u.Id == id);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
2024-10-18 19:15:05 -05:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<T> GetAsync<T>(Guid id)
|
2024-11-12 00:00:31 -06:00
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await FirstOrDefaultAsync<T>(u => u.Id == id);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
2024-11-12 00:00:31 -06:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<ICollection<User>> GetAsync(Expression<Func<User, bool>> predicate)
|
2024-10-18 19:15:05 -05:00
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
try
|
|
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var context = await ContextFactory.CreateDbContextAsync();
|
|
|
|
|
var queryable = context.Users.AsQueryable();
|
|
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
2026-06-12 23:48:11 -05:00
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
return await queryable.Where(predicate).ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
2024-10-18 19:15:05 -05:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<ICollection<T>> GetAsync<T>(Expression<Func<User, bool>> predicate)
|
2024-11-12 00:00:31 -06:00
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
try
|
|
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var context = await ContextFactory.CreateDbContextAsync();
|
|
|
|
|
var queryable = context.Users.AsQueryable();
|
|
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
2026-06-12 23:48:11 -05:00
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
return await queryable
|
|
|
|
|
.Where(predicate)
|
|
|
|
|
.ProjectTo<T>(Mapper.ConfigurationProvider)
|
|
|
|
|
.ToListAsync();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
2024-11-12 00:00:31 -06:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<User> FirstOrDefaultAsync(Expression<Func<User, bool>> predicate)
|
2024-10-18 19:15:05 -05:00
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
try
|
|
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var context = await ContextFactory.CreateDbContextAsync();
|
|
|
|
|
var queryable = context.Users.AsQueryable();
|
|
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
2026-06-12 23:48:11 -05:00
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
return await queryable.FirstOrDefaultAsync(predicate);
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
2024-10-18 19:15:05 -05:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<T> FirstOrDefaultAsync<T>(Expression<Func<User, bool>> predicate)
|
2024-11-12 00:00:31 -06:00
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
try
|
|
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var context = await ContextFactory.CreateDbContextAsync();
|
|
|
|
|
var queryable = context.Users.AsQueryable();
|
|
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
2026-06-12 23:48:11 -05:00
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
return await queryable
|
|
|
|
|
.Where(predicate)
|
|
|
|
|
.ProjectTo<T>(Mapper.ConfigurationProvider)
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
2024-11-12 00:00:31 -06:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<User> FirstOrDefaultAsync<TKey>(Expression<Func<User, bool>> predicate, Expression<Func<User, TKey>> orderKeySelector)
|
2024-10-18 19:15:05 -05:00
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
try
|
|
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var context = await ContextFactory.CreateDbContextAsync();
|
|
|
|
|
var queryable = context.Users.AsQueryable();
|
|
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
2026-06-12 23:48:11 -05:00
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
return await queryable
|
|
|
|
|
.Where(predicate)
|
|
|
|
|
.OrderBy(orderKeySelector)
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
2024-11-12 00:00:31 -06:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<T> FirstOrDefaultAsync<T, TKey>(Expression<Func<User, bool>> predicate, Expression<Func<T, TKey>> orderKeySelector)
|
2024-11-12 00:00:31 -06:00
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
try
|
|
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var context = await ContextFactory.CreateDbContextAsync();
|
|
|
|
|
var queryable = context.Users.AsQueryable();
|
|
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
foreach (var modifier in _modifiers)
|
|
|
|
|
queryable = modifier.Invoke(queryable);
|
2026-06-12 23:48:11 -05:00
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
return await queryable
|
|
|
|
|
.Where(predicate)
|
|
|
|
|
.ProjectTo<T>(Mapper.ConfigurationProvider)
|
|
|
|
|
.OrderBy(orderKeySelector)
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Reset();
|
|
|
|
|
}
|
2024-10-18 19:15:05 -05:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<bool> ExistsAsync(Guid id)
|
2024-10-18 19:15:05 -05:00
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var context = await ContextFactory.CreateDbContextAsync();
|
|
|
|
|
return await context.Users.AnyAsync(u => u.Id == id);
|
2024-10-18 19:15:05 -05:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<ExistingEntityResult<User>> AddMissingAsync(Expression<Func<User, bool>> predicate, User entity)
|
2024-10-18 19:15:05 -05:00
|
|
|
{
|
2025-02-08 12:32:44 -06:00
|
|
|
var result = new ExistingEntityResult<User>();
|
2024-10-18 19:15:05 -05:00
|
|
|
|
2026-06-12 23:48:11 -05:00
|
|
|
var user = await FirstOrDefaultAsync(predicate);
|
2024-10-18 19:15:05 -05:00
|
|
|
|
2025-02-08 12:32:44 -06:00
|
|
|
if (user == null)
|
|
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var identityContext = await _identityContextFactory.CreateAsync();
|
|
|
|
|
await identityContext.UserManager.CreateAsync(entity);
|
2024-10-18 19:15:05 -05:00
|
|
|
|
2025-02-08 12:32:44 -06:00
|
|
|
result.Existing = false;
|
2026-06-12 23:48:11 -05:00
|
|
|
result.Value = await identityContext.UserManager.FindByNameAsync(entity.UserName);
|
2024-10-18 19:15:05 -05:00
|
|
|
}
|
2025-02-08 12:32:44 -06:00
|
|
|
else
|
2024-11-08 00:38:47 -06:00
|
|
|
{
|
2025-02-08 12:32:44 -06:00
|
|
|
result.Existing = true;
|
|
|
|
|
result.Value = user;
|
2024-11-08 00:38:47 -06:00
|
|
|
}
|
2025-02-08 12:32:44 -06:00
|
|
|
|
|
|
|
|
return result;
|
2024-10-18 19:15:05 -05:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task<User> UpdateAsync(User entity)
|
2024-10-18 19:15:05 -05:00
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var identityContext = await _identityContextFactory.CreateAsync();
|
|
|
|
|
|
|
|
|
|
var user = await identityContext.UserManager.FindByIdAsync(entity.Id.ToString());
|
2024-10-18 19:15:05 -05:00
|
|
|
|
2025-02-08 12:32:44 -06:00
|
|
|
user.UserName = entity.UserName;
|
|
|
|
|
user.PhoneNumber = entity.PhoneNumber;
|
|
|
|
|
user.Email = entity.Email;
|
|
|
|
|
user.TwoFactorEnabled = entity.TwoFactorEnabled;
|
|
|
|
|
user.Alias = entity.Alias;
|
|
|
|
|
user.Approved = entity.Approved;
|
|
|
|
|
user.ApprovedOn = entity.ApprovedOn;
|
2026-06-28 14:09:02 -05:00
|
|
|
user.StorageQuotaMB = entity.StorageQuotaMB;
|
|
|
|
|
user.DownloadSpeedKBps = entity.DownloadSpeedKBps;
|
|
|
|
|
user.EnableSaves = entity.EnableSaves;
|
2024-10-18 19:15:05 -05:00
|
|
|
|
2026-06-12 23:48:11 -05:00
|
|
|
await identityContext.UserManager.UpdateAsync(user);
|
2024-10-18 19:15:05 -05:00
|
|
|
|
2026-06-28 14:09:02 -05:00
|
|
|
await Cache.RemoveByTagAsync($"User/{entity.Id}");
|
|
|
|
|
|
2025-02-08 12:32:44 -06:00
|
|
|
return user;
|
2024-10-18 19:15:05 -05:00
|
|
|
}
|
|
|
|
|
|
2024-11-12 18:32:46 -06:00
|
|
|
public async Task DeleteAsync(User entity)
|
2024-10-18 19:15:05 -05:00
|
|
|
{
|
2026-06-12 23:48:11 -05:00
|
|
|
using var identityContext = await _identityContextFactory.CreateAsync();
|
|
|
|
|
|
|
|
|
|
var user = await identityContext.UserManager.FindByIdAsync(entity.Id.ToString());
|
2024-11-08 00:38:47 -06:00
|
|
|
|
2026-06-12 23:48:11 -05:00
|
|
|
await identityContext.UserManager.DeleteAsync(user);
|
2024-10-18 19:15:05 -05:00
|
|
|
}
|
2026-06-12 23:48:11 -05:00
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
public IBaseDatabaseService<User> AsNoTracking()
|
|
|
|
|
{
|
|
|
|
|
return Query((queryable) =>
|
|
|
|
|
{
|
|
|
|
|
return queryable.AsNoTracking();
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-11-11 18:22:30 -06:00
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
public IBaseDatabaseService<User> AsSplitQuery()
|
2024-11-11 18:22:30 -06:00
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
return Query((queryable) =>
|
|
|
|
|
{
|
|
|
|
|
return queryable.AsSplitQuery();
|
|
|
|
|
});
|
2024-11-11 18:22:30 -06:00
|
|
|
}
|
2024-11-25 22:29:56 -06:00
|
|
|
|
|
|
|
|
public IBaseDatabaseService<User> Query(Func<IQueryable<User>, IQueryable<User>> modifier)
|
|
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
_modifiers.Add(modifier);
|
|
|
|
|
|
|
|
|
|
return this;
|
2024-11-25 22:29:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IBaseDatabaseService<User> Include(params Expression<Func<User, object>>[] expressions)
|
|
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
return Query((queryable) =>
|
|
|
|
|
{
|
|
|
|
|
foreach (var expression in expressions)
|
|
|
|
|
{
|
|
|
|
|
queryable = queryable.Include(expression);
|
|
|
|
|
}
|
2024-11-25 22:29:56 -06:00
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
return queryable;
|
|
|
|
|
});
|
2024-11-25 22:29:56 -06:00
|
|
|
}
|
|
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
public IBaseDatabaseService<User> SortBy(Expression<Func<User, object>> expression, SortDirection direction = SortDirection.Ascending)
|
2024-11-25 22:29:56 -06:00
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
switch (direction)
|
|
|
|
|
{
|
|
|
|
|
case SortDirection.Descending:
|
|
|
|
|
return Query((queryable) =>
|
|
|
|
|
{
|
|
|
|
|
return queryable.OrderByDescending(expression);
|
|
|
|
|
});
|
|
|
|
|
case SortDirection.Ascending:
|
|
|
|
|
default:
|
|
|
|
|
return Query((queryable) =>
|
|
|
|
|
{
|
|
|
|
|
return queryable.OrderBy(expression);
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-11-25 22:29:56 -06:00
|
|
|
}
|
2026-06-12 23:48:11 -05:00
|
|
|
|
2025-03-08 14:13:51 -06:00
|
|
|
protected void Reset()
|
2024-11-25 22:29:56 -06:00
|
|
|
{
|
2025-03-08 14:13:51 -06:00
|
|
|
_modifiers.Clear();
|
2024-11-25 22:29:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
}
|
2024-08-21 19:40:27 -05:00
|
|
|
}
|
|
|
|
|
}
|