Пример #1
0
        public async Task<ActionResult> Edit(EditViewModel model)
        {
            try
            {
                if (_userManager.FindByName(model.UserName) != null)
                {

                    if (model.GroupVal != null)
                    {
                        var group = _repository.Find<Group>(Guid.Parse(model.GroupVal));

                        var userData = _userManager.FindById(model.Id);
                        TempData["firstname"] =userData.FirstName;
                        TempData["lastname"] = userData.LastName;
                        TempData["group"] = userData.GroupId;
                        TempData["country"] = userData.Country;
                        TempData["company"] = userData.Company;
                        TempData["alternatenumber"] = userData.AlternateNumber;
                        TempData["phnenumber"] = userData.PhoneNumber;
                        TempData["email"] = userData.Email;
                        TempData["id"] = userData.IdentificationNumber;




                        var userEdit = _userManager.FindById(model.Id);
                        userEdit.UserName = model.UserName.ToLower();
                        userEdit.PasswordHash = model.UserName.ToLower() + "1234";
                        String hashedNewPassword = _userManager.PasswordHasher.HashPassword(userEdit.PasswordHash);
                        userEdit.EditDateTime = DateTime.Now;
                        userEdit.Maker = User.Identity.GetUserName();
                        userEdit.FirstName = model.Firstname;
                        userEdit.LastName = model.Lastname;
                        userEdit.GroupId = group.Id;
                        userEdit.Country = model.Country;
                        userEdit.Company = model.Company;
                        userEdit.AlternateNumber = model.AlternateNumber;
                        userEdit.PhoneNumber = model.PhoneNumber;
                        userEdit.Email = model.Email;
                        userEdit.IdentificationNumber = model.IdentificationNumber;
                        userEdit.Status = MyEnums.StatusOptions.Edited;
                        
                        if (userEdit.Cheker == User.Identity.GetUserName())
                        {
                            userEdit.Cheker = "";
                        }
                        UserStore<ApplicationUser> store = new UserStore<ApplicationUser>();
                        await store.SetPasswordHashAsync(userEdit, hashedNewPassword);
                        await _userManager.UpdateAsync(userEdit);
                      
                        _getVals.LogAudit(User.Identity.GetUserName(), "Edited", Request.UserHostName,
                            "Edited User: "******"Old FirstName: " + TempData["firstname"] + " " + "New FirstName: " + model.Firstname + Environment.NewLine
                        + "Old LastName: " + TempData["lastname"] + " " + "New LastName: " + " " + model.Lastname + Environment.NewLine
                        + "Old GroupId: " + TempData["group"] + " " + "New GroupId: " + group.Id + Environment.NewLine
                        + "Old Country: " + TempData["country"] + " " + "New Country: " + model.Country + Environment.NewLine
                        + "Old Company: " + TempData["company"] + " " + "New Company: " + model.Company + Environment.NewLine
                        + "Old AlternateNumber: " + TempData["alternatenumber"] + " " + "New AlternateNumber: " + model.AlternateNumber + Environment.NewLine
                        + "Old PhoneNumber: " + TempData["phnenumber"] + " " + "New PhoneNumber: " + model.PhoneNumber + Environment.NewLine
                        + "Old Email: " + TempData["email"] + " " + "New Email: " + model.Email + Environment.NewLine
                        + "Old IdentificationNumber: " + TempData["id"] + " " + "New IdentificationNumber: " + model.IdentificationNumber + Environment.NewLine + " With Group: " + group.Name, "ISA", "UserManagement");
                    }
                    else
                    {

                        var userData = _userManager.FindById(model.Id);
                        TempData["firstname"] = userData.FirstName;
                        TempData["lastname"] = userData.LastName;
                        TempData["group"] = userData.GroupId;
                        TempData["country"] = userData.Country;
                        TempData["company"] = userData.Company;
                        TempData["alternatenumber"] = userData.AlternateNumber;
                        TempData["phnenumber"] = userData.PhoneNumber;
                        TempData["email"] = userData.Email;
                        TempData["id"] = userData.IdentificationNumber;



                        var userEdit = _userManager.FindById(model.Id);
                        userEdit.UserName = model.UserName.ToLower();
                        userEdit.PasswordHash = model.UserName.ToLower() + "1234";
                        String hashedNewPassword = _userManager.PasswordHasher.HashPassword(userEdit.PasswordHash);
                        userEdit.EditDateTime = DateTime.Now;
                        userEdit.Maker = User.Identity.GetUserName();
                        userEdit.FirstName = model.Firstname;
                        userEdit.LastName = model.Lastname;
                        userEdit.GroupId = userEdit.GroupId;
                        userEdit.Country = model.Country;
                        userEdit.Company = model.Company;
                        userEdit.AlternateNumber = model.AlternateNumber;
                        userEdit.PhoneNumber = model.PhoneNumber;
                        userEdit.Email = model.Email;
                        userEdit.IdentificationNumber = model.IdentificationNumber;
                        userEdit.Status = MyEnums.StatusOptions.Edited;
                        if (userEdit.Cheker == User.Identity.GetUserName())
                        {
                            userEdit.Cheker = "";
                        }
                        UserStore<ApplicationUser> store = new UserStore<ApplicationUser>();
                        await store.SetPasswordHashAsync(userEdit, hashedNewPassword);
                        await _userManager.UpdateAsync(userEdit);
                        
                        var group = _repository.Find<Group>(userEdit.GroupId);
                        _getVals.LogAudit(User.Identity.GetUserName(), "Edited", Request.UserHostName,
                             "Edited User: "******"Old FirstName: " + TempData["firstname"] + " " + "New FirstName: " + model.Firstname + Environment.NewLine
                         + "Old LastName: " + TempData["lastname"] + " " + "New LastName: " + " " + model.Lastname + Environment.NewLine
                         + "Old GroupId: " + TempData["group"] + " " + "New GroupId: " + group.Id + Environment.NewLine
                         + "Old Country: " + TempData["country"] + " " + "New Country: " + model.Country + Environment.NewLine
                         + "Old Company: " + TempData["company"] + " " + "New Company: " + model.Company + Environment.NewLine
                         + "Old AlternateNumber: " + TempData["alternatenumber"] + " " + "New AlternateNumber: " + model.AlternateNumber + Environment.NewLine
                         + "Old PhoneNumber: " + TempData["phnenumber"] + " " + "New PhoneNumber: " + model.PhoneNumber + Environment.NewLine
                         + "Old Email: " + TempData["email"] + " " + "New Email: " + model.Email + Environment.NewLine
                         + "Old IdentificationNumber: " + TempData["id"] + " " + "New IdentificationNumber: " + model.IdentificationNumber + Environment.NewLine + " With Group: " + group.Name, "ISA", "UserManagement");
                    }
                    return RedirectToAction("AccountIndex");
                }

                return RedirectToAction("AccountIndex");
            }
            catch (Exception ex)
            {
                _logs.LogError(User.Identity.GetUserName(), "EditUser", "Error: " + ex.Message,
                Request.ServerVariables["REMOTE_ADDR"], HttpContext.Server.MapPath("."), "ISA");
                System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/errorLOG.txt"), System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.ReadWrite);
                System.IO.StreamWriter s = new System.IO.StreamWriter(fs);
                s.BaseStream.Seek(0, System.IO.SeekOrigin.End);
                s.WriteLine("ERROR DATE: " + System.DateTime.Now.ToString(System.Globalization.CultureInfo.InvariantCulture) + " \nERROR MESSAGE: " + ex.Message + "\nSOURCE: " + ex.Source + "\nFORM NAME: " + System.Web.HttpContext.Current.Request.Url.ToString() + "\nQUERYSTRING: " + Request.QueryString.ToString() + "\nTARGETSITE: " + ex.TargetSite.ToString() + "\nSTACKTRACE: " + ex.StackTrace + System.Diagnostics.EventLogEntryType.Error);
                s.WriteLine("-------------------------------------------------------------------------------------------------------------");
                s.Close();
                return RedirectToAction("AccountIndex");
            }

        }
