Esempio n. 1
0
        public async Task <IActionResult> PostComment(string commentBody, string puzzleId)
        {
            if (!int.TryParse(puzzleId, out int puzzleIdI))
            {
                return(Json(new { success = false, error = "Invalid puzzle ID." }));
            }
            Puzzle puzzle = await puzzleRepository.GetAsync(puzzleIdI);

            bool    success = false;
            Comment comment = null;

            if (puzzle != null)
            {
                comment = new Comment(await counterRepository.GetAndIncreaseAsync(Counter.COMMENT_ID), (await loginHandler.LoggedInUserIdAsync(HttpContext)).Value, commentBody, null, puzzleIdI, false, DateTime.UtcNow);
                success = await commentRepository.AddAsync(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);
                await notificationRepository.AddAsync(notificationForParentAuthor);

                return(Json(new { success = true }));
            }
            else
            {
                return(Json(new { success = false, error = "Could not post comment." }));
            }
        }
Esempio n. 2
0
        public async Task <IActionResult> New(string username, string email, string password, string passwordConfirmation, bool consent, [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 (await userRepository.FindByUsernameAsync(username) != null)
            {
                ViewBag.Error.Add("The username is already taken.");
            }
            if (await userRepository.FindByEmailAsync(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 (!consent)
            {
                ViewBag.Error.Add("You must consent to your Terms and Privacy Policy to register.");
            }

            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;
            }
            Task <int>             userId      = counterRepository.GetAndIncreaseAsync(Counter.USER_ID);
            Tuple <string, string> hashAndSalt = passwordHasher.HashPassword(password);
            string hash = hashAndSalt.Item1;
            string salt = hashAndSalt.Item2;
            User   user = new User(await 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;
            }
            await userRepository.AddAsync(user);

            if (requireEmailVerification)
            {
                await userVerifier.SendVerificationEmailToAsync(user.ID);
            }
            await loginHandler.RegisterLoginAsync(user.ID, HttpContext);

            return(RedirectToAction("Profile", new { id = user.ID }));
        }
Esempio n. 3
0
        public async Task <IActionResult> SubmitPuzzle(string id, string solution, string explanation)
        {
            if (!int.TryParse(id, out int 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 != (await loginHandler.LoggedInUserIdAsync(HttpContext)).Value)
            {
                return(Json(new { success = false, error = "Only the puzzle author can access this right now." }));
            }
            Puzzle possibleDuplicate = await puzzleRepository.FindByFenAndVariantAsync(puzzle.InitialFen, puzzle.Variant);

            if (possibleDuplicate != null && possibleDuplicate.Approved)
            {
                return(Json(new { success = false, error = "Duplicate; same FEN and variant: " + Url.Action("TrainId", "Puzzle", new { id = possibleDuplicate.ID }) }));
            }

            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." }));
            }
            Task <int> pid = counterRepository.GetAndIncreaseAsync(Counter.PUZZLE_ID);

            puzzle.Game = null;
            puzzle.ExplanationUnsafe = explanation;
            puzzle.Rating            = new Rating(1500, 350, 0.06);
            puzzle.Reviewers         = new List <int>();
            if (UserRole.HasAtLeastThePrivilegesOf((await loginHandler.LoggedInUserAsync(HttpContext)).Roles, UserRole.PUZZLE_REVIEWER))
            {
                puzzle.InReview = false;
                puzzle.Approved = true;
                puzzle.Reviewers.Add((await loginHandler.LoggedInUserIdAsync(HttpContext)).Value);
            }
            else
            {
                puzzle.InReview = true;
                puzzle.Approved = false;
            }
            puzzle.DateSubmittedUtc = DateTime.UtcNow;
            puzzle.ID = await pid;
            if (await puzzleRepository.AddAsync(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." }));
            }
        }