Exemplo n.º 1
0
        public ActionResult Create(HellViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Create a new event.
                Event e = new Event();
                e.Brief = model.CreateViewModel.Brief;
                e.Detailed = model.CreateViewModel.Detailed;
                e.Visibility = model.CreateViewModel.Visibility;
                e.Address = model.CreateViewModel.Address;
                e.Latitude = model.CreateViewModel.Latitude;
                e.Longitude = model.CreateViewModel.Longitude;
                e.StartTime = model.CreateViewModel.StartTime;
                e.ModificationState = ModificationState.Added;
                e.OwnerId = User.Identity.GetUserId();
                eventUoW.Events.Attach(e);

                // Create a share link for this event.
                InviteLink link = new InviteLink();
                link.Event = e;
                link.LinkGUID = Guid.NewGuid().ToString();
                link.OneTimeUse = false;
                link.ModificationState = ModificationState.Added;
                eventUoW.InviteLinks.Attach(link);

                // Save the changes.
                eventUoW.Save();

                return RedirectToAction("Details", "Event", new { id = e.Id });
            }

            return View();
        }
Exemplo n.º 2
0
        public async Task<ActionResult> LogIn(HellViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = await eventUoW.Users.UserManager.FindAsync(model.LoginUserViewModel.UserName, model.LoginUserViewModel.Password);
                if (user != null)
                {
                    await SignInAsync(user, model.LoginUserViewModel.RememberMe);
                    return RedirectToLocal(returnUrl);
                }
                else
                {
                    ModelState.AddModelError("", "Invalid username or password.");
                }
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Exemplo n.º 3
0
        public async Task<ActionResult> Register(HellViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = new AppUser() { UserName = model.RegisterUserViewModel.UserName };
                var result = await eventUoW.Users.UserManager.CreateAsync(user, model.RegisterUserViewModel.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToLocal(returnUrl);
                }
                else
                {
                    AddErrors(result);
                }
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Exemplo n.º 4
0
        public ActionResult Edit(HellViewModel model)
        {
            if (ModelState.IsValid)
            {
                Event e = eventUoW.Events.GetEventByID(model.EditViewModel.EventId);

                if (e == null)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.NotFound);
                }

                if (e.OwnerId != User.Identity.GetUserId())
                {
                    return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
                }

                e.Brief = model.EditViewModel.Brief;
                e.Detailed = model.EditViewModel.Detailed;
                e.Visibility = model.EditViewModel.Visibility;
                e.Address = model.EditViewModel.Address;
                e.Latitude = model.EditViewModel.Latitude;
                e.Longitude = model.EditViewModel.Longitude;
                e.StartTime = model.EditViewModel.StartTime;
                e.ModificationState = ModificationState.Modified;

                eventUoW.Events.Attach(e);
                eventUoW.Save();

                return RedirectToAction("Details", "Event", new { id = e.Id });
            }

            return View(model);
        }
Exemplo n.º 5
0
        public ActionResult Delete(HellViewModel model)
        {
            Event e = eventUoW.Events.GetEventByID(model.Event.Id);
            if (e == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }

            if (e.OwnerId != User.Identity.GetUserId())
            {
                return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
            }

            eventUoW.Events.DeleteEvent(e);
            eventUoW.Save();

            return RedirectToAction("Index", "Home");
        }
Exemplo n.º 6
0
 public ActionResult RemoveAccount(HellViewModel model)
 {
     if (ModelState.IsValid)
     {
         var user = eventUoW.Users.GetUserById(User.Identity.GetUserId());
         eventUoW.Users.RemoveAccount(user);
         eventUoW.Save();
         AuthenticationManager.SignOut();
     }
     return RedirectToAction("Index", "Home");
 }
