Ejemplo n.º 1
0
        public ActionResult MyProfile(UserProfileViewModel modal, HttpPostedFileBase Avatar)
        {
            if (ModelState.IsValid)
            {
                var EditedUser = db.Users.Find(User.Identity.GetUserId());
                EditedUser.FirstName   = modal.FirstName;
                EditedUser.LastName    = modal.LastName;
                EditedUser.DisplayName = modal.DisplayName;

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

                db.SaveChanges();


                return(RedirectToAction("MyProfile", "Manage", new { Message = ManageMessageId.ChangeInfoSuccess }));
            }

            return(View(modal));
        }
        public ActionResult EditProfile(UserViewModel user, HttpPostedFileBase Avatar)
        {
            var newUser = db.Users.Find(user.Id);

            if (user.Avatar != null)
            {
                if (AvatarUploadValidator.IsWebFriendlyImage(user.Avatar))
                {
                    var fileName = Path.GetFileName(user.Avatar.FileName).Replace(' ', '_');
                    user.Avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatar/"), fileName));
                    newUser.ProfilePic = "/Avatar/" + fileName;
                }
            }

            var userId = User.Identity.GetUserId();

            newUser.FirstName = user.FirstName;
            newUser.LastName  = user.LastName;
            newUser.Email     = user.Email;
            newUser.UserName  = user.Email;
            user.ProfilePic   = user.ProfilePic;


            db.SaveChanges();
            return(RedirectToAction("Dashboard", "Home"));
        }
        public async Task <ActionResult> Register(RegisterViewModel model, HttpPostedFileBase avatar)
        {
            if (ModelState.IsValid)
            {
                //This is where the user is created...
                var user = new ApplicationUser
                {
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    DisplayName = model.DisplayName,
                    UserName    = model.Email,
                    Email       = model.Email,
                    ProfilePic  = "/Avatar/user.png"
                };

                //Now I have to determine whether or not to replace the default profile pic with an incoming image
                if (avatar != null)
                {
                    if (AvatarUploadValidator.IsWebFriendlyImage(avatar))
                    {
                        var fileName = Path.GetFileName(avatar.FileName);
                        avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatar/"), fileName));
                        user.ProfilePic = "/Avatar/" + fileName;
                    }
                }

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

                if (result.Succeeded)
                {
                    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("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Ejemplo n.º 4
0
        public async Task <ActionResult> Register(RegisterViewModel model, HttpPostedFileBase Avatar)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    DisplayName = model.DisplayName,
                    Email       = model.Email,
                    UserName    = model.Email,

                    AvatarPath = "/Images/default.jpg"
                };

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

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

                if (result.Succeeded)
                {
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

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

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
        public ActionResult Index(UserProfileViewModel viewModel)
        {
            string ProfilePic = null;

            if (AvatarUploadValidator.IsWebFriendlyImage(viewModel.UserViewModel.Avatar))
            {
                var fileName = Path.GetFileName(viewModel.UserViewModel.Avatar.FileName);
                viewModel.UserViewModel.Avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatar/"), fileName));
                ProfilePic = "/Avatar/" + fileName;
            }
            var user = db.Users.FirstOrDefault(u => u.Email == viewModel.UserViewModel.Email);

            user.FirstName  = viewModel.UserViewModel.FirstName;
            user.LastName   = viewModel.UserViewModel.LastName;
            user.Email      = viewModel.UserViewModel.Email;
            user.ProfilePic = ProfilePic;
            db.SaveChanges();

            return(RedirectToAction("Index"));
        }
Ejemplo n.º 6
0
        public async Task <ActionResult> Index(UserViewModel userViewModel, ChangePasswordViewModel changePasswordViewModel)
        {
            if (userViewModel.Avatar != null)
            {
                if (AvatarUploadValidator.IsWebFriendlyImage(userViewModel.Avatar))
                {
                    var fileName = Path.GetFileName(userViewModel.Avatar.FileName).Replace(' ', '_');
                    userViewModel.Avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatar/"), fileName));
                    userViewModel.ProfilePic = "/Avatar/" + fileName;
                }
            }

            if (userViewModel.FirstName != null && userViewModel.LastName != null && userViewModel.Email != null)
            {
                var userId = User.Identity.GetUserId();
                var user   = db.Users.Find(userId);
                user.FirstName  = userViewModel.FirstName;
                user.LastName   = userViewModel.LastName;
                user.Email      = userViewModel.Email;
                user.ProfilePic = userViewModel.ProfilePic;
            }
            db.SaveChanges();

            if (changePasswordViewModel.NewPassword != null && changePasswordViewModel.NewPassword == changePasswordViewModel.ConfirmPassword)
            {
                var result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), changePasswordViewModel.OldPassword, changePasswordViewModel.NewPassword);

                if (result.Succeeded)
                {
                    var userPass = await UserManager.FindByIdAsync(User.Identity.GetUserId());

                    if (userPass != null)
                    {
                        await SignInManager.SignInAsync(userPass, isPersistent : false, rememberBrowser : false);
                    }
                }
                AddErrors(result);
            }
            TempData["ProfileUpdate"] = "true";
            return(RedirectToAction("Index"));
        }
        public async Task <ActionResult> InviteRegister(InviteRegisterViewModel model, HttpPostedFileBase Avatar)
        {
            if (ModelState.IsValid)
            {
                //I have to checke that the invite is still valid
                var invite = db.Invitations.FirstOrDefault(i => i.Code == model.Code && i.RecipientEmail == model.Email && i.IsValid);
                if (invite == null)
                {
                    return(RedirectToAction("Oops", "Admin"));
                }

                var isUsed = invite.IsValid;
                if (!isUsed)
                {
                    var user = new ApplicationUser {
                        UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName, HouseholdId = db.Invitations.FirstOrDefault(i => i.Code == model.Code).HouseholdId
                    };

                    if (Avatar != null)
                    {
                        //start processing my image
                        if (AvatarUploadValidator.IsWebFriendlyImage(Avatar))
                        {
                            var fileName = Path.GetFileName(Avatar.FileName).Replace(' ', '_');
                            Avatar.SaveAs(Path.Combine(Server.MapPath("~/Avatar/"), fileName));
                            user.ProfilePic = "/Avatar/" + fileName;
                        }
                    }
                    else
                    {
                        user.ProfilePic = "/Avatar/avatar2.jpg";
                    }

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

                    if (result.Succeeded)
                    {
                        invite.IsValid = true;
                        db.SaveChanges();
                        roleHelper.AddUserToRole(user.Id, "Member");

                        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", "Homeholds"));
                    }
                    AddErrors(result);
                }

                else
                {
                    RedirectToAction("Register", "Account");
                }
            }

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