public async Task <SantaSecurityUser> FindByIdAsync([NotNull] string userId) { if (string.IsNullOrWhiteSpace(userId)) { throw new ArgumentException($"{nameof(userId)} cannot be null or whitespace.", nameof(userId)); } var id = SantaSecurityUser.GetId(userId, out var isAdmin); return(await(isAdmin ? GetAdminById(id) : GetUserById(id))); }
public async Task <ActionResult> EditAdmin(int id) { var userId = SantaSecurityUser.GetId(id, true); var santaAdmin = await _userManager.FindByIdAsync(userId); if (santaAdmin == null) { return(HttpNotFound()); } var santaAdminPostModel = _mapper.Map <SantaAdminPostModel>(santaAdmin); return(View(santaAdminPostModel)); }
public static long?GetUserId(IPrincipal principal) { if (!(principal is ClaimsPrincipal identity)) { return(null); } var id = identity.GetClaimValue(ClaimTypes.NameIdentifier); if (string.IsNullOrEmpty(id)) { return(null); } var userId = SantaSecurityUser.GetId(id, out var isAdmin); return(isAdmin ? null : (long?)userId); }
private static void RegisterMappings([NotNull] IMapperConfigurationExpression cfg, IComponentContext context) { var encryptionProvider = context.Resolve <IEncryptionProvider>(); var countryProvider = context.Resolve <CountryProvider>(); var configProvider = context.Resolve <IConfigProvider>(); cfg.CreateMap <RegistrationPostModel, SantaUser>() .ForMember(dest => dest.PasswordHash, opt => opt.MapFrom(post => encryptionProvider.CalculatePasswordHash(post.Password, null))) .ForMember(dest => dest.Country, opt => opt.MapFrom(post => countryProvider.ById[post.Country.Id].ThreeLetterIsoCode)) .ForMember(dest => dest.IsAdult, opt => opt.MapFrom(post => post.DateOfBirth.AddYears(configProvider.AdultAge) <= DateTime.Today)) .ForMember(dest => dest.EmailConfirmed, opt => opt.Ignore()) .ForMember(dest => dest.AdminConfirmed, opt => opt.Ignore()) .ForMember(dest => dest.Id, opt => opt.Ignore()) .ForMember(dest => dest.CreateDate, opt => opt.MapFrom(_ => DateTime.Now)) ; cfg.CreateMap <SantaUser, SantaSecurityUser>() .ForMember(dest => dest.Id, opt => opt.MapFrom(model => SantaSecurityUser.GetId(model.Id, false))) .ForMember(dest => dest.UserName, opt => opt.MapFrom(model => model.Email)); cfg.CreateMap <SantaAdminPostModel, SantaAdmin>() .ForMember(dest => dest.PasswordHash, opt => opt.MapFrom(post => encryptionProvider.CalculatePasswordHash(post.Password, null))) .ForMember(dest => dest.Id, opt => opt.Ignore()) .ForMember(dest => dest.DisplayName, opt => opt.Ignore()); cfg.CreateMap <SantaAdmin, SantaAdminPostModel>() .ForMember(dest => dest.Password, opt => opt.Ignore()); cfg.CreateMap <ISettingsRepository, SettingsViewModel>(); cfg.CreateMap <SantaUser, UserHomeViewModel>() .ForMember(dest => dest.Assignment, opt => opt.Ignore()) .ForMember(dest => dest.InboundGiftArrived, opt => opt.Ignore()) .ForMember(dest => dest.InboundGiftEnRoute, opt => opt.Ignore()) .ForMember(dest => dest.OutboundGiftEnRoute, opt => opt.Ignore()) .ForMember(dest => dest.OutboundGiftArrived, opt => opt.Ignore()) .ForMember(dest => dest.AssignmentPerformed, opt => opt.Ignore()) .ForMember(dest => dest.InboundGiftMissing, opt => opt.Ignore()) .ForMember(dest => dest.OutboundGiftMissing, opt => opt.Ignore()); cfg.CreateMap <SantaUser, AssignmentViewModel>() .ForMember(dest => dest.Country, opt => opt.MapFrom(src => countryProvider.ByThreeLetterCode[src.Country].Name)); cfg.CreateMap <PasswordResetViewModel, PasswordResetModel>() .ForMember(dest => dest.PasswordBytes, opt => opt.MapFrom(post => encryptionProvider.CalculatePasswordHash(post.NewPassword, null))); cfg.CreateMap <SantaUser, SantaUserViewModel>() .ForMember(dest => dest.Country, opt => opt.MapFrom(src => new CountryEntryViewModel { Id = countryProvider.ByThreeLetterCode[src.Country].Id })); cfg.CreateMap <SantaUserPostModel, SantaUser>() // let's do explicit for safety .ForMember(dest => dest.DisplayName, opt => opt.MapFrom(src => src.DisplayName)) .ForMember(dest => dest.Email, opt => opt.MapFrom(src => src.Email)) .ForMember(dest => dest.FacebookProfileUrl, opt => opt.MapFrom(src => src.FacebookProfileUrl)) .ForMember(dest => dest.FullName, opt => opt.MapFrom(src => src.FullName)) .ForMember(dest => dest.AddressLine1, opt => opt.MapFrom(src => src.AddressLine1)) .ForMember(dest => dest.AddressLine2, opt => opt.MapFrom(src => src.AddressLine2)) .ForMember(dest => dest.City, opt => opt.MapFrom(src => src.City)) .ForMember(dest => dest.PostalCode, opt => opt.MapFrom(src => src.PostalCode)) .ForMember(dest => dest.Country, opt => opt.MapFrom(post => countryProvider.ById[post.Country.Id].ThreeLetterIsoCode)) .ForMember(dest => dest.SendAbroad, opt => opt.MapFrom(src => src.SendAbroad)) .ForMember(dest => dest.Note, opt => opt.MapFrom(src => src.Note)) .ForAllOtherMembers(opt => opt.Ignore()); }
public async Task <ActionResult> Home() { var user = await _userManager.FindByNameAsync(User.Identity.Name); if (user == null) { return(HttpNotFound()); } var userId = SantaSecurityUser.GetId(user.Id, out var isAdmin); if (isAdmin) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError)); } var santaUser = _userRepository.GetUserWithoutProtectedData(userId); if (santaUser == null) { return(HttpNotFound()); } var model = _mapper.Map <UserHomeViewModel>(santaUser); var assignedUserId = _userRepository.GetAssignedPartnerIdForUser(userId); if (assignedUserId.HasValue) { model.AssignmentPerformed = true; // Check assignment var outbound = _userRepository.GetOutboundAssignment(userId); var inbound = _userRepository.GetInboundAssignment(userId); // Has gift been sent? model.InboundGiftEnRoute = inbound.Sent; // Has gift been confirmed as received? model.InboundGiftArrived = inbound.Received; // Has user notified us about missing gift? model.InboundGiftMissing = inbound.Missing; // Has outbound gift been received? model.OutboundGiftArrived = outbound.Received; // Has outbound gift been sent? model.OutboundGiftEnRoute = outbound.Sent; // Has user's recipient notified us about missing gift? model.OutboundGiftMissing = outbound.Missing; if (!outbound.Sent) { // nope, show that it awaits var assignedUser = _userRepository.GetUser(assignedUserId.Value); if (assignedUser == null) { // TODO: NOTIFY ADMIN - THIS IS BAD Log.Error($"Tried to retrieve current user id={userId} from repository, but got null"); return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError)); } model.Assignment = _mapper.Map <AssignmentViewModel>(assignedUser); } } return(View(model)); }