diff --git a/audit/email/src/main/java/org/keycloak/audit/email/EmailAuditListener.java b/audit/email/src/main/java/org/keycloak/audit/email/EmailAuditListener.java index 53a8fcd9cc..f763928e23 100644 --- a/audit/email/src/main/java/org/keycloak/audit/email/EmailAuditListener.java +++ b/audit/email/src/main/java/org/keycloak/audit/email/EmailAuditListener.java @@ -7,6 +7,7 @@ import org.keycloak.audit.EventType; import org.keycloak.email.EmailException; import org.keycloak.email.EmailProvider; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.ModelProvider; import org.keycloak.models.RealmModel; import org.keycloak.models.UserModel; @@ -20,11 +21,13 @@ public class EmailAuditListener implements AuditListener { private static final Logger log = Logger.getLogger(EmailAuditListener.class); private KeycloakSession session; + private ModelProvider model; private EmailProvider emailProvider; private Set includedEvents; public EmailAuditListener(KeycloakSession session, EmailProvider emailProvider, Set includedEvents) { this.session = session; + this.model = session.getModel(); this.emailProvider = emailProvider; this.includedEvents = includedEvents; } @@ -33,7 +36,7 @@ public class EmailAuditListener implements AuditListener { public void onEvent(Event event) { if (includedEvents.contains(event.getEvent())) { if (event.getRealmId() != null && event.getUserId() != null) { - RealmModel realm = session.getRealm(event.getRealmId()); + RealmModel realm = model.getRealm(event.getRealmId()); UserModel user = realm.getUserById(event.getUserId()); if (user != null && user.getEmail() != null && user.isEmailVerified()) { try { diff --git a/authentication/authentication-model/src/main/java/org/keycloak/authentication/model/ExternalModelAuthenticationProvider.java b/authentication/authentication-model/src/main/java/org/keycloak/authentication/model/ExternalModelAuthenticationProvider.java index 9381bb96ed..b6e22c7d3b 100644 --- a/authentication/authentication-model/src/main/java/org/keycloak/authentication/model/ExternalModelAuthenticationProvider.java +++ b/authentication/authentication-model/src/main/java/org/keycloak/authentication/model/ExternalModelAuthenticationProvider.java @@ -1,9 +1,9 @@ package org.keycloak.authentication.model; -import org.jboss.resteasy.spi.ResteasyProviderFactory; import org.keycloak.authentication.AuthProviderConstants; import org.keycloak.authentication.AuthenticationProviderException; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.ModelProvider; import org.keycloak.models.RealmModel; import java.util.Arrays; @@ -17,7 +17,10 @@ import java.util.Map; */ public class ExternalModelAuthenticationProvider extends AbstractModelAuthenticationProvider { - public ExternalModelAuthenticationProvider() { + private ModelProvider model; + + public ExternalModelAuthenticationProvider(KeycloakSession session) { + this.model = session.getModel(); } @Override @@ -37,13 +40,7 @@ public class ExternalModelAuthenticationProvider extends AbstractModelAuthentica throw new AuthenticationProviderException("Option '" + AuthProviderConstants.EXTERNAL_REALM_ID + "' not specified in configuration"); } - // TODO: This won't be needed when KeycloakSession is available from ProviderSession - KeycloakSession session = ResteasyProviderFactory.getContextData(KeycloakSession.class); - if (session == null) { - throw new AuthenticationProviderException("KeycloakSession not available"); - } - - RealmModel realm = session.getRealm(realmId); + RealmModel realm = model.getRealm(realmId); if (realm == null) { throw new AuthenticationProviderException("Realm with id '" + realmId + "' doesn't exists"); } diff --git a/authentication/authentication-model/src/main/java/org/keycloak/authentication/model/ExternalModelAuthenticationProviderFactory.java b/authentication/authentication-model/src/main/java/org/keycloak/authentication/model/ExternalModelAuthenticationProviderFactory.java index 7f1962351c..7ef0a5d9f9 100644 --- a/authentication/authentication-model/src/main/java/org/keycloak/authentication/model/ExternalModelAuthenticationProviderFactory.java +++ b/authentication/authentication-model/src/main/java/org/keycloak/authentication/model/ExternalModelAuthenticationProviderFactory.java @@ -13,7 +13,7 @@ public class ExternalModelAuthenticationProviderFactory implements Authenticatio @Override public AuthenticationProvider create(KeycloakSession session) { - return new ExternalModelAuthenticationProvider(); + return new ExternalModelAuthenticationProvider(session); } @Override diff --git a/export-import/export-import-impl/src/main/java/org/keycloak/exportimport/ExportImportProviderImpl.java b/export-import/export-import-impl/src/main/java/org/keycloak/exportimport/ExportImportProviderImpl.java index 1064201e83..fd0a2bb60a 100644 --- a/export-import/export-import-impl/src/main/java/org/keycloak/exportimport/ExportImportProviderImpl.java +++ b/export-import/export-import-impl/src/main/java/org/keycloak/exportimport/ExportImportProviderImpl.java @@ -41,11 +41,11 @@ public class ExportImportProviderImpl implements ExportImportProvider { if (export) { ExportWriter exportWriter = getProvider().getExportWriter(); - new ModelExporter().exportModel(session, exportWriter); + new ModelExporter().exportModel(session.getModel(), exportWriter); logger.infof("Export finished successfully"); } else { ImportReader importReader = getProvider().getImportReader(); - new ModelImporter().importModel(session, importReader); + new ModelImporter().importModel(session.getModel(), importReader); logger.infof("Import finished successfully"); } diff --git a/export-import/export-import-impl/src/main/java/org/keycloak/exportimport/ModelExporter.java b/export-import/export-import-impl/src/main/java/org/keycloak/exportimport/ModelExporter.java index 951ea73b56..e63f0ac6c9 100755 --- a/export-import/export-import-impl/src/main/java/org/keycloak/exportimport/ModelExporter.java +++ b/export-import/export-import-impl/src/main/java/org/keycloak/exportimport/ModelExporter.java @@ -12,6 +12,7 @@ import org.keycloak.models.AuthenticationLinkModel; import org.keycloak.models.AuthenticationProviderModel; import org.keycloak.models.ClientModel; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.ModelProvider; import org.keycloak.models.OAuthClientModel; import org.keycloak.models.RealmModel; import org.keycloak.models.RequiredCredentialModel; @@ -43,24 +44,24 @@ public class ModelExporter { private ExportWriter exportWriter; private ExportImportPropertiesManager propertiesManager; - public void exportModel(KeycloakSession session, ExportWriter exportWriter) { + public void exportModel(ModelProvider model, ExportWriter exportWriter) { // Initialize needed objects this.exportWriter = exportWriter; this.propertiesManager = new ExportImportPropertiesManager(); // Create separate files for "realms", "applications", "oauthClients", "roles" and finally "users". Users may be done in more files (pagination) - exportRealms(session, "realms.json"); - exportApplications(session, "applications.json"); - exportOAuthClients(session, "oauthClients.json"); - exportRoles(session, "roles.json"); - exportUsers(session, "users.json"); - exportUserFailures(session, "userFailures.json"); + exportRealms(model, "realms.json"); + exportApplications(model, "applications.json"); + exportOAuthClients(model, "oauthClients.json"); + exportRoles(model, "roles.json"); + exportUsers(model, "users.json"); + exportUserFailures(model, "userFailures.json"); this.exportWriter.closeExportWriter(); } - protected void exportRealms(KeycloakSession session, String fileName) { - List realms = session.getRealms(); + protected void exportRealms(ModelProvider model, String fileName) { + List realms = model.getRealms(); // Convert models to entities, which will be written into JSON file List result = new LinkedList(); @@ -106,8 +107,8 @@ public class ModelExporter { logger.infof("Realms exported: " + result); } - protected void exportApplications(KeycloakSession session, String fileName) { - List allApplications = getAllApplications(session); + protected void exportApplications(ModelProvider model, String fileName) { + List allApplications = getAllApplications(model); List result = new LinkedList(); for (ApplicationModel appModel : allApplications) { @@ -129,8 +130,8 @@ public class ModelExporter { logger.infof("Applications exported: " + result); } - protected void exportOAuthClients(KeycloakSession session, String fileName) { - List realms = session.getRealms(); + protected void exportOAuthClients(ModelProvider model, String fileName) { + List realms = model.getRealms(); List allClients = new ArrayList(); for (RealmModel realmModel : realms) { allClients.addAll(realmModel.getOAuthClients()); @@ -156,8 +157,8 @@ public class ModelExporter { logger.infof("OAuth clients exported: " + result); } - protected void exportRoles(KeycloakSession session, String fileName) { - List allRoles = getAllRoles(session); + protected void exportRoles(ModelProvider model, String fileName) { + List allRoles = getAllRoles(model); List result = new LinkedList(); for (RoleModel roleModel : allRoles) { @@ -198,8 +199,8 @@ public class ModelExporter { } } - protected void exportUsers(KeycloakSession session, String fileName) { - List realms = session.getRealms(); + protected void exportUsers(ModelProvider model, String fileName) { + List realms = model.getRealms(); List result = new LinkedList(); for (RealmModel realm : realms) { @@ -277,8 +278,8 @@ public class ModelExporter { // Does it makes sense to export user failures ? - protected void exportUserFailures(KeycloakSession session, String fileName) { - List realms = session.getRealms(); + protected void exportUserFailures(ModelProvider model, String fileName) { + List realms = model.getRealms(); List allFailures = new ArrayList(); for (RealmModel realmModel : realms) { allFailures.addAll(realmModel.getAllUserLoginFailures()); @@ -306,8 +307,8 @@ public class ModelExporter { return scopeIds; } - private List getAllApplications(KeycloakSession session) { - List realms = session.getRealms(); + private List getAllApplications(ModelProvider model) { + List realms = model.getRealms(); List allApplications = new ArrayList(); for (RealmModel realmModel : realms) { allApplications.addAll(realmModel.getApplications()); @@ -315,15 +316,15 @@ public class ModelExporter { return allApplications; } - private List getAllRoles(KeycloakSession session) { + private List getAllRoles(ModelProvider model) { List allRoles = new ArrayList(); - List realms = session.getRealms(); + List realms = model.getRealms(); for (RealmModel realmModel : realms) { allRoles.addAll(realmModel.getRoles()); } - List allApplications = getAllApplications(session); + List allApplications = getAllApplications(model); for (ApplicationModel appModel : allApplications) { allRoles.addAll(appModel.getRoles()); } diff --git a/export-import/export-import-impl/src/main/java/org/keycloak/exportimport/ModelImporter.java b/export-import/export-import-impl/src/main/java/org/keycloak/exportimport/ModelImporter.java index 697aab7476..eb10fbc58a 100755 --- a/export-import/export-import-impl/src/main/java/org/keycloak/exportimport/ModelImporter.java +++ b/export-import/export-import-impl/src/main/java/org/keycloak/exportimport/ModelImporter.java @@ -15,6 +15,7 @@ import org.keycloak.models.AuthenticationProviderModel; import org.keycloak.models.ClientModel; import org.keycloak.Config; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.ModelProvider; import org.keycloak.models.OAuthClientModel; import org.keycloak.models.PasswordPolicy; import org.keycloak.models.RealmModel; @@ -47,34 +48,31 @@ public class ModelImporter { private ImportReader importReader; private ExportImportPropertiesManager propertiesManager; - public void importModel(KeycloakSession session, ImportReader importReader) { + public void importModel(ModelProvider model, ImportReader importReader) { // Initialize needed objects this.importReader = importReader; this.propertiesManager = new ExportImportPropertiesManager(); - // Delete all the data from current model - session.removeAllData(); - - importRealms(session, "realms.json"); - importApplications(session, "applications.json"); - importRoles(session, "roles.json"); + importRealms(model, "realms.json"); + importApplications(model, "applications.json"); + importRoles(model, "roles.json"); // Now we have all realms,applications and roles filled. So fill other objects (default roles, scopes etc) - importRealmsStep2(session, "realms.json"); - importApplicationsStep2(session, "applications.json"); + importRealmsStep2(model, "realms.json"); + importApplicationsStep2(model, "applications.json"); - importOAuthClients(session, "oauthClients.json"); - importUsers(session, "users.json"); - importUserFailures(session, "userFailures.json"); + importOAuthClients(model, "oauthClients.json"); + importUsers(model, "users.json"); + importUserFailures(model, "userFailures.json"); this.importReader.closeImportReader(); } - protected void importRealms(KeycloakSession session, String fileName) { + protected void importRealms(ModelProvider model, String fileName) { List realms = this.importReader.readEntities(fileName, RealmEntity.class); for (RealmEntity realmEntity : realms) { - RealmModel realm = session.createRealm(realmEntity.getId(), realmEntity.getName()); + RealmModel realm = model.createRealm(realmEntity.getId(), realmEntity.getName()); this.propertiesManager.setBasicPropertiesToModel(realm, realmEntity); @@ -103,10 +101,10 @@ public class ModelImporter { logger.infof("Realms imported: " + realms); } - protected void importApplications(KeycloakSession session, String fileName) { + protected void importApplications(ModelProvider model, String fileName) { List apps = this.importReader.readEntities(fileName, ApplicationEntity.class); for (ApplicationEntity appEntity : apps) { - RealmModel realm = session.getRealm(appEntity.getRealmId()); + RealmModel realm = model.getRealm(appEntity.getRealmId()); ApplicationModel app = realm.addApplication(appEntity.getId(), appEntity.getName()); this.propertiesManager.setBasicPropertiesToModel(app , appEntity); @@ -117,7 +115,7 @@ public class ModelImporter { logger.infof("Applications imported: " + apps); } - protected void importRoles(KeycloakSession session, String fileName) { + protected void importRoles(ModelProvider model, String fileName) { // helper map for composite roles Map rolesMap = new HashMap(); @@ -125,10 +123,10 @@ public class ModelImporter { for (RoleEntity roleEntity : roles) { RoleModel role = null; if (roleEntity.getRealmId() != null) { - RealmModel realm = session.getRealm(roleEntity.getRealmId()); + RealmModel realm = model.getRealm(roleEntity.getRealmId()); role = realm.addRole(roleEntity.getId(), roleEntity.getName()); } else if (roleEntity.getApplicationId() != null) { - ApplicationModel app = findApplicationById(session, roleEntity.getApplicationId()); + ApplicationModel app = findApplicationById(model, roleEntity.getApplicationId()); role = app.addRole(roleEntity.getId(), roleEntity.getName()); } else { throw new IllegalStateException("Role " + roleEntity.getId() + " doesn't have realmId nor applicationId"); @@ -140,7 +138,7 @@ public class ModelImporter { } // All roles were added. Fill composite roles now - for (RealmModel realm : session.getRealms()) { + for (RealmModel realm : model.getRealms()) { // realm roles fillCompositeRoles(rolesMap, realm, realm); @@ -172,12 +170,12 @@ public class ModelImporter { } } - protected void importRealmsStep2(KeycloakSession session, String fileName) { + protected void importRealmsStep2(ModelProvider model, String fileName) { List realms = this.importReader.readEntities(fileName, RealmEntity.class); - RealmModel adminRealm = session.getRealm(Config.getAdminRealm()); + RealmModel adminRealm = model.getRealm(Config.getAdminRealm()); for (RealmEntity realmEntity : realms) { - RealmModel realm = session.getRealm(realmEntity.getId()); + RealmModel realm = model.getRealm(realmEntity.getId()); // admin app String adminAppId = realmEntity.getAdminAppId(); @@ -190,10 +188,10 @@ public class ModelImporter { } } - protected void importApplicationsStep2(KeycloakSession session, String fileName) { + protected void importApplicationsStep2(ModelProvider model, String fileName) { List apps = this.importReader.readEntities(fileName, ApplicationEntity.class); for (ApplicationEntity appEntity : apps) { - RealmModel realm = session.getRealm(appEntity.getRealmId()); + RealmModel realm = model.getRealm(appEntity.getRealmId()); ApplicationModel application = realm.getApplicationById(appEntity.getId()); // Default roles @@ -211,10 +209,10 @@ public class ModelImporter { } } - protected void importOAuthClients(KeycloakSession session, String fileName) { + protected void importOAuthClients(ModelProvider model, String fileName) { List clients = this.importReader.readEntities(fileName, OAuthClientEntity.class); for (OAuthClientEntity clientEntity : clients) { - RealmModel realm = session.getRealm(clientEntity.getRealmId()); + RealmModel realm = model.getRealm(clientEntity.getRealmId()); OAuthClientModel client = realm.addOAuthClient(clientEntity.getId(), clientEntity.getName()); this.propertiesManager.setBasicPropertiesToModel(client, clientEntity); @@ -228,8 +226,8 @@ public class ModelImporter { logger.info("OAuth clients imported: " + clients); } - protected ApplicationModel findApplicationById(KeycloakSession session, String applicationId) { - for (RealmModel realm : session.getRealms()) { + protected ApplicationModel findApplicationById(ModelProvider model, String applicationId) { + for (RealmModel realm : model.getRealms()) { ApplicationModel appModel = realm.getApplicationById(applicationId); if (appModel != null) { return appModel; @@ -239,10 +237,10 @@ public class ModelImporter { return null; } - public void importUsers(KeycloakSession session, String fileName) { + public void importUsers(ModelProvider model, String fileName) { List users = this.importReader.readEntities(fileName, UserEntity.class); for (UserEntity userEntity : users) { - RealmModel realm = session.getRealm(userEntity.getRealmId()); + RealmModel realm = model.getRealm(userEntity.getRealmId()); UserModel user = realm.addUser(userEntity.getId(), userEntity.getUsername(), false); // We need to remove defaultRoles here as realm.addUser is automatically adding them. We may add them later during roles mapping processing @@ -313,16 +311,16 @@ public class ModelImporter { } } - public void importUserFailures(KeycloakSession session, String fileName) { + public void importUserFailures(ModelProvider model, String fileName) { List userFailures = this.importReader.readEntities(fileName, UsernameLoginFailureEntity.class); for (UsernameLoginFailureEntity entity : userFailures) { - RealmModel realm = session.getRealm(entity.getRealmId()); - UsernameLoginFailureModel model = realm.addUserLoginFailure(entity.getUsername()); + RealmModel realm = model.getRealm(entity.getRealmId()); + UsernameLoginFailureModel loginFailureModel = realm.addUserLoginFailure(entity.getUsername()); - this.propertiesManager.setBasicPropertiesToModel(model , entity); + this.propertiesManager.setBasicPropertiesToModel(loginFailureModel , entity); for (int i=0 ; i Set getAllProviders(Class clazz); - RealmModel createRealm(String name); - RealmModel createRealm(String id, String name); - RealmModel getRealm(String id); - RealmModel getRealmByName(String name); - - UserModel getUserById(String id, RealmModel realm); - UserModel getUserByUsername(String username, RealmModel realm); - UserModel getUserByEmail(String email, RealmModel realm); - UserModel getUserBySocialLink(SocialLinkModel socialLink, RealmModel realm); - List getUsers(RealmModel realm); - List searchForUser(String search, RealmModel realm); - List searchForUserByAttributes(Map attributes, RealmModel realm); - - Set getSocialLinks(UserModel user, RealmModel realm); - SocialLinkModel getSocialLink(UserModel user, String socialProvider, RealmModel realm); - - - RoleModel getRoleById(String id, RealmModel realm); - ApplicationModel getApplicationById(String id, RealmModel realm); - OAuthClientModel getOAuthClientById(String id, RealmModel realm); - List getRealms(); - boolean removeRealm(String id); - - UsernameLoginFailureModel getUserLoginFailure(String username, RealmModel realm); - UsernameLoginFailureModel addUserLoginFailure(String username, RealmModel realm); - List getAllUserLoginFailures(RealmModel realm); - - UserSessionModel createUserSession(RealmModel realm, UserModel user, String ipAddress); - UserSessionModel getUserSession(String id, RealmModel realm); - List getUserSessions(UserModel user, RealmModel realm); - Set getUserSessions(RealmModel realm, ClientModel client); - int getActiveUserSessions(RealmModel realm, ClientModel client); - void removeUserSession(UserSessionModel session); - void removeUserSessions(RealmModel realm, UserModel user); - void removeExpiredUserSessions(RealmModel realm); - void removeUserSessions(RealmModel realm); - - - void removeAllData(); + ModelProvider getModel(); void close(); + } diff --git a/model/api/src/main/java/org/keycloak/models/ModelProvider.java b/model/api/src/main/java/org/keycloak/models/ModelProvider.java index 8ec25803d0..6e2ddf5f9a 100755 --- a/model/api/src/main/java/org/keycloak/models/ModelProvider.java +++ b/model/api/src/main/java/org/keycloak/models/ModelProvider.java @@ -52,8 +52,5 @@ public interface ModelProvider extends Provider { void removeExpiredUserSessions(RealmModel realm); void removeUserSessions(RealmModel realm); - - void removeAllData(); - void close(); } diff --git a/model/hybrid/src/main/java/org/keycloak/models/hybrid/HybridModelProvider.java b/model/hybrid/src/main/java/org/keycloak/models/hybrid/HybridModelProvider.java index 0a3d7785eb..8de241dd48 100644 --- a/model/hybrid/src/main/java/org/keycloak/models/hybrid/HybridModelProvider.java +++ b/model/hybrid/src/main/java/org/keycloak/models/hybrid/HybridModelProvider.java @@ -245,10 +245,6 @@ public class HybridModelProvider implements ModelProvider { sessions().removeUserSessions(realm.getId()); } - @Override - public void removeAllData() { - } - @Override public void close() { } diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheModelProvider.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheModelProvider.java index 79dea57ccd..186d76b4e1 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheModelProvider.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheModelProvider.java @@ -291,13 +291,6 @@ public class DefaultCacheModelProvider implements CacheModelProvider { return didIt; } - @Override - public void removeAllData() { - cache.clear(); - getDelegate().removeAllData(); - clearAll = true; - } - @Override public void close() { if (delegate != null) delegate.close(); diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/NoCacheModelProvider.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/NoCacheModelProvider.java index a4e8d841c6..b9cf8d18de 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/NoCacheModelProvider.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/NoCacheModelProvider.java @@ -156,11 +156,6 @@ public class NoCacheModelProvider implements CacheModelProvider { return getDelegate().removeRealm(id); } - @Override - public void removeAllData() { - getDelegate().removeAllData(); - } - @Override public void close() { if (delegate != null) delegate.close(); diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaModelProvider.java b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaModelProvider.java index 940e0671ff..bab6fc0351 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaModelProvider.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaModelProvider.java @@ -144,15 +144,6 @@ public class JpaModelProvider implements ModelProvider { if (em.isOpen()) em.close(); } - @Override - public void removeAllData() { - // Should be sufficient to delete all realms. Rest data should be removed in cascade - List realms = getRealms(); - for (RealmModel realm : realms) { - removeRealm(realm.getId()); - } - } - @Override public UserModel getUserBySocialLink(SocialLinkModel socialLink, RealmModel realm) { TypedQuery query = em.createNamedQuery("findUserByLinkAndRealm", UserEntity.class); diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java index 23602ec49c..04d31b5428 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/RealmAdapter.java @@ -6,6 +6,7 @@ import org.keycloak.models.ClientModel; import org.keycloak.models.CredentialValidation; import org.keycloak.models.KeycloakSession; import org.keycloak.models.ModelDuplicateException; +import org.keycloak.models.ModelProvider; import org.keycloak.models.RoleContainerModel; import org.keycloak.models.UserCredentialValueModel; import org.keycloak.models.UserSessionModel; @@ -66,10 +67,12 @@ public class RealmAdapter implements RealmModel { protected volatile transient PublicKey publicKey; protected volatile transient PrivateKey privateKey; protected KeycloakSession session; + protected ModelProvider model; private PasswordPolicy passwordPolicy; public RealmAdapter(KeycloakSession session, EntityManager em, RealmEntity realm) { this.session = session; + this.model = session.getModel(); this.em = em; this.realm = realm; } @@ -426,32 +429,32 @@ public class RealmAdapter implements RealmModel { @Override public UserModel getUser(String name) { - return session.getUserByUsername(name, this); + return model.getUserByUsername(name, this); } @Override public UsernameLoginFailureModel getUserLoginFailure(String username) { - return session.getUserLoginFailure(username, this); + return model.getUserLoginFailure(username, this); } @Override public UsernameLoginFailureModel addUserLoginFailure(String username) { - return session.addUserLoginFailure(username, this); + return model.addUserLoginFailure(username, this); } @Override public List getAllUserLoginFailures() { - return session.getAllUserLoginFailures(this); + return model.getAllUserLoginFailures(this); } @Override public UserModel getUserByEmail(String email) { - return session.getUserByEmail(email, this); + return model.getUserByEmail(email, this); } @Override public UserModel getUserById(String id) { - return session.getUserById(id, this); + return model.getUserById(id, this); } @Override @@ -661,7 +664,7 @@ public class RealmAdapter implements RealmModel { @Override public ApplicationModel getApplicationById(String id) { - return session.getApplicationById(id, this); + return model.getApplicationById(id, this); } @Override @@ -671,17 +674,17 @@ public class RealmAdapter implements RealmModel { @Override public UserModel getUserBySocialLink(SocialLinkModel socialLink) { - return session.getUserBySocialLink(socialLink, this); + return model.getUserBySocialLink(socialLink, this); } @Override public Set getSocialLinks(UserModel user) { - return session.getSocialLinks(user, this); + return model.getSocialLinks(user, this); } @Override public SocialLinkModel getSocialLink(UserModel user, String socialProvider) { - return session.getSocialLink(user, socialProvider, this); + return model.getSocialLink(user, socialProvider, this); } @Override @@ -744,17 +747,17 @@ public class RealmAdapter implements RealmModel { @Override public List getUsers() { - return session.getUsers(this); + return model.getUsers(this); } @Override public List searchForUser(String search) { - return session.searchForUser(search, this); + return model.searchForUser(search, this); } @Override public List searchForUserByAttributes(Map attributes) { - return session.searchForUserByAttributes(attributes, this); + return model.searchForUserByAttributes(attributes, this); } @Override @@ -798,7 +801,7 @@ public class RealmAdapter implements RealmModel { @Override public OAuthClientModel getOAuthClientById(String id) { - return session.getOAuthClientById(id, this); + return model.getOAuthClientById(id, this); } @@ -960,7 +963,7 @@ public class RealmAdapter implements RealmModel { @Override public RoleModel getRoleById(String id) { - return session.getRoleById(id, this); + return model.getRoleById(id, this); } @Override @@ -1105,38 +1108,38 @@ public class RealmAdapter implements RealmModel { @Override public UserSessionModel createUserSession(UserModel user, String ipAddress) { - return session.createUserSession(this, user, ipAddress); + return model.createUserSession(this, user, ipAddress); } @Override public UserSessionModel getUserSession(String id) { - return session.getUserSession(id, this); + return model.getUserSession(id, this); } @Override public List getUserSessions(UserModel user) { - return session.getUserSessions(user, this); + return model.getUserSessions(user, this); } @Override public void removeUserSession(UserSessionModel session) { - this.session.removeUserSession(session); + this.model.removeUserSession(session); } @Override public void removeUserSessions() { - session.removeUserSessions(this); + model.removeUserSessions(this); } @Override public void removeUserSessions(UserModel user) { - session.removeUserSessions(this, user); + model.removeUserSessions(this, user); } @Override public void removeExpiredUserSessions() { - session.removeExpiredUserSessions(this); + model.removeExpiredUserSessions(this); } } diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/api/MongoStore.java b/model/mongo/src/main/java/org/keycloak/models/mongo/api/MongoStore.java index 56951ebb10..cfd5a26a50 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/api/MongoStore.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/api/MongoStore.java @@ -41,8 +41,4 @@ public interface MongoStore { boolean pullItemFromList(MongoIdentifiableEntity entity, String listPropertyName, S itemToPull, MongoStoreInvocationContext context); - /** - * Completely remove all data from DB - */ - void removeAllEntities(); } diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/impl/MongoStoreImpl.java b/model/mongo/src/main/java/org/keycloak/models/mongo/impl/MongoStoreImpl.java index a9c1d969e5..5b0631da11 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/impl/MongoStoreImpl.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/impl/MongoStoreImpl.java @@ -417,13 +417,6 @@ public class MongoStoreImpl implements MongoStore { } } - @Override - public void removeAllEntities() { - Set> managedTypes = this.entityInfoCache.keySet(); - Class[] arrayTemplate = (Class[])new Class[0]; - this.clearManagedCollections(managedTypes.toArray(arrayTemplate)); - } - // Possibility to add user-defined mappers public void addAppObjectConverter(Mapper mapper) { mapperRegistry.addAppObjectMapper(mapper); diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ApplicationAdapter.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ApplicationAdapter.java index d2ab912872..c92308725b 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ApplicationAdapter.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ApplicationAdapter.java @@ -7,8 +7,6 @@ import org.keycloak.models.ClientModel; import org.keycloak.models.KeycloakSession; import org.keycloak.models.RealmModel; import org.keycloak.models.RoleModel; -import org.keycloak.models.UserModel; -import org.keycloak.models.UserSessionModel; import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; import org.keycloak.models.mongo.keycloak.entities.MongoApplicationEntity; import org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity; diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ClientAdapter.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ClientAdapter.java index 614204b41d..2796f6a15f 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ClientAdapter.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/ClientAdapter.java @@ -1,14 +1,8 @@ package org.keycloak.models.mongo.keycloak.adapters; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - -import com.mongodb.DBObject; -import com.mongodb.QueryBuilder; import org.keycloak.models.ClientModel; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.ModelProvider; import org.keycloak.models.RealmModel; import org.keycloak.models.RoleModel; import org.keycloak.models.UserSessionModel; @@ -16,9 +10,13 @@ import org.keycloak.models.entities.ClientEntity; import org.keycloak.models.mongo.api.MongoIdentifiableEntity; import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; import org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity; -import org.keycloak.models.mongo.keycloak.entities.MongoUserSessionEntity; import org.keycloak.models.mongo.utils.MongoModelUtils; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + /** * @author Marek Posolda */ @@ -27,12 +25,14 @@ public abstract class ClientAdapter extends A protected final T clientEntity; private final RealmModel realm; protected KeycloakSession session; + private final ModelProvider model; public ClientAdapter(KeycloakSession session, RealmModel realm, T clientEntity, MongoStoreInvocationContext invContext) { super(invContext); this.clientEntity = clientEntity; this.realm = realm; this.session = session; + this.model = session.getModel(); } @Override @@ -176,12 +176,12 @@ public abstract class ClientAdapter extends A @Override public Set getUserSessions() { - return session.getUserSessions(realm, this); + return model.getUserSessions(realm, this); } @Override public int getActiveUserSessions() { - return session.getActiveUserSessions(realm, this); + return model.getActiveUserSessions(realm, this); } @Override diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoModelProvider.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoModelProvider.java index efc827753f..7d567ca5b4 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoModelProvider.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoModelProvider.java @@ -67,11 +67,6 @@ public class MongoModelProvider implements ModelProvider { // TODO } - @Override - public void removeAllData() { - getMongoStore().removeAllEntities(); - } - @Override public RealmModel createRealm(String name) { return createRealm(KeycloakModelUtils.generateId(), name); diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/RealmAdapter.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/RealmAdapter.java index 50886e77a7..dd62a0756e 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/RealmAdapter.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/RealmAdapter.java @@ -1,15 +1,14 @@ package org.keycloak.models.mongo.keycloak.adapters; -import com.mongodb.BasicDBObject; import com.mongodb.DBObject; import com.mongodb.QueryBuilder; import org.jboss.logging.Logger; import org.keycloak.models.ApplicationModel; -import org.keycloak.models.AuthenticationLinkModel; import org.keycloak.models.AuthenticationProviderModel; import org.keycloak.models.ClientModel; import org.keycloak.models.CredentialValidation; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.ModelProvider; import org.keycloak.models.OAuthClientModel; import org.keycloak.models.PasswordPolicy; import org.keycloak.models.RealmModel; @@ -21,9 +20,7 @@ import org.keycloak.models.UserCredentialValueModel; import org.keycloak.models.UserModel; import org.keycloak.models.UserSessionModel; import org.keycloak.models.UsernameLoginFailureModel; -import org.keycloak.models.entities.AuthenticationLinkEntity; import org.keycloak.models.entities.AuthenticationProviderEntity; -import org.keycloak.models.entities.CredentialEntity; import org.keycloak.models.entities.RequiredCredentialEntity; import org.keycloak.models.entities.SocialLinkEntity; import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; @@ -32,13 +29,8 @@ import org.keycloak.models.mongo.keycloak.entities.MongoOAuthClientEntity; import org.keycloak.models.mongo.keycloak.entities.MongoRealmEntity; import org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity; import org.keycloak.models.mongo.keycloak.entities.MongoUserEntity; -import org.keycloak.models.mongo.keycloak.entities.MongoUserSessionEntity; -import org.keycloak.models.mongo.keycloak.entities.MongoUsernameLoginFailureEntity; -import org.keycloak.models.mongo.utils.MongoModelUtils; import org.keycloak.models.utils.KeycloakModelUtils; -import org.keycloak.models.utils.Pbkdf2PasswordEncoder; import org.keycloak.models.utils.TimeBasedOTP; -import org.keycloak.util.Time; import java.security.PrivateKey; import java.security.PublicKey; @@ -47,11 +39,9 @@ import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.regex.Pattern; /** * @author Marek Posolda @@ -61,6 +51,7 @@ public class RealmAdapter extends AbstractMongoAdapter impleme private static final Logger logger = Logger.getLogger(RealmAdapter.class); private final MongoRealmEntity realm; + private final ModelProvider model; protected volatile transient PublicKey publicKey; protected volatile transient PrivateKey privateKey; @@ -72,6 +63,7 @@ public class RealmAdapter extends AbstractMongoAdapter impleme super(invocationContext); this.realm = realmEntity; this.session = session; + this.model = session.getModel(); } @Override @@ -450,33 +442,33 @@ public class RealmAdapter extends AbstractMongoAdapter impleme @Override public UserModel getUser(String name) { - return session.getUserByUsername(name, this); + return model.getUserByUsername(name, this); } @Override public UsernameLoginFailureModel getUserLoginFailure(String name) { - return session.getUserLoginFailure(name, this); + return model.getUserLoginFailure(name, this); } @Override public UsernameLoginFailureModel addUserLoginFailure(String username) { - return session.addUserLoginFailure(username, this); + return model.addUserLoginFailure(username, this); } @Override public List getAllUserLoginFailures() { - return session.getAllUserLoginFailures(this); + return model.getAllUserLoginFailures(this); } @Override public UserModel getUserByEmail(String email) { - return session.getUserByEmail(email, this); + return model.getUserByEmail(email, this); } @Override public UserModel getUserById(String id) { - return session.getUserById(id, this); + return model.getUserById(id, this); } @Override @@ -584,7 +576,7 @@ public class RealmAdapter extends AbstractMongoAdapter impleme @Override public RoleModel getRoleById(String id) { - return session.getRoleById(id, this); + return model.getRoleById(id, this); } @Override @@ -636,7 +628,7 @@ public class RealmAdapter extends AbstractMongoAdapter impleme @Override public ApplicationModel getApplicationById(String id) { - return session.getApplicationById(id, this); + return model.getApplicationById(id, this); } @Override @@ -727,7 +719,7 @@ public class RealmAdapter extends AbstractMongoAdapter impleme @Override public OAuthClientModel getOAuthClientById(String id) { - return session.getOAuthClientById(id, this); + return model.getOAuthClientById(id, this); } @Override @@ -830,17 +822,17 @@ public class RealmAdapter extends AbstractMongoAdapter impleme @Override public UserModel getUserBySocialLink(SocialLinkModel socialLink) { - return session.getUserBySocialLink(socialLink, this); + return model.getUserBySocialLink(socialLink, this); } @Override public Set getSocialLinks(UserModel user) { - return session.getSocialLinks(user, this); + return model.getSocialLinks(user, this); } @Override public SocialLinkModel getSocialLink(UserModel user, String socialProvider) { - return session.getSocialLink(user, socialProvider, this); + return model.getSocialLink(user, socialProvider, this); } @Override @@ -893,18 +885,18 @@ public class RealmAdapter extends AbstractMongoAdapter impleme @Override public List getUsers() { - return session.getUsers(this); + return model.getUsers(this); } @Override public List searchForUser(String search) { - return session.searchForUser(search, this); + return model.searchForUser(search, this); } @Override public List searchForUserByAttributes(Map attributes) { - return session.searchForUserByAttributes(attributes, this); + return model.searchForUserByAttributes(attributes, this); } @@ -1023,37 +1015,37 @@ public class RealmAdapter extends AbstractMongoAdapter impleme @Override public UserSessionModel createUserSession(UserModel user, String ipAddress) { - return session.createUserSession(this, user, ipAddress); + return model.createUserSession(this, user, ipAddress); } @Override public UserSessionModel getUserSession(String id) { - return session.getUserSession(id, this); + return model.getUserSession(id, this); } @Override public List getUserSessions(UserModel user) { - return session.getUserSessions(user, this); + return model.getUserSessions(user, this); } @Override public void removeUserSession(UserSessionModel session) { - this.session.removeUserSession(session); + this.model.removeUserSession(session); } @Override public void removeUserSessions(UserModel user) { - this.session.removeUserSessions(this, user); + this.model.removeUserSessions(this, user); } @Override public void removeUserSessions() { - this.session.removeUserSessions(this); + this.model.removeUserSessions(this); } @Override public void removeExpiredUserSessions() { - this.session.removeExpiredUserSessions(this); + this.model.removeExpiredUserSessions(this); } @Override diff --git a/model/tests/src/test/java/org/keycloak/model/test/AbstractModelTest.java b/model/tests/src/test/java/org/keycloak/model/test/AbstractModelTest.java index b59b1ca8be..5f1063d1c4 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/AbstractModelTest.java +++ b/model/tests/src/test/java/org/keycloak/model/test/AbstractModelTest.java @@ -8,6 +8,7 @@ import org.junit.BeforeClass; import org.keycloak.Config; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; +import org.keycloak.models.ModelProvider; import org.keycloak.models.RealmModel; import org.keycloak.models.RoleModel; import org.keycloak.representations.idm.RealmRepresentation; @@ -30,6 +31,7 @@ public class AbstractModelTest { protected KeycloakSession session; protected RealmManager realmManager; + protected ModelProvider model; @BeforeClass public static void beforeClass() { @@ -54,6 +56,7 @@ public class AbstractModelTest { public void before() throws Exception { session = sessionFactory.create(); session.getTransaction().begin(); + model = session.getModel(); realmManager = new RealmManager(session); } @@ -65,9 +68,10 @@ public class AbstractModelTest { session = sessionFactory.create(); try { session.getTransaction().begin(); + model = session.getModel(); RealmManager rm = new RealmManager(session); - for (RealmModel realm : session.getRealms()) { + for (RealmModel realm : model.getRealms()) { if (!realm.getName().equals(Config.getAdminRealm())) { rm.removeRealm(realm); } @@ -98,6 +102,7 @@ public class AbstractModelTest { session = sessionFactory.create(); session.getTransaction().begin(); + model = session.getModel(); realmManager = new RealmManager(session); } diff --git a/model/tests/src/test/java/org/keycloak/model/test/AdapterTest.java b/model/tests/src/test/java/org/keycloak/model/test/AdapterTest.java index fa0460dee6..3109fd3747 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/AdapterTest.java +++ b/model/tests/src/test/java/org/keycloak/model/test/AdapterTest.java @@ -94,7 +94,7 @@ public class AdapterTest extends AbstractModelTest { realmModel.getId(); commit(); - List realms = session.getRealms(); + List realms = model.getRealms(); Assert.assertEquals(realms.size(), 2); } @@ -179,7 +179,7 @@ public class AdapterTest extends AbstractModelTest { commit(); - realmModel = session.getRealm("JUGGLER"); + realmModel = model.getRealm("JUGGLER"); Assert.assertTrue(realmModel.removeUser("bburke")); Assert.assertFalse(realmModel.removeUser("bburke")); assertNull(realmModel.getUser("bburke")); @@ -235,7 +235,7 @@ public class AdapterTest extends AbstractModelTest { app.addScopeMapping(realmRole); commit(); - realmModel = session.getRealm("JUGGLER"); + realmModel = model.getRealm("JUGGLER"); Assert.assertTrue(realmManager.removeRealm(realmModel)); Assert.assertFalse(realmManager.removeRealm(realmModel)); @@ -261,7 +261,7 @@ public class AdapterTest extends AbstractModelTest { app.addScopeMapping(realmRole); commit(); - realmModel = session.getRealm("JUGGLER"); + realmModel = model.getRealm("JUGGLER"); app = realmModel.getApplicationByName("test-app"); Assert.assertTrue(realmModel.removeRoleById(realmRole.getId())); @@ -513,7 +513,7 @@ public class AdapterTest extends AbstractModelTest { commit(); - realmModel = session.getRealmByName("JUGGLER"); + realmModel = model.getRealmByName("JUGGLER"); app1 = realmModel.getApplicationByName("app1"); app2 = realmModel.getApplicationByName("app2"); client = realmModel.getOAuthClient("client"); diff --git a/model/tests/src/test/java/org/keycloak/model/test/MultipleRealmsTest.java b/model/tests/src/test/java/org/keycloak/model/test/MultipleRealmsTest.java index a3eda8c0e6..ce6b937c1f 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/MultipleRealmsTest.java +++ b/model/tests/src/test/java/org/keycloak/model/test/MultipleRealmsTest.java @@ -49,8 +49,8 @@ public class MultipleRealmsTest extends AbstractModelTest { Assert.assertEquals(2, realm1.searchForUser("user").size()); commit(); - realm1 = session.getRealm("id1"); - realm2 = session.getRealm("id2"); + realm1 = model.getRealm("id1"); + realm2 = model.getRealm("id2"); realm1.removeUser("user1"); realm1.removeUser("user2"); @@ -60,10 +60,10 @@ public class MultipleRealmsTest extends AbstractModelTest { @Test public void testGetById() { - Assert.assertEquals(realm1, session.getRealm("id1")); - Assert.assertEquals(realm1, session.getRealmByName("realm1")); - Assert.assertEquals(realm2, session.getRealm("id2")); - Assert.assertEquals(realm2, session.getRealmByName("realm2")); + Assert.assertEquals(realm1, model.getRealm("id1")); + Assert.assertEquals(realm1, model.getRealmByName("realm1")); + Assert.assertEquals(realm2, model.getRealm("id2")); + Assert.assertEquals(realm2, model.getRealmByName("realm2")); ApplicationModel r1app1 = realm1.getApplicationByName("app1"); ApplicationModel r1app2 = realm1.getApplicationByName("app2"); diff --git a/services/src/main/java/org/keycloak/services/DefaultKeycloakSession.java b/services/src/main/java/org/keycloak/services/DefaultKeycloakSession.java index dce401905c..99db9b1a81 100755 --- a/services/src/main/java/org/keycloak/services/DefaultKeycloakSession.java +++ b/services/src/main/java/org/keycloak/services/DefaultKeycloakSession.java @@ -1,24 +1,14 @@ package org.keycloak.services; -import org.keycloak.models.ApplicationModel; -import org.keycloak.models.ClientModel; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakTransaction; import org.keycloak.models.ModelProvider; -import org.keycloak.models.OAuthClientModel; -import org.keycloak.models.RealmModel; -import org.keycloak.models.RoleModel; -import org.keycloak.models.SocialLinkModel; -import org.keycloak.models.UserModel; -import org.keycloak.models.UserSessionModel; -import org.keycloak.models.UsernameLoginFailureModel; import org.keycloak.models.cache.CacheModelProvider; import org.keycloak.provider.Provider; import org.keycloak.provider.ProviderFactory; import java.util.HashMap; import java.util.HashSet; -import java.util.List; import java.util.Map; import java.util.Set; @@ -27,8 +17,8 @@ import java.util.Set; */ public class DefaultKeycloakSession implements KeycloakSession { - private DefaultKeycloakSessionFactory factory; - private Map providers = new HashMap(); + private final DefaultKeycloakSessionFactory factory; + private final Map providers = new HashMap(); private final ModelProvider model; public DefaultKeycloakSession(DefaultKeycloakSessionFactory factory) { @@ -85,159 +75,8 @@ public class DefaultKeycloakSession implements KeycloakSession { return providers; } - @Override - public RealmModel createRealm(String name) { - return model.createRealm(name); - } - - @Override - public RealmModel createRealm(String id, String name) { - return model.createRealm(id, name); - } - - @Override - public RealmModel getRealm(String id) { - return model.getRealm(id); - } - - @Override - public RealmModel getRealmByName(String name) { - return model.getRealmByName(name); - } - - @Override - public UserModel getUserById(String id, RealmModel realm) { - return model.getUserById(id, realm); - } - - @Override - public UserModel getUserByUsername(String username, RealmModel realm) { - return model.getUserByUsername(username, realm); - } - - @Override - public UserModel getUserByEmail(String email, RealmModel realm) { - return model.getUserByEmail(email, realm); - } - - @Override - public UserModel getUserBySocialLink(SocialLinkModel socialLink, RealmModel realm) { - return model.getUserBySocialLink(socialLink, realm); - } - - @Override - public List getUsers(RealmModel realm) { - return model.getUsers(realm); - } - - @Override - public List searchForUser(String search, RealmModel realm) { - return model.searchForUser(search, realm); - } - - @Override - public List searchForUserByAttributes(Map attributes, RealmModel realm) { - return model.searchForUserByAttributes(attributes, realm); - } - - @Override - public Set getSocialLinks(UserModel user, RealmModel realm) { - return model.getSocialLinks(user, realm); - } - - @Override - public SocialLinkModel getSocialLink(UserModel user, String socialProvider, RealmModel realm) { - return model.getSocialLink(user, socialProvider, realm); - } - - @Override - public RoleModel getRoleById(String id, RealmModel realm) { - return model.getRoleById(id, realm); - } - - @Override - public ApplicationModel getApplicationById(String id, RealmModel realm) { - return model.getApplicationById(id, realm); - } - - @Override - public OAuthClientModel getOAuthClientById(String id, RealmModel realm) { - return model.getOAuthClientById(id, realm); - } - - @Override - public List getRealms() { - return model.getRealms(); - } - - @Override - public boolean removeRealm(String id) { - return model.removeRealm(id); - } - - @Override - public UsernameLoginFailureModel getUserLoginFailure(String username, RealmModel realm) { - return model.getUserLoginFailure(username, realm); - } - - @Override - public UsernameLoginFailureModel addUserLoginFailure(String username, RealmModel realm) { - return model.addUserLoginFailure(username, realm); - } - - @Override - public List getAllUserLoginFailures(RealmModel realm) { - return model.getAllUserLoginFailures(realm); - } - - @Override - public UserSessionModel createUserSession(RealmModel realm, UserModel user, String ipAddress) { - return model.createUserSession(realm, user, ipAddress); - } - - @Override - public UserSessionModel getUserSession(String id, RealmModel realm) { - return model.getUserSession(id, realm); - } - - @Override - public List getUserSessions(UserModel user, RealmModel realm) { - return model.getUserSessions(user, realm); - } - - @Override - public Set getUserSessions(RealmModel realm, ClientModel client) { - return model.getUserSessions(realm, client); - } - - @Override - public int getActiveUserSessions(RealmModel realm, ClientModel client) { - return model.getActiveUserSessions(realm, client); - } - - @Override - public void removeUserSession(UserSessionModel session) { - model.removeUserSession(session); - } - - @Override - public void removeUserSessions(RealmModel realm, UserModel user) { - model.removeUserSessions(realm, user); - } - - @Override - public void removeExpiredUserSessions(RealmModel realm) { - model.removeExpiredUserSessions(realm); - } - - @Override - public void removeUserSessions(RealmModel realm) { - model.removeUserSessions(realm); - } - - @Override - public void removeAllData() { - model.removeAllData(); + public ModelProvider getModel() { + return model; } public void close() { diff --git a/services/src/main/java/org/keycloak/services/managers/ApplianceBootstrap.java b/services/src/main/java/org/keycloak/services/managers/ApplianceBootstrap.java index 8e4440c1cb..76f1376cd8 100755 --- a/services/src/main/java/org/keycloak/services/managers/ApplianceBootstrap.java +++ b/services/src/main/java/org/keycloak/services/managers/ApplianceBootstrap.java @@ -8,6 +8,7 @@ import org.keycloak.models.AuthenticationProviderModel; import org.keycloak.models.Constants; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; +import org.keycloak.models.ModelProvider; import org.keycloak.models.RealmModel; import org.keycloak.models.RoleModel; import org.keycloak.models.UserCredentialModel; @@ -39,7 +40,7 @@ public class ApplianceBootstrap { public void bootstrap(KeycloakSession session, String contextPath) { String adminRealmName = Config.getAdminRealm(); - if (session.getRealm(adminRealmName) != null) { + if (session.getModel().getRealm(adminRealmName) != null) { return; } diff --git a/services/src/main/java/org/keycloak/services/managers/BruteForceProtector.java b/services/src/main/java/org/keycloak/services/managers/BruteForceProtector.java index f44b752178..09593b8b01 100755 --- a/services/src/main/java/org/keycloak/services/managers/BruteForceProtector.java +++ b/services/src/main/java/org/keycloak/services/managers/BruteForceProtector.java @@ -128,7 +128,7 @@ public class BruteForceProtector implements Runnable { } protected RealmModel getRealmModel(KeycloakSession session, LoginEvent event) { - RealmModel realm = session.getRealm(event.realmId); + RealmModel realm = session.getModel().getRealm(event.realmId); if (realm == null) return null; return 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 fd2c61fc0d..e4359d27b0 100755 --- a/services/src/main/java/org/keycloak/services/managers/RealmManager.java +++ b/services/src/main/java/org/keycloak/services/managers/RealmManager.java @@ -10,6 +10,7 @@ import org.keycloak.models.ClientModel; import org.keycloak.Config; import org.keycloak.models.Constants; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.ModelProvider; import org.keycloak.models.OAuthClientModel; import org.keycloak.models.PasswordPolicy; import org.keycloak.models.RealmModel; @@ -53,6 +54,7 @@ public class RealmManager { protected static final Logger logger = Logger.getLogger(RealmManager.class); protected KeycloakSession session; + protected ModelProvider model; protected String contextPath = ""; public String getContextPath() { @@ -65,6 +67,7 @@ public class RealmManager { public RealmManager(KeycloakSession session) { this.session = session; + this.model = session.getModel(); } public RealmModel getKeycloakAdminstrationRealm() { @@ -72,11 +75,11 @@ public class RealmManager { } public RealmModel getRealm(String id) { - return session.getRealm(id); + return model.getRealm(id); } public RealmModel getRealmByName(String name) { - return session.getRealmByName(name); + return model.getRealmByName(name); } public RealmModel createRealm(String name) { @@ -85,7 +88,7 @@ public class RealmManager { public RealmModel createRealm(String id, String name) { if (id == null) id = KeycloakModelUtils.generateId(); - RealmModel realm = session.createRealm(id, name); + RealmModel realm = model.createRealm(id, name); realm.setName(name); // setup defaults @@ -143,7 +146,7 @@ public class RealmManager { } public boolean removeRealm(RealmModel realm) { - boolean removed = session.removeRealm(realm.getId()); + boolean removed = model.removeRealm(realm.getId()); if (removed) { getKeycloakAdminstrationRealm().removeApplication(realm.getMasterAdminApp().getId()); } @@ -244,7 +247,7 @@ public class RealmManager { RoleModel createRealmRole = realm.addRole(AdminRoles.CREATE_REALM); adminRole.addCompositeRole(createRealmRole); } else { - adminRealm = session.getRealmByName(Config.getAdminRealm()); + adminRealm = model.getRealmByName(Config.getAdminRealm()); adminRole = adminRealm.getRole(AdminRoles.ADMIN); } diff --git a/services/src/main/java/org/keycloak/services/resources/admin/AdminConsole.java b/services/src/main/java/org/keycloak/services/resources/admin/AdminConsole.java index a7c2f7d10a..d3f6920a96 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/AdminConsole.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/AdminConsole.java @@ -12,6 +12,7 @@ import org.keycloak.models.AdminRoles; import org.keycloak.models.ApplicationModel; import org.keycloak.models.Constants; import org.keycloak.models.KeycloakSession; +import org.keycloak.models.ModelProvider; import org.keycloak.models.RealmModel; import org.keycloak.models.RoleModel; import org.keycloak.models.UserModel; @@ -224,7 +225,7 @@ public class AdminConsole { } private void addMasterRealmAccess(RealmModel masterRealm, UserModel user, Map> realmAdminAccess) { - List realms = session.getRealms(); + List realms = session.getModel().getRealms(); for (RealmModel realm : realms) { ApplicationModel realmAdminApp = realm.getMasterAdminApp(); Set roles = realmAdminApp.getRoles(); 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 7600c871af..e5662063ee 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 @@ -87,7 +87,7 @@ public class RealmsAdminResource { RealmManager realmManager = new RealmManager(session); List reps = new ArrayList(); if (auth.getRealm().equals(realmManager.getKeycloakAdminstrationRealm())) { - List realms = session.getRealms(); + List realms = session.getModel().getRealms(); for (RealmModel realm : realms) { addRealmRep(reps, realm, realm.getMasterAdminApp()); } diff --git a/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredAuditEvents.java b/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredAuditEvents.java index 5884f49845..5345423e11 100644 --- a/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredAuditEvents.java +++ b/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredAuditEvents.java @@ -13,7 +13,7 @@ public class ClearExpiredAuditEvents implements ScheduledTask { public void run(KeycloakSession session) { AuditProvider audit = session.getProvider(AuditProvider.class); if (audit != null) { - for (RealmModel realm : session.getRealms()) { + for (RealmModel realm : session.getModel().getRealms()) { if (realm.isAuditEnabled() && realm.getAuditExpiration() > 0) { long olderThan = System.currentTimeMillis() - realm.getAuditExpiration() * 1000; audit.clear(realm.getId(), olderThan); diff --git a/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredUserSessions.java b/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredUserSessions.java index b6b5f993b5..c6f4361d0a 100755 --- a/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredUserSessions.java +++ b/services/src/main/java/org/keycloak/services/scheduled/ClearExpiredUserSessions.java @@ -10,7 +10,7 @@ public class ClearExpiredUserSessions implements ScheduledTask { @Override public void run(KeycloakSession session) { - for (RealmModel realm : session.getRealms()) { + for (RealmModel realm : session.getModel().getRealms()) { realm.removeExpiredUserSessions(); } } diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/AdapterTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/AdapterTest.java index 1cc65cafc1..aba2beb488 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/AdapterTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/AdapterTest.java @@ -231,7 +231,7 @@ public class AdapterTest { Assert.assertTrue(pageSource.contains("Bill Burke") && pageSource.contains("Stian Thorgersen")); KeycloakSession session = keycloakRule.startSession(); - RealmModel realm = session.getRealmByName("demo"); + RealmModel realm = session.getModel().getRealmByName("demo"); int originalIdle = realm.getSsoSessionIdleTimeout(); realm.setSsoSessionIdleTimeout(1); session.getTransaction().commit(); @@ -245,7 +245,7 @@ public class AdapterTest { Assert.assertTrue(driver.getCurrentUrl().startsWith(LOGIN_URL)); session = keycloakRule.startSession(); - realm = session.getRealmByName("demo"); + realm = session.getModel().getRealmByName("demo"); realm.setSsoSessionIdleTimeout(originalIdle); session.getTransaction().commit(); session.close(); @@ -265,7 +265,7 @@ public class AdapterTest { Assert.assertTrue(pageSource.contains("Bill Burke") && pageSource.contains("Stian Thorgersen")); KeycloakSession session = keycloakRule.startSession(); - RealmModel realm = session.getRealmByName("demo"); + RealmModel realm = session.getModel().getRealmByName("demo"); int originalIdle = realm.getSsoSessionIdleTimeout(); realm.setSsoSessionIdleTimeout(1); session.getTransaction().commit(); @@ -274,7 +274,7 @@ public class AdapterTest { Thread.sleep(2000); session = keycloakRule.startSession(); - realm = session.getRealmByName("demo"); + realm = session.getModel().getRealmByName("demo"); realm.removeExpiredUserSessions(); session.getTransaction().commit(); session.close(); @@ -284,7 +284,7 @@ public class AdapterTest { Assert.assertTrue(driver.getCurrentUrl().startsWith(LOGIN_URL)); session = keycloakRule.startSession(); - realm = session.getRealmByName("demo"); + realm = session.getModel().getRealmByName("demo"); realm.setSsoSessionIdleTimeout(originalIdle); session.getTransaction().commit(); session.close(); @@ -304,7 +304,7 @@ public class AdapterTest { Assert.assertTrue(pageSource.contains("Bill Burke") && pageSource.contains("Stian Thorgersen")); KeycloakSession session = keycloakRule.startSession(); - RealmModel realm = session.getRealmByName("demo"); + RealmModel realm = session.getModel().getRealmByName("demo"); int original = realm.getSsoSessionMaxLifespan(); realm.setSsoSessionMaxLifespan(1); session.getTransaction().commit(); @@ -318,7 +318,7 @@ public class AdapterTest { Assert.assertTrue(driver.getCurrentUrl().startsWith(LOGIN_URL)); session = keycloakRule.startSession(); - realm = session.getRealmByName("demo"); + realm = session.getModel().getRealmByName("demo"); realm.setSsoSessionMaxLifespan(original); session.getTransaction().commit(); session.close(); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/oauth/RefreshTokenTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/oauth/RefreshTokenTest.java index 43f386e431..1e7468749a 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/oauth/RefreshTokenTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/oauth/RefreshTokenTest.java @@ -183,7 +183,7 @@ public class RefreshTokenTest { String refreshId = oauth.verifyRefreshToken(tokenResponse.getRefreshToken()).getId(); KeycloakSession session = keycloakRule.startSession(); - RealmModel realm = session.getRealmByName("test"); + RealmModel realm = session.getModel().getRealmByName("test"); UserSessionModel userSession = realm.getUserSession(sessionId); int last = userSession.getLastSessionRefresh(); session.getTransaction().commit(); @@ -199,7 +199,7 @@ public class RefreshTokenTest { Assert.assertEquals(200, tokenResponse.getStatusCode()); session = keycloakRule.startSession(); - realm = session.getRealmByName("test"); + realm = session.getModel().getRealmByName("test"); userSession = realm.getUserSession(sessionId); int next = userSession.getLastSessionRefresh(); session.getTransaction().commit(); @@ -211,7 +211,7 @@ public class RefreshTokenTest { session = keycloakRule.startSession(); - realm = session.getRealmByName("test"); + realm = session.getModel().getRealmByName("test"); int lastAccessTokenLifespan = realm.getAccessTokenLifespan(); realm.setAccessTokenLifespan(100000); session.getTransaction().commit(); @@ -221,7 +221,7 @@ public class RefreshTokenTest { tokenResponse = oauth.doRefreshTokenRequest(tokenResponse.getRefreshToken(), "password"); session = keycloakRule.startSession(); - realm = session.getRealmByName("test"); + realm = session.getModel().getRealmByName("test"); userSession = realm.getUserSession(sessionId); next = userSession.getLastSessionRefresh(); session.getTransaction().commit(); @@ -231,7 +231,7 @@ public class RefreshTokenTest { Assert.assertThat(next, allOf(greaterThan(last), lessThan(last + 6))); session = keycloakRule.startSession(); - realm = session.getRealmByName("test"); + realm = session.getModel().getRealmByName("test"); int originalIdle = realm.getSsoSessionIdleTimeout(); realm.setSsoSessionIdleTimeout(1); session.getTransaction().commit(); @@ -249,7 +249,7 @@ public class RefreshTokenTest { events.expectRefresh(refreshId, sessionId).error(Errors.INVALID_TOKEN); session = keycloakRule.startSession(); - realm = session.getRealmByName("test"); + realm = session.getModel().getRealmByName("test"); realm.setSsoSessionIdleTimeout(originalIdle); realm.setAccessTokenLifespan(lastAccessTokenLifespan); session.getTransaction().commit(); @@ -274,7 +274,7 @@ public class RefreshTokenTest { String refreshId = oauth.verifyRefreshToken(tokenResponse.getRefreshToken()).getId(); KeycloakSession session = keycloakRule.startSession(); - RealmModel realm = session.getRealmByName("test"); + RealmModel realm = session.getModel().getRealmByName("test"); int maxLifespan = realm.getSsoSessionMaxLifespan(); realm.setSsoSessionMaxLifespan(1); session.getTransaction().commit(); @@ -289,7 +289,7 @@ public class RefreshTokenTest { assertNull(tokenResponse.getRefreshToken()); session = keycloakRule.startSession(); - realm = session.getRealmByName("test"); + realm = session.getModel().getRealmByName("test"); realm.setSsoSessionMaxLifespan(maxLifespan); session.getTransaction().commit(); session.close(); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java index 2db2269e43..0871feb84d 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/AbstractKeycloakRule.java @@ -40,7 +40,7 @@ public abstract class AbstractKeycloakRule extends ExternalResource { KeycloakSession session = server.getSessionFactory().create(); session.getTransaction().begin(); try { - UserModel user = session.getRealmByName(realm).getUser(name); + UserModel user = session.getModel().getRealmByName(realm).getUser(name); return user != null ? ModelToRepresentation.toRepresentation(user) : null; } finally { session.close(); @@ -51,7 +51,7 @@ public abstract class AbstractKeycloakRule extends ExternalResource { KeycloakSession session = server.getSessionFactory().create(); session.getTransaction().begin(); try { - return ModelToRepresentation.toRepresentation(session.getRealmByName(realm).getUserById(id)); + return ModelToRepresentation.toRepresentation(session.getModel().getRealmByName(realm).getUserById(id)); } finally { session.close(); } diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/KeycloakRule.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/KeycloakRule.java index d502c6e274..bb1ccf66bf 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/KeycloakRule.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/rule/KeycloakRule.java @@ -100,7 +100,7 @@ public class KeycloakRule extends AbstractKeycloakRule { public void removeUserSession(String sessionId) { KeycloakSession session = startSession(); - RealmModel realm = session.getRealm("test"); + RealmModel realm = session.getModel().getRealm("test"); UserSessionModel userSession = realm.getUserSession(sessionId); assertNotNull(userSession); realm.removeUserSession(userSession); diff --git a/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/BaseJMeterPerformanceTest.java b/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/BaseJMeterPerformanceTest.java index ed5c98e097..53332dbb4c 100755 --- a/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/BaseJMeterPerformanceTest.java +++ b/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/BaseJMeterPerformanceTest.java @@ -40,7 +40,7 @@ public class BaseJMeterPerformanceTest extends AbstractJavaSamplerClient { try { String adminRealmName = Config.getAdminRealm(); - if (keycloakSession.getRealm(adminRealmName) == null) { + if (keycloakSession.getModel().getRealm(adminRealmName) == null) { RealmManager manager = new RealmManager(keycloakSession); manager.setContextPath(contextPath); diff --git a/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/CreateUsersWorker.java b/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/CreateUsersWorker.java index ce2f08a314..b523842a30 100755 --- a/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/CreateUsersWorker.java +++ b/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/CreateUsersWorker.java @@ -59,7 +59,7 @@ public class CreateUsersWorker implements Worker { @Override public void run(SampleResult result, KeycloakSession session) { // We need to obtain realm first - RealmModel realm = session.getRealm(realmId); + RealmModel realm = session.getModel().getRealm(realmId); if (realm == null) { throw new IllegalStateException("Realm '" + realmId + "' not found"); } diff --git a/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/ReadUsersWorker.java b/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/ReadUsersWorker.java index 35616ac9f2..dc74290fa8 100755 --- a/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/ReadUsersWorker.java +++ b/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/ReadUsersWorker.java @@ -68,7 +68,7 @@ public class ReadUsersWorker implements Worker { @Override public void run(SampleResult result, KeycloakSession session) { // We need to obtain realm first - RealmModel realm = session.getRealm(realmId); + RealmModel realm = session.getModel().getRealm(realmId); if (realm == null) { throw new IllegalStateException("Realm '" + realmId + "' not found"); } diff --git a/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/RemoveUsersWorker.java b/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/RemoveUsersWorker.java index 20f0fd63f1..38d7ccb2ea 100755 --- a/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/RemoveUsersWorker.java +++ b/testsuite/performance/src/test/java/org/keycloak/testsuite/performance/RemoveUsersWorker.java @@ -32,7 +32,7 @@ public class RemoveUsersWorker implements Worker { int realmNumber = realmsOffset + workerId; String realmId = PerfTestUtils.getRealmName(realmNumber); - realm = session.getRealm(realmId); + realm = session.getModel().getRealm(realmId); if (realm == null) { throw new IllegalStateException("Realm '" + realmId + "' not found"); } diff --git a/testsuite/tools/src/main/java/org/keycloak/test/tools/PerfTools.java b/testsuite/tools/src/main/java/org/keycloak/test/tools/PerfTools.java index 30906545c8..f3cf2edb87 100644 --- a/testsuite/tools/src/main/java/org/keycloak/test/tools/PerfTools.java +++ b/testsuite/tools/src/main/java/org/keycloak/test/tools/PerfTools.java @@ -170,7 +170,7 @@ public class PerfTools { } private int getUsersCount(String realmName, String prefix) { - RealmModel realm = session.getRealmByName(realmName); + RealmModel realm = session.getModel().getRealmByName(realmName); // TODO: method for count on model if (prefix == null) {