| | 1 | | using LGDXRobotCloud.API.Exceptions; |
| | 2 | | using LGDXRobotCloud.API.Services.Administration; |
| | 3 | | using LGDXRobotCloud.Data.Entities; |
| | 4 | | using LGDXRobotCloud.Data.Models.Business.Administration; |
| | 5 | | using LGDXRobotCloud.Data.Models.Business.Identity; |
| | 6 | | using LGDXRobotCloud.Utilities.Enums; |
| | 7 | | using Microsoft.AspNetCore.Identity; |
| | 8 | |
|
| | 9 | | namespace LGDXRobotCloud.API.Services.Identity; |
| | 10 | |
|
| | 11 | | public 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 | |
|
| 14 | 21 | | public class CurrentUserService( |
| 14 | 22 | | IActivityLogService activityLogService, |
| 14 | 23 | | UserManager<LgdxUser> userManager |
| 14 | 24 | | ) : ICurrentUserService |
| | 25 | | { |
| 14 | 26 | | private readonly IActivityLogService _activityLogService = activityLogService ?? throw new ArgumentNullException(nameo |
| 14 | 27 | | private readonly UserManager<LgdxUser> _userManager = userManager ?? throw new ArgumentNullException(nameof(userManage |
| | 28 | |
|
| | 29 | | public async Task<LgdxUserBusinessModel> GetUserAsync(string userId) |
| 2 | 30 | | { |
| 2 | 31 | | var user = await _userManager.FindByIdAsync(userId) |
| 2 | 32 | | ?? throw new LgdxNotFound404Exception(); |
| | 33 | |
|
| 1 | 34 | | return new LgdxUserBusinessModel |
| 1 | 35 | | { |
| 1 | 36 | | Id = Guid.Parse(user.Id!), |
| 1 | 37 | | Name = user.Name ?? string.Empty, |
| 1 | 38 | | UserName = user.UserName ?? string.Empty, |
| 1 | 39 | | Email = user.Email ?? string.Empty, |
| 1 | 40 | | Roles = await _userManager.GetRolesAsync(user), |
| 1 | 41 | | TwoFactorEnabled = user.TwoFactorEnabled, |
| 1 | 42 | | AccessFailedCount = user.AccessFailedCount |
| 1 | 43 | | }; |
| 1 | 44 | | } |
| | 45 | |
|
| | 46 | | public async Task<bool> UpdateUserAsync(string userId, LgdxUserUpdateBusinessModel lgdxUserBusinessModel) |
| 3 | 47 | | { |
| 3 | 48 | | var user = await _userManager.FindByIdAsync(userId) |
| 3 | 49 | | ?? throw new LgdxNotFound404Exception(); |
| | 50 | |
|
| 2 | 51 | | user.Name = lgdxUserBusinessModel.Name; |
| 2 | 52 | | user.Email = lgdxUserBusinessModel.Email; |
| 2 | 53 | | var result = await _userManager.UpdateAsync(user); |
| 2 | 54 | | if (!result.Succeeded) |
| 1 | 55 | | { |
| 1 | 56 | | throw new LgdxIdentity400Expection(result.Errors); |
| | 57 | | } |
| | 58 | |
|
| 1 | 59 | | await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel |
| 1 | 60 | | { |
| 1 | 61 | | EntityName = nameof(LgdxUser), |
| 1 | 62 | | EntityId = user.Id.ToString(), |
| 1 | 63 | | Action = ActivityAction.Update, |
| 1 | 64 | | }); |
| | 65 | |
|
| 1 | 66 | | return true; |
| 1 | 67 | | } |
| | 68 | |
|
| | 69 | | public async Task<string> InitiateTwoFactorAsync(string userId) |
| 1 | 70 | | { |
| 1 | 71 | | var user = await _userManager.FindByIdAsync(userId) |
| 1 | 72 | | ?? throw new LgdxNotFound404Exception(); |
| | 73 | |
|
| 1 | 74 | | await _userManager.SetTwoFactorEnabledAsync(user, false); |
| 1 | 75 | | var key = await _userManager.GetAuthenticatorKeyAsync(user); |
| 1 | 76 | | if (string.IsNullOrEmpty(key)) |
| 0 | 77 | | { |
| 0 | 78 | | await _userManager.ResetAuthenticatorKeyAsync(user); |
| 0 | 79 | | key = await _userManager.GetAuthenticatorKeyAsync(user); |
| | 80 | |
|
| 0 | 81 | | if (string.IsNullOrEmpty(key)) |
| 0 | 82 | | { |
| 0 | 83 | | throw new NotSupportedException("The user manager must produce an authenticator key after reset."); |
| | 84 | | } |
| 0 | 85 | | } |
| 1 | 86 | | return key; |
| 1 | 87 | | } |
| | 88 | |
|
| | 89 | | public async Task<List<string>> EnableTwoFactorAsync(string userId, string twoFactorCode) |
| 4 | 90 | | { |
| 4 | 91 | | var user = await _userManager.FindByIdAsync(userId) |
| 4 | 92 | | ?? throw new LgdxNotFound404Exception(); |
| | 93 | |
|
| 3 | 94 | | if (string.IsNullOrEmpty(twoFactorCode)) |
| 1 | 95 | | { |
| 1 | 96 | | throw new LgdxValidation400Expection("RequiresTwoFactor", "The 2FA code is required."); |
| | 97 | | } |
| 2 | 98 | | if (!await _userManager.VerifyTwoFactorTokenAsync(user, _userManager.Options.Tokens.AuthenticatorTokenProvider, twoF |
| 1 | 99 | | { |
| 1 | 100 | | throw new LgdxValidation400Expection("InvalidTwoFactorCode", "The 2FA code is invalid."); |
| | 101 | | } |
| 1 | 102 | | await _userManager.SetTwoFactorEnabledAsync(user, true); |
| | 103 | |
|
| 1 | 104 | | await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel |
| 1 | 105 | | { |
| 1 | 106 | | EntityName = nameof(LgdxUser), |
| 1 | 107 | | EntityId = user.Id.ToString(), |
| 1 | 108 | | Action = ActivityAction.UserTwoFactorAuthenticationEnabled, |
| 1 | 109 | | }); |
| | 110 | |
|
| 1 | 111 | | var recoveryCodesEnumerable = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10); |
| 1 | 112 | | return recoveryCodesEnumerable?.ToList() ?? []; |
| 1 | 113 | | } |
| | 114 | |
|
| | 115 | | public async Task<List<string>> ResetRecoveryCodesAsync(string userId) |
| 2 | 116 | | { |
| 2 | 117 | | var user = await _userManager.FindByIdAsync(userId) |
| 2 | 118 | | ?? throw new LgdxNotFound404Exception(); |
| | 119 | |
|
| 1 | 120 | | var recoveryCodesEnumerable = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10); |
| 1 | 121 | | return recoveryCodesEnumerable?.ToList() ?? []; |
| 1 | 122 | | } |
| | 123 | |
|
| | 124 | | public async Task<bool> DisableTwoFactorAsync(string userId) |
| 2 | 125 | | { |
| 2 | 126 | | var user = await _userManager.FindByIdAsync(userId) |
| 2 | 127 | | ?? throw new LgdxNotFound404Exception(); |
| | 128 | |
|
| 1 | 129 | | await _userManager.ResetAuthenticatorKeyAsync(user); |
| 1 | 130 | | await _userManager.SetTwoFactorEnabledAsync(user, false); |
| | 131 | |
|
| 1 | 132 | | await _activityLogService.CreateActivityLogAsync(new ActivityLogCreateBusinessModel |
| 1 | 133 | | { |
| 1 | 134 | | EntityName = nameof(LgdxUser), |
| 1 | 135 | | EntityId = user.Id.ToString(), |
| 1 | 136 | | Action = ActivityAction.UserTwoFactorAuthenticationDisabled, |
| 1 | 137 | | }); |
| 1 | 138 | | return true; |
| 1 | 139 | | } |
| | 140 | | } |