public async Task <ActionResult> AcceptInvitation(AcceptInvitationVM model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, HouseholdId = model.HouseholdId
                };
                if (model.Avatar != null)
                {
                    //Run the avatar upload code from blog/bt here
                }
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    roleHelper.UpdateUserRole(user.Id, "Member");
                    InvitationHelper.MarkAsInvalid(model.InvitationId);
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Dashboard", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public async Task <ActionResult> ManualJoin(string code)
        {
            var user       = db.Users.Find(User.Identity.GetUserId());
            var realGuid   = Guid.Parse(code);
            var invitation = db.Invitations.FirstOrDefault(i => i.RecipientEmail == user.Email && i.Code == realGuid);

            if (invitation == null)
            {
                return(View("NotFoundError"));
            }
            var expirationDate = invitation.Created.AddDays(invitation.TTL);

            if (invitation.IsValid && DateTime.Now < expirationDate)
            {
                InvitationHelper.MarkAsInvalid(invitation.Id);
                user.HouseholdId = invitation.HouseholdId;
                roleHelper.UpdateUserRole(user.Id, "Member");

                await AuthorizeExtensions.RefreshAuthentication(HttpContext, user);

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

            return(View("AcceptError", invitation));
        }
Exemplo n.º 3
0
        public async Task <ActionResult> AcceptRegisterInvitation(AcceptRegisterInvitationViewModel invitationVM)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    FirstName   = invitationVM.FirstName,
                    LastName    = invitationVM.LastName,
                    DisplayName = invitationVM.DisplayName,
                    UserName    = invitationVM.Email,
                    Email       = invitationVM.Email,
                    AvatarPath  = "/Avatars/Stuart.png",
                    HouseholdId = invitationVM.HouseholdId
                };

                var result = await UserManager.CreateAsync(user, invitationVM.Password);

                if (result.Succeeded)
                {
                    InvitationHelper.MarkAsInvalid(invitationVM.Id);
                    rHelp.AddUserToRole(user.Id, "Member");

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    return(RedirectToAction("Hooray", "Account"));
                }

                AddErrors(result);
            }

            return(View(invitationVM));
        }
Exemplo n.º 4
0
        public async Task <ActionResult> AcceptLoginInvitation(AcceptLoginInvitationViewModel invitationVM, string returnUrl)
        {
            var user = db.Users.Where(u => u.Email == invitationVM.Email).FirstOrDefault();

            if (!ModelState.IsValid)
            {
                return(View(invitationVM));
            }

            var result = await SignInManager.PasswordSignInAsync(invitationVM.Email, invitationVM.Password, invitationVM.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
                InvitationHelper.MarkAsInvalid(invitationVM.Id);
                rHelp.RemoveUserFromRole(user.Id, "Guest");
                rHelp.AddUserToRole(user.Id, "Member");
                user.HouseholdId = invitationVM.HouseholdId;
                db.SaveChanges();
                return(RedirectToAction("Hooray", "Account"));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = invitationVM.RememberMe }));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(invitationVM));
            }
        }
Exemplo n.º 5
0
        public async Task <ActionResult> AcceptInvitation(AcceptInvitationViewModel invitationvm)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    FirstName   = invitationvm.FirstName,
                    LastName    = invitationvm.LastName,
                    UserName    = invitationvm.Email,
                    Email       = invitationvm.Email,
                    AvatarPath  = "/Avatars/default_user.png",
                    HouseholdId = invitationvm.HouseholdId
                };

                var result = await UserManager.CreateAsync(user, invitationvm.Password);

                if (result.Succeeded)
                {
                    InvitationHelper.MarkAsInvalid(invitationvm.Id);

                    roleHelper.AddUserToRole(user.Id, "Member");

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    return(RedirectToAction("Dashboard", "Home"));
                }
                AddErrors(result);
            }
            // If we got this far, something failed, redisplay form
            return(View(invitationvm));
        }
Exemplo n.º 6
0
        public async Task <ActionResult> ManualJoin(string code)
        {
            var userId     = User.Identity.GetUserId();
            var user       = db.Users.Find(userId);
            var realGuid   = Guid.Parse(code);
            var invitation = db.Invitations.FirstOrDefault(i => i.Code == realGuid);

            InvitationHelper.MarkAsInvalid(invitation.Id);
            roleHelper.RemoveUserFromRole(userId, "Guest");
            roleHelper.AddUserToRole(userId, "Member");

            await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

            return(RedirectToAction("Dashboard", "Home"));
        }
