Files
mixtape/zero.Web/Mapper/UserMapperConfig.cs
T

96 lines
3.1 KiB
C#
Raw Normal View History

2020-04-24 12:46:25 +02:00
using System;
2020-04-28 14:51:17 +02:00
using System.Linq;
2020-04-24 12:46:25 +02:00
using zero.Core.Entities;
2020-04-29 12:11:35 +02:00
using zero.Core.Identity;
using zero.Core.Extensions;
2020-04-17 15:32:33 +02:00
using zero.Web.Models;
2020-04-29 12:11:35 +02:00
using Raven.Client.Documents;
using zero.Core.Mapper;
2020-04-17 15:32:33 +02:00
namespace zero.Web.Mapper
{
2020-04-17 15:51:11 +02:00
public class UserMapperConfig : IMapperConfig
2020-04-17 15:32:33 +02:00
{
/// <inheritdoc />
public void Configure(IMapper config)
{
config.CreateMap<User, UserEditModel>((source, target) =>
{
target.Id = source.Id;
target.Name = source.Name;
target.IsActive = source.IsActive;
target.CreatedDate = source.CreatedDate;
target.IsSuper = source.IsSuper;
target.Email = source.Email;
target.IsEmailConfirmed = source.IsEmailConfirmed;
2020-05-14 15:40:41 +02:00
target.AvatarId = source.AvatarId;
2020-04-17 15:32:33 +02:00
target.LanguageId = source.LanguageId;
target.Roles = source.Roles;
target.Claims = source.Claims;
target.LockoutEnd = source.LockoutEnd;
target.IsLockedOut = source.LockoutEnabled && source.LockoutEnd.HasValue;
2020-04-17 15:32:33 +02:00
});
config.CreateMap<UserEditModel, User>((source, target) =>
{
target.Name = source.Name;
target.IsActive = source.IsActive;
target.IsSuper = source.IsSuper;
target.Email = source.Email;
2020-05-14 15:40:41 +02:00
target.AvatarId = source.AvatarId;
2020-04-17 15:32:33 +02:00
target.LanguageId = source.LanguageId;
target.Roles = source.Roles;
target.Claims = source.Claims;
});
2020-04-24 12:46:25 +02:00
config.CreateMap<User, UserListModel>((source, target) =>
{
target.Id = source.Id;
target.Name = source.Name;
target.IsActive = source.IsActive && (!source.LockoutEnabled || !source.LockoutEnd.HasValue);
target.Email = source.Email;
2020-05-14 15:40:41 +02:00
target.AvatarId = source.AvatarId;
2020-04-24 12:46:25 +02:00
target.Roles = String.Join(", ", source.Roles); // TODO get name from alias
});
config.CreateMap<UserRole, UserRoleEditModel>((source, target) =>
{
target.Id = source.Id;
target.Name = source.Name;
target.IsActive = source.IsActive;
target.CreatedDate = source.CreatedDate;
target.Description = source.Description;
target.Icon = source.Icon;
2020-04-28 14:51:17 +02:00
target.Claims = source.Claims.Cast<UserClaim>().ToList();
2020-04-24 12:46:25 +02:00
});
config.CreateMap<UserRoleEditModel, UserRole>((source, target) =>
{
target.Name = source.Name;
target.IsActive = source.IsActive;
target.Description = source.Description;
target.Icon = source.Icon;
2020-04-28 14:51:17 +02:00
target.Claims = source.Claims.Cast<IUserClaim>().ToList();
2020-04-24 12:46:25 +02:00
});
config.CreateMap<UserRole, UserRoleListModel>((source, target) =>
{
target.Id = source.Id;
target.Name = source.Name;
2020-04-29 12:11:35 +02:00
target.Alias = source.Alias;
target.CountClaims = source.Claims.Count(x =>
{
string[] parts = x.Value.Split(':');
if (parts.Length < 2)
{
return true;
}
2020-08-11 16:01:10 +02:00
return parts[1] == PermissionsValue.True || parts[1] == PermissionsValue.Read || parts[1] == PermissionsValue.Update;
2020-04-29 12:11:35 +02:00
});
2020-04-24 12:46:25 +02:00
target.Icon = source.Icon;
});
2020-04-17 15:32:33 +02:00
}
}
}