public void SaveUser(peAppUser user) { using (var dbContext = new AccountDbContext()) { if (user.ID > 0) { dbContext.Update <peAppUser>(user); var roles = dbContext.Roles.Where(r => user.RoleIds.Contains(r.ID)).ToList(); user.Roles = roles; dbContext.SaveChanges(); } else { var existUser = dbContext.FindAll <peAppUser>(u => u.LoginName == user.LoginName); if (existUser.Count > 0) { throw new BusinessException("LoginName", "此登录名已存在!"); } else { dbContext.Insert <peAppUser>(user); var roles = dbContext.Roles.Where(r => user.RoleIds.Contains(r.ID)).ToList(); user.Roles = roles; dbContext.SaveChanges(); } } } }
// // GET: /Account/peAppUser/Create public ActionResult Create() { var roles = this.AccountService.GetRoleList(); this.ViewBag.RoleIds = new SelectList(roles, "ID", "Name"); var model = new peAppUser(); model.Password = "******"; return(View("Edit", model)); }
public void ModifyPwd(peAppUser user) { user.Password = Encrypt.MD5(user.Password); using (var dbContext = new AccountDbContext()) { if (dbContext.Users.Any(l => l.ID == user.ID && user.Password == l.Password)) { if (!string.IsNullOrEmpty(user.NewPassword)) { user.Password = Encrypt.MD5(user.NewPassword); } dbContext.Update <peAppUser>(user); } else { throw new BusinessException("Password", "原密码不正确!"); } } }
public ActionResult Create(FormCollection collection) { var model = new peAppUser(); this.TryUpdateModel <peAppUser>(model); model.Password = "******"; model.Password = Encrypt.MD5(model.Password); try { this.AccountService.SaveUser(model); } catch (BusinessException e) { this.ModelState.AddModelError(e.Name, e.Message); var roles = this.AccountService.GetRoleList(); this.ViewBag.RoleIds = new SelectList(roles, "ID", "Name"); return(View("Edit", model)); } return(this.RefreshParent()); }