KEYCLOAK-2136: Unexpected error when trying to create mapper with already existing name
This commit is contained in:
parent
426a2b46fc
commit
a99df49bc9
3 changed files with 17 additions and 5 deletions
|
@ -3,6 +3,7 @@ package org.keycloak.models.jpa;
|
||||||
import org.keycloak.connections.jpa.util.JpaUtils;
|
import org.keycloak.connections.jpa.util.JpaUtils;
|
||||||
import org.keycloak.models.ClientModel;
|
import org.keycloak.models.ClientModel;
|
||||||
import org.keycloak.models.KeycloakSession;
|
import org.keycloak.models.KeycloakSession;
|
||||||
|
import org.keycloak.models.ModelDuplicateException;
|
||||||
import org.keycloak.models.ProtocolMapperModel;
|
import org.keycloak.models.ProtocolMapperModel;
|
||||||
import org.keycloak.models.RealmModel;
|
import org.keycloak.models.RealmModel;
|
||||||
import org.keycloak.models.RoleContainerModel;
|
import org.keycloak.models.RoleContainerModel;
|
||||||
|
@ -328,7 +329,7 @@ public class ClientAdapter implements ClientModel {
|
||||||
@Override
|
@Override
|
||||||
public ProtocolMapperModel addProtocolMapper(ProtocolMapperModel model) {
|
public ProtocolMapperModel addProtocolMapper(ProtocolMapperModel model) {
|
||||||
if (getProtocolMapperByName(model.getProtocol(), model.getName()) != null) {
|
if (getProtocolMapperByName(model.getProtocol(), model.getName()) != null) {
|
||||||
throw new RuntimeException("protocol mapper name must be unique per protocol");
|
throw new ModelDuplicateException("Protocol mapper name must be unique per protocol");
|
||||||
}
|
}
|
||||||
String id = model.getId() != null ? model.getId() : KeycloakModelUtils.generateId();
|
String id = model.getId() != null ? model.getId() : KeycloakModelUtils.generateId();
|
||||||
ProtocolMapperEntity entity = new ProtocolMapperEntity();
|
ProtocolMapperEntity entity = new ProtocolMapperEntity();
|
||||||
|
|
|
@ -5,6 +5,7 @@ import com.mongodb.QueryBuilder;
|
||||||
import org.keycloak.connections.mongo.api.context.MongoStoreInvocationContext;
|
import org.keycloak.connections.mongo.api.context.MongoStoreInvocationContext;
|
||||||
import org.keycloak.models.ClientModel;
|
import org.keycloak.models.ClientModel;
|
||||||
import org.keycloak.models.KeycloakSession;
|
import org.keycloak.models.KeycloakSession;
|
||||||
|
import org.keycloak.models.ModelDuplicateException;
|
||||||
import org.keycloak.models.ProtocolMapperModel;
|
import org.keycloak.models.ProtocolMapperModel;
|
||||||
import org.keycloak.models.RealmModel;
|
import org.keycloak.models.RealmModel;
|
||||||
import org.keycloak.models.RoleModel;
|
import org.keycloak.models.RoleModel;
|
||||||
|
@ -342,7 +343,7 @@ public class ClientAdapter extends AbstractMongoAdapter<MongoClientEntity> imple
|
||||||
@Override
|
@Override
|
||||||
public ProtocolMapperModel addProtocolMapper(ProtocolMapperModel model) {
|
public ProtocolMapperModel addProtocolMapper(ProtocolMapperModel model) {
|
||||||
if (getProtocolMapperByName(model.getProtocol(), model.getName()) != null) {
|
if (getProtocolMapperByName(model.getProtocol(), model.getName()) != null) {
|
||||||
throw new RuntimeException("protocol mapper name must be unique per protocol");
|
throw new ModelDuplicateException("Protocol mapper name must be unique per protocol");
|
||||||
}
|
}
|
||||||
ProtocolMapperEntity entity = new ProtocolMapperEntity();
|
ProtocolMapperEntity entity = new ProtocolMapperEntity();
|
||||||
String id = model.getId() != null ? model.getId() : KeycloakModelUtils.generateId();
|
String id = model.getId() != null ? model.getId() : KeycloakModelUtils.generateId();
|
||||||
|
|
|
@ -6,10 +6,12 @@ import org.jboss.resteasy.spi.NotFoundException;
|
||||||
import org.keycloak.events.admin.OperationType;
|
import org.keycloak.events.admin.OperationType;
|
||||||
import org.keycloak.models.ClientModel;
|
import org.keycloak.models.ClientModel;
|
||||||
import org.keycloak.models.KeycloakSession;
|
import org.keycloak.models.KeycloakSession;
|
||||||
|
import org.keycloak.models.ModelDuplicateException;
|
||||||
import org.keycloak.models.ProtocolMapperModel;
|
import org.keycloak.models.ProtocolMapperModel;
|
||||||
import org.keycloak.models.utils.ModelToRepresentation;
|
import org.keycloak.models.utils.ModelToRepresentation;
|
||||||
import org.keycloak.models.utils.RepresentationToModel;
|
import org.keycloak.models.utils.RepresentationToModel;
|
||||||
import org.keycloak.representations.idm.ProtocolMapperRepresentation;
|
import org.keycloak.representations.idm.ProtocolMapperRepresentation;
|
||||||
|
import org.keycloak.services.ErrorResponse;
|
||||||
|
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.DELETE;
|
import javax.ws.rs.DELETE;
|
||||||
|
@ -86,9 +88,17 @@ public class ProtocolMappersResource {
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
public Response createMapper(ProtocolMapperRepresentation rep) {
|
public Response createMapper(ProtocolMapperRepresentation rep) {
|
||||||
auth.requireManage();
|
auth.requireManage();
|
||||||
ProtocolMapperModel model = RepresentationToModel.toModel(rep);
|
|
||||||
model = client.addProtocolMapper(model);
|
ProtocolMapperModel model = null;
|
||||||
adminEvent.operation(OperationType.CREATE).resourcePath(uriInfo, model.getId()).representation(rep).success();
|
try {
|
||||||
|
model = RepresentationToModel.toModel(rep);
|
||||||
|
model = client.addProtocolMapper(model);
|
||||||
|
adminEvent.operation(OperationType.CREATE).resourcePath(uriInfo, model.getId()).representation(rep).success();
|
||||||
|
|
||||||
|
} catch (ModelDuplicateException e) {
|
||||||
|
return ErrorResponse.exists("Protocol mapper exists with same name");
|
||||||
|
}
|
||||||
|
|
||||||
return Response.created(uriInfo.getAbsolutePathBuilder().path(model.getId()).build()).build();
|
return Response.created(uriInfo.getAbsolutePathBuilder().path(model.getId()).build()).build();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue