public ActionResult Create(Pilot model) { if (!User.IsManager()) return RedirectToAction("Restricted", "Error", new { message = "Restricted to your own club" }); // Create the pilot on the managers club if not it is invalid if (!User.IsAdministrator() && model.ClubId != User.Pilot().ClubId) { return RedirectToAction("Restricted", "Error", new { message = "Trying to create pilot in club where you are not a manager... " }); } // Resolve club information manually to avoid conflict with club route //model.Club = db.Clubs.Find(model.ClubId); if (Request.IsClub()) { // There is a bug where the club from the request is bound into the resolving of the club on the pilot modelstate validation, we force this through // HACK: This probably means there is an evil bug hidden when we are going to save more information on club level. ModelState.Remove("Club"); } if (ModelState.IsValid) { db.Pilots.Add(model); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.ClubId = new SelectList(db.Clubs, "ClubId", "Name", model.ClubId); return View(model); }
public static Pilot GetCurrentUserPilot(HttpContextBase context) { // Return Session Cache if set if (context.Items["CurrentUserPilot"] != null) { var ghostUserPilotSession = context.Items["CurrentUserPilot"] as Pilot; if (ghostUserPilotSession != null) { return ghostUserPilotSession; } } if (!context.Request.IsAuthenticated) return new Pilot(); var userManager = context.GetOwinContext().GetUserManager<ApplicationUserManager>(); var user = userManager.FindById(context.User.Identity.GetUserId()); Pilot ghost = new Pilot(); if (user != null) { ghost = user.Pilot ?? new Pilot(); } // Set Session Cache context.Items.Remove("CurrentUserPilot"); context.Items.Add("CurrentUserPilot", ghost); // Return Current User Pilot return ghost; }
public ActionResult Edit(Pilot pilot) { if (!User.IsManager()) return RedirectToAction("Restricted", "Error", new { message = "Restricted to your own club" }); if (Request.IsClub()) { // There is a bug where the club from the request is bound into the resolving of the club on the pilot modelstate validation, we force this through // HACK: This probably means there is an evil bug hidden when we are going to save more information on club level. ModelState.Remove("Club"); } if (!string.IsNullOrWhiteSpace(pilot.MobilNumber)) { if (!MobilNumberValidator.IsValid(pilot.MobilNumber, false)) { ModelState.AddModelError("MobilNumber", "Invalid format, please use the format +4512345678"); } else { pilot.MobilNumber = MobilNumberValidator.ParseMobilNumber(pilot.MobilNumber); } } if (ModelState.IsValid) { db.Entry(pilot).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.ClubId = new SelectList(db.Clubs, "ClubId", "ShortName", pilot.ClubId); return View(pilot); }