use of io.gravitee.am.gateway.handler.account.model.UpdateEnrolledFactor in project gravitee-access-management by gravitee-io.
the class AccountFactorsEndpointHandler method updateEnrolledFactor.
public void updateEnrolledFactor(RoutingContext routingContext) {
try {
if (routingContext.getBodyAsString() == null) {
routingContext.fail(new InvalidRequestException("Unable to parse body message"));
return;
}
final User user = routingContext.get(ConstantKeys.USER_CONTEXT_KEY);
final String factorId = routingContext.request().getParam("factorId");
final UpdateEnrolledFactor updateEnrolledFactor = Json.decodeValue(routingContext.getBodyAsString(), UpdateEnrolledFactor.class);
// find factor
findFactor(factorId, h -> {
if (h.failed()) {
routingContext.fail(h.cause());
return;
}
// get enrolled factor for the current user
Optional<EnrolledFactor> optionalEnrolledFactor = user.getFactors().stream().filter(enrolledFactor -> factorId.equals(enrolledFactor.getFactorId())).findFirst();
if (optionalEnrolledFactor.isEmpty()) {
routingContext.fail(new FactorNotFoundException(factorId));
return;
}
// update the factor
final EnrolledFactor enrolledFactor = optionalEnrolledFactor.get();
enrolledFactor.setPrimary(updateEnrolledFactor.isPrimary());
accountService.upsertFactor(user.getId(), enrolledFactor, new DefaultUser(user)).subscribe(__ -> AccountResponseHandler.handleDefaultResponse(routingContext, enrolledFactor), error -> routingContext.fail(error));
});
} catch (DecodeException ex) {
routingContext.fail(new InvalidRequestException("Unable to parse body message"));
}
}
Aggregations