< 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
92%
Covered lines: 83
Uncovered lines: 7
Coverable lines: 90
Total lines: 140
Line coverage: 92.2%
Branch coverage
67%
Covered branches: 27
Total branches: 40
Branch coverage: 67.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)50%44100%
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.API.Services.Administration;
 3using LGDXRobotCloud.Data.Entities;
 4using LGDXRobotCloud.Data.Models.Business.Administration;
 5using LGDXRobotCloud.Data.Models.Business.Identity;
 6using LGDXRobotCloud.Utilities.Enums;
 7using Microsoft.AspNetCore.Identity;
 8
 9namespace LGDXRobotCloud.API.Services.Identity;
 10
 11public interface ICurrentUserService
 12{
 13  Task<LgdxUserBusinessModel> GetUserAsync(string userId);
 14  Task<bool> UpdateUserAsync(string userId, LgdxUserUpdateBusinessModel lgdxUserBusinessModel);
 15  Task<string> InitiateTwoFactorAsync(string userId);
 16  Task<List<string>> EnableTwoFactorAsync(string userId, string twoFactorCode);
 17  Task<List<string>> ResetRecoveryCodesAsync(string userId);
 18  Task<bool> DisableTwoFactorAsync(string userId);
 19}
 20
 1421public class CurrentUserService(
 1422    IActivityLogService activityLogService,
 1423    UserManager<LgdxUser> userManager
 1424  ) : ICurrentUserService
 25{
 1426  private readonly IActivityLogService _activityLogService = activityLogService ?? throw new ArgumentNullException(nameo
 1427  private readonly UserManager<LgdxUser> _userManager = userManager ?? throw new ArgumentNullException(nameof(userManage
 28
 29  public async Task<LgdxUserBusinessModel> GetUserAsync(string userId)
 230  {
 231    var user = await _userManager.FindByIdAsync(userId)
 232      ?? throw new LgdxNotFound404Exception();
 33
 134    return new LgdxUserBusinessModel
 135    {
 136      Id = Guid.Parse(user.Id!),
 137      Name = user.Name ?? string.Empty,
 138      UserName = user.UserName ?? string.Empty,
 139      Email = user.Email ?? string.Empty,
 140      Roles = await _userManager.GetRolesAsync(user),
 141      TwoFactorEnabled = user.TwoFactorEnabled,
 142      AccessFailedCount = user.AccessFailedCount
 143    };
 144  }
 45
 46  public async Task<bool> UpdateUserAsync(string userId, LgdxUserUpdateBusinessModel lgdxUserBusinessModel)
 347  {
 348    var user = await _userManager.FindByIdAsync(userId)
 349      ?? throw new LgdxNotFound404Exception();
 50
 251    user.Name = lgdxUserBusinessModel.Name;
 252    user.Email = lgdxUserBusinessModel.Email;
 253    var result = await _userManager.UpdateAsync(user);
 254    if (!result.Succeeded)
 155    {
 156      throw new LgdxIdentity400Expection(result.Errors);
 57    }
 58
 159    await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel
 160    {
 161      EntityName = nameof(LgdxUser),
 162      EntityId = user.Id.ToString(),
 163      Action = ActivityAction.Update,
 164    });
 65
 166    return true;
 167  }
 68
 69  public async Task<string> InitiateTwoFactorAsync(string userId)
 170  {
 171    var user = await _userManager.FindByIdAsync(userId)
 172      ?? throw new LgdxNotFound404Exception();
 73
 174    await _userManager.SetTwoFactorEnabledAsync(user, false);
 175    var key = await _userManager.GetAuthenticatorKeyAsync(user);
 176    if (string.IsNullOrEmpty(key))
 077    {
 078      await _userManager.ResetAuthenticatorKeyAsync(user);
 079      key = await _userManager.GetAuthenticatorKeyAsync(user);
 80
 081      if (string.IsNullOrEmpty(key))
 082      {
 083        throw new NotSupportedException("The user manager must produce an authenticator key after reset.");
 84      }
 085    }
 186    return key;
 187  }
 88
 89  public async Task<List<string>> EnableTwoFactorAsync(string userId, string twoFactorCode)
 490  {
 491    var user = await _userManager.FindByIdAsync(userId)
 492      ?? throw new LgdxNotFound404Exception();
 93
 394    if (string.IsNullOrEmpty(twoFactorCode))
 195    {
 196      throw new LgdxValidation400Expection("RequiresTwoFactor", "The 2FA code is required.");
 97    }
 298    if (!await _userManager.VerifyTwoFactorTokenAsync(user, _userManager.Options.Tokens.AuthenticatorTokenProvider, twoF
 199      {
 1100        throw new LgdxValidation400Expection("InvalidTwoFactorCode", "The 2FA code is invalid.");
 101      }
 1102    await _userManager.SetTwoFactorEnabledAsync(user, true);
 103
 1104    await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel
 1105    {
 1106      EntityName = nameof(LgdxUser),
 1107      EntityId = user.Id.ToString(),
 1108      Action = ActivityAction.UserTwoFactorAuthenticationEnabled,
 1109    });
 110
 1111    var recoveryCodesEnumerable = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10);
 1112    return recoveryCodesEnumerable?.ToList() ?? [];
 1113  }
 114
 115  public async Task<List<string>> ResetRecoveryCodesAsync(string userId)
 2116  {
 2117    var user = await _userManager.FindByIdAsync(userId)
 2118      ?? throw new LgdxNotFound404Exception();
 119
 1120    var recoveryCodesEnumerable = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10);
 1121    return recoveryCodesEnumerable?.ToList() ?? [];
 1122  }
 123
 124  public async Task<bool> DisableTwoFactorAsync(string userId)
 2125  {
 2126    var user = await _userManager.FindByIdAsync(userId)
 2127      ?? throw new LgdxNotFound404Exception();
 128
 1129    await _userManager.ResetAuthenticatorKeyAsync(user);
 1130    await _userManager.SetTwoFactorEnabledAsync(user, false);
 131
 1132    await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel
 1133    {
 1134      EntityName = nameof(LgdxUser),
 1135      EntityId = user.Id.ToString(),
 1136      Action = ActivityAction.UserTwoFactorAuthenticationDisabled,
 1137    });
 1138    return true;
 1139  }
 140}