public ActionResult AddDetail(Models.LedgerAddDetailModel model)
 {
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         var theLedger = context.Ledgers.FirstOrDefault(l => l.LedgerId == model.LedgerId);
         if (theLedger == default(Ledger))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger!", Message = "The ledger you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         else if (theLedger.UserProfile.UserName != User.Identity.Name && theLedger.Editors.FirstOrDefault(u => u.UserName == User.Identity.Name) == default(UserProfile))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are neither the owner nor an editor of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         var theDetail = new LedgerDetail();
         theDetail.Amount = model.Amount;
         theDetail.Legder_LedgerId = model.LedgerId;
         theDetail.Memo = model.Memo;
         theDetail.Payor = model.Payor;
         theDetail.PaySource = model.PaySource;
         theDetail.Category = model.Category;
         theDetail.When = model.When;
         theLedger.LedgerDetails.Add(theDetail);
         context.SaveChanges();
         return RedirectToAction("Detail", new { id=model.LedgerId });
     }
 }
 public ActionResult AddEditor(Models.LedgerAddEditorModel model)
 {
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         var theLedger = context.Ledgers.FirstOrDefault(l => l.LedgerId == model.LedgerId);
         if (theLedger == default(Ledger))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger!", Message = "The ledger you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         else if (theLedger.UserProfile.UserName != User.Identity.Name)
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are not the owner of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         var theUser = context.UserProfiles.FirstOrDefault(u => u.UserName == model.EditorName);
         if (theUser == default(UserProfile))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such User!", Message = "The user you are attempting to add does not exist in the database.", ReturnAction = "Editors", ReturnRouteValues = new { id= model.LedgerId } });
         }
         if (theLedger.Editors.Any(u => u.UserId == theUser.UserId))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "User Already an Editor!", Message = "The user you are attempting to add is already an editor.", ReturnAction = "Editors", ReturnRouteValues = new { id=model.LedgerId } });
         }
         theLedger.Editors.Add(theUser);
         context.SaveChanges();
         return RedirectToAction("Editors", new { id = model.LedgerId });
     }
 }
 public ActionResult TransferOwnership(LedgerTransferOwnershipModel model)
 {
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         var theLedger = context.Ledgers.FirstOrDefault(l => l.LedgerId == model.LedgerId);
         if (theLedger == default(Ledger))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger!", Message = "The ledger you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         else if (theLedger.UserProfile.UserName != User.Identity.Name)
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are not the owner of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         var theOldOwner = theLedger.UserProfile;
         var theNewOwner = context.UserProfiles.FirstOrDefault(u => u.UserName == model.NewOwner);
         if (theNewOwner == default(UserProfile))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "User Does Not Exist!", Message = "The specified new owner does not exist!", ReturnAction = "Detail", ReturnRouteValues = new { id=model.LedgerId } });
         }
         if (theOldOwner.UserId != theNewOwner.UserId)
         {
             if (!theLedger.Editors.Any(u => u.UserId == theOldOwner.UserId) && model.MakeOldOwnerAnEditor)
             {
                 theLedger.Editors.Add(theOldOwner);
             }
             theLedger.UserProfile = theNewOwner;
             context.SaveChanges();
         }
         return RedirectToAction("Index");
     }
 }
 public ActionResult Rename(Models.LedgerRenameModel model)
 {
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         var theLedger = context.Ledgers.FirstOrDefault(l => l.LedgerId == model.Id);
         if (theLedger == default(Ledger))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger!", Message = "The ledger you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         else if (theLedger.UserProfile.UserName != User.Identity.Name)
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are not the owner of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         theLedger.LedgerName = model.Name;
         context.SaveChanges();
         return RedirectToAction("Index");
     }
 }
 public ActionResult RemoveDetail(int id)
 {
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         var theDetail = context.LedgerDetails.FirstOrDefault(d => d.LedgerDetailId == id);
         if (theDetail == default(LedgerDetail))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger Detail!", Message = "The ledger detail you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         var theLedger = context.Ledgers.FirstOrDefault(l => l.LedgerId == theDetail.Legder_LedgerId);
         if (theLedger == default(Ledger))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Such Ledger!", Message = "The ledger you are attempting to access does not exist in the database.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         else if (theLedger.UserProfile.UserName != User.Identity.Name && theLedger.Editors.FirstOrDefault(u => u.UserName == User.Identity.Name) == default(UserProfile))
         {
             return View("ErrorMessage", new Models.ErrorMessageModel { Title = "No Permission!", Message = "You are neither the owner nor an editor of the ledger you are attempting to access.", ReturnAction = "Index", ReturnRouteValues = new { } });
         }
         context.LedgerDetails.Remove(theDetail);
         context.SaveChanges();
         return RedirectToAction("Detail", new { id = theLedger.LedgerId });
     }
 }
 public ActionResult Create(Models.LedgerCreateModel model)
 {
     if (!ModelState.IsValid)
     {
         return View(model);
     }
     using (db00ccd2da5aff4a5983c0a17b010f53a6Entities context = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
     {
         UserProfile theUser = context.UserProfiles.First(u => u.UserName == User.Identity.Name);
         Ledger theLedger = new Ledger();
         theLedger.LedgerName = model.Name;
         theLedger.User_UserId = theUser.UserId;
         context.Ledgers.Add(theLedger);
         context.SaveChanges();
         return RedirectToAction("Detail", new { id=theLedger.LedgerId });
     }
 }
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                // Insert a new user into the database
                using (db00ccd2da5aff4a5983c0a17b010f53a6Entities db = new db00ccd2da5aff4a5983c0a17b010f53a6Entities())
                {
                    UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower());
                    // Check if user already exists
                    if (user == null)
                    {
                        // Insert name into the profile table
                        db.UserProfiles.Add(new UserProfile { UserName = model.UserName });
                        db.SaveChanges();

                        OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                        OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                        return RedirectToLocal(returnUrl);
                    }
                    else
                    {
                        ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                    }
                }
            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }