Ejemplo n.º 1
0
        public IActionResult RemoveGame(int id)
        {
            var user = StateHelper.GetUserFromCookie(Request);

            if (user == null)
            {
                return(RedirectToAction("Index", "Login"));
            }

            var player = PlayerManager.GetOrCreatePlayer(user.Username);

            if ((int)user.Role < (int)UserRole.Moderator)
            {
                ViewData["message"] = "You don't have permission to do this";
                return(ShowGameList(player));
            }

            var game = GameManager.GetGame(id);

            if (game == null)
            {
                return(new NotFoundResult());
            }

            GameManager.Games.Remove(game);
            ViewData["message"] = "Game removed succesfully";
            return(ShowGameList(player));
        }
Ejemplo n.º 2
0
        // GET: Login/Register
        public ActionResult Register()
        {
            if (StateHelper.GetUserFromCookie(Request) != null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            return(View());
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> Index(RegisterFormModel userInput)
        {
            if (StateHelper.GetUserFromCookie(Request) != null)
            {
                return(RedirectToAction(nameof(Index)));
            }

            if (!ModelState.IsValid || !userInput.IsValid())
            {
                return(View("Login"));
            }

            try
            {
                var ual  = new UserAccessLayer();
                var user = await ual.GetUserByUsername(userInput.Username);

                if (user == null)
                {
                    ViewData["Error"] = "Invalid username or password.";
                    return(View("Login"));
                }

                if (user.LoginAttempts > 3)
                {
                    ViewData["Error"] = "Account has been locked.";
                    return(View("Login"));
                }

                var passwordHash = Crypto.CalculateArgon2Hash(userInput.Password, user.HashSalt);
                if (!Crypto.SecureCompareByteArrays(passwordHash, user.PasswordHash))
                {
                    ViewData["Error"] = "Invalid username or password.";
                    user.LoginAttempts++;
                    await ual.UpdateUser(user);

                    return(View("Login"));
                }

                user.Token         = StateHelper.GenerateUniqueToken();
                user.TokenDate     = DateTime.Now.AddMinutes(StateHelper.ValidTokenDuration);
                user.LoginAttempts = 0;
                await ual.UpdateUser(user);

                StateHelper.SetUserCookie(user, Response);

                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                ViewData["Error"] = "An unknown error has occured.";
                return(View("Login"));
            }
        }
Ejemplo n.º 4
0
        // GET: Login
        public ActionResult Index()
        {
            var user = StateHelper.GetUserFromCookie(Request);

            if (user == null)
            {
                return(View("Login"));
            }

            StateHelper.SetUserCookie(user, Response);
            return(RedirectToAction("Index", "Home"));
        }
Ejemplo n.º 5
0
        public IActionResult Index()
        {
            var user = StateHelper.GetUserFromCookie(Request);

            if (user == null)
            {
                return(RedirectToAction("Index", "Login"));
            }

            return(GameController.GetPlayerAndGame(out var player, out var game, Request, _context)
                                ? View(!game.Started ? "Waiting" : "Game")
                                : ShowGameList(player));
        }
Ejemplo n.º 6
0
        public static bool GetPlayer(out Player player, HttpRequest request, GameContext context)
        {
            player = null;

            // Try to get the user from the cookie
            var user = StateHelper.GetUserFromCookie(request);

            if (user == null)
            {
                return(false);
            }

            // Check if we have a player that matches the user in memory
            player = PlayerManager.GetOrCreatePlayer(user.Username);
            return(true);
        }
Ejemplo n.º 7
0
        public async Task <ActionResult> Logout()
        {
            var user = StateHelper.GetUserFromCookie(Request);

            if (user == null)
            {
                return(View("Login"));
            }

            var ual = new UserAccessLayer();

            user.TokenDate = DateTime.Now;
            await ual.UpdateUser(user);

            return(View("Login"));
        }
Ejemplo n.º 8
0
        public IActionResult CreateGame()
        {
            var user = StateHelper.GetUserFromCookie(Request);

            if (user == null)
            {
                return(RedirectToAction("Index", "Login"));
            }

            var player = PlayerManager.GetOrCreatePlayer(user.Username);
            var game   = new Game(player);

            GameManager.RememberGame(game);

            return(RedirectToAction(nameof(Index)));
        }
Ejemplo n.º 9
0
        public async Task <IActionResult> JoinGame(int id)
        {
            var user = StateHelper.GetUserFromCookie(Request);

            if (user == null)
            {
                return(RedirectToAction("Index", "Login"));
            }

            var player = PlayerManager.GetOrCreatePlayer(user.Username);
            var game   = GameManager.GetGame(id);

            if (game == null)
            {
                return(new NotFoundResult());
            }

            game.Start(player);
            await GameManager.SaveGame(game, _context);

            return(RedirectToAction(nameof(Index)));
        }
Ejemplo n.º 10
0
        public async Task <ActionResult> Register(RegisterFormModel userInput)
        {
            if (StateHelper.GetUserFromCookie(Request) != null)
            {
                return(RedirectToAction(nameof(Index)));
            }
            if (!ModelState.IsValid || !userInput.IsValid())
            {
                return(View());
            }

            try
            {
                var ual = new UserAccessLayer();
                if (await ual.GetUserByUsername(userInput.Username) != null)
                {
                    ViewData["Error"] = "A user with that username already exists";
                    return(View());
                }

                if (userInput.Username.Length < 3)
                {
                    ViewData["Error"] = "Your username must have at least 3 characters";
                    return(View());
                }

                if (!Regex.IsMatch(userInput.Password, @"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z\d]).{10,}$"))
                {
                    ViewData["Error"] =
                        "Your password must contain at least 1 number, 1 uppercase letter, 1 lowercase letter, 1 special character and must be at least 10 characters long.";
                    return(View());
                }

                if (userInput.Password.Length > 128)
                {
                    ViewData["Error"] = "Your password cannot be longer than 128 characters";
                    return(View());
                }

                var token        = StateHelper.GenerateUniqueToken();
                var hashSalt     = Crypto.GenerateRandomString(64);
                var passwordHash = Crypto.CalculateArgon2Hash(userInput.Password, hashSalt);
                var user         = new UserModel
                {
                    Username     = userInput.Username,
                    HashSalt     = hashSalt,
                    PasswordHash = passwordHash,
                    Token        = token,
                    TokenDate    = DateTime.Now.AddMinutes(StateHelper.ValidTokenDuration),
                    Role         = UserRole.User
                };

                if (!await ual.AddUser(user))
                {
                    return(View());
                }

                StateHelper.SetUserCookie(user, Response);
                return(RedirectToAction(nameof(Index)));
            }
            catch
            {
                ViewData["Error"] = "An unknown error has occured.";
                return(View());
            }
        }