public IActionResult PostComment(string commentBody, string puzzleId)
        {
            int puzzleIdI;

            if (!int.TryParse(puzzleId, out puzzleIdI))
            {
                return(Json(new { success = false, error = "Invalid puzzle ID." }));
            }
            Puzzle  puzzle  = puzzleRepository.Get(puzzleIdI);
            bool    success = false;
            Comment comment = null;

            if (puzzle != null)
            {
                comment = new Comment(counterRepository.GetAndIncrease(Counter.COMMENT_ID), loginHandler.LoggedInUserId(HttpContext).Value, commentBody, null, puzzleIdI, false, DateTime.UtcNow);
                success = commentRepository.Add(comment);
            }
            if (success)
            {
                Notification notificationForParentAuthor = new Notification(Guid.NewGuid().ToString(), puzzle.Author, "You received a comment on your puzzle.", false,
                                                                            string.Format("/Puzzle/{0}?comment={1}", comment.PuzzleID, comment.ID), DateTime.UtcNow);
                notificationRepository.Add(notificationForParentAuthor);
                return(Json(new { success = true }));
            }
            else
            {
                return(Json(new { success = false, error = "Could not post comment." }));
            }
        }
Exemplo n.º 2
0
        public IActionResult SubmitPuzzle(string id, string solution, string explanation)
        {
            int puzzleId;

            if (!int.TryParse(id, out puzzleId))
            {
                return(Json(new { success = false, error = "The given ID is invalid." }));
            }

            if (string.IsNullOrWhiteSpace(solution))
            {
                return(Json(new { success = false, error = "There are no accepted variations." }));
            }

            Puzzle puzzle = puzzlesBeingEdited.Get(puzzleId);

            if (puzzle == null)
            {
                return(Json(new { success = false, error = string.Format("The given puzzle (ID: {0}) cannot be published because it isn't being created.", id) }));
            }
            if (puzzle.Author != loginHandler.LoggedInUserId(HttpContext).Value)
            {
                return(Json(new { success = false, error = "Only the puzzle author can access this right now." }));
            }

            puzzle.Solutions = new List <string>(solution.Split(';').Where(x => !string.IsNullOrWhiteSpace(x)));
            if (puzzle.Solutions.Count == 0)
            {
                return(Json(new { success = false, error = "There are no accepted variations." }));
            }
            puzzle.Game = null;
            puzzle.ExplanationUnsafe = explanation;
            puzzle.Rating            = new Rating(1500, 350, 0.06);
            puzzle.Reviewers         = new List <int>();
            if (UserRole.HasAtLeastThePrivilegesOf(loginHandler.LoggedInUser(HttpContext).Roles, UserRole.PUZZLE_REVIEWER))
            {
                puzzle.InReview = false;
                puzzle.Approved = true;
                puzzle.Reviewers.Add(loginHandler.LoggedInUserId(HttpContext).Value);
            }
            else
            {
                puzzle.InReview = true;
                puzzle.Approved = false;
            }
            puzzle.DateSubmittedUtc = DateTime.UtcNow;
            puzzle.ID = counterRepository.GetAndIncrease(Counter.PUZZLE_ID);
            if (puzzleRepository.Add(puzzle))
            {
                return(Json(new { success = true, link = Url.Action("TrainId", "Puzzle", new { id = puzzle.ID }) }));
            }
            else
            {
                return(Json(new { success = false, error = "Something went wrong." }));
            }
        }
        public IActionResult New(string username, string email, string password)
        {
            ViewBag.Error = new List <string>();
            if (!validator.IsValidUsername(username))
            {
                ViewBag.Error.Add("Invalid username. Usernames can only contain the characters a-z, A-Z, 0-9, _ and -.");
            }
            if (!validator.IsValidEmail(email))
            {
                ViewBag.Error.Add("Invalid email address.");
            }
            if (userRepository.FindByUsername(username) != null)
            {
                ViewBag.Error.Add("The username is already taken.");
            }
            if (userRepository.FindByEmail(email) != null)
            {
                ViewBag.Error.Add("The email address is already taken.");
            }
            if (ViewBag.Error.Count > 0)
            {
                return(View("Register"));
            }
            else
            {
                ViewBag.Error = null;
            }
            Tuple <string, string> hashAndSalt = passwordHasher.HashPassword(password);
            string hash   = hashAndSalt.Item1;
            string salt   = hashAndSalt.Item2;
            int    userId = counterRepository.GetAndIncrease(Counter.USER_ID);
            User   user   = new User(userId, username, email, hash, salt, "", 0, 0,
                                     new List <string>()
            {
                UserRole.NONE
            }, new Dictionary <string, Rating>()
            {
                { "Atomic", new Rating(1500, 350, 0.06) },
                { "ThreeCheck", new Rating(1500, 350, 0.06) },
                { "KingOfTheHill", new Rating(1500, 350, 0.06) },
                { "Antichess", new Rating(1500, 350, 0.06) },
                { "Horde", new Rating(1500, 350, 0.06) },
                { "RacingKings", new Rating(1500, 350, 0.06) }
            }, new List <int>());
            bool added = userRepository.Add(user);

            userVerifier.SendVerificationEmailTo(user.ID);
            loginHandler.RegisterLogin(user.ID, HttpContext);
            return(RedirectToAction("Profile", new { id = user.ID }));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> New(string username, string email, string password, string passwordConfirmation, [FromForm(Name = "g-recaptcha-response")] string gRecaptchaResponse)
        {
            ViewBag.Error = new List <string>();
            if (!validator.IsValidUsername(username))
            {
                ViewBag.Error.Add("Invalid username. Usernames can only contain the characters a-z, A-Z, 0-9, _ and -.");
            }
            if (!validator.IsValidEmail(email))
            {
                ViewBag.Error.Add("Invalid email address.");
            }
            if (userRepository.FindByUsername(username) != null)
            {
                ViewBag.Error.Add("The username is already taken.");
            }
            if (userRepository.FindByEmail(email) != null)
            {
                ViewBag.Error.Add("The email address is already taken.");
            }

            if (string.IsNullOrEmpty(password) || string.IsNullOrEmpty(passwordConfirmation))
            {
                ViewBag.Error.Add("Your password or its confirmation cannot be empty.");
            }
            else if (!password.Equals(passwordConfirmation))
            {
                ViewBag.Error.Add("The password does not match its confirmation.");
            }

            if (!string.IsNullOrWhiteSpace(recaptchaKey))
            {
                Dictionary <string, string> captchaRequestValues = new Dictionary <string, string>()
                {
                    { "secret", recaptchaKey },
                    { "response", gRecaptchaResponse }
                };
                FormUrlEncodedContent content  = new FormUrlEncodedContent(captchaRequestValues);
                HttpResponseMessage   response = await captchaClient.PostAsync("https://www.google.com/recaptcha/api/siteverify", content);

                string responseString = await response.Content.ReadAsStringAsync();

                Dictionary <string, dynamic> jsonResponse = JsonConvert.DeserializeObject <Dictionary <string, dynamic> >(responseString);
                if (!((bool)jsonResponse["success"]))
                {
                    ViewBag.Error.Add("Captcha verification failed.");
                }
            }

            if (ViewBag.Error.Count > 0)
            {
                return(View("Register"));
            }
            else
            {
                ViewBag.Error = null;
            }
            Tuple <string, string> hashAndSalt = passwordHasher.HashPassword(password);
            string hash   = hashAndSalt.Item1;
            string salt   = hashAndSalt.Item2;
            int    userId = counterRepository.GetAndIncrease(Counter.USER_ID);
            User   user   = new User(userId, username, email, hash, salt, "", 0, 0,
                                     new List <string>()
            {
                UserRole.NONE
            }, new Dictionary <string, Rating>()
            {
                { "Atomic", new Rating(1500, 350, 0.06) },
                { "ThreeCheck", new Rating(1500, 350, 0.06) },
                { "KingOfTheHill", new Rating(1500, 350, 0.06) },
                { "Antichess", new Rating(1500, 350, 0.06) },
                { "Horde", new Rating(1500, 350, 0.06) },
                { "RacingKings", new Rating(1500, 350, 0.06) },
                { "Crazyhouse", new Rating(1500, 350, 0.06) }
            }, new List <int>());

            if (!requireEmailVerification)
            {
                user.VerificationCode = 0;
                user.Verified         = true;
            }
            bool added = userRepository.Add(user);

            if (requireEmailVerification)
            {
                userVerifier.SendVerificationEmailTo(user.ID);
            }
            loginHandler.RegisterLogin(user.ID, HttpContext);
            return(RedirectToAction("Profile", new { id = user.ID }));
        }