public async Task UpdateProfileAsAdmin(UpdateProfileAsAdmin userParam) { var user = _context.Users.Find(userParam.Id); if (user == null) { throw new AppException("User not found"); } if (userParam.UserName != user.UserName) { // username has changed so check if the new username is already taken if (_context.Users.Any(x => x.UserName == userParam.UserName)) { throw new AppException("El usuario " + userParam.UserName + " ya existe en la db."); } } // update user properties user.Dni = userParam.Dni; user.UserName = userParam.UserName; user.Email = userParam.UserName; user.PhoneNumber = userParam.PhoneNumber; //actualizo los roles del usuario foreach (var role in userParam.RolesUser) { await UpdateUserRoleWhenModify(user.Id, role.Id, role.RolBelongUser); } // update password if it was entered if (!string.IsNullOrWhiteSpace(userParam.Password)) { user.PasswordHash = _userManager.PasswordHasher.HashPassword(user, userParam.Password); } _context.Users.Update(user); _context.SaveChanges(); }
public async Task UpdateProfileAsAdmin(UpdateProfileAsAdmin userParam) { var user = _context.Users.Find(userParam.Id); if (user == null) { throw new AppException("User not found"); } if (userParam.UserName != user.UserName) { // username has changed so check if the new username is already taken if (_context.Users.Any(x => x.UserName == userParam.UserName)) { throw new AppException("El usuario " + userParam.UserName + " ya existe en la db."); } } // update user properties user.Dni = userParam.Dni; user.UserName = userParam.UserName; user.Email = userParam.UserName; user.PhoneNumber = userParam.PhoneNumber; user.DistributionId = userParam.DistributionId; user.FirstName = userParam.FirstName; user.LastName = userParam.LastName; user.CategoryId = userParam.CategoryId; //actualizo los roles del usuario foreach (var role in userParam.RolesUser) { await UpdateUserRoleWhenModify(user.Id, role.Id, role.RolBelongUser); } // update password if it was entered if (!string.IsNullOrWhiteSpace(userParam.Password)) { user.PasswordHash = _userManager.PasswordHasher.HashPassword(user, userParam.Password); } var ministro = userParam.RolesUser.FirstOrDefault(x => x.Name == Role.Ministro && x.RolBelongUser == true); var admin = userParam.RolesUser.FirstOrDefault(x => x.Name == Role.Admin && x.RolBelongUser == true); var newrelationshipAgentsupervisor = new CreateSupervisorAgentDto(); var RelationshipTodelete = _context.SupervisorUserAgents.FirstOrDefault(sup => sup.AgentId == user.Id); newrelationshipAgentsupervisor.AgentId = user.Id; if (ministro == null && admin == null) { if (userParam.SupervisorAgentId.CompareTo(Guid.Empty) != 0) { newrelationshipAgentsupervisor.SupervisorId = userParam.SupervisorAgentId; } if (userParam.SupervisorAgentId2.CompareTo(Guid.Empty) != 0) { newrelationshipAgentsupervisor.SupervisorId2 = userParam.SupervisorAgentId2; } _supervisorUserAgentService.Create(new List <CreateSupervisorAgentDto>() { new CreateSupervisorAgentDto() { SupervisorId = newrelationshipAgentsupervisor.SupervisorId, SupervisorId2 = newrelationshipAgentsupervisor.SupervisorId2, AgentId = newrelationshipAgentsupervisor.AgentId } }); } //Elimino la relación actual entre supervisor y agente para crear una nueva if (RelationshipTodelete != null) { _context.SupervisorUserAgents.Remove(RelationshipTodelete); } _context.Users.Update(user); _context.SaveChanges(); }
public async Task <IActionResult> UpdateProfileAsAdmin([FromBody] UpdateProfileAsAdmin userDto) { await _userService.UpdateProfileAsAdmin(userDto); return(Ok()); }