Exemplo n.º 7
0
        public async Task<ActionResult> ConnectExistingAccount(HellViewModel model, string returnUrl)
        {
            if (HasPassword())
            {
                return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
            }

            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Invalid username or password.");
                ViewBag.ReturnUrl = returnUrl;
                return View("ConnectAccount", model);
            }

            AppUser existingUser = await eventUoW.Users.UserManager.FindAsync(model.ConnectExistingAccountViewModel.UserName, model.ConnectExistingAccountViewModel.Password);
            if (existingUser == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }

            if (!HasPassword(existingUser))
            {
                return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
            }

            AppUser socialUser = eventUoW.Users.UserManager.FindById(User.Identity.GetUserId());
            if (socialUser == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }

            IList<UserLoginInfo> logins = await eventUoW.Users.UserManager.GetLoginsAsync(socialUser.Id);
            if (logins.Count > 1)
            {
                throw new InvalidOperationException("A social account was found to have more than one login.");
            }

            // Migrate the invites by the social user. Ignore invites to events hosted by the existingUser, let them be destroyed with the socialUser.
            eventUoW.Invites.TransferInviteOwnership(socialUser, existingUser);

            // Remove any invites for the existing user by the social user, since they will soon be invites to theirselves.
            eventUoW.Invites.RemoveInvitesByHost(existingUser, socialUser);

            // Migrate the created events by the social user.
            eventUoW.Events.TransferEventOwnership(socialUser, existingUser);

            // Remove the dedicated social account.
            AuthenticationManager.SignOut();
            eventUoW.Users.RemoveAccount(socialUser);
            eventUoW.Save();

            // Add this social login to the existing user.
            IdentityResult result = await eventUoW.Users.UserManager.AddLoginAsync(existingUser.Id, logins[0]);
            if (!result.Succeeded)
            {
                throw new Exception("Failed to connect " + logins[0].LoginProvider + " login to existing user.");
            }

            // Login as the existing user.
            await SignInAsync(existingUser, false);

            // Return to the homepage.
            return RedirectToLocal(returnUrl);
        }
Exemplo n.º 8
0
        public async Task<ActionResult> ConnectNewAccount(HellViewModel model, string returnUrl)
        {
            if (HasPassword())
            {
                return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
            }

            if (ModelState.IsValid)
            {
                var socialUser = eventUoW.Users.GetUserById(User.Identity.GetUserId());
                IList<UserLoginInfo> loginInfo = eventUoW.Users.UserManager.GetLogins(socialUser.Id);

                var newUser = new AppUser() { UserName = model.ConnectNewAccountViewModel.UserName };
                var result = await eventUoW.Users.UserManager.CreateAsync(newUser, model.ConnectNewAccountViewModel.Password);
                eventUoW.Users.RemoveAccount(socialUser);
                eventUoW.Save();
                eventUoW.Users.UserManager.AddLogin(newUser.Id, loginInfo[0]);
                if (result.Succeeded)
                {
                    await SignInAsync(newUser, isPersistent: false);
                    return RedirectToLocal(returnUrl);
                }
                else
                {
                    AddErrors(result);
                }
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Exemplo n.º 9
0
        public async Task<ActionResult> Manage(HellViewModel model)
        {
            bool hasPassword = HasPassword();
            ViewBag.HasLocalPassword = hasPassword;
            ViewBag.ReturnUrl = Url.Action("Manage");
            if (hasPassword)
            {
                if (ModelState.IsValid)
                {
                    IdentityResult result = await eventUoW.Users.UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.ManageUserViewModel.OldPassword, model.ManageUserViewModel.NewPassword);
                    if (result.Succeeded)
                    {
                        return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }
            else
            {
                // User does not have a password so remove any validation errors caused by a missing OldPassword field
                ModelState state = ModelState["OldPassword"];
                if (state != null)
                {
                    state.Errors.Clear();
                }

                if (ModelState.IsValid)
                {
                    IdentityResult result = await eventUoW.Users.UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.ManageUserViewModel.NewPassword);
                    if (result.Succeeded)
                    {
                        return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
                    }
                    else
                    {
                        AddErrors(result);
                    }
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }