public static SafeModeResult SavePlayer(Player player) { if (player == null) { throw new Exception("<player> parameter cannot be null"); } return Dbh.GetCollection<Player>("Players").Save(player); }
// COOKIE DOUGH public static void CreateRememberMeCookie(Player player, HttpContextBase httpContext) { httpContext.Response.Cookies.Add(new HttpCookie("RankTangleAuth")); var httpCookie = httpContext.Response.Cookies["RankTangleAuth"]; if (httpCookie != null) { httpCookie["Token"] = GetAuthToken(player); } if (httpCookie != null) { httpCookie.Expires = DateTime.Now.AddDays(30); } }
public bool AutoLogin(Player player) { // Set or remove cookie for future auto-login if (player != null) { if (player.RememberMe) { // Save an autologin token as cookie and in the Db var playerCollection = this.Dbh.GetCollection<Player>("Players"); var autoLoginCollection = this.Dbh.GetCollection<AutoLogin>("AutoLogin"); var autoLogin = autoLoginCollection.FindOne(Query.EQ("Email", player.Email)); if (!player.Activated) { RedirectToAction("Activate"); } if (autoLogin == null) { autoLogin = new AutoLogin { Email = player.Email, Token = AccountControllerHelpers.GetAuthToken(player), Created = DateTime.Now }; autoLoginCollection.Save(autoLogin); } AccountControllerHelpers.CreateRememberMeCookie(player, HttpContext); player.RememberMe = player.RememberMe; playerCollection.Save(player); } else { AccountControllerHelpers.RemoveRememberMeCookie(HttpContext); } this.Session["Admin"] = this.Settings.AdminAccount == player.Email; this.Session["IsLoggedIn"] = true; this.Session["User"] = player; return true; } return false; }
public static Match CreateMatch(Player user, FormCollection formValues) { var newMatch = new Match { CreationTime = new BsonDateTime(DateTime.Now), GameOverTime = new BsonDateTime(DateTime.MinValue), Created = new BsonDateTime(DateTime.Now), CreatedBy = user.Id }; var playerCollection = Dbh.GetCollection<Player>("Players"); const string MatchPlayerRegEx = "^team-(a|b)-player-[0-9]+$"; const string MatchTeamARegEx = "^team-a-player-[0-9]+$"; foreach (var formValue in formValues.Keys) { var formElementName = formValue.ToString(); var formElementValue = formValues.GetValue(formElementName).AttemptedValue; if (Regex.IsMatch(formElementName, MatchPlayerRegEx) && !string.IsNullOrEmpty(formElementValue)) { var player = playerCollection.FindOne(Query.EQ("_id", BsonObjectId.Create(formElementValue))); if (Regex.IsMatch(formElementName, MatchTeamARegEx)) { newMatch.TeamA.TeamPlayers.Add(player); } else { newMatch.TeamB.TeamPlayers.Add(player); } } } return newMatch; }
private static bool PlayerEmailAlreadyInUse(Player player) { var resultCount = Dbh.GetCollection<Player>("Players").Find(Query.EQ("Email", player.Email.ToLower())).Count(); return resultCount > 0; }
public static string GetAuthToken(Player player) { return Md5.CalculateMd5(player.Id + player.Email + "RankTangle4Ever"); }
public ActionResult Register(RegisterViewModel viewModel) { if (ModelState.IsValid) { viewModel = AccountControllerHelpers.ValidateRegisterViewModel(viewModel); if (viewModel.ListOfErrorMessages.Count > 0) { viewModel.Settings = this.Settings; viewModel.FormIsInvalid = true; return this.View(viewModel); } var email = viewModel.Player.Email.ToLower(); var name = viewModel.Player.Name; var password = Md5.CalculateMd5(viewModel.Player.Password); var gender = viewModel.Player.Gender; var playerCollection = this.Dbh.GetCollection<Player>("Players"); var newPlayer = new Player { Id = BsonObjectId.GenerateNewId().ToString(), Email = email, Name = name, Gender = gender, Password = password, Won = 0, Lost = 0, Played = 0 }; playerCollection.Save(newPlayer); AutoLogin(newPlayer); var response = Email.SendSimpleEmail(newPlayer.Email); // TODO: Change call to this static class return this.Redirect(Url.Action("Index", "Players") + "#" + newPlayer.Id); } return RedirectToAction("Register"); }