public IActionResult Create(UserCreateViewModel model) { ResponseModel response = ResponseModelFactory.CreateInstance; if (model.LoginName.Trim().Length <= 0) { response.SetFailed("请输入登录名称"); return(Ok(response)); } if (_dbContext.DncUser.Count(x => x.LoginName == model.LoginName) > 0) { response.SetFailed("登录名已存在"); return(Ok(response)); } DncUser entity = _mapper.Map <UserCreateViewModel, DncUser>(model); entity.CreatedOn = DateTime.Now; entity.Guid = Guid.NewGuid(); entity.Status = model.Status; _dbContext.DncUser.Add(entity); _dbContext.SaveChanges(); response.SetSuccess(); response.SetData(entity.Guid); return(Ok(response)); }
public IActionResult SaveRoles(SaveUserRolesViewModel model) { ResponseModel response = ResponseModelFactory.CreateInstance; System.Collections.Generic.List <DncUserRoleMapping> roles = model.AssignedRoles.Select(x => new DncUserRoleMapping { UserGuid = model.UserGuid, CreatedOn = DateTime.Now, RoleCode = x.Trim() }).ToList(); _dbContext.Database.ExecuteSqlCommand("DELETE FROM DncUserRoleMapping WHERE UserGuid={0}", model.UserGuid); bool success = true; if (roles.Count > 0) { _dbContext.DncUserRoleMapping.AddRange(roles); success = _dbContext.SaveChanges() > 0; } if (success) { response.SetSuccess(); } else { response.SetFailed("保存用户角色数据失败"); } return(Ok(response)); }
public IActionResult ExposeChangePassword([FromBody] ChangePasswordViewModel changePassword) { ResponseModel response = ResponseModelFactory.CreateInstance; DncUser user = _dbContext.DncUser.First(x => x.Guid == AuthContextService.CurrentUser.Guid); string[] passwordSplit = user.Password.Split("$"); if (passwordSplit[1] != PasswordCalculator.SaltPassword(passwordSplit[0], changePassword.OldPasswordMD5)) { response.SetFailed("旧密码错误"); return(Ok(response)); } else { _dbContext.DncUser.First(x => x.Guid == AuthContextService.CurrentUser.Guid).Password = user.Password = $"{user.LoginName}${PasswordCalculator.SaltPassword(user.LoginName, changePassword.NewPasswordMD5)}"; _dbContext.SaveChanges(); response.SetSuccess(); return(Ok(response)); } }