KEYCLOAK-1110 Fix role not removed from default roles when not deleted

This commit is contained in:
Stian Thorgersen 2015-03-20 06:05:01 +01:00
parent 1d4e8118f0
commit 93f9706297
4 changed files with 62 additions and 2 deletions

View file

@ -3,6 +3,7 @@ package org.keycloak.admin.client.resource;
import org.keycloak.representations.idm.RoleRepresentation;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
@ -27,5 +28,8 @@ public interface RolesResource {
@Path("{roleName}")
public RoleResource get(@PathParam("roleName") String roleName);
@Path("{role-name}")
@DELETE
public void deleteRole(final @PathParam("role-name") String roleName);
}

View file

@ -1000,8 +1000,8 @@ public class RealmAdapter implements RealmModel {
if (!role.getContainer().equals(this)) return false;
session.users().preRemove(this, role);
RoleEntity roleEntity = RoleAdapter.toRoleEntity(role, em);
realm.getRoles().remove(role);
realm.getDefaultRoles().remove(role);
realm.getRoles().remove(roleEntity);
realm.getDefaultRoles().remove(roleEntity);
em.createNativeQuery("delete from COMPOSITE_ROLE where CHILD_ROLE = :role").setParameter("role", roleEntity).executeUpdate();
em.createNamedQuery("deleteScopeMappingByRole").setParameter("role", roleEntity).executeUpdate();

View file

@ -6,6 +6,8 @@ import org.keycloak.admin.client.resource.ApplicationResource;
import org.keycloak.models.ApplicationModel;
import org.keycloak.models.RealmModel;
import org.keycloak.representations.idm.ApplicationRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.UserSessionRepresentation;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.testsuite.OAuthClient;
@ -14,11 +16,16 @@ import org.keycloak.testsuite.rule.WebResource;
import org.keycloak.testsuite.rule.WebRule;
import org.openqa.selenium.WebDriver;
import javax.ws.rs.NotFoundException;
import java.util.LinkedList;
import java.util.List;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
@ -84,4 +91,26 @@ public class ApplicationTest extends AbstractClientTest {
assertEquals(1, userSessions.get(0).getApplications().size());
}
@Test
// KEYCLOAK-1110
public void deleteDefaultRole() {
ApplicationRepresentation rep = new ApplicationRepresentation();
rep.setName("my-app");
rep.setEnabled(true);
realm.applications().create(rep);
RoleRepresentation role = new RoleRepresentation("test", "test");
realm.applications().get("my-app").roles().create(role);
rep = realm.applications().get("my-app").toRepresentation();
rep.setDefaultRoles(new String[] { "test" });
realm.applications().get("my-app").update(rep);
assertArrayEquals(new String[] { "test" }, realm.applications().get("my-app").toRepresentation().getDefaultRoles());
realm.applications().get("my-app").roles().deleteRole("test");
assertNull(realm.applications().get("my-app").toRepresentation().getDefaultRoles());
}
}

View file

@ -4,14 +4,18 @@ import org.junit.Test;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.services.managers.RealmManager;
import javax.ws.rs.NotFoundException;
import java.util.LinkedList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
@ -101,4 +105,27 @@ public class RealmTest extends AbstractClientTest {
assertNotNull(rep.getCertificate());
}
@Test
// KEYCLOAK-1110
public void deleteDefaultRole() {
RoleRepresentation role = new RoleRepresentation("test", "test");
realm.roles().create(role);
assertNotNull(realm.roles().get("test").toRepresentation());
RealmRepresentation rep = realm.toRepresentation();
rep.setDefaultRoles(new LinkedList<String>());
rep.getDefaultRoles().add("test");
realm.update(rep);
realm.roles().deleteRole("test");
try {
realm.roles().get("testsadfsadf").toRepresentation();
fail("Expected NotFoundException");
} catch (NotFoundException e) {
}
}
}