Delete realm

This commit is contained in:
Stian Thorgersen 2013-11-25 15:38:25 +00:00
parent 5210f674c0
commit 52c7ae057a
7 changed files with 68 additions and 26 deletions

View file

@ -137,7 +137,7 @@ module.controller('RealmDetailCtrl', function($scope, Current, Realm, realm, $ht
$scope.remove = function() {
Dialog.confirmDelete($scope.realm.realm, 'realm', function() {
Realm.remove($scope.realm, function() {
Realm.remove({ id : $scope.realm.id }, function() {
Current.realms = Realm.get();
$location.url("/realms");
Notifications.success("The realm has been deleted.");

View file

@ -13,7 +13,7 @@ public interface KeycloakSession {
RealmModel createRealm(String id, String name);
RealmModel getRealm(String id);
List<RealmModel> getRealms(UserModel admin);
void deleteRealm(RealmModel realm);
boolean removeRealm(String id);
void close();
}

View file

@ -1,15 +1,13 @@
package org.keycloak.models.jpa;
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.KeycloakTransaction;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.jpa.entities.RealmEntity;
import org.keycloak.models.*;
import org.keycloak.models.jpa.entities.*;
import org.keycloak.models.utils.KeycloakSessionUtils;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
@ -62,8 +60,26 @@ public class JpaKeycloakSession implements KeycloakSession {
}
@Override
public void deleteRealm(RealmModel realm) {
throw new RuntimeException("Not Implemented Yet");
public boolean removeRealm(String id) {
RealmEntity realm = em.find(RealmEntity.class, id);
if (realm == null) {
return false;
}
RealmAdapter adapter = new RealmAdapter(em, realm);
for (ApplicationEntity a : new LinkedList<ApplicationEntity>(realm.getApplications())) {
adapter.removeApplication(a.getId());
}
for (UserEntity u : em.createQuery("from UserEntity", UserEntity.class).getResultList()) {
adapter.removeUser(u.getLoginName());
}
em.createQuery("delete from " + OAuthClientEntity.class.getSimpleName() + " where realm = :realm").setParameter("realm", realm).executeUpdate();
em.remove(realm);
return true;
}
@Override

View file

@ -95,9 +95,13 @@ public class PicketlinkKeycloakSession implements KeycloakSession {
}
@Override
public void deleteRealm(RealmModel realm) {
throw new RuntimeException("Not Implemented Yet");
public boolean removeRealm(String id) {
RealmData partition = partitionManager.getPartition(RealmData.class, id);
if (partition == null) {
return false;
}
partitionManager.remove(partition);
return true;
}
@Override

View file

@ -8,11 +8,7 @@ import org.keycloak.models.UserModel;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.services.managers.RealmManager;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.*;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.Context;
@ -65,6 +61,12 @@ public class RealmAdminResource extends RoleContainerResource {
new RealmManager(session).updateRealm(rep, realm);
}
@DELETE
public void deleteRealms() {
if (!session.removeRealm(realm.getId())) {
throw new NotFoundException();
}
}
@Path("users")
public UsersResource users() {

View file

@ -9,13 +9,7 @@ import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.services.resources.SaasService;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.*;
import javax.ws.rs.container.ResourceContext;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context;
@ -94,6 +88,4 @@ public class RealmsAdminResource {
resourceContext.initResource(adminResource);
return adminResource;
}
}

View file

@ -228,6 +228,34 @@ public class AdapterTest extends AbstractKeycloakTest {
}
@Test
public void testRemoveRealm() throws Exception {
test1CreateRealm();
UserModel user = realmModel.addUser("bburke");
UserCredentialModel cred = new UserCredentialModel();
cred.setType(CredentialRepresentation.PASSWORD);
cred.setValue("password");
realmModel.updateCredential(user, cred);
OAuthClientModel client = realmModel.addOAuthClient("client");
ApplicationModel app = realmModel.addApplication("test-app");
RoleModel appRole = app.addRole("test");
app.grantRole(user, appRole);
app.addScopeMapping(client.getOAuthAgent(), appRole);
RoleModel realmRole = realmModel.addRole("test");
realmModel.addScopeMapping(app.getApplicationUser(), realmRole);
Assert.assertTrue(identitySession.removeRealm(realmModel.getId()));
Assert.assertFalse(identitySession.removeRealm(realmModel.getId()));
Assert.assertNull(identitySession.getRealm(realmModel.getId()));
}
@Test
public void testRemoveRole() throws Exception {
test1CreateRealm();