Ejemplo n.º 1
0
        public ActionResult EditProfile(AccountModel model)
        {
            var oldPassword = EncryptHelper.EncryptPassword(model.OldPassword);

            if (oldPassword != CurrentUser.Identity.Password)
            {
                ModelState.AddModelError("OldPassword", Resource.TheOldPasswordDoNotMatch);
            }
            if (ModelState.IsValid)
            {
                // Update user profile picture
                if (model.Picture != null && model.Picture.ContentLength > 0)
                {
                    UserPicture.Delete(CurrentUser.Identity.ID, CurrentUser.Identity.Picture);
                    string pictureFileName = UserPicture.Upload(CurrentUser.Identity.ID, model.Picture);
                    CurrentUser.Identity.Picture = pictureFileName;
                }

                // Update user primitive info
                //CurrentUser.Identity.Password = model.Password;
                CurrentUser.Identity.DisplayName = model.Username;
                CurrentUser.Identity.Email       = model.Email;
                CurrentUser.Identity.Phone       = model.Phone;
                CurrentUser.Identity.MobilePhone = model.MobilePhone;
                if (!string.IsNullOrEmpty(model.Password))
                {
                    CurrentUser.Identity.Password = EncryptHelper.EncryptPassword(model.Password);
                }
                if (_membership.UpdateUser(CurrentUser.Identity))
                {
                    FormsAuthentication.SetAuthCookie(CurrentUser.Identity.Email, false);
                }

                _loginTracker.ReloadUser(CurrentUser.Identity.Email, CurrentUser.Identity);


                ViewBag.Success = true;
                ViewBag.Message = Resource.YourProfileHasBeenUpdated;
                return(EditProfile());
            }

            return(View(model));
        }
        public ActionResult Edit(EditUserModel model)
        {
            User user = _membershipService.GetUserByName(model.Username);

            if (user != null && user.ID != model.ID)
            {
                ModelState.AddModelError("DisplayName", Resource.UserNameExists);
            }

            user = _membershipService.GetUserByEmail(model.Email);
            if (user != null && user.ID != model.ID)
            {
                ModelState.AddModelError("Email", Resource.UserEmailExists);
            }

            if (string.IsNullOrEmpty(model.Password) && model.Password != model.ConfirmPassword)
            {
                ModelState.AddModelError("User.Password", Resource.PasswordMismatch);
            }

            IEnumerable <int> userRoles = StringHelper.Ensure(Request.Form["SelectedRoles"])
                                          .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
                                          .Select(id => Convert.ToInt32(id));

            //IEnumerable<int> branches = StringHelper.Ensure(Request.Form["SelectedBranches"])
            //                                         .Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
            //                                         .Select(id => Convert.ToInt32(id));

            if (!ModelState.IsValid)
            {
                //return Edit(model.ID);
                var brands = _repoUnit.GetAllBranches().ToList();
                brands.Insert(0, new Branch {
                    ID = 0, Code = "-- " + Resource.SelectBranch + " --"
                });
                if (user.BranchID == null)
                {
                    user.BranchID = 0;
                }
                var oldUser = _membershipService.GetUser(model.ID);

                model.UserRoles    = _roleService.GetAllRoles().Where(m => userRoles.Contains(m.ID));
                model.UserBranches = oldUser.Branches.ToList();
                model.Roles        = _roleService.GetAllRoles();
                model.Branches     = brands;
                model.BranchID     = Convert.ToInt32(user.BranchID);
                return(View(model));
            }

            user = _membershipService.GetUser(model.ID);
            if (user.BranchID != null && user.BranchID != model.BranchID)
            {
                _repoUnit.AddToUserBranchList(model.ID, Convert.ToInt32(user.BranchID));
            }
            var oldBranches = user.Branches.Select(m => m.ID).ToList();

            if (user.BranchID != null && user.BranchID > 0 && user.BranchID != model.BranchID)
            {
                oldBranches.Add((int)user.BranchID);
                oldBranches = oldBranches.Distinct().ToList();
                _roleService.AssignBranches(user, oldBranches);
            }

            user.DisplayName = model.Username;
            user.Email       = model.Email;
            user.Phone       = model.Phone;
            user.MobilePhone = model.MobilePhone;
            if (!string.IsNullOrEmpty(model.Password))
            {
                user.Password = EncryptHelper.EncryptPassword(model.Password);
            }
            user.BranchID = model.BranchID == 0 ? null : (int?)model.BranchID;
            //user.LastAccess = model.LastAccess;
            var    success     = _membershipService.UpdateUser(user);
            string userPicture = UserPicture.Upload(model.ID, model.Picture);

            if (!string.IsNullOrEmpty(userPicture))
            {
                _membershipService.UpdateUserPicture(user.ID, userPicture);
            }

            _roleService.AssignRoles(user, userRoles);

            _loginTracker.ReloadUser(user.Email, user);
            if (success)
            {
                TempData["message"] = Resource.SaveSuccessful;
                return(RedirectToAction("Index"));
            }
            ViewBag.Success = true;
            ViewBag.Message = Resource.SaveFailed;
            return(RedirectToAction("Edit", new { Id = model.ID }));
        }