realm name for url param

This commit is contained in:
Bill Burke 2014-01-14 13:03:13 -05:00
parent 4825974edc
commit 2dbd7cfa02
6 changed files with 47 additions and 32 deletions

View file

@ -24,15 +24,15 @@ module.controller('HomeCtrl', function(Realm, $location) {
Realm.query(null, function(realms) { Realm.query(null, function(realms) {
var realm; var realm;
if (realms.length == 1) { if (realms.length == 1) {
realm = realms[0].id; realm = realms[0].realm;
} else if (realms.length == 2) { } else if (realms.length == 2) {
if (realms[0].realm == 'Keycloak Administration') { if (realms[0].realm == 'Keycloak Administration') {
realm = realms[1].id; realm = realms[1].realm;
} else if (realms[1].realm == 'Keycloak Administration') { } else if (realms[1].realm == 'Keycloak Administration') {
realm = realms[0].id; realm = realms[0].realm;
} }
} }
console.log("****** HomeCtrl ******");
if (realm) { if (realm) {
$location.url('/realms/' + realm); $location.url('/realms/' + realm);
} else { } else {
@ -118,18 +118,16 @@ module.controller('RealmCreateCtrl', function($scope, Current, Realm, $upload, $
$scope.save = function() { $scope.save = function() {
var realmCopy = angular.copy($scope.realm); var realmCopy = angular.copy($scope.realm);
Realm.save(realmCopy, function(data, headers) { console.log('creating new realm **');
console.log('creating new realm'); Realm.create(realmCopy, function(data, headers) {
var l = headers().location;
var id = l.substring(l.lastIndexOf("/") + 1);
var data = Realm.query(function() { var data = Realm.query(function() {
Current.realms = data; Current.realms = data;
for (var i = 0; i < Current.realms.length; i++) { 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]; Current.realm = Current.realms[i];
} }
} }
$location.url("/realms/" + id); $location.url("/realms/" + realmCopy.realm);
Notifications.success("The realm has been created."); Notifications.success("The realm has been created.");
}); });
}); });
@ -156,17 +154,26 @@ module.controller('RealmDetailCtrl', function($scope, Current, Realm, realm, $ht
} else { } else {
if (Current.realm == null || Current.realm.realm != realm.realm) { if (Current.realm == null || Current.realm.realm != realm.realm) {
for (var i = 0; i < Current.realms.length; i++) { 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]; Current.realm = Current.realms[i];
break; 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) { if (Current.realm == null || Current.realm.realm != realm.realm) {
console.log('should be unreachable'); console.log('should be unreachable');
console.log('Why? ' + Current.realms.length + ' ' + Current.realm); console.log('Why? ' + Current.realms.length + ' ' + Current.realm);
return; return;
} }
*/
$scope.realm = angular.copy(realm); $scope.realm = angular.copy(realm);
$scope.realm.requireSsl = !realm.sslNotRequired; $scope.realm.requireSsl = !realm.sslNotRequired;
} }
@ -193,16 +200,14 @@ module.controller('RealmDetailCtrl', function($scope, Current, Realm, realm, $ht
if ($scope.createRealm) { if ($scope.createRealm) {
Realm.save(realmCopy, function(data, headers) { Realm.save(realmCopy, function(data, headers) {
console.log('creating new realm'); console.log('creating new realm');
var l = headers().location;
var id = l.substring(l.lastIndexOf("/") + 1);
var data = Realm.query(function() { var data = Realm.query(function() {
Current.realms = data; Current.realms = data;
for (var i = 0; i < Current.realms.length; i++) { 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]; Current.realm = Current.realms[i];
} }
} }
$location.url("/realms/" + id); $location.url("/realms/" + realmCopy.realm);
Notifications.success("The realm has been created."); Notifications.success("The realm has been created.");
$scope.social = $scope.realm.social; $scope.social = $scope.realm.social;
$scope.registrationAllowed = $scope.realm.registrationAllowed; $scope.registrationAllowed = $scope.realm.registrationAllowed;
@ -211,18 +216,18 @@ module.controller('RealmDetailCtrl', function($scope, Current, Realm, realm, $ht
} else { } else {
console.log('updating realm...'); console.log('updating realm...');
$scope.changed = false; $scope.changed = false;
Realm.update(realmCopy, function () { console.log('oldCopy.realm - ' + oldCopy.realm);
var id = realmCopy.id; Realm.update({ id : oldCopy.realm}, realmCopy, function () {
var data = Realm.query(function () { var data = Realm.query(function () {
Current.realms = data; Current.realms = data;
for (var i = 0; i < Current.realms.length; i++) { 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]; Current.realm = Current.realms[i];
oldCopy = angular.copy($scope.realm); oldCopy = angular.copy($scope.realm);
} }
} }
}); });
$location.url("/realms/" + id); $location.url("/realms/" + realmCopy.realm);
Notifications.success("Your changes have been saved to the realm."); Notifications.success("Your changes have been saved to the realm.");
$scope.social = $scope.realm.social; $scope.social = $scope.realm.social;
$scope.registrationAllowed = $scope.realm.registrationAllowed; $scope.registrationAllowed = $scope.realm.registrationAllowed;

View file

@ -107,12 +107,17 @@ module.factory('Notifications', function($rootScope, $timeout) {
module.factory('Realm', function($resource) { module.factory('Realm', function($resource) {
return $resource('/auth/rest/admin/realms/:id', { return $resource('/auth/rest/admin/realms/:id', {
id : '@id' id : '@realm'
}, { }, {
update : { update : {
method : 'PUT' method : 'PUT'
} },
}); create : {
method : 'POST',
params : { id : ''}
}
});
}); });
module.factory('User', function($resource) { module.factory('User', function($resource) {
@ -120,9 +125,9 @@ module.factory('User', function($resource) {
realm : '@realm', realm : '@realm',
userId : '@userId' userId : '@userId'
}, { }, {
update : { update : {
method : 'PUT' method : 'PUT'
} }
}); });
}); });
@ -321,7 +326,7 @@ module.factory('Current', function(Realm, $route) {
current.realms = Realm.query(null, function(realms) { current.realms = Realm.query(null, function(realms) {
if ($route.current.params.realm) { if ($route.current.params.realm) {
for (var i = 0; i < realms.length; i++) { 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]; current.realm = realms[i];
} }
} }

View file

@ -24,7 +24,7 @@
</tr> </tr>
</thead> </thead>
<tr data-ng-repeat="r in realms"> <tr data-ng-repeat="r in realms">
<td><a href="#/realms/{{r.id}}">{{r.realm}}</a></td> <td><a href="#/realms/{{r.realm}}">{{r.realm}}</a></td>
</tr> </tr>
</table> </table>
</div> </div>

View file

@ -63,7 +63,10 @@ public class JpaKeycloakSession implements KeycloakSession {
public RealmModel getRealmByName(String name) { public RealmModel getRealmByName(String name) {
TypedQuery<RealmEntity> query = em.createNamedQuery("getRealmByName", RealmEntity.class); TypedQuery<RealmEntity> query = em.createNamedQuery("getRealmByName", RealmEntity.class);
query.setParameter("name", name); query.setParameter("name", name);
RealmEntity realm = query.getSingleResult(); List<RealmEntity> 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; if (realm == null) return null;
return new RealmAdapter(em, realm); return new RealmAdapter(em, realm);
} }

View file

@ -65,7 +65,7 @@ public class RealmManager {
} }
public RealmModel getRealmByName(String name) { public RealmModel getRealmByName(String name) {
return identitySession.getRealm(name); return identitySession.getRealmByName(name);
} }
public RealmModel createRealm(String name) { public RealmModel createRealm(String name) {
@ -93,7 +93,10 @@ public class RealmManager {
} }
public void updateRealm(RealmRepresentation rep, RealmModel realm) { 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.isEnabled() != null) realm.setEnabled(rep.isEnabled());
if (rep.isSocial() != null) realm.setSocial(rep.isSocial()); if (rep.isSocial() != null) realm.setSocial(rep.isSocial());
if (rep.isCookieLoginAllowed() != null) realm.setCookieLoginAllowed(rep.isCookieLoginAllowed()); if (rep.isCookieLoginAllowed() != null) realm.setCookieLoginAllowed(rep.isCookieLoginAllowed());
@ -169,7 +172,6 @@ public class RealmManager {
} }
public RealmModel importRealm(RealmRepresentation rep, UserModel realmCreator) { public RealmModel importRealm(RealmRepresentation rep, UserModel realmCreator) {
//verifyRealmRepresentation(rep);
RealmModel realm = createRealm(rep.getRealm()); RealmModel realm = createRealm(rep.getRealm());
importRealm(rep, realm); importRealm(rep, realm);
return realm; return realm;

View file

@ -107,7 +107,7 @@ public class RealmsAdminResource {
@PathParam("realm") final String name) { @PathParam("realm") final String name) {
RealmManager realmManager = new RealmManager(session); RealmManager realmManager = new RealmManager(session);
RealmModel realm = realmManager.getRealmByName(name); 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); RealmAdminResource adminResource = new RealmAdminResource(admin, realm);
resourceContext.initResource(adminResource); resourceContext.initResource(adminResource);