< Summary

Information
Class: Anichron.API.Services.BootstrapResetService
Assembly: Anichron.API
File(s): /home/runner/work/anichron/anichron/src/Anichron.API/Services/BootstrapResetService.cs
Tag: 228_36821334185
Line coverage
100%
Covered lines: 34
Uncovered lines: 0
Coverable lines: 34
Total lines: 75
Line coverage: 100%
Branch coverage
100%
Covered branches: 12
Total branches: 12
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
ResetIfRequestedAsync()100%1212100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.API/Services/BootstrapResetService.cs

#LineLine coverage
 1using Anichron.API.Security;
 2using Anichron.Core.Data;
 3using Anichron.Core.Data.Repository;
 4using Anichron.Core.Domain;
 5
 6namespace Anichron.API.Services;
 7
 8public interface IBootstrapResetService
 9{
 10    Task ResetIfRequestedAsync(CancellationToken ct);
 11}
 12
 713public sealed partial class BootstrapResetService(
 714    IUserRepository users,
 715    IUnitOfWork unitOfWork,
 716    IPasswordHasher passwordHasher,
 717    IConfiguration configuration,
 718    ILogger<BootstrapResetService> logger) : IBootstrapResetService
 19{
 20    public async Task ResetIfRequestedAsync(CancellationToken ct)
 721    {
 722        var resetPassword = configuration["ADMIN_RESET_PASSWORD"];
 723        if (string.IsNullOrEmpty(resetPassword))
 124            return;
 25
 626        var targetUsername = configuration["ADMIN_RESET_USERNAME"]?.Trim().ToLowerInvariant();
 27        User? admin;
 28
 629        if (!string.IsNullOrEmpty(targetUsername))
 330        {
 331            admin = await users.FindAdminByUsernameAsync(targetUsername, ct);
 332            if (admin is null)
 133            {
 134                Log.AdminNotFoundByUsername(logger, targetUsername);
 135                return;
 36            }
 237        }
 38        else
 339        {
 340            var admins = await users.FindAdminsAsync(take: 2, ct);
 341            switch (admins.Count)
 42            {
 43                case 0:
 144                    Log.NoAdminFound(logger);
 145                    return;
 46                case > 1:
 147                    Log.MultipleAdminsFound(logger);
 148                    return;
 49                default:
 150                    admin = admins[0];
 151                    break;
 52            }
 153        }
 54
 355        admin.PasswordHash = passwordHasher.Hash(resetPassword);
 356        admin.MustChangePassword = true;
 357        await unitOfWork.SaveChangesAsync(ct);
 358        Log.AdminPasswordReset(logger, admin.Username);
 759    }
 60
 61    private static partial class Log
 62    {
 63        [LoggerMessage(Level = LogLevel.Error, Message = "ADMIN_RESET_PASSWORD is set but no admin with username '{Usern
 64        public static partial void AdminNotFoundByUsername(ILogger logger, string username);
 65
 66        [LoggerMessage(Level = LogLevel.Warning, Message = "ADMIN_RESET_PASSWORD is set but no admin user was found.")]
 67        public static partial void NoAdminFound(ILogger logger);
 68
 69        [LoggerMessage(Level = LogLevel.Error, Message = "ADMIN_RESET_PASSWORD is set but multiple admins exist. Set ADM
 70        public static partial void MultipleAdminsFound(ILogger logger);
 71
 72        [LoggerMessage(Level = LogLevel.Warning, Message = "Admin password has been reset for '{Username}'. Remove ADMIN
 73        public static partial void AdminPasswordReset(ILogger logger, string username);
 74    }
 75}