Esempio n. 1
0
        public async Task <ActionResult> JoinHousehold(string code)
        {
            var user       = db.Users.Find(User.Identity.GetUserId());
            var invitation = db.Invitations.Where(i => i.RecipientEmail == user.Email && i.Valid == true).First();

            try
            {
                if (Guid.Parse(code) != invitation.Code)
                {
                    return(RedirectToAction("Main", "Dashboard"));
                }
            }
            catch
            {
                return(RedirectToAction("Main", "Dashboard"));
            }
            invitation.Valid           = false;
            db.Entry(invitation).State = EntityState.Modified;

            user.HouseholdId     = invitation.HouseholdId;
            db.Entry(user).State = EntityState.Modified;

            db.SaveChanges();

            userManager.RemoveFromRoles(user.Id, "Personal");
            userManager.AddToRole(user.Id, "Member");

            await UserAuthorization.RefreshAuthentication(HttpContext, user);

            return(RedirectToAction("Main", "Dashboard"));
        }
        public async Task <ActionResult> Create(string HouseholdName, string HouseholdGreeting)
        {
            if (!HouseholdName.IsNullOrEmpty() && !HouseholdGreeting.IsNullOrEmpty())
            {
                var userId = User.Identity.GetUserId();
                var user   = db.Users.Find(User.Identity.GetUserId());

                Household household = new Household
                {
                    Created  = DateTime.Now,
                    Name     = HouseholdName,
                    Greeting = HouseholdGreeting,
                    OwnerId  = userId
                };
                db.Households.Add(household);
                db.SaveChanges();
                user.HouseholdId = household.Id;
                db.SaveChanges();

                userManager.RemoveFromRole(userId, "Personal");
                userManager.AddToRole(userId, "Owner");

                await UserAuthorization.RefreshAuthentication(HttpContext, user);

                return(RedirectToAction("Main", "Dashboard"));
            }
            else
            {
                return(RedirectToAction("Main", "Dashboard", new { ErrorMessage = "Your input was incomplete." }));
            }
        }
        public async Task <ActionResult> Leave()
        {
            var user = db.Users.Find(User.Identity.GetUserId());

            user.HouseholdId = null;
            db.BankAccounts.Where(ba => ba.OwnerId == user.Id).ForEach(ba => ba.HouseholdId = null);
            db.BankAccounts.Where(ba => ba.OwnerId == user.Id).SelectMany(ba => ba.Transactions).ForEach(t => t.CategoryItemId = null);
            db.SaveChanges();

            userManager.RemoveFromRole(user.Id, "Member");
            userManager.AddToRole(user.Id, "Personal");
            await UserAuthorization.RefreshAuthentication(HttpContext, user);

            return(RedirectToAction("Main", "Dashboard"));
        }
        public async Task <ActionResult> Delete()
        {
            var householdId = db.Users.Find(User.Identity.GetUserId()).HouseholdId;
            var users       = db.Users.Where(u => u.HouseholdId == householdId).ToList();

            foreach (var user in users)
            {
                user.HouseholdId = null;
                db.BankAccounts.Where(ba => ba.OwnerId == user.Id && ba.HouseholdId == householdId).SelectMany(ba => ba.Transactions).ForEach(t => t.CategoryItemId = null);
                db.BankAccounts.Where(ba => ba.OwnerId == user.Id && ba.HouseholdId == householdId).ForEach(ba => ba.HouseholdId = null);

                if (User.IsInRole("Owner"))
                {
                    userManager.RemoveFromRole(user.Id, "Owner");
                }
                else
                {
                    userManager.RemoveFromRole(user.Id, "Member");
                }

                userManager.AddToRole(user.Id, "Personal");
            }

            foreach (var budget in db.Categories.Where(c => c.HouseholdId == householdId).ToList())
            {
                foreach (var budgetItem in budget.CategoryItems.ToList())
                {
                    db.CategoryItems.Remove(budgetItem);
                }
                db.Categories.Remove(budget);
            }

            db.Households.Remove(db.Households.Find(householdId));
            db.SaveChanges();

            await UserAuthorization.RefreshAuthentication(HttpContext, db.Users.Find(User.Identity.GetUserId()));

            return(RedirectToAction("Main", "Dashboard"));
        }