| | 1 | | using System.Security.Claims; |
| | 2 | | using LGDXRobotCloud.API.Exceptions; |
| | 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 IRoleService |
| | 13 | | { |
| | 14 | | Task<(IEnumerable<LgdxRoleListBusinessModel>, PaginationHelper)> GetRolesAsync(string? name, int pageNumber, int pageS |
| | 15 | | Task<LgdxRoleBusinessModel> GetRoleAsync(Guid id); |
| | 16 | | Task<LgdxRoleBusinessModel> CreateRoleAsync(LgdxRoleCreateBusinessModel lgdxRoleBusinessModel); |
| | 17 | | Task<bool> UpdateRoleAsync(Guid id, LgdxRoleUpdateBusinessModel lgdxRoleUpdateBusinessModel); |
| | 18 | | Task<bool> DeleteRoleAsync(Guid id); |
| | 19 | |
|
| | 20 | | Task<IEnumerable<LgdxRoleSearchBusinessModel>> SearchRoleAsync(string? name); |
| | 21 | | } |
| | 22 | |
|
| 0 | 23 | | public class RoleService( |
| 0 | 24 | | LgdxContext context, |
| 0 | 25 | | RoleManager<LgdxRole> roleManager |
| 0 | 26 | | ) : IRoleService |
| | 27 | | { |
| 0 | 28 | | private readonly LgdxContext _context = context ?? throw new ArgumentNullException(nameof(context)); |
| 0 | 29 | | private readonly RoleManager<LgdxRole> _roleManager = roleManager ?? throw new ArgumentNullException(nameof(roleManage |
| | 30 | |
|
| | 31 | | public async Task<(IEnumerable<LgdxRoleListBusinessModel>, PaginationHelper)> GetRolesAsync(string? name, int pageNumb |
| 0 | 32 | | { |
| 0 | 33 | | var query = _context.Roles as IQueryable<LgdxRole>; |
| 0 | 34 | | if (!string.IsNullOrWhiteSpace(name)) |
| 0 | 35 | | { |
| 0 | 36 | | name = name.Trim().ToUpper(); |
| 0 | 37 | | query = query.Where(u => u.NormalizedName!.Contains(name)); |
| 0 | 38 | | } |
| 0 | 39 | | var itemCount = await query.CountAsync(); |
| 0 | 40 | | var PaginationHelper = new PaginationHelper(itemCount, pageNumber, pageSize); |
| 0 | 41 | | var roles = await query.AsNoTracking() |
| 0 | 42 | | .OrderBy(t => t.Id) |
| 0 | 43 | | .Skip(pageSize * (pageNumber - 1)) |
| 0 | 44 | | .Take(pageSize) |
| 0 | 45 | | .Select(t => new LgdxRoleListBusinessModel { |
| 0 | 46 | | Id = Guid.Parse(t.Id!), |
| 0 | 47 | | Name = t.Name!, |
| 0 | 48 | | Description = t.Description, |
| 0 | 49 | | }) |
| 0 | 50 | | .ToListAsync(); |
| 0 | 51 | | return (roles, PaginationHelper); |
| 0 | 52 | | } |
| | 53 | |
|
| | 54 | | public async Task<LgdxRoleBusinessModel> GetRoleAsync(Guid id) |
| 0 | 55 | | { |
| 0 | 56 | | var role = await _context.Roles.AsNoTracking() |
| 0 | 57 | | .Where(r => r.Id == id.ToString()) |
| 0 | 58 | | .Select(r => new{ |
| 0 | 59 | | r.Id, |
| 0 | 60 | | r.Name, |
| 0 | 61 | | r.Description, |
| 0 | 62 | | }) |
| 0 | 63 | | .FirstOrDefaultAsync() |
| 0 | 64 | | ?? throw new LgdxNotFound404Exception(); |
| | 65 | |
|
| 0 | 66 | | var claims = await _context.RoleClaims.AsNoTracking() |
| 0 | 67 | | .Where(c => c.RoleId == id.ToString()) |
| 0 | 68 | | .Where(c => c.ClaimType == "scope") |
| 0 | 69 | | .Select(r => new { |
| 0 | 70 | | ClaimValue = r.ClaimValue!, |
| 0 | 71 | | }).ToListAsync(); |
| | 72 | |
|
| 0 | 73 | | return new LgdxRoleBusinessModel { |
| 0 | 74 | | Id = Guid.Parse(role.Id), |
| 0 | 75 | | Name = role.Name!, |
| 0 | 76 | | Description = role.Description, |
| 0 | 77 | | Scopes = claims.Select(c => c.ClaimValue!), |
| 0 | 78 | | }; |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | public async Task<LgdxRoleBusinessModel> CreateRoleAsync(LgdxRoleCreateBusinessModel lgdxRoleBusinessModel) |
| 0 | 82 | | { |
| 0 | 83 | | var role = new LgdxRole { |
| 0 | 84 | | Id = Guid.CreateVersion7().ToString(), |
| 0 | 85 | | Name = lgdxRoleBusinessModel.Name, |
| 0 | 86 | | Description = lgdxRoleBusinessModel.Description, |
| 0 | 87 | | NormalizedName = lgdxRoleBusinessModel.Name.ToUpper(), |
| 0 | 88 | | }; |
| 0 | 89 | | var result = await _roleManager.CreateAsync(role); |
| 0 | 90 | | if (!result.Succeeded) |
| 0 | 91 | | { |
| 0 | 92 | | throw new LgdxIdentity400Expection(result.Errors); |
| | 93 | | } |
| | 94 | |
|
| 0 | 95 | | if (lgdxRoleBusinessModel.Scopes.Any()) |
| 0 | 96 | | { |
| 0 | 97 | | foreach (var scope in lgdxRoleBusinessModel.Scopes) |
| 0 | 98 | | { |
| 0 | 99 | | var addScopeResult = await _roleManager.AddClaimAsync(role, new Claim("scope", scope)); |
| 0 | 100 | | if (!addScopeResult.Succeeded) |
| 0 | 101 | | { |
| 0 | 102 | | throw new LgdxIdentity400Expection(addScopeResult.Errors); |
| | 103 | | } |
| 0 | 104 | | } |
| 0 | 105 | | } |
| | 106 | |
|
| 0 | 107 | | return new LgdxRoleBusinessModel { |
| 0 | 108 | | Id = Guid.Parse(role.Id), |
| 0 | 109 | | Name = role.Name!, |
| 0 | 110 | | Description = role.Description, |
| 0 | 111 | | Scopes = lgdxRoleBusinessModel.Scopes, |
| 0 | 112 | | }; |
| 0 | 113 | | } |
| | 114 | |
|
| | 115 | | public async Task<bool> UpdateRoleAsync(Guid id, LgdxRoleUpdateBusinessModel lgdxRoleUpdateBusinessModel) |
| 0 | 116 | | { |
| 0 | 117 | | if (LgdxRolesHelper.IsSystemRole(id)) |
| 0 | 118 | | { |
| 0 | 119 | | throw new LgdxValidation400Expection(nameof(lgdxRoleUpdateBusinessModel.Name), "Cannot update system role."); |
| | 120 | | } |
| 0 | 121 | | var role = await _roleManager.FindByIdAsync(id.ToString()) ?? throw new LgdxNotFound404Exception(); |
| | 122 | |
|
| 0 | 123 | | role.Name = lgdxRoleUpdateBusinessModel.Name; |
| 0 | 124 | | role.Description = lgdxRoleUpdateBusinessModel.Description; |
| 0 | 125 | | role.NormalizedName = lgdxRoleUpdateBusinessModel.Name.ToUpper(); |
| | 126 | |
|
| 0 | 127 | | var result = await _roleManager.UpdateAsync(role); |
| 0 | 128 | | if (!result.Succeeded) |
| 0 | 129 | | { |
| 0 | 130 | | throw new LgdxIdentity400Expection(result.Errors); |
| | 131 | | } |
| | 132 | |
|
| 0 | 133 | | var scopes = await _context.RoleClaims |
| 0 | 134 | | .Where(r => r.RoleId == id.ToString()) |
| 0 | 135 | | .Where(r => r.ClaimType == "scope") |
| 0 | 136 | | .Select(r => new { |
| 0 | 137 | | r.ClaimValue |
| 0 | 138 | | }) |
| 0 | 139 | | .ToListAsync(); |
| 0 | 140 | | var scopesList = scopes.Select(s => s.ClaimValue).ToList(); |
| 0 | 141 | | var scopeToAdd = lgdxRoleUpdateBusinessModel.Scopes.Except(scopesList); |
| 0 | 142 | | foreach (var scope in scopeToAdd) |
| 0 | 143 | | { |
| 0 | 144 | | var addScopeResult = await _roleManager.AddClaimAsync(role, new Claim("scope", scope!)); |
| 0 | 145 | | if (!addScopeResult.Succeeded) |
| 0 | 146 | | { |
| 0 | 147 | | throw new LgdxIdentity400Expection(addScopeResult.Errors); |
| | 148 | | } |
| 0 | 149 | | } |
| 0 | 150 | | var scopeToRemove = scopesList.Except(lgdxRoleUpdateBusinessModel.Scopes); |
| 0 | 151 | | foreach (var scope in scopeToRemove) |
| 0 | 152 | | { |
| 0 | 153 | | var removeScopeResult = await _roleManager.RemoveClaimAsync(role, new Claim("scope", scope!)); |
| 0 | 154 | | if (!removeScopeResult.Succeeded) |
| 0 | 155 | | { |
| 0 | 156 | | throw new LgdxIdentity400Expection(removeScopeResult.Errors); |
| | 157 | | } |
| 0 | 158 | | } |
| 0 | 159 | | return true; // Error is handled in the Identity service |
| 0 | 160 | | } |
| | 161 | |
|
| | 162 | | public async Task<bool> DeleteRoleAsync(Guid id) |
| 0 | 163 | | { |
| 0 | 164 | | if (LgdxRolesHelper.IsSystemRole(id)) |
| 0 | 165 | | { |
| 0 | 166 | | throw new LgdxValidation400Expection(nameof(id), "Cannot delete system role."); |
| | 167 | | } |
| 0 | 168 | | var role = await _roleManager.FindByIdAsync(id.ToString()) ?? throw new LgdxNotFound404Exception(); |
| 0 | 169 | | var result = await _roleManager.DeleteAsync(role); |
| 0 | 170 | | if (!result.Succeeded) |
| 0 | 171 | | { |
| 0 | 172 | | throw new LgdxIdentity400Expection(result.Errors); |
| | 173 | | } |
| 0 | 174 | | return true; // Error is handled in the Identity service |
| 0 | 175 | | } |
| | 176 | |
|
| | 177 | | public async Task<IEnumerable<LgdxRoleSearchBusinessModel>> SearchRoleAsync(string? name) |
| 0 | 178 | | { |
| 0 | 179 | | var n = name ?? string.Empty; |
| 0 | 180 | | return await _context.Roles.AsNoTracking() |
| 0 | 181 | | .Where(w => w.Name!.ToLower().Contains(n.ToLower())) |
| 0 | 182 | | .Take(10) |
| 0 | 183 | | .Select(t => new LgdxRoleSearchBusinessModel { |
| 0 | 184 | | Id = Guid.Parse(t.Id!), |
| 0 | 185 | | Name = t.Name!, |
| 0 | 186 | | }) |
| 0 | 187 | | .ToListAsync(); |
| 0 | 188 | | } |
| | 189 | | } |