From 657b9b52045c94b4b12132d5cefa5a48b2a853f5 Mon Sep 17 00:00:00 2001 From: Tudor Stanciu Date: Mon, 3 Apr 2023 02:00:16 +0300 Subject: [PATCH] add user groups and roles to user-info result --- .../Extensions/EntityConversions.cs | 20 ++++++++ .../Extensions/EntityExtensions.cs | 31 +++++++++++++ .../Mappings/MappingProfile.cs | 25 +--------- .../Mappings/UserInfoMappings.cs | 34 ++++++++++++++ src/Tuitio.Application/Queries/GetUserInfo.cs | 46 +++++++++++++------ .../Repositories/UserRepository.cs | 2 + 6 files changed, 120 insertions(+), 38 deletions(-) create mode 100644 src/Tuitio.Application/Extensions/EntityConversions.cs create mode 100644 src/Tuitio.Application/Extensions/EntityExtensions.cs create mode 100644 src/Tuitio.Application/Mappings/UserInfoMappings.cs diff --git a/src/Tuitio.Application/Extensions/EntityConversions.cs b/src/Tuitio.Application/Extensions/EntityConversions.cs new file mode 100644 index 0000000..1c7ea25 --- /dev/null +++ b/src/Tuitio.Application/Extensions/EntityConversions.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; +using Tuitio.Domain.Entities; + +namespace Tuitio.Application.Extensions +{ + internal static class EntityConversions + { + public static Dictionary ToDictionary(this ICollection claims) + { + if (claims == null) + return null; + + var result = new Dictionary(); + foreach (var claim in claims) + result.Add(claim.ClaimKey, claim.ClaimValue); + + return result; + } + } +} diff --git a/src/Tuitio.Application/Extensions/EntityExtensions.cs b/src/Tuitio.Application/Extensions/EntityExtensions.cs new file mode 100644 index 0000000..fe891a2 --- /dev/null +++ b/src/Tuitio.Application/Extensions/EntityExtensions.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; +using System.Linq; +using Tuitio.Domain.Entities; + +namespace Tuitio.Application.Extensions +{ + internal static class EntityExtensions + { + public static UserRole[] GetUserRoles(this AppUser user) + { + var roles = new List(); + + var groups = user.UserGroups?.Select(z => z.UserGroup); + if (groups != null) + { + foreach (var group in groups) + { + var groupRoles = group.GroupRoles?.Select(z => z.UserRole); + if (groupRoles == null) + continue; + roles.AddRange(groupRoles); + } + } + + if (user.UserRoles != null) + roles.AddRange(user.UserRoles.Select(z => z.UserRole)); + + return roles.ToArray(); + } + } +} diff --git a/src/Tuitio.Application/Mappings/MappingProfile.cs b/src/Tuitio.Application/Mappings/MappingProfile.cs index 9b031f5..c2fd52d 100644 --- a/src/Tuitio.Application/Mappings/MappingProfile.cs +++ b/src/Tuitio.Application/Mappings/MappingProfile.cs @@ -1,11 +1,10 @@ // Copyright (c) 2020 Tudor Stanciu using AutoMapper; +using Tuitio.Application.Extensions; using Tuitio.Domain.Entities; -using System.Collections.Generic; using dto = Tuitio.PublishedLanguage.Dto; using models = Tuitio.Domain.Models; -using Tuitio.Application.Queries; namespace Tuitio.Application.Mappings { @@ -14,30 +13,10 @@ namespace Tuitio.Application.Mappings public MappingProfile() { CreateMap(); - CreateMap() - .ForMember(z => z.Claims, src => src.MapFrom(z => ComposeClaims(z.Claims))); - - CreateMap() - .ForMember(z => z.Id, src => src.MapFrom(z => z.ContactOptionId)) - .ForMember(z => z.ContactTypeCode, src => src.MapFrom(z => z.ContactType.ContactTypeCode)) - .ForMember(z => z.ContactTypeName, src => src.MapFrom(z => z.ContactType.ContactTypeName)); - CreateMap() - .ForMember(z => z.Claims, src => src.MapFrom(z => ComposeClaims(z.Claims))); + .ForMember(z => z.Claims, src => src.MapFrom(z => z.Claims.ToDictionary())); CreateMap(); } - - private Dictionary ComposeClaims(ICollection claims) - { - if (claims == null) - return null; - - var result = new Dictionary(); - foreach (var claim in claims) - result.Add(claim.ClaimKey, claim.ClaimValue); - - return result; - } } } diff --git a/src/Tuitio.Application/Mappings/UserInfoMappings.cs b/src/Tuitio.Application/Mappings/UserInfoMappings.cs new file mode 100644 index 0000000..d220dc2 --- /dev/null +++ b/src/Tuitio.Application/Mappings/UserInfoMappings.cs @@ -0,0 +1,34 @@ +using AutoMapper; +using System.Linq; +using Tuitio.Application.Extensions; +using Tuitio.Application.Queries; +using Tuitio.Domain.Entities; + +namespace Tuitio.Application.Mappings +{ + public class UserInfoMappings : Profile + { + public UserInfoMappings() + { + CreateMap() + .ForMember(z => z.Claims, src => src.MapFrom(z => z.Claims.ToDictionary())) + .ForMember(z => z.UserGroups, src => src.MapFrom(z => z.UserGroups.Select(z => z.UserGroup))) + .ForMember(z => z.UserRoles, src => src.MapFrom(z => z.GetUserRoles())); + + CreateMap() + .ForMember(z => z.Id, src => src.MapFrom(z => z.ContactOptionId)) + .ForMember(z => z.ContactTypeCode, src => src.MapFrom(z => z.ContactType.ContactTypeCode)) + .ForMember(z => z.ContactTypeName, src => src.MapFrom(z => z.ContactType.ContactTypeName)); + + CreateMap() + .ForMember(z => z.Id, src => src.MapFrom(z => z.UserGroupId)) + .ForMember(z => z.Code, src => src.MapFrom(z => z.UserGroupCode)) + .ForMember(z => z.Name, src => src.MapFrom(z => z.UserGroupName)); + + CreateMap() + .ForMember(z => z.Id, src => src.MapFrom(z => z.UserRoleId)) + .ForMember(z => z.Code, src => src.MapFrom(z => z.UserRoleCode)) + .ForMember(z => z.Name, src => src.MapFrom(z => z.UserRoleName)); + } + } +} diff --git a/src/Tuitio.Application/Queries/GetUserInfo.cs b/src/Tuitio.Application/Queries/GetUserInfo.cs index 89d1cfb..1996c01 100644 --- a/src/Tuitio.Application/Queries/GetUserInfo.cs +++ b/src/Tuitio.Application/Queries/GetUserInfo.cs @@ -17,26 +17,42 @@ namespace Tuitio.Application.Queries public record Model { - public int UserId { get; set; } - public string UserName { get; set; } - public string FirstName { get; set; } - public string LastName { get; set; } - public string Email { get; set; } - public string ProfilePictureUrl { get; set; } - public string SecurityStamp { get; set; } - public DateTime CreationDate { get; set; } - public int? FailedLoginAttempts { get; set; } - public DateTime? LastLoginDate { get; set; } + public int UserId { get; init; } + public string UserName { get; init; } + public string FirstName { get; init; } + public string LastName { get; init; } + public string Email { get; init; } + public string ProfilePictureUrl { get; init; } + public string SecurityStamp { get; init; } + public DateTime CreationDate { get; init; } + public int? FailedLoginAttempts { get; init; } + public DateTime? LastLoginDate { get; init; } public Dictionary Claims { get; init; } - public ContactOption[] ContactOptions { get; set; } + public UserRole[] UserRoles { get; init; } + public UserGroup[] UserGroups { get; init; } + public ContactOption[] ContactOptions { get; init; } } public record ContactOption { - public int Id { get; set; } - public string ContactTypeCode { get; set; } - public string ContactTypeName { get; set; } - public string ContactValue { get; set; } + public int Id { get; init; } + public string ContactTypeCode { get; init; } + public string ContactTypeName { get; init; } + public string ContactValue { get; init; } + } + + public record UserGroup + { + public int Id { get; init; } + public string Code { get; init; } + public string Name { get; init; } + } + + public record UserRole + { + public int Id { get; init; } + public string Code { get; init; } + public string Name { get; init; } } public class QueryHandler : IRequestHandler diff --git a/src/Tuitio.Domain.Data/Repositories/UserRepository.cs b/src/Tuitio.Domain.Data/Repositories/UserRepository.cs index f894100..31999ed 100644 --- a/src/Tuitio.Domain.Data/Repositories/UserRepository.cs +++ b/src/Tuitio.Domain.Data/Repositories/UserRepository.cs @@ -33,7 +33,9 @@ namespace Tuitio.Domain.Data.Repositories return _dbContext.Users .Include(z => z.Status) .Include(z => z.Claims) + .Include(z => z.UserRoles).ThenInclude(z => z.UserRole) .Include(z => z.ContactOptions).ThenInclude(z => z.ContactType) + .Include(z => z.UserGroups).ThenInclude(z => z.UserGroup).ThenInclude(z => z.GroupRoles).ThenInclude(z => z.UserRole) .FirstOrDefaultAsync(z => z.UserId == userId); }