| | 1 | | using LGDXRobotCloud.API.Exceptions; |
| | 2 | | using LGDXRobotCloud.API.Services.Common; |
| | 3 | | using LGDXRobotCloud.Data.DbContexts; |
| | 4 | | using LGDXRobotCloud.Data.Entities; |
| | 5 | | using LGDXRobotCloud.Data.Models.Business.Administration; |
| | 6 | | using LGDXRobotCloud.Utilities.Helpers; |
| | 7 | | using Microsoft.AspNetCore.Identity; |
| | 8 | | using Microsoft.EntityFrameworkCore; |
| | 9 | |
|
| | 10 | | namespace LGDXRobotCloud.API.Services.Administration; |
| | 11 | |
|
| | 12 | | public interface IUserService |
| | 13 | | { |
| | 14 | | Task<(IEnumerable<LgdxUserListBusinessModel>, PaginationHelper)> GetUsersAsync(string? name, int pageNumber, int pageS |
| | 15 | | Task<LgdxUserBusinessModel> GetUserAsync(Guid id); |
| | 16 | | Task<LgdxUserBusinessModel> CreateUserAsync(LgdxUserCreateAdminBusinessModel lgdxUserCreateAdminBusinessModel); |
| | 17 | | Task<bool> UpdateUserAsync(Guid id, LgdxUserUpdateAdminBusinessModel lgdxUserUpdateAdminBusinessModel); |
| | 18 | | Task<bool> UnlockUserAsync(Guid id); |
| | 19 | | Task<bool> DeleteUserAsync(Guid id, string operatorId); |
| | 20 | | } |
| | 21 | |
|
| 0 | 22 | | public class UserService( |
| 0 | 23 | | IEmailService emailService, |
| 0 | 24 | | UserManager<LgdxUser> userManager, |
| 0 | 25 | | LgdxContext context |
| 0 | 26 | | ) : IUserService |
| | 27 | | { |
| 0 | 28 | | private readonly IEmailService _emailService = emailService ?? throw new ArgumentNullException(nameof(emailService)); |
| 0 | 29 | | private readonly UserManager<LgdxUser> _userManager = userManager ?? throw new ArgumentNullException(nameof(userManage |
| 0 | 30 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| | 31 | |
|
| | 32 | | public async Task<(IEnumerable<LgdxUserListBusinessModel>, PaginationHelper)> GetUsersAsync(string? name, int pageNumb |
| 0 | 33 | | { |
| 0 | 34 | | var query = _context.Users as IQueryable<LgdxUser>; |
| 0 | 35 | | if (!string.IsNullOrWhiteSpace(name)) |
| 0 | 36 | | { |
| 0 | 37 | | name = name.Trim().ToUpper(); |
| 0 | 38 | | query = query.Where(u => u.NormalizedUserName!.Contains(name)); |
| 0 | 39 | | } |
| 0 | 40 | | var itemCount = await query.CountAsync(); |
| 0 | 41 | | var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize); |
| 0 | 42 | | var users = await query.AsNoTracking() |
| 0 | 43 | | .OrderBy(t => t.Id) |
| 0 | 44 | | .Skip(pageSize * (pageNumber - 1)) |
| 0 | 45 | | .Take(pageSize) |
| 0 | 46 | | .Select(t => new LgdxUserListBusinessModel { |
| 0 | 47 | | Id = Guid.Parse(t.Id!), |
| 0 | 48 | | Name = t.Name!, |
| 0 | 49 | | UserName = t.UserName!, |
| 0 | 50 | | TwoFactorEnabled = t.TwoFactorEnabled, |
| 0 | 51 | | AccessFailedCount = t.AccessFailedCount, |
| 0 | 52 | | }) |
| 0 | 53 | | .ToListAsync(); |
| 0 | 54 | | return (users, PaginationHelper); |
| 0 | 55 | | } |
| | 56 | |
|
| | 57 | | public async Task<LgdxUserBusinessModel> GetUserAsync(Guid id) |
| 0 | 58 | | { |
| 0 | 59 | | var user = await _userManager.FindByIdAsync(id.ToString()) ?? throw new LgdxNotFound404Exception(); |
| 0 | 60 | | var roles = await _userManager.GetRolesAsync(user); |
| 0 | 61 | | return new LgdxUserBusinessModel { |
| 0 | 62 | | Id = Guid.Parse(user.Id), |
| 0 | 63 | | Name = user.Name!, |
| 0 | 64 | | UserName = user.UserName!, |
| 0 | 65 | | Email = user.Email!, |
| 0 | 66 | | Roles = roles, |
| 0 | 67 | | TwoFactorEnabled = user.TwoFactorEnabled, |
| 0 | 68 | | AccessFailedCount = user.AccessFailedCount, |
| 0 | 69 | | }; |
| 0 | 70 | | } |
| | 71 | |
|
| | 72 | | public async Task<LgdxUserBusinessModel> CreateUserAsync(LgdxUserCreateAdminBusinessModel lgdxUserCreateAdminBusinessM |
| 0 | 73 | | { |
| 0 | 74 | | var user = new LgdxUser { |
| 0 | 75 | | Id = Guid.CreateVersion7().ToString(), |
| 0 | 76 | | Email = lgdxUserCreateAdminBusinessModel.Email, |
| 0 | 77 | | EmailConfirmed = true, |
| 0 | 78 | | LockoutEnabled = true, |
| 0 | 79 | | Name = lgdxUserCreateAdminBusinessModel.Name, |
| 0 | 80 | | NormalizedEmail = lgdxUserCreateAdminBusinessModel.Email.ToUpper(), |
| 0 | 81 | | NormalizedUserName = lgdxUserCreateAdminBusinessModel.UserName.ToUpper(), |
| 0 | 82 | | SecurityStamp = Guid.CreateVersion7().ToString(), |
| 0 | 83 | | UserName = lgdxUserCreateAdminBusinessModel.UserName |
| 0 | 84 | | }; |
| 0 | 85 | | if (!string.IsNullOrWhiteSpace(lgdxUserCreateAdminBusinessModel.Password)) |
| 0 | 86 | | { |
| 0 | 87 | | var result = await _userManager.CreateAsync(user, lgdxUserCreateAdminBusinessModel.Password); |
| 0 | 88 | | if (!result.Succeeded) |
| 0 | 89 | | { |
| 0 | 90 | | throw new LgdxIdentity400Expection(result.Errors); |
| | 91 | | } |
| 0 | 92 | | } |
| | 93 | | else |
| 0 | 94 | | { |
| 0 | 95 | | var result = await _userManager.CreateAsync(user); |
| 0 | 96 | | if (!result.Succeeded) |
| 0 | 97 | | { |
| 0 | 98 | | throw new LgdxIdentity400Expection(result.Errors); |
| | 99 | | } |
| 0 | 100 | | } |
| | 101 | |
|
| | 102 | | // Add Roles |
| 0 | 103 | | var roleToAdd = lgdxUserCreateAdminBusinessModel.Roles; |
| 0 | 104 | | var roleAddingResult = await _userManager.AddToRolesAsync(user, roleToAdd); |
| 0 | 105 | | if (!roleAddingResult.Succeeded) |
| 0 | 106 | | { |
| 0 | 107 | | throw new LgdxIdentity400Expection(roleAddingResult.Errors); |
| | 108 | | } |
| | 109 | |
|
| | 110 | | // Send Email |
| 0 | 111 | | if (string.IsNullOrWhiteSpace(lgdxUserCreateAdminBusinessModel.Password)) |
| 0 | 112 | | { |
| | 113 | | // No password is specified |
| 0 | 114 | | var token = await _userManager.GeneratePasswordResetTokenAsync(user!); |
| 0 | 115 | | await _emailService.SendWellcomePasswordSetEmailAsync( |
| 0 | 116 | | lgdxUserCreateAdminBusinessModel.Email, |
| 0 | 117 | | lgdxUserCreateAdminBusinessModel.Name, |
| 0 | 118 | | lgdxUserCreateAdminBusinessModel.UserName, |
| 0 | 119 | | token |
| 0 | 120 | | ); |
| 0 | 121 | | } |
| | 122 | | else |
| 0 | 123 | | { |
| | 124 | | // Password is specified |
| 0 | 125 | | await _emailService.SendWelcomeEmailAsync( |
| 0 | 126 | | lgdxUserCreateAdminBusinessModel.Email, |
| 0 | 127 | | lgdxUserCreateAdminBusinessModel.Name, |
| 0 | 128 | | lgdxUserCreateAdminBusinessModel.UserName |
| 0 | 129 | | ); |
| 0 | 130 | | } |
| | 131 | |
|
| 0 | 132 | | return new LgdxUserBusinessModel { |
| 0 | 133 | | Id = Guid.Parse(user.Id), |
| 0 | 134 | | Name = user.Name!, |
| 0 | 135 | | UserName = user.UserName!, |
| 0 | 136 | | Email = user.Email!, |
| 0 | 137 | | Roles = lgdxUserCreateAdminBusinessModel.Roles, |
| 0 | 138 | | TwoFactorEnabled = user.TwoFactorEnabled, |
| 0 | 139 | | AccessFailedCount = user.AccessFailedCount, |
| 0 | 140 | | }; |
| 0 | 141 | | } |
| | 142 | |
|
| | 143 | | public async Task<bool> UpdateUserAsync(Guid id, LgdxUserUpdateAdminBusinessModel lgdxUserUpdateAdminBusinessModel) |
| 0 | 144 | | { |
| 0 | 145 | | var user = await _userManager.FindByIdAsync(id.ToString()) ?? throw new LgdxNotFound404Exception(); |
| | 146 | |
|
| 0 | 147 | | user.Name = lgdxUserUpdateAdminBusinessModel.Name; |
| 0 | 148 | | user.UserName = lgdxUserUpdateAdminBusinessModel.UserName; |
| 0 | 149 | | user.Email = lgdxUserUpdateAdminBusinessModel.Email; |
| 0 | 150 | | user.NormalizedEmail = lgdxUserUpdateAdminBusinessModel.Email.ToUpper(); |
| 0 | 151 | | user.NormalizedUserName = lgdxUserUpdateAdminBusinessModel.UserName.ToUpper(); |
| | 152 | |
|
| 0 | 153 | | var result = await _userManager.UpdateAsync(user); |
| 0 | 154 | | if (!result.Succeeded) |
| 0 | 155 | | { |
| 0 | 156 | | throw new LgdxIdentity400Expection(result.Errors); |
| | 157 | | } |
| | 158 | |
|
| 0 | 159 | | var roles = await _userManager.GetRolesAsync(user); |
| 0 | 160 | | var roleToAdd = lgdxUserUpdateAdminBusinessModel.Roles.Except(roles); |
| 0 | 161 | | result = await _userManager.AddToRolesAsync(user, roleToAdd); |
| 0 | 162 | | if (!result.Succeeded) |
| 0 | 163 | | { |
| 0 | 164 | | throw new LgdxIdentity400Expection(result.Errors); |
| | 165 | | } |
| 0 | 166 | | var roleToRemove = roles.Except(lgdxUserUpdateAdminBusinessModel.Roles); |
| 0 | 167 | | result = await _userManager.RemoveFromRolesAsync(user, roleToRemove); |
| 0 | 168 | | if (!result.Succeeded) |
| 0 | 169 | | { |
| 0 | 170 | | throw new LgdxIdentity400Expection(result.Errors); |
| | 171 | | } |
| 0 | 172 | | return true; |
| 0 | 173 | | } |
| | 174 | |
|
| | 175 | | public async Task<bool> UnlockUserAsync(Guid id) |
| 0 | 176 | | { |
| 0 | 177 | | var user = await _userManager.FindByIdAsync(id.ToString()) ?? throw new LgdxNotFound404Exception(); |
| | 178 | |
|
| 0 | 179 | | user.AccessFailedCount = 0; |
| 0 | 180 | | user.LockoutEnd = null; |
| | 181 | |
|
| 0 | 182 | | var result = await _userManager.UpdateAsync(user); |
| 0 | 183 | | if (!result.Succeeded) |
| 0 | 184 | | { |
| 0 | 185 | | throw new LgdxIdentity400Expection(result.Errors); |
| | 186 | | } |
| 0 | 187 | | return true; |
| 0 | 188 | | } |
| | 189 | |
|
| | 190 | | public async Task<bool> DeleteUserAsync(Guid id, string operatorId) |
| 0 | 191 | | { |
| 0 | 192 | | var user = await _userManager.FindByIdAsync(id.ToString()) ?? throw new LgdxNotFound404Exception(); |
| 0 | 193 | | if (user.Id == operatorId) |
| 0 | 194 | | { |
| 0 | 195 | | throw new LgdxValidation400Expection(nameof(id), "Cannot delete yourself."); |
| | 196 | | } |
| 0 | 197 | | var result = await _userManager.DeleteAsync(user); |
| 0 | 198 | | if (!result.Succeeded) |
| 0 | 199 | | { |
| 0 | 200 | | throw new LgdxIdentity400Expection(result.Errors); |
| | 201 | | } |
| 0 | 202 | | return true; |
| 0 | 203 | | } |
| | 204 | | } |