remove RealmModel.getClientNameMap()

This commit is contained in:
Bill Burke 2016-02-17 16:05:25 -05:00
parent 3143a2e500
commit 20348e5d0b
15 changed files with 36 additions and 82 deletions

View file

@ -587,20 +587,6 @@ public class RealmAdapter implements RealmModel {
updated.updateDefaultRoles(defaultRoles);
}
@Override
public Map<String, ClientModel> getClientNameMap() {
if (updated != null) return updated.getClientNameMap();
Map<String, ClientModel> map = new HashMap<String, ClientModel>();
for (String id : cached.getClients().values()) {
ClientModel model = cacheSession.getClientById(id, this);
if (model == null) {
throw new IllegalStateException("Cached application not found: " + id);
}
map.put(model.getClientId(), model);
}
return Collections.unmodifiableMap(map);
}
@Override
public List<ClientModel> getClients() {
if (updated != null) return updated.getClients();

View file

@ -721,17 +721,6 @@ public class RealmAdapter implements RealmModel {
}
@Override
public Map<String, ClientModel> getClientNameMap() {
List<ClientModel> clients = getClients();
if (clients.isEmpty()) return Collections.EMPTY_MAP;
Map<String, ClientModel> map = new HashMap<String, ClientModel>();
for (ClientModel app : clients) {
map.put(app.getClientId(), app);
}
return Collections.unmodifiableMap(map);
}
@Override
public List<ClientModel> getClients() {
TypedQuery<ClientEntity> query = em.createNamedQuery("getClientsByRealm", ClientEntity.class);

View file

@ -813,17 +813,6 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
return session.realms().getClientByClientId(clientId, this);
}
@Override
public Map<String, ClientModel> getClientNameMap() {
List<ClientModel> clients = getClients();
if (clients.isEmpty()) return Collections.EMPTY_MAP;
Map<String, ClientModel> resourceMap = new HashMap<String, ClientModel>();
for (ClientModel resource : clients) {
resourceMap.put(resource.getClientId(), resource);
}
return Collections.unmodifiableMap(resourceMap);
}
@Override
public List<ClientModel> getClients() {
DBObject query = new QueryBuilder()

View file

@ -36,7 +36,7 @@ public class MigrationTo1_2_0_CR1 {
public static final ModelVersion VERSION = new ModelVersion("1.2.0.CR1");
public void setupBrokerService(RealmModel realm) {
ClientModel client = realm.getClientNameMap().get(Constants.BROKER_SERVICE_CLIENT_ID);
ClientModel client = realm.getClientByClientId(Constants.BROKER_SERVICE_CLIENT_ID);
if (client == null) {
client = KeycloakModelUtils.createClient(realm, Constants.BROKER_SERVICE_CLIENT_ID);
client.setEnabled(true);
@ -52,16 +52,13 @@ public class MigrationTo1_2_0_CR1 {
}
private void setupClientNames(RealmModel realm) {
Map<String, ClientModel> clients = realm.getClientNameMap();
setupClientName(clients, Constants.ACCOUNT_MANAGEMENT_CLIENT_ID);
setupClientName(clients, Constants.ADMIN_CONSOLE_CLIENT_ID);
setupClientName(clients, Constants.REALM_MANAGEMENT_CLIENT_ID);
setupClientName(realm.getClientByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID));
setupClientName(realm.getClientByClientId(Constants.ADMIN_CONSOLE_CLIENT_ID));
setupClientName(realm.getClientByClientId(Constants.REALM_MANAGEMENT_CLIENT_ID));
}
private void setupClientName(Map<String, ClientModel> clients, String clientId) {
ClientModel client = clients.get(clientId);
if (client != null && client.getName() == null) client.setName("${client_" + clientId + "}");
private void setupClientName(ClientModel client) {
if (client != null && client.getName() == null) client.setName("${client_" + client.getClientId() + "}");
}
public void migrate(KeycloakSession session) {

View file

@ -193,9 +193,6 @@ public interface RealmModel extends RoleContainerModel {
void removeDefaultGroup(GroupModel group);
// Key is clientId
Map<String, ClientModel> getClientNameMap();
List<ClientModel> getClients();
ClientModel addClient(String name);

View file

@ -234,12 +234,12 @@ public class RepresentationToModel {
// Now that all possible roles and clients are created, create scope mappings
Map<String, ClientModel> appMap = newRealm.getClientNameMap();
//Map<String, ClientModel> appMap = newRealm.getClientNameMap();
if (rep.getClientScopeMappings() != null) {
for (Map.Entry<String, List<ScopeMappingRepresentation>> entry : rep.getClientScopeMappings().entrySet()) {
ClientModel app = appMap.get(entry.getKey());
ClientModel app = newRealm.getClientByClientId(entry.getKey());
if (app == null) {
throw new RuntimeException("Unable to find client role mappings for client: " + entry.getKey());
}
@ -320,7 +320,7 @@ public class RepresentationToModel {
if (rep.getUsers() != null) {
for (UserRepresentation userRep : rep.getUsers()) {
UserModel user = createUser(session, newRealm, userRep, appMap);
UserModel user = createUser(session, newRealm, userRep);
}
}
@ -383,14 +383,13 @@ public class RepresentationToModel {
List<GroupRepresentation> groups = rep.getGroups();
if (groups == null) return;
Map<String, ClientModel> clientMap = realm.getClientNameMap();
GroupModel parent = null;
for (GroupRepresentation group : groups) {
importGroup(realm, clientMap, parent, group);
importGroup(realm, parent, group);
}
}
public static void importGroup(RealmModel realm, Map<String, ClientModel> clientMap, GroupModel parent, GroupRepresentation group) {
public static void importGroup(RealmModel realm, GroupModel parent, GroupRepresentation group) {
GroupModel newGroup = realm.createGroup(group.getId(), group.getName());
if (group.getAttributes() != null) {
for (Map.Entry<String, List<String>> attr : group.getAttributes().entrySet()) {
@ -410,7 +409,7 @@ public class RepresentationToModel {
}
if (group.getClientRoles() != null) {
for (Map.Entry<String, List<String>> entry : group.getClientRoles().entrySet()) {
ClientModel client = clientMap.get(entry.getKey());
ClientModel client = realm.getClientByClientId(entry.getKey());
if (client == null) {
throw new RuntimeException("Unable to find client role mappings for client: " + entry.getKey());
}
@ -427,7 +426,7 @@ public class RepresentationToModel {
}
if (group.getSubGroups() != null) {
for (GroupRepresentation subGroup : group.getSubGroups()) {
importGroup(realm, clientMap, newGroup, subGroup);
importGroup(realm, newGroup, subGroup);
}
}
}
@ -1180,7 +1179,7 @@ public class RepresentationToModel {
// Users
public static UserModel createUser(KeycloakSession session, RealmModel newRealm, UserRepresentation userRep, Map<String, ClientModel> clientMap) {
public static UserModel createUser(KeycloakSession session, RealmModel newRealm, UserRepresentation userRep) {
convertDeprecatedSocialProviders(userRep);
// Import users just to user storage. Don't federate
@ -1228,7 +1227,7 @@ public class RepresentationToModel {
}
if (userRep.getServiceAccountClientId() != null) {
String clientId = userRep.getServiceAccountClientId();
ClientModel client = clientMap.get(clientId);
ClientModel client = newRealm.getClientByClientId(clientId);
if (client == null) {
throw new RuntimeException("Unable to find client specified for service account link. Client: " + clientId);
}
@ -1316,9 +1315,8 @@ public class RepresentationToModel {
}
}
if (userRep.getClientRoles() != null) {
Map<String, ClientModel> clientMap = realm.getClientNameMap();
for (Map.Entry<String, List<String>> entry : userRep.getClientRoles().entrySet()) {
ClientModel client = clientMap.get(entry.getKey());
ClientModel client = realm.getClientByClientId(entry.getKey());
if (client == null) {
throw new RuntimeException("Unable to find client role mappings for client: " + entry.getKey());
}

View file

@ -199,9 +199,8 @@ public class ImportUtils {
private static void importUsers(KeycloakSession session, RealmProvider model, String realmName, List<UserRepresentation> userReps) {
RealmModel realm = model.getRealmByName(realmName);
Map<String, ClientModel> apps = realm.getClientNameMap();
for (UserRepresentation user : userReps) {
RepresentationToModel.createUser(session, realm, user, apps);
RepresentationToModel.createUser(session, realm, user);
}
}

View file

@ -107,9 +107,8 @@ public class UsersPartialImport extends AbstractPartialImport<UserRepresentation
@Override
public void create(RealmModel realm, KeycloakSession session, UserRepresentation user) {
Map<String, ClientModel> apps = realm.getClientNameMap();
user.setId(KeycloakModelUtils.generateId());
UserModel userModel = RepresentationToModel.createUser(session, realm, user, apps);
UserModel userModel = RepresentationToModel.createUser(session, realm, user);
if (userModel == null) throw new RuntimeException("Unable to create user " + getName(user));
createdIds.put(getName(user), userModel.getId());
}

View file

@ -327,7 +327,7 @@ public class RealmManager implements RealmImporter {
private void setupAccountManagement(RealmModel realm) {
ClientModel client = realm.getClientNameMap().get(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID);
ClientModel client = realm.getClientByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID);
if (client == null) {
client = KeycloakModelUtils.createClient(realm, Constants.ACCOUNT_MANAGEMENT_CLIENT_ID);
client.setName("${client_" + Constants.ACCOUNT_MANAGEMENT_CLIENT_ID + "}");
@ -352,7 +352,7 @@ public class RealmManager implements RealmImporter {
}
public void setupBrokerService(RealmModel realm) {
ClientModel client = realm.getClientNameMap().get(Constants.BROKER_SERVICE_CLIENT_ID);
ClientModel client = realm.getClientByClientId(Constants.BROKER_SERVICE_CLIENT_ID);
if (client == null) {
client = KeycloakModelUtils.createClient(realm, Constants.BROKER_SERVICE_CLIENT_ID);
client.setEnabled(true);

View file

@ -139,7 +139,7 @@ public class RealmsResource {
public AccountService getAccountService(final @PathParam("realm") String name) {
RealmModel realm = init(name);
ClientModel client = realm.getClientNameMap().get(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID);
ClientModel client = realm.getClientByClientId(Constants.ACCOUNT_MANAGEMENT_CLIENT_ID);
if (client == null || !client.isEnabled()) {
logger.debug("account management not enabled");
throw new NotFoundException("account management not enabled");

View file

@ -73,7 +73,7 @@ public class AccountTest {
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
UserModel user = manager.getSession().users().getUserByUsername("test-user@localhost", appRealm);
ClientModel accountApp = appRealm.getClientNameMap().get(org.keycloak.models.Constants.ACCOUNT_MANAGEMENT_CLIENT_ID);
ClientModel accountApp = appRealm.getClientByClientId(org.keycloak.models.Constants.ACCOUNT_MANAGEMENT_CLIENT_ID);
UserModel user2 = manager.getSession().users().addUser(appRealm, "test-user-no-access@localhost");
user2.setEnabled(true);

View file

@ -75,7 +75,7 @@ public class ClientModelTest extends AbstractModelTest {
public void persist() {
RealmModel persisted = realmManager.getRealm(realm.getId());
ClientModel actual = persisted.getClientNameMap().get("app-name");
ClientModel actual = persisted.getClientByClientId("app-name");
assertEquals(client, actual);
}

View file

@ -118,12 +118,12 @@ public class ImportTest extends AbstractModelTest {
Assert.assertNotNull(application);
Assert.assertNotNull(otherApp);
Assert.assertNull(nonExisting);
Map<String, ClientModel> clients = realm.getClientNameMap();
List<ClientModel> clients = realm.getClients();
Assert.assertEquals(8, clients.size());
Assert.assertTrue(clients.values().contains(application));
Assert.assertTrue(clients.values().contains(otherApp));
Assert.assertTrue(clients.values().contains(accountApp));
realm.getClients().containsAll(clients.values());
Assert.assertTrue(clients.contains(application));
Assert.assertTrue(clients.contains(otherApp));
Assert.assertTrue(clients.contains(accountApp));
realm.getClients().containsAll(clients);
Assert.assertEquals("Applicationn", application.getName());
Assert.assertEquals(50, application.getNodeReRegistrationTimeout());

View file

@ -91,7 +91,7 @@ public class AuthorizationCodeTest {
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.getClientNameMap().get("test-app").addRedirectUri(Constants.INSTALLED_APP_URN);
appRealm.getClientByClientId("test-app").addRedirectUri(Constants.INSTALLED_APP_URN);
}
});
oauth.redirectUri(Constants.INSTALLED_APP_URN);
@ -110,7 +110,7 @@ public class AuthorizationCodeTest {
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.getClientNameMap().get("test-app").removeRedirectUri(Constants.INSTALLED_APP_URN);
appRealm.getClientByClientId("test-app").removeRedirectUri(Constants.INSTALLED_APP_URN);
}
});
}

View file

@ -113,7 +113,7 @@ public class OAuthRedirectUriTest {
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.getClientNameMap().get("test-app").addRedirectUri("http://localhost:8081/app2");
appRealm.getClientByClientId("test-app").addRedirectUri("http://localhost:8081/app2");
}
});
@ -127,7 +127,7 @@ public class OAuthRedirectUriTest {
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.getClientNameMap().get("test-app").removeRedirectUri("http://localhost:8081/app2");
appRealm.getClientByClientId("test-app").removeRedirectUri("http://localhost:8081/app2");
}
});
}
@ -138,7 +138,7 @@ public class OAuthRedirectUriTest {
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.getClientNameMap().get("test-app").removeRedirectUri("http://localhost:8081/app/*");
appRealm.getClientByClientId("test-app").removeRedirectUri("http://localhost:8081/app/*");
}
});
@ -152,7 +152,7 @@ public class OAuthRedirectUriTest {
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.getClientNameMap().get("test-app").addRedirectUri("http://localhost:8081/app/*");
appRealm.getClientByClientId("test-app").addRedirectUri("http://localhost:8081/app/*");
}
});
}
@ -163,7 +163,7 @@ public class OAuthRedirectUriTest {
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.getClientNameMap().get("test-app").removeRedirectUri("http://localhost:8081/app/*");
appRealm.getClientByClientId("test-app").removeRedirectUri("http://localhost:8081/app/*");
}
});
@ -177,7 +177,7 @@ public class OAuthRedirectUriTest {
keycloakRule.update(new KeycloakRule.KeycloakSetup() {
@Override
public void config(RealmManager manager, RealmModel adminstrationRealm, RealmModel appRealm) {
appRealm.getClientNameMap().get("test-app").addRedirectUri("http://localhost:8081/app/*");
appRealm.getClientByClientId("test-app").addRedirectUri("http://localhost:8081/app/*");
}
});
}