Exemplo n.º 7
0
        public async Task <ActionResult> AcceptInvitation(AcceptInvitationVM model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName    = model.Email,
                    Email       = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    HouseholdId = model.HouseholdId
                };

                if (model.Avatar != null)
                {
                    if (FileUploadValidator.IsWebFriendlyImage(model.Avatar))
                    {
                        var fileName = FileStamp.MakeUnique(model.Avatar.FileName);

                        var serverFolder = WebConfigurationManager.AppSettings["DefaultServerFolder"];
                        model.Avatar.SaveAs(Path.Combine(Server.MapPath("~" + serverFolder), fileName));
                        user.AvatarPath = $"{serverFolder}/{fileName}";
                    }
                }

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    rolesHelper.AddUserToRole(user.Id, "Member");
                    InvitationHelper.MarkAsInvalid(model.InvitationId);
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit https://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Dashboard", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form

            return(View(model));
        }
Exemplo n.º 8
0
        public async Task <ActionResult> AcceptInvitation(AcceptInvitationViewModel invitationvm, HttpPostedFileBase avatar)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    FirstName   = invitationvm.FirstName,
                    LastName    = invitationvm.LastName,
                    DisplayName = invitationvm.DisplayName,
                    UserName    = invitationvm.Email,
                    Email       = invitationvm.Email,
                    AvatarPath  = "/Avatars/default_user.png",
                    HouseholdId = invitationvm.HouseholdId
                };

                if (avatar != null)
                {
                    if (ImageUploadValidator.IsWebFriendlyImage(avatar))
                    {
                        var fileName     = Path.GetFileName(avatar.FileName);
                        var justFileName = Path.GetFileNameWithoutExtension(fileName);
                        justFileName = StringUtilities.URLFriendly(justFileName);
                        fileName     = $"{justFileName}_{DateTime.Now.Ticks}{Path.GetExtension(fileName)}";
                        avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatars/"), fileName));
                        user.AvatarPath = "/Avatars/" + fileName;
                    }
                }

                var result = await UserManager.CreateAsync(user, invitationvm.Password);

                if (result.Succeeded)
                {
                    InvitationHelper.MarkAsInvalid(invitationvm.Id);

                    roleHelper.AddUserToRole(user.Id, "Member");

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    return(RedirectToAction("Index", "Home"));
                }

                AddErrors(result);
            }

            //If we got this far, something failed, redisplay form
            return(View(invitationvm));
        }
Exemplo n.º 9
0
        public async Task <ActionResult> AcceptInvitationWithAcct(AcceptInvitationWithAcctViewModel invitationVm)
        {
            if (!ModelState.IsValid)
            {
                return(View(invitationVm));
            }

            var existingUser = db.Users.FirstOrDefault(u => u.Email == invitationVm.Email);

            existingUser.HouseholdId = invitationVm.HouseholdId;

            if (!roleHelper.IsDemoUser(db.Users.FirstOrDefault(u => u.Email == invitationVm.Email).Id))
            {
                InvitationHelper.MarkAsInvalid(invitationVm.Id);
                roleHelper.RemoveUserFromRole(db.Users.FirstOrDefault(u => u.Email == invitationVm.Email).Id, "UnAssigned");
                roleHelper.AddUserToRole(db.Users.FirstOrDefault(u => u.Email == invitationVm.Email).Id, "Member");
            }

            if (!roleHelper.IsDemoUser(existingUser.Id))
            {
                db.SaveChanges();
            }

            var result = await SignInManager.PasswordSignInAsync(invitationVm.Email, invitationVm.Password, false, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
                //InvitationHelper.MarkAsInvalid(invitationVm.Id);
                return(RedirectToAction("Index", "Home"));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(invitationVm));
            }
        }
        public async Task <ActionResult> Join(JoinInvitationViewModel invitation)
        {
            if (ModelState.IsValid)
            {
                var user     = db.Users.Find(User.Identity.GetUserId());
                var userRole = roleHelper.ListUserRoles(user.Id).FirstOrDefault();
                if (userRole != null)
                {
                    roleHelper.RemoveUserFromRole(user.Id, userRole);
                }
                roleHelper.AddUserToRole(user.Id, "Member");
                user.HouseholdId = invitation.HouseholdId;
                var invite = db.Invitations.Find(invitation.Id);
                notifyHelp.DidTheyJoinUp(invite);
                inviteHelp.MarkAsInvalid(invitation.Id);
                db.SaveChanges();
                await ControllerContext.HttpContext.RefreshAuthentication(user);

                var houseName = db.Households.Find(invitation.HouseholdId).Name;
                TempData["Appointed"] = $"You joined the '{houseName}' household!";
                return(RedirectToAction("Index", "Home"));
            }
            return(View(invitation));
        }