71 lines
2.3 KiB
C#
71 lines
2.3 KiB
C#
namespace MyOffice.Web.Models.Account;
|
|
|
|
using AutoMapper;
|
|
using MyOffice.Services.Identity;
|
|
using Services.Account.Domain;
|
|
using Services.Mapper;
|
|
using System.Security.Cryptography;
|
|
using Core.Extensions;
|
|
using User;
|
|
using static MyOffice.Web.Models.Account.AccountAccessViewModel;
|
|
|
|
public class CustomResolver : IValueResolver<AccountAccessDto, AccessRightsViewModel, bool>
|
|
{
|
|
private readonly ILogger<CustomResolver> _logger;
|
|
private readonly IContextProvider _contextProvider;
|
|
|
|
public CustomResolver(
|
|
ILogger<CustomResolver> logger,
|
|
IContextProvider contextProvider
|
|
)
|
|
{
|
|
_logger = logger;
|
|
_contextProvider = contextProvider;
|
|
}
|
|
|
|
public bool Resolve(AccountAccessDto source, AccessRightsViewModel destination, bool member, ResolutionContext context)
|
|
{
|
|
return source.OwnerId == _contextProvider.UserId;
|
|
}
|
|
}
|
|
|
|
public class AccountViewModelProfile : Profile
|
|
{
|
|
public AccountViewModelProfile()
|
|
{
|
|
CreateMap<AccountDetailedDto, AccountDetailedViewModel>()
|
|
.ForMember(x => x.Account, o => o.MapFrom(x => x.Account))
|
|
;
|
|
|
|
CreateMap<UserDto, UserViewModel>();
|
|
|
|
CreateMap<MyOffice.Data.Models.Users.User, UserViewModel>();
|
|
|
|
CreateMap<AccountDto, AccountViewModel>()
|
|
.ForMember(x => x.CurrencyId, o => o.MapFrom(x => x.CurrencyGlobalId))
|
|
.ForMember(x => x.CurrencyName, o => o.MapFrom(x => x.Currency!.Name))
|
|
.ForMember(x => x.AllowDelete, o => o.MapFrom(x => !x.HasMotions))
|
|
.ForMember(x => x.AllowManage, o => o.MapFrom(x => x.AccessRights!.Any(a => a.OwnerId == x.CurrentUserId)))
|
|
.ForMember(x => x.AccessRights, o => o.MapFrom((s, d) => s.OwnerId != s.CurrentUserId ? null : s.AccessRights))
|
|
;
|
|
|
|
CreateMap<AccountAccountCategoryDto, AccountCategoryViewModel>()
|
|
.ForMember(x => x.Id, o => o.MapFrom(x => x.CategoryId))
|
|
.ForMember(x => x.Name, o => o.MapFrom(x => x.Category!.Name))
|
|
;
|
|
|
|
CreateMap<AccountAccessDto, AccessRightsViewModel>()
|
|
.ForMember(x => x.IsAllowDelete, o => o.MapFrom(
|
|
(src, dst) => src.Account!.OwnerId == src.Account.CurrentUserId && src.UserId != src.Account.CurrentUserId))
|
|
.ForMember(x => x.IsOwner, o => o.MapFrom(
|
|
(src, dst) => src.OwnerId == src.UserId))
|
|
;
|
|
|
|
CreateMap<AccountAccessItemViewModel, AccountAccessDto>()
|
|
.ForMember(x => x.IsAllowWrite, o => o.MapFrom(x => x.AllowWrite))
|
|
;
|
|
|
|
CreateMap<AccountCategoryDto, AccountCategoryViewModel>()
|
|
;
|
|
}
|
|
} |