public ActionResult Edit(int id) { var model = new UserViewModel(); if (id != -1) { var svc = new UserAppService(); var o = svc.GetUser(id); model.UserId = o.UserId; model.UserName = o.UserName; model.LastLoginDateText = CommonHelper.GetPrettyDate(o.LastLoginDate); model.HasChangePassword = o.HasChangePassword; model.UserStateId = o.UserStateId; model.UserStates = GetUserStates(); model.UserRoles = o.UserRoles.ToList(); } else { model.UserId = -1; model.UserName = string.Empty; model.LastLoginDateText = CommonHelper.GetPrettyDate(null); model.HasChangePassword = true; model.UserStates = GetUserStates(); } return View(model); }
public DataTablesResult<UserViewModel> GetAllUsers(DataTablesParam dataTableParam) { var roleList = CommonHelper.GetRoles(); var svc = new UserAppService(); var users = svc.GetAllUser(); var usersVm = new List<UserViewModel>(); foreach (var itm in users) { var itmVm = new UserViewModel { UserName = itm.UserName, UserStateName = itm.UserState.Description, UserId = itm.UserId, }; var roleNames=" ";//2 foreach (var rol in itm.UserRoles) { var r1 = rol; var r = roleList.SingleOrDefault(l => l.RoleId == r1.RoleId); Debug.Assert(r != null, "r != null"); roleNames += r.Name + ", "; } itmVm.RoleNames = roleNames.Remove(roleNames.Length - 2); //2 var employee = itm.Employees.SingleOrDefault(); if (employee != null) { itmVm.EmployeeName = employee.FullName; } else { itmVm.EmployeeName = "No Asignado"; } var lld = itm.LastLoginDate; itmVm.LastLoginDateText = CommonHelper.GetPrettyDate(lld); var sb = new StringBuilder(); string editUrl = Url.Action("Edit", "User"); sb.AppendLine("<div class=\"btn-group\">"); sb.AppendLine( "<button type=\"button\" class=\"btn btn-default dropdown-toggle\" data-toggle=\"dropdown\" aria-expanded=\"false\">"); sb.AppendLine("Acciones <span class=\"caret\"></span>"); sb.AppendLine("</button>"); sb.AppendLine("<ul class=\"dropdown-menu\" role=\"menu\">"); sb.AppendLine("<li><a href=\"" + editUrl + "?id=-1\"><i class=\"fa fa-plus\"></i> Nuevo Usuario</a></li>"); sb.AppendLine("<li><a href=\"" + editUrl + "?id=" + itmVm.UserId + "\"><i class=\"fa fa-edit\"></i> Editar " + itmVm.UserName + "</a></li>"); sb.AppendLine("</ul>"); sb.AppendLine("</div>"); var actionButton = sb.ToString(); itmVm.ActionButton = actionButton; usersVm.Add(itmVm); } var usersVmQueryable = usersVm.AsQueryable(); return DataTablesResult.Create(usersVmQueryable, dataTableParam); }
public ActionResult Edit(UserViewModel model) { model.UserStates = GetUserStates(); //Reload List For Clean Model try { var svc = new UserAppService(); var o = new User { UserName = model.UserName, UserStateId = model.UserStateId, HasChangePassword = model.HasChangePassword }; if (model.UserId == -1) { string salt; var hashedPassword = WebHelper.EncryptPassword(model.Password, out salt); o.Password = hashedPassword; o.Salt = salt; } else { if (String.IsNullOrEmpty(model.Password)) { var ousr = new UserAppService().GetUser(model.UserId); o.Password = ousr.Password; o.Salt = ousr.Salt; } else { //GetCurrentData string salt; var hashedPassword = WebHelper.EncryptPassword(model.Password, out salt); o.Password = hashedPassword; o.Salt = salt; } } if (model.UserId == -1) { if(CommonHelper.ExistUserName(model.UserName)) { ViewBag.Feed = 2; return View(model); } o.LastLoginDate = DateTime.Now; o.AccessCount = 0; o.TryAccessCount = 0; var userId= svc.AddUser(o); var svcUserRole = new UserRoleAppService(); var roles = model.AssignedUserRole.Split(','); foreach (var r in roles) { svcUserRole.AddUserRole(new UserRole { RoleId = Convert.ToInt32(r), UserId = userId }); } ViewBag.Feed = 0; } else { o.UserId = model.UserId; o.LastLoginDate = DateTime.Now; if (model.IsDeleteAction == 0) { svc.SaveUser(o); var svcUserRole = new UserRoleAppService(); svcUserRole.RemoveUserRoleByUserId(o.UserId); var roles = model.AssignedUserRole.Split(','); foreach (var r in roles) { svcUserRole.AddUserRole(new UserRole { RoleId = Convert.ToInt32(r), UserId = o.UserId }); } var eusr = new UserAppService().GetUser(model.UserId); model.UserRoles = eusr.UserRoles.ToList(); } else { var svcUserRole = new UserRoleAppService(); svcUserRole.RemoveUserRoleByUserId(o.UserId); svc.RemoveUser(model.UserId); model.UserRoles = new List<UserRole>(); } ViewBag.Feed = 0; } } catch (Exception) { ViewBag.Feed = 1; } return View(model); }