public async Task <ActionResult> LeaveHousehold() { ApplicationUser user = db.Users.Find(User.Identity.GetUserId()); LeaveHouseholdHelper leaveHelper = new LeaveHouseholdHelper(); leaveHelper.LeaveHousehold(user); await HttpContext.RefreshAuthentication(db.Users.Find(User.Identity.GetUserId())); return(RedirectToAction("Index", "Home")); }
public async Task <ActionResult> InvitationAccept(int invitationId) { Invitation invitation = db.Invitations.AsNoTracking().First(i => i.Id == invitationId); ApplicationUser user = db.Users.Find(User.Identity.GetUserId()); // Verify that the Invitation has not already been Accepted or Declined - This is a second layer of security as it was already handled in the View if (invitation.HasAccepted == true || invitation.HasDeclined == true) { return(RedirectToAction("NotAuthorized", "Account")); } // Doublechecking that the logged in user is also the Invitee if (user.Email != invitation.InviteeEmail) { return(RedirectToAction("NotAuthorized", "Account")); } // If Invitee is in another HH, they have to leave that HH before joining this one. if (user.HouseholdId != null) { LeaveHouseholdHelper leaveHelper = new LeaveHouseholdHelper(); leaveHelper.LeaveHousehold(user); } // Add User to the Household user.HouseholdId = invitation.HouseholdId; user.DateJoined = DateTime.UtcNow; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); // Mark Invitation as Accepted so it does not show up in the list of Unresponded Invitations invitation.HasAccepted = true; db.Entry(invitation).State = EntityState.Modified; db.SaveChanges(); // Mark Notification as Acknowledged so it does not show up on the list of Unread/Unacknowledged Notifications Notification notification = db.Notifications.AsNoTracking().First(n => n.Id == invitation.NotificationId); notification.IsAcknowledged = true; db.Entry(notification).State = EntityState.Modified; db.SaveChanges(); // Send a notification to the Members of the Household indicating that there is a new Member NotificationsController notificationController = new NotificationsController(); notificationController.CreateInvitationResponseNotification(invitation.Id, true); await HttpContext.RefreshAuthentication(db.Users.Find(User.Identity.GetUserId())); return(RedirectToAction("Index", "Households")); }
public async Task <ActionResult> Create([Bind(Include = "Id,Name,Description")] Household household) { if (ModelState.IsValid) { ApplicationUser user = db.Users.Find(User.Identity.GetUserId()); // First verify that no other HH have the same name // We will also check this at the input field via an Ajax button if (db.Households.Any(h => h.Name == household.Name)) { ViewBag.ErrorMessage = "A Household already exists with that name."; return(View(household)); } // If User is in another HH, they have to leave that HH before Creating this one. if (user.HouseholdId != null) { LeaveHouseholdHelper leaveHelper = new LeaveHouseholdHelper(); leaveHelper.LeaveHousehold(user); } // Now, for our regularly scheduled broadcast: household.OwnerId = user.Id; household.Created = DateTime.UtcNow; household.IsActive = true; //household.LogoUrl = "~/Content/Images/Household Logos/HouseholdDefaultLogo.png"; db.Households.Add(household); db.SaveChanges(); //Now create a folder for any Logos to be added //var attPath = "~/Content/Images/Household Logos/" + household.Id; //var newPath = Server.MapPath(attPath); //Directory.CreateDirectory(newPath); // Creating user must belong to the household user.HouseholdId = household.Id; user.DateJoined = DateTime.UtcNow; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); // If a Household Logo was uploaded, we have to add that //if (logo != null && logo.ContentLength > 0) { // var ext = Path.GetExtension(logo.FileName).ToLower(); // Gets image's extension and then sets it to lower case // var filePath = "Content/Images/Household Logos/" + household.Id; // var absPath = Server.MapPath("~/" + filePath); // string newFileName = logo.FileName; // var num = 0; // while (System.IO.File.Exists(Path.Combine(absPath, newFileName))) { // //Sets "filename" back to the default value // newFileName = Path.GetFileNameWithoutExtension(logo.FileName); // //Add's parentheses after the name with a number ex. filename(4) // newFileName = string.Format(newFileName + "(" + ++num + ")" + ext); // //Makes sure pPic gets updated with the new filename so it could check // //attach = fileName + Path.GetExtension(doc.FileName); // } // logo.SaveAs(Path.Combine(absPath, newFileName)); // // Update attachment info and add to database // household.LogoUrl = "/" + filePath + "/" + newFileName; // db.Entry(household).State = EntityState.Modified; // db.SaveChanges(); //} await HttpContext.RefreshAuthentication(db.Users.Find(User.Identity.GetUserId())); return(RedirectToAction("Index", "Home")); } else { return(View(household)); } }