Пример #2
0
        public ActionResult Edit(string id)
        {
            //fill the roles 
            var listSelectListItems = new List<SelectListItem>();
            var groups = _repository.Fetch<Group>().Where(p => p.Status == MyEnums.StatusOptions.Approved);
            foreach (var n in groups)
            {
                var selectList = new SelectListItem()
                {
                    Text = n.Name,
                    Value = n.Id.ToString()
                };
                listSelectListItems.Add(selectList);
            }

            //Get the User to Edit
            var editUser = _userManager.FindById(id);
            var editViewModel = new EditViewModel();
            editViewModel.Groups = listSelectListItems;
            editViewModel.UserName = editUser.UserName;
            editViewModel.Firstname = editUser.FirstName;
            editViewModel.Lastname = editUser.LastName;
            editViewModel.Country = editUser.Country;
            editViewModel.Company = editUser.Company;
            editViewModel.Email = editUser.Email;
            editViewModel.PhoneNumber = editUser.PhoneNumber;
            editViewModel.AlternateNumber = editUser.AlternateNumber;
            editViewModel.IdentificationNumber = editUser.IdentificationNumber;
            editViewModel.Id = editUser.Id;
            var group = _repository.Find<Group>(editUser.GroupId);
            editViewModel.GroupVal = group.Name;
            return View(editViewModel);
        }