add user groups and roles to user-info result
parent
286bebdf36
commit
657b9b5204
|
@ -0,0 +1,20 @@
|
|||
using System.Collections.Generic;
|
||||
using Tuitio.Domain.Entities;
|
||||
|
||||
namespace Tuitio.Application.Extensions
|
||||
{
|
||||
internal static class EntityConversions
|
||||
{
|
||||
public static Dictionary<string, string> ToDictionary(this ICollection<UserClaim> claims)
|
||||
{
|
||||
if (claims == null)
|
||||
return null;
|
||||
|
||||
var result = new Dictionary<string, string>();
|
||||
foreach (var claim in claims)
|
||||
result.Add(claim.ClaimKey, claim.ClaimValue);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<UserRole>();
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<models.Token, dto.AuthorizationResult>();
|
||||
CreateMap<AppUser, GetUserInfo.Model>()
|
||||
.ForMember(z => z.Claims, src => src.MapFrom(z => ComposeClaims(z.Claims)));
|
||||
|
||||
CreateMap<ContactOption, GetUserInfo.ContactOption>()
|
||||
.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<AppUser, models.Token>()
|
||||
.ForMember(z => z.Claims, src => src.MapFrom(z => ComposeClaims(z.Claims)));
|
||||
.ForMember(z => z.Claims, src => src.MapFrom(z => z.Claims.ToDictionary()));
|
||||
|
||||
CreateMap<models.Account.LogoutResult, dto.AccountLogoutResult>();
|
||||
}
|
||||
|
||||
private Dictionary<string, string> ComposeClaims(ICollection<UserClaim> claims)
|
||||
{
|
||||
if (claims == null)
|
||||
return null;
|
||||
|
||||
var result = new Dictionary<string, string>();
|
||||
foreach (var claim in claims)
|
||||
result.Add(claim.ClaimKey, claim.ClaimValue);
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<AppUser, GetUserInfo.Model>()
|
||||
.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<ContactOption, GetUserInfo.ContactOption>()
|
||||
.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<UserGroup, GetUserInfo.UserGroup>()
|
||||
.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<UserRole, GetUserInfo.UserRole>()
|
||||
.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));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<string, string> 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<Query, Model>
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue