KEYCLOAK-2844 Unexpected error when trying to remove clientTemplate in use

This commit is contained in:
mposolda 2016-04-20 10:28:39 +02:00
parent e490a6e3bd
commit 919a3791ea
2 changed files with 46 additions and 4 deletions

View file

@ -22,6 +22,7 @@ import org.keycloak.events.admin.OperationType;
import org.keycloak.models.ClientTemplateModel; import org.keycloak.models.ClientTemplateModel;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import org.keycloak.models.ModelDuplicateException; import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.ModelException;
import org.keycloak.models.RealmModel; import org.keycloak.models.RealmModel;
import org.keycloak.models.utils.ModelToRepresentation; import org.keycloak.models.utils.ModelToRepresentation;
import org.keycloak.models.utils.RepresentationToModel; import org.keycloak.models.utils.RepresentationToModel;
@ -133,11 +134,16 @@ public class ClientTemplateResource {
*/ */
@DELETE @DELETE
@NoCache @NoCache
public void deleteClientTemplate() { public Response deleteClientTemplate() {
auth.requireManage(); auth.requireManage();
try {
realm.removeClientTemplate(template.getId()); realm.removeClientTemplate(template.getId());
adminEvent.operation(OperationType.DELETE).resourcePath(uriInfo).success(); adminEvent.operation(OperationType.DELETE).resourcePath(uriInfo).success();
return Response.noContent().build();
} catch (ModelException me) {
return ErrorResponse.error(me.getMessage(), Response.Status.BAD_REQUEST);
}
} }

View file

@ -23,6 +23,7 @@ import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.NotFoundException; import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
@ -35,6 +36,7 @@ import org.keycloak.models.AccountRoles;
import org.keycloak.models.Constants; import org.keycloak.models.Constants;
import org.keycloak.protocol.oidc.OIDCLoginProtocol; import org.keycloak.protocol.oidc.OIDCLoginProtocol;
import org.keycloak.protocol.saml.SamlProtocol; import org.keycloak.protocol.saml.SamlProtocol;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.ClientTemplateRepresentation; import org.keycloak.representations.idm.ClientTemplateRepresentation;
import org.keycloak.representations.idm.ErrorRepresentation; import org.keycloak.representations.idm.ErrorRepresentation;
import org.keycloak.representations.idm.MappingsRepresentation; import org.keycloak.representations.idm.MappingsRepresentation;
@ -270,6 +272,40 @@ public class ClientTemplateTest extends AbstractClientTest {
} }
// KEYCLOAK-2844
@Test
public void testRemoveTemplateInUse() {
// Add client template
ClientTemplateRepresentation templateRep = new ClientTemplateRepresentation();
templateRep.setName("foo-template");
templateRep.setFullScopeAllowed(false);
String templateId = createTemplate(templateRep);
// Add client with the clientTemplate
ClientRepresentation clientRep = new ClientRepresentation();
clientRep.setClientId("bar-client");
clientRep.setName("bar-client");
clientRep.setRootUrl("foo");
clientRep.setProtocol("openid-connect");
clientRep.setClientTemplate("foo-template");
String clientDbId = createClient(clientRep);
// Can't remove clientTemplate
try {
clientTemplates().get(templateId).remove();
} catch (BadRequestException bre) {
ErrorRepresentation error = bre.getResponse().readEntity(ErrorRepresentation.class);
Assert.assertEquals("Cannot remove client template, it is currently in use", error.getErrorMessage());
}
// Remove client
testRealmResource().clients().get(clientDbId).remove();
// Can remove clientTemplate now
clientTemplates().get(templateId).remove();
}
private ClientTemplatesResource clientTemplates() { private ClientTemplatesResource clientTemplates() {
return testRealmResource().clientTemplates(); return testRealmResource().clientTemplates();
} }