示例#1
0
        public ActionResult EditUserProfile(MyClinic.Infrastructure.UserEdit userEdit)
        {
            bool       checkError = true;
            UserModels viewModel  = null;
            var        objSession = Session["user"] as MyClinic.Infrastructure.SessUser;

            if (objSession == null)
            {
                return(HttpNotFound());
            }
            else
            {
                userEdit.Id = objSession.UserId;
            }
            User user      = userRepository.GetUserById(userEdit.Id);
            User checkUser = userRepository.GetUserByUsername(userEdit.UserName);

            try
            {
                if (String.IsNullOrEmpty(checkUser.UserName) == false)
                {
                    if (user.UserName != userEdit.UserName)
                    {
                        ModelState.AddModelError("UserName", "UserName Exist");
                    }
                }

                if (String.IsNullOrEmpty(userEdit.ConfirmPassword))
                {
                    userEdit.ConfirmPassword = String.Empty;
                }

                if (String.IsNullOrEmpty(userEdit.Password))
                {
                    userEdit.Password = String.Empty;
                }

                if (userEdit.ConfirmPassword.Trim() != userEdit.Password.Trim())
                {
                    ModelState.AddModelError("ConfirmPassword", "Confirm Password is not match.");
                }

                if (ModelState.IsValid)
                {
                    User oldUser       = userRepository.GetUserById(userEdit.Id);
                    var  newUser       = ObjectCopier.Copy <User>(oldUser);
                    var  savePathImage = Server.MapPath("~/Uploads/User/") + user.Id;
                    ImageHelper.SaveImage(savePathImage, user.Id + ".jpg", userEdit.ImageStream);
                    if (String.IsNullOrEmpty(userEdit.Password) == false)
                    {
                        newUser.Password = Common.EncryptString(userEdit.Password);
                    }
                    newUser.Name         = userEdit.Name;
                    newUser.UserName     = userEdit.UserName;
                    newUser.Email        = userEdit.Email;
                    newUser.Name         = userEdit.Name;
                    newUser.ModifiedBy   = objSession.UserId;
                    newUser.ModifiedDate = DateTime.Now;
                    string diffString = oldUser.EnumeratePropertyDifferencesInString(newUser);
                    objSession.Password = newUser.Password;
                    userRepository.UpdateFieldChangedOnly(oldUser, newUser);
                    /*For Add New Record to LogTable*/
                    int userId = objSession.UserId;
                    logTran.UserId      = userId;
                    logTran.ProcessType = "Update Profile";
                    logTran.Description = "Update Profile User " + user.Name + " value as follow: " + diffString;
                    logTran.LogDate     = DateTime.Now;
                    logRepository.Add(logTran);
                    checkError = false;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                ModelState.AddModelError(string.Empty, Translator.UnexpectedError);
            }

            if (checkError == true)
            {
                viewModel = new UserModels
                {
                    user       = user,
                    userEdit   = userEdit,
                    checkError = checkError,
                    checkPost  = true
                };
                return(View(viewModel));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
示例#2
0
        public ActionResult Edit(MyClinic.Infrastructure.UserEdit userEdit)
        {
            bool       checkError = true;
            UserModels viewModel  = null;
            User       user       = null;
            User       checkUser  = null;

            try
            {
                user      = userRepository.GetUserById(userEdit.Id);
                checkUser = userRepository.GetUserByUsername(userEdit.UserName);
                if (String.IsNullOrEmpty(checkUser.UserName) == false)
                {
                    if (user.UserName != userEdit.UserName)
                    {
                        ModelState.AddModelError("UserName", "UserName Exist");
                    }
                }

                if (String.IsNullOrEmpty(userEdit.ConfirmPassword))
                {
                    userEdit.ConfirmPassword = String.Empty;
                }

                if (String.IsNullOrEmpty(userEdit.Password))
                {
                    userEdit.Password = String.Empty;
                }

                if (userEdit.ConfirmPassword.Trim() != userEdit.Password.Trim())
                {
                    ModelState.AddModelError("ConfirmPassword", "The Confirm Password is not match.");
                }

                if (ModelState.IsValid)
                {
                    User userOld    = userRepository.GetUserById(userEdit.Id);
                    User userDiff   = Utilities.ObjectCopier.Copy <User>(userOld);
                    var  objSession = Session["user"] as MyClinic.Infrastructure.SessUser;
                    if (objSession.UserId == user.Id)
                    {
                        objSession.Password = Common.EncryptString(userEdit.Password);
                    }
                    var userType = "";
                    if (Request.Form["ADM"] != null)
                    {
                        userType = userType + Request.Form["ADM"] + ",";
                    }
                    if (Request.Form["SPU"] != null)
                    {
                        userType = userType + Request.Form["SPU"] + ",";
                    }
                    if (Request.Form["RPV"] != null)
                    {
                        userType = userType + Request.Form["RPV"] + ",";
                    }
                    if (Request.Form["USR"] != null)
                    {
                        userType = userType + Request.Form["USR"] + ",";
                    }
                    if (Request.Form["FUR"] != null)
                    {
                        userType = userType + Request.Form["FUR"] + ",";
                    }
                    if (userType == "")
                    {
                        userType = "USR";
                    }
                    else
                    {
                        userType = userType.Substring(0, userType.Count() - 1);
                    }
                    user.UserType = userType;
                    var savePathImage = Server.MapPath("~/Uploads/User/") + user.Id;
                    ImageHelper.SaveImage(savePathImage, user.Id + ".jpg", userEdit.ImageStream);

                    if (String.IsNullOrEmpty(userEdit.Password) == false)
                    {
                        user.Password = Common.EncryptString(userEdit.Password);
                    }
                    user.Name      = userEdit.Name;
                    user.UserName  = userEdit.UserName;
                    user.Email     = userEdit.Email;
                    user.IsActived = userEdit.IsActived;
                    user.Tel       = userEdit.Tel;
                    user.Image     = user.Id + ".jpg";
                    string diffString = userDiff.EnumeratePropertyDifferencesInString(user);
                    user.ModifiedBy   = objSession.UserId;
                    user.ModifiedDate = DateTime.Now;

                    userRepository.UpdateFieldChangedOnly(userOld, user);
                    /*For Add New Record to LogTable*/
                    int userId = objSession.UserId;
                    logTran.UserId      = userId;
                    logTran.ProcessType = "Edit User";
                    logTran.Description = "Edit User value as follow: " + diffString;
                    logTran.LogDate     = DateTime.Now;
                    logRepository.Add(logTran);
                    checkError = false;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                ModelState.AddModelError(string.Empty, Translator.UnexpectedError);
            }

            if (checkError == true)
            {
                viewModel = new UserModels
                {
                    user       = user,
                    userEdit   = userEdit,
                    checkError = checkError,
                    checkPost  = true
                };
                return(View(viewModel));
            }
            else
            {
                return(RedirectToAction("Index", "User"));
            }
        }