示例#1
0
        public IActionResult NewPost(Post post)
        {
            var user = _authHandler.UserFromClaimsPrincipal(User);

            if (string.IsNullOrWhiteSpace(post.Topic) || string.IsNullOrWhiteSpace(post.Content) ||
                string.IsNullOrWhiteSpace(post.Title) || string.IsNullOrWhiteSpace(post.Captcha))
            {
                _logger.LogInformation("Topic, Content, Title or Captcha is null or empty.");
                _logger.LogInformation($"Terminating session. User: {user.Uuid}" +
                                       $", IP: {HttpContext?.Connection?.RemoteIpAddress?.ToString() ?? "Unknown IP"}");
                _authHandler.TerminateSession(user);

                return(BadRequest());
            }

            if (!_captcha.VerifyCaptcha(post.Captcha, HttpContext.Connection.RemoteIpAddress, "newPost"))
            {
                _logger.LogInformation("Captcha failed verification.");
                _logger.LogInformation($"Terminating session. User: {user.Uuid}" +
                                       $", IP: {HttpContext?.Connection?.RemoteIpAddress?.ToString() ?? "Unknown IP"}");
                _authHandler.TerminateSession(user);

                return(BadRequest());
            }

            if (post.Content.Length > 512 || post.Title.Length > 50)
            {
                _logger.LogInformation("Content or Title exceeds max permissible length.");
                _logger.LogInformation($"Terminating session. User: {user.Uuid}" +
                                       $", IP: {HttpContext?.Connection?.RemoteIpAddress?.ToString() ?? "Unknown IP"}");
                _authHandler.TerminateSession(user);

                return(BadRequest());
            }


            if (_database.CreatePost(post, user))
            {
                _activityLogger.LogNewPost(Request.HttpContext.Connection.RemoteIpAddress, user, post);
                return(Ok());
            }

            _logger.LogInformation("DB failed to create post.");
            _logger.LogInformation($"Terminating session. User: {user.Uuid}" +
                                   $", IP: {HttpContext?.Connection?.RemoteIpAddress?.ToString() ?? "Unknown IP"}");
            _authHandler.TerminateSession(user);

            return(BadRequest());
        }