public async Task <IActionResult> Create(PostAnonymousCreateViewModel viewModel) { if (ModelState.IsValid) { var thread = await _threadService.GetAsync(viewModel.ThreadId); if (!thread.IsClosed) { var postDto = _mapper.Map <PostDto>(viewModel); postDto.UserIpAddress = UserIpAddress.ToString(); postDto.UserAgent = UserAgent; var postId = await _postService.CreateAsync(viewModel.Attachments, postDto); return (Redirect(Url.Action("Details", "Threads", new { categoryAlias = viewModel.CategoryAlias, threadId = viewModel.ThreadId }) + "#" + postId)); } else { throw new HttpResponseException(HttpStatusCode.Forbidden, $"Thread {thread.Id} is closed."); } } else { ViewBag.ErrorMessage = ModelState.ModelErrorsToString(); return(View(viewModel)); } }
public static void UpdateAttempts(this UserIpAddress userIpAddress, bool isLoginSuccessful) { if (isLoginSuccessful) { userIpAddress.ResetIncorrectAttempts(); } else { userIpAddress.IncreaseIncorrectAttempts(); } }
private static void IncreaseIncorrectAttempts(this UserIpAddress userIpAddress) { userIpAddress.IncorrectLoginCount++; DateTime?blockToDate = userIpAddress.IncorrectLoginCount switch { { } n when n >= 4 => DateTime.MaxValue, { } n when n >= 3 => DateTime.Now.AddSeconds(10), { } n when n >= 2 => DateTime.Now.AddSeconds(5), _ => null }; userIpAddress.BlockedTo = blockToDate; }
public static string GenerateBlockMessage(this UserIpAddress userIpAddress) { var blockMessage = userIpAddress.IncorrectLoginCount switch { { } n when n >= 4 => "Your Ip permanently blocked.", { } n when n >= 3 => "Your Ip blocked for 10 seconds.", { } n when n >= 2 => "Your Ip blocked for 5 seconds.", _ => "" }; return(blockMessage); } }
public async Task <IActionResult> Create(ThreadAnonymousCreateViewModel viewModel) { if (ModelState.IsValid) { try { var categoryDto = await _categoryService.GetAsync(viewModel.CategoryAlias); var threadDto = _mapper.Map <ThreadDto>(viewModel); threadDto.BumpLimit = categoryDto.DefaultBumpLimit; threadDto.ShowThreadLocalUserHash = categoryDto.DefaultShowThreadLocalUserHash; threadDto.CategoryId = categoryDto.Id; var postDto = _mapper.Map <PostDto>(viewModel); postDto.UserIpAddress = UserIpAddress.ToString(); postDto.UserAgent = UserAgent; var threadCreateDto = new ThreadPostCreateDto { Category = categoryDto, Thread = threadDto, Post = postDto, }; var createResultDto = await _threadService.CreateThreadPostAsync(viewModel.Attachments, threadCreateDto, true); return(RedirectToAction("Details", "Threads", new { categoryAlias = viewModel.CategoryAlias, threadId = createResultDto.ThreadId })); } catch (Exception ex) { _logger.LogError(EventIdentifiers.ThreadCreateError.ToEventId(), ex, $"Can't create new thread due to exception"); ViewBag.ErrorMessage = "Can't create new thread"; return(View(viewModel)); } } else { ViewBag.ErrorMessage = ModelState.ModelErrorsToString(); return(View(viewModel)); } }
public async Task <IActionResult> Create(ThreadAnonymousCreateViewModel viewModel) { if (ModelState.IsValid) { var category = await _categoryService.GetAsync(viewModel.CategoryAlias); var threadDto = _mapper.Map <ThreadDto>(viewModel); threadDto.BumpLimit = category.DefaultBumpLimit; threadDto.ShowThreadLocalUserHash = category.DefaultShowThreadLocalUserHash; threadDto.CategoryId = category.Id; var threadId = await _threadService.CreateAsync(threadDto); var postDto = _mapper.Map <PostDto>(viewModel); postDto.ThreadId = threadId; postDto.UserIpAddress = UserIpAddress.ToString(); postDto.UserAgent = UserAgent; try { var postId = await _postService.CreateAsync(viewModel.Attachments, postDto); } catch (Exception ex) { _logger.LogError($"Can't create new post due to exception: {ex}. Thread creation failed."); await _threadService.DeleteAsync(threadId); throw; } return(RedirectToAction("Details", "Threads", new { categoryAlias = viewModel.CategoryAlias, threadId = threadId })); } else { ViewBag.ErrorMessage = ModelState.ModelErrorsToString(); return(View(viewModel)); } }
public virtual void Validate(ValidationResult result) { ((int)Type).ValidateRequired(result, nameof(Type)); UserId.ValidateMaxLength(result, nameof(UserId), 50); Source.ValidateMaxLength(result, nameof(Source), 256); Message.ValidateRequired(result, nameof(Message)); Message.ValidateMaxLength(result, nameof(Message), 1024); Data.ValidateMaxLength(result, nameof(Data), 1024); Url.ValidateMaxLength(result, nameof(Url), 512); StackTrace.ValidateMaxLength(result, nameof(StackTrace), 2048); HostIpAddress.ValidateMaxLength(result, nameof(HostIpAddress), 25); UserIpAddress.ValidateMaxLength(result, nameof(UserIpAddress), 25); UserAgent.ValidateMaxLength(result, nameof(UserAgent), 25); }
public static void Unblock(this UserIpAddress userIpAddress) { userIpAddress.ResetIncorrectAttempts(); }
private static void ResetIncorrectAttempts(this UserIpAddress userIpAddress) { userIpAddress.BlockedTo = null; userIpAddress.IncorrectLoginCount = 0; }