< Summary

Information
Class: LGDXRobotCloud.API.Services.Identity.CurrentUserService
Assembly: LGDXRobotCloud.API
File(s): /builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Identity/CurrentUserService.cs
Line coverage
90%
Covered lines: 63
Uncovered lines: 7
Coverable lines: 70
Total lines: 115
Line coverage: 90%
Branch coverage
68%
Covered branches: 26
Total branches: 38
Branch coverage: 68.4%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%22100%
GetUserAsync()62.5%88100%
UpdateUserAsync()100%44100%
InitiateTwoFactorAsync()33.33%10653.33%
EnableTwoFactorAsync()80%1010100%
ResetRecoveryCodesAsync()66.66%66100%
DisableTwoFactorAsync()100%22100%

File(s)

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Services/Identity/CurrentUserService.cs

#LineLine coverage
 1using LGDXRobotCloud.API.Exceptions;
 2using LGDXRobotCloud.Data.Entities;
 3using LGDXRobotCloud.Data.Models.Business.Administration;
 4using LGDXRobotCloud.Data.Models.Business.Identity;
 5using Microsoft.AspNetCore.Identity;
 6
 7namespace LGDXRobotCloud.API.Services.Identity;
 8
 9public interface ICurrentUserService
 10{
 11  Task<LgdxUserBusinessModel> GetUserAsync(string userId);
 12  Task<bool> UpdateUserAsync(string userId, LgdxUserUpdateBusinessModel lgdxUserBusinessModel);
 13  Task<string> InitiateTwoFactorAsync(string userId);
 14  Task<List<string>> EnableTwoFactorAsync(string userId, string twoFactorCode);
 15  Task<List<string>> ResetRecoveryCodesAsync(string userId);
 16  Task<bool> DisableTwoFactorAsync(string userId);
 17}
 18
 1419public class CurrentUserService(
 1420    UserManager<LgdxUser> userManager
 1421  ) : ICurrentUserService
 22{
 1423  private readonly UserManager<LgdxUser> _userManager = userManager ?? throw new ArgumentNullException(nameof(userManage
 24
 25  public async Task<LgdxUserBusinessModel> GetUserAsync(string userId)
 226  {
 227    var user = await _userManager.FindByIdAsync(userId)
 228      ?? throw new LgdxNotFound404Exception();
 29
 130    return new LgdxUserBusinessModel
 131    {
 132      Id = Guid.Parse(user.Id!),
 133      Name = user.Name ?? string.Empty,
 134      UserName = user.UserName ?? string.Empty,
 135      Email = user.Email ?? string.Empty,
 136      Roles = await _userManager.GetRolesAsync(user),
 137      TwoFactorEnabled = user.TwoFactorEnabled,
 138      AccessFailedCount = user.AccessFailedCount
 139    };
 140  }
 41
 42  public async Task<bool> UpdateUserAsync(string userId, LgdxUserUpdateBusinessModel lgdxUserBusinessModel)
 343  {
 344    var user = await _userManager.FindByIdAsync(userId)
 345      ?? throw new LgdxNotFound404Exception();
 46
 247    user.Name = lgdxUserBusinessModel.Name;
 248    user.Email = lgdxUserBusinessModel.Email;
 249    var result = await _userManager.UpdateAsync(user);
 250    if (!result.Succeeded)
 151    {
 152      throw new LgdxIdentity400Expection(result.Errors);
 53    }
 54
 155    return true;
 156  }
 57
 58  public async Task<string> InitiateTwoFactorAsync(string userId)
 159  {
 160    var user = await _userManager.FindByIdAsync(userId)
 161      ?? throw new LgdxNotFound404Exception();
 62
 163    await _userManager.SetTwoFactorEnabledAsync(user, false);
 164    var key = await _userManager.GetAuthenticatorKeyAsync(user);
 165    if (string.IsNullOrEmpty(key))
 066    {
 067      await _userManager.ResetAuthenticatorKeyAsync(user);
 068      key = await _userManager.GetAuthenticatorKeyAsync(user);
 69
 070      if (string.IsNullOrEmpty(key))
 071      {
 072        throw new NotSupportedException("The user manager must produce an authenticator key after reset.");
 73      }
 074    }
 175    return key;
 176  }
 77
 78  public async Task<List<string>> EnableTwoFactorAsync(string userId, string twoFactorCode)
 479  {
 480    var user = await _userManager.FindByIdAsync(userId)
 481      ?? throw new LgdxNotFound404Exception();
 82
 383    if (string.IsNullOrEmpty(twoFactorCode))
 184    {
 185      throw new LgdxValidation400Expection("RequiresTwoFactor", "The 2FA code is required.");
 86    }
 287    if (!await _userManager.VerifyTwoFactorTokenAsync(user, _userManager.Options.Tokens.AuthenticatorTokenProvider, twoF
 188      {
 189        throw new LgdxValidation400Expection("InvalidTwoFactorCode", "The 2FA code is invalid.");
 90      }
 191    await _userManager.SetTwoFactorEnabledAsync(user, true);
 92
 193    var recoveryCodesEnumerable = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10);
 194    return recoveryCodesEnumerable?.ToList() ?? [];
 195  }
 96
 97  public async Task<List<string>> ResetRecoveryCodesAsync(string userId)
 298  {
 299    var user = await _userManager.FindByIdAsync(userId)
 2100      ?? throw new LgdxNotFound404Exception();
 101
 1102    var recoveryCodesEnumerable = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10);
 1103    return recoveryCodesEnumerable?.ToList() ?? [];
 1104  }
 105
 106  public async Task<bool> DisableTwoFactorAsync(string userId)
 2107  {
 2108    var user = await _userManager.FindByIdAsync(userId)
 2109      ?? throw new LgdxNotFound404Exception();
 110
 1111    await _userManager.ResetAuthenticatorKeyAsync(user);
 1112    await _userManager.SetTwoFactorEnabledAsync(user, false);
 1113    return true;
 1114  }
 115}