diff --git a/admin-ui/src/main/resources/META-INF/resources/admin/js/controllers/realm.js b/admin-ui/src/main/resources/META-INF/resources/admin/js/controllers/realm.js index 6a97145d13..d006c718cc 100755 --- a/admin-ui/src/main/resources/META-INF/resources/admin/js/controllers/realm.js +++ b/admin-ui/src/main/resources/META-INF/resources/admin/js/controllers/realm.js @@ -24,15 +24,15 @@ module.controller('HomeCtrl', function(Realm, $location) { Realm.query(null, function(realms) { var realm; if (realms.length == 1) { - realm = realms[0].id; + realm = realms[0].realm; } else if (realms.length == 2) { if (realms[0].realm == 'Keycloak Administration') { - realm = realms[1].id; + realm = realms[1].realm; } else if (realms[1].realm == 'Keycloak Administration') { - realm = realms[0].id; + realm = realms[0].realm; } } - + console.log("****** HomeCtrl ******"); if (realm) { $location.url('/realms/' + realm); } else { @@ -118,18 +118,16 @@ module.controller('RealmCreateCtrl', function($scope, Current, Realm, $upload, $ $scope.save = function() { var realmCopy = angular.copy($scope.realm); - Realm.save(realmCopy, function(data, headers) { - console.log('creating new realm'); - var l = headers().location; - var id = l.substring(l.lastIndexOf("/") + 1); + console.log('creating new realm **'); + Realm.create(realmCopy, function(data, headers) { var data = Realm.query(function() { Current.realms = data; for (var i = 0; i < Current.realms.length; i++) { - if (Current.realms[i].id == id) { + if (Current.realms[i].realm == realmCopy.realm) { Current.realm = Current.realms[i]; } } - $location.url("/realms/" + id); + $location.url("/realms/" + realmCopy.realm); Notifications.success("The realm has been created."); }); }); @@ -156,17 +154,26 @@ module.controller('RealmDetailCtrl', function($scope, Current, Realm, realm, $ht } else { if (Current.realm == null || Current.realm.realm != realm.realm) { for (var i = 0; i < Current.realms.length; i++) { - if (realm.realm == Current.realms[i].id) { + if (realm.realm == Current.realms[i].realm) { Current.realm = Current.realms[i]; break; } } } + console.log('realm name: ' + realm.realm); + for (var i = 0; i < Current.realms.length; i++) { + console.log('checking Current.realm:' + Current.realms[i].realm); + if (Current.realms[i].realm == realm.realm) { + Current.realm = Current.realms[i]; + } + } + /* if (Current.realm == null || Current.realm.realm != realm.realm) { console.log('should be unreachable'); console.log('Why? ' + Current.realms.length + ' ' + Current.realm); return; } + */ $scope.realm = angular.copy(realm); $scope.realm.requireSsl = !realm.sslNotRequired; } @@ -193,16 +200,14 @@ module.controller('RealmDetailCtrl', function($scope, Current, Realm, realm, $ht if ($scope.createRealm) { Realm.save(realmCopy, function(data, headers) { console.log('creating new realm'); - var l = headers().location; - var id = l.substring(l.lastIndexOf("/") + 1); var data = Realm.query(function() { Current.realms = data; for (var i = 0; i < Current.realms.length; i++) { - if (Current.realms[i].id == id) { + if (Current.realms[i].realm == realmCopy.realm) { Current.realm = Current.realms[i]; } } - $location.url("/realms/" + id); + $location.url("/realms/" + realmCopy.realm); Notifications.success("The realm has been created."); $scope.social = $scope.realm.social; $scope.registrationAllowed = $scope.realm.registrationAllowed; @@ -211,18 +216,18 @@ module.controller('RealmDetailCtrl', function($scope, Current, Realm, realm, $ht } else { console.log('updating realm...'); $scope.changed = false; - Realm.update(realmCopy, function () { - var id = realmCopy.id; + console.log('oldCopy.realm - ' + oldCopy.realm); + Realm.update({ id : oldCopy.realm}, realmCopy, function () { var data = Realm.query(function () { Current.realms = data; for (var i = 0; i < Current.realms.length; i++) { - if (Current.realms[i].id == id) { + if (Current.realms[i].realm == realmCopy.realm) { Current.realm = Current.realms[i]; oldCopy = angular.copy($scope.realm); } } }); - $location.url("/realms/" + id); + $location.url("/realms/" + realmCopy.realm); Notifications.success("Your changes have been saved to the realm."); $scope.social = $scope.realm.social; $scope.registrationAllowed = $scope.realm.registrationAllowed; diff --git a/admin-ui/src/main/resources/META-INF/resources/admin/js/services.js b/admin-ui/src/main/resources/META-INF/resources/admin/js/services.js index 5f1b3d88e6..1b661f8d88 100755 --- a/admin-ui/src/main/resources/META-INF/resources/admin/js/services.js +++ b/admin-ui/src/main/resources/META-INF/resources/admin/js/services.js @@ -107,12 +107,17 @@ module.factory('Notifications', function($rootScope, $timeout) { module.factory('Realm', function($resource) { return $resource('/auth/rest/admin/realms/:id', { - id : '@id' + id : '@realm' }, { update : { method : 'PUT' - } - }); + }, + create : { + method : 'POST', + params : { id : ''} + } + + }); }); module.factory('User', function($resource) { @@ -120,9 +125,9 @@ module.factory('User', function($resource) { realm : '@realm', userId : '@userId' }, { - update : { - method : 'PUT' - } + update : { + method : 'PUT' + } }); }); @@ -321,7 +326,7 @@ module.factory('Current', function(Realm, $route) { current.realms = Realm.query(null, function(realms) { if ($route.current.params.realm) { for (var i = 0; i < realms.length; i++) { - if (realms[i].id == $route.current.params.realm) { + if (realms[i].realm == $route.current.params.realm) { current.realm = realms[i]; } } diff --git a/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-list.html b/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-list.html index decb41892e..969046206b 100755 --- a/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-list.html +++ b/admin-ui/src/main/resources/META-INF/resources/admin/partials/realm-list.html @@ -24,7 +24,7 @@ - {{r.realm}} + {{r.realm}} diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaKeycloakSession.java b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaKeycloakSession.java index 2bc62e2795..7b7db28237 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaKeycloakSession.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaKeycloakSession.java @@ -63,7 +63,10 @@ public class JpaKeycloakSession implements KeycloakSession { public RealmModel getRealmByName(String name) { TypedQuery query = em.createNamedQuery("getRealmByName", RealmEntity.class); query.setParameter("name", name); - RealmEntity realm = query.getSingleResult(); + List entities = query.getResultList(); + if (entities.size() == 0) return null; + if (entities.size() > 1) throw new IllegalStateException("Should not be more than one realm with same name"); + RealmEntity realm = query.getResultList().get(0); if (realm == null) return null; return new RealmAdapter(em, realm); } diff --git a/services/src/main/java/org/keycloak/services/managers/RealmManager.java b/services/src/main/java/org/keycloak/services/managers/RealmManager.java index 957c559e2d..9aea0b87a5 100755 --- a/services/src/main/java/org/keycloak/services/managers/RealmManager.java +++ b/services/src/main/java/org/keycloak/services/managers/RealmManager.java @@ -65,7 +65,7 @@ public class RealmManager { } public RealmModel getRealmByName(String name) { - return identitySession.getRealm(name); + return identitySession.getRealmByName(name); } public RealmModel createRealm(String name) { @@ -93,7 +93,10 @@ public class RealmManager { } public void updateRealm(RealmRepresentation rep, RealmModel realm) { - if (rep.getRealm() != null) realm.setName(rep.getRealm()); + if (rep.getRealm() != null) { + logger.info("Updating realm name to " + rep.getRealm()); + realm.setName(rep.getRealm()); + } if (rep.isEnabled() != null) realm.setEnabled(rep.isEnabled()); if (rep.isSocial() != null) realm.setSocial(rep.isSocial()); if (rep.isCookieLoginAllowed() != null) realm.setCookieLoginAllowed(rep.isCookieLoginAllowed()); @@ -169,7 +172,6 @@ public class RealmManager { } public RealmModel importRealm(RealmRepresentation rep, UserModel realmCreator) { - //verifyRealmRepresentation(rep); RealmModel realm = createRealm(rep.getRealm()); importRealm(rep, realm); return realm; diff --git a/services/src/main/java/org/keycloak/services/resources/admin/RealmsAdminResource.java b/services/src/main/java/org/keycloak/services/resources/admin/RealmsAdminResource.java index c435390539..97883cb2d9 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/RealmsAdminResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/RealmsAdminResource.java @@ -107,7 +107,7 @@ public class RealmsAdminResource { @PathParam("realm") final String name) { RealmManager realmManager = new RealmManager(session); RealmModel realm = realmManager.getRealmByName(name); - if (realm == null) throw new NotFoundException(); + if (realm == null) throw new NotFoundException("{realm} = " + name); RealmAdminResource adminResource = new RealmAdminResource(admin, realm); resourceContext.initResource(adminResource);