public UpdateUserResponse Update(UpdateUserRequest request) { crypto.HashIterations = 10; crypto.SaltSize = 12; var response = new UpdateUserResponse(); try { //var user = request.MapTo<User>(); var user = DataContext.Users.Include(u => u.Role).Include(r=>r.RolePrivileges).First(x => x.Id == request.Id).MapTo<User>(); user.Role = DataContext.RoleGroups.First(x => x.Id == request.RoleId); user.FullName = request.FullName; user.RolePrivileges.Clear(); if (request.RolePrivilegeIds.Count > 0) { foreach (var role in request.RolePrivilegeIds) { var rolePrivilege = DataContext.RolePrivileges.Find(role); user.RolePrivileges.Add(rolePrivilege); } } user.SignatureImage = request.SignatureImage; user.Position = request.Position; user.Username = request.Username; user.IsActive = request.IsActive; user.ChangeModel = request.ChangeModel; if (request.ChangePassword && request.Password != null) { user.PasswordSalt = crypto.GenerateSalt(crypto.HashIterations, crypto.SaltSize); user.Password = crypto.Compute(request.Password, user.PasswordSalt); } DataContext.Users.Attach(user); DataContext.Entry(user).State = EntityState.Modified; DataContext.SaveChanges(); response.IsSuccess = true; response.Message = "User item has been updated successfully"; } catch (DbUpdateException dbUpdateException) { response.Message = dbUpdateException.Message; } return response; }
public UpdateUserResponse Update(UpdateUserRequest request) { var response = new UpdateUserResponse(); try { var user = request.MapTo<User>(); user.Role = DataContext.RoleGroups.First(x => x.Id == request.RoleId); if (request.ChangePassword && request.Password != null) { user.Password = crypto.Compute(request.Password); user.PasswordSalt = crypto.Salt; } DataContext.Users.Attach(user); DataContext.Entry(user).State = EntityState.Modified; DataContext.SaveChanges(); response.IsSuccess = true; response.Message = "User item has been updated successfully"; } catch (DbUpdateException dbUpdateException) { response.Message = dbUpdateException.Message; } return response; }