From 099743d448e38016ea92cd865b83d990a339ad43 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Mon, 7 Jul 2014 15:21:54 +0100 Subject: [PATCH 1/3] Rename loginName to username --- .../AuthenticationProviderManager.java | 18 +++++++++--------- .../AbstractModelAuthenticationProvider.java | 4 ++-- .../PicketlinkAuthenticationProvider.java | 2 +- .../PropertiesAuthenticationProvider.java | 2 +- .../keycloak/exportimport/ModelExporter.java | 2 +- .../keycloak/exportimport/ModelImporter.java | 2 +- .../account/freemarker/model/AccountBean.java | 2 +- .../java/org/keycloak/models/UserModel.java | 4 ++-- .../keycloak/models/entities/UserEntity.java | 10 +++++----- .../org/keycloak/models/cache/UserAdapter.java | 10 +++++----- .../models/cache/entities/CachedUser.java | 10 +++++----- .../keycloak/models/jpa/JpaModelProvider.java | 10 +++++----- .../org/keycloak/models/jpa/RealmAdapter.java | 6 +++--- .../org/keycloak/models/jpa/UserAdapter.java | 8 ++++---- .../models/jpa/entities/UserEntity.java | 14 +++++++------- .../keycloak/adapters/MongoModelProvider.java | 6 +++--- .../mongo/keycloak/adapters/RealmAdapter.java | 4 ++-- .../mongo/keycloak/adapters/UserAdapter.java | 8 ++++---- .../keycloak/entities/MongoRoleEntity.java | 4 ++-- .../keycloak/entities/MongoUserEntity.java | 2 +- .../org/keycloak/model/test/AdapterTest.java | 6 +++--- .../test/AuthProvidersExternalModelTest.java | 2 +- .../model/test/AuthProvidersLDAPTest.java | 2 +- .../org/keycloak/model/test/ImportTest.java | 2 +- .../model/test/MultipleRealmsTest.java | 2 +- .../org/keycloak/model/test/UserModelTest.java | 2 +- .../ups/security/UpsSecurityApplication.java | 2 +- .../managers/AuthenticationManager.java | 4 ++-- .../managers/ModelToRepresentation.java | 4 ++-- .../services/managers/RealmManager.java | 2 +- .../managers/ResourceAdminManager.java | 2 +- .../services/managers/TokenManager.java | 4 ++-- .../services/resources/AccountService.java | 2 +- .../resources/RequiredActionsService.java | 2 +- .../services/resources/SocialResource.java | 2 +- .../services/resources/TokenService.java | 2 +- .../services/resources/admin/AdminConsole.java | 2 +- .../services/resources/admin/AdminRoot.java | 2 +- .../resources/admin/UsersResource.java | 2 +- .../org/keycloak/test/tools/PerfTools.java | 2 +- 40 files changed, 89 insertions(+), 89 deletions(-) diff --git a/authentication/authentication-api/src/main/java/org/keycloak/authentication/AuthenticationProviderManager.java b/authentication/authentication-api/src/main/java/org/keycloak/authentication/AuthenticationProviderManager.java index 20d08bbf35..9c8166585e 100755 --- a/authentication/authentication-api/src/main/java/org/keycloak/authentication/AuthenticationProviderManager.java +++ b/authentication/authentication-api/src/main/java/org/keycloak/authentication/AuthenticationProviderManager.java @@ -70,7 +70,7 @@ public class AuthenticationProviderManager { AuthenticationLinkModel authLink = user.getAuthenticationLink(); if (authLink == null) { // User not yet linked with any authenticationProvider. Find provider with biggest priority where he is and link - AuthUser authUser = getUser(user.getLoginName()); + AuthUser authUser = getUser(user.getUsername()); authLink = new AuthenticationLinkModel(authUser.getProviderName(), authUser.getId()); user.setAuthenticationLink(authLink); logger.infof("User '%s' linked with provider '%s'", authUser.getUsername(), authUser.getProviderName()); @@ -85,10 +85,10 @@ public class AuthenticationProviderManager { } try { - checkCorrectAuthLink(delegate, providerModel, authLink, user.getLoginName()); + checkCorrectAuthLink(delegate, providerModel, authLink, user.getUsername()); - AuthProviderStatus currentResult = delegate.validatePassword(realm, providerModel.getConfig(), user.getLoginName(), password); - logger.debugf("Authentication provider '%s' finished with '%s' for authentication of '%s'", delegate.getName(), currentResult.toString(), user.getLoginName()); + AuthProviderStatus currentResult = delegate.validatePassword(realm, providerModel.getConfig(), user.getUsername(), password); + logger.debugf("Authentication provider '%s' finished with '%s' for authentication of '%s'", delegate.getName(), currentResult.toString(), user.getUsername()); return currentResult; } catch (AuthenticationProviderException ape) { logger.warn(ape.getMessage(), ape); @@ -105,7 +105,7 @@ public class AuthenticationProviderManager { if (providerModel.isPasswordUpdateSupported()) { AuthenticationProvider delegate = getProvider(providerModel.getProviderName()); if (delegate != null) { - AuthUser authUser = delegate.getUser(realm, providerModel.getConfig(), user.getLoginName()); + AuthUser authUser = delegate.getUser(realm, providerModel.getConfig(), user.getUsername()); if (authUser != null) { // Linking existing user supported just for "model" provider. In other cases throw exception if (providerModel.getProviderName().equals(AuthenticationProviderModel.DEFAULT_PROVIDER.getProviderName())) { @@ -120,7 +120,7 @@ public class AuthenticationProviderManager { String userIdInProvider = delegate.registerUser(realm, providerModel.getConfig(), user); authLink = new AuthenticationLinkModel(providerModel.getProviderName(), userIdInProvider); user.setAuthenticationLink(authLink); - logger.infof("User '%s' registered in provider '%s' and linked", user.getLoginName(), providerModel.getProviderName()); + logger.infof("User '%s' registered in provider '%s' and linked", user.getUsername(), providerModel.getProviderName()); } break; } @@ -128,7 +128,7 @@ public class AuthenticationProviderManager { } if (authLink == null) { - logger.warnf("No providers found where password update is supported for user '%s'", user.getLoginName()); + logger.warnf("No providers found where password update is supported for user '%s'", user.getUsername()); return false; } } @@ -140,7 +140,7 @@ public class AuthenticationProviderManager { return false; } - String username = user.getLoginName(); + String username = user.getUsername(); // Update just if password update is supported if (providerModel.isPasswordUpdateSupported()) { @@ -152,7 +152,7 @@ public class AuthenticationProviderManager { checkCorrectAuthLink(delegate, providerModel, authLink, username); - if (delegate.updateCredential(realm,providerModel.getConfig(), user.getLoginName(), password)) { + if (delegate.updateCredential(realm,providerModel.getConfig(), user.getUsername(), password)) { logger.debugf("Updated password in authentication provider '%s' for user '%s'", providerName, username); return true; } else { diff --git a/authentication/authentication-model/src/main/java/org/keycloak/authentication/model/AbstractModelAuthenticationProvider.java b/authentication/authentication-model/src/main/java/org/keycloak/authentication/model/AbstractModelAuthenticationProvider.java index ea20abcaba..d3433a98e7 100755 --- a/authentication/authentication-model/src/main/java/org/keycloak/authentication/model/AbstractModelAuthenticationProvider.java +++ b/authentication/authentication-model/src/main/java/org/keycloak/authentication/model/AbstractModelAuthenticationProvider.java @@ -32,7 +32,7 @@ public abstract class AbstractModelAuthenticationProvider implements Authenticat @Override public String registerUser(RealmModel currentRealm, Map config, UserModel user) throws AuthenticationProviderException { RealmModel realm = getRealm(currentRealm, config); - UserModel newUser = realm.addUser(user.getLoginName()); + UserModel newUser = realm.addUser(user.getUsername()); newUser.setFirstName(user.getFirstName()); newUser.setLastName(user.getLastName()); newUser.setEmail(user.getEmail()); @@ -80,7 +80,7 @@ public abstract class AbstractModelAuthenticationProvider implements Authenticat protected abstract RealmModel getRealm(RealmModel currentRealm, Map config) throws AuthenticationProviderException; protected AuthUser createAuthenticatedUserInstance(UserModel user) { - return new AuthUser(user.getId(), user.getLoginName(), getName()) + return new AuthUser(user.getId(), user.getUsername(), getName()) .setName(user.getFirstName(), user.getLastName()) .setEmail(user.getEmail()); } diff --git a/authentication/authentication-picketlink/src/main/java/org/keycloak/authentication/picketlink/PicketlinkAuthenticationProvider.java b/authentication/authentication-picketlink/src/main/java/org/keycloak/authentication/picketlink/PicketlinkAuthenticationProvider.java index 772f6da6e0..6cbcaad1c0 100755 --- a/authentication/authentication-picketlink/src/main/java/org/keycloak/authentication/picketlink/PicketlinkAuthenticationProvider.java +++ b/authentication/authentication-picketlink/src/main/java/org/keycloak/authentication/picketlink/PicketlinkAuthenticationProvider.java @@ -72,7 +72,7 @@ public class PicketlinkAuthenticationProvider implements AuthenticationProvider IdentityManager identityManager = getIdentityManager(realm); try { - User picketlinkUser = new User(user.getLoginName()); + User picketlinkUser = new User(user.getUsername()); picketlinkUser.setFirstName(user.getFirstName()); picketlinkUser.setLastName(user.getLastName()); picketlinkUser.setEmail(user.getEmail()); diff --git a/examples/providers/authentication-properties/src/main/java/org/keycloak/examples/providers/authentication/PropertiesAuthenticationProvider.java b/examples/providers/authentication-properties/src/main/java/org/keycloak/examples/providers/authentication/PropertiesAuthenticationProvider.java index 9b83ec0de8..8c5ff1af16 100644 --- a/examples/providers/authentication-properties/src/main/java/org/keycloak/examples/providers/authentication/PropertiesAuthenticationProvider.java +++ b/examples/providers/authentication-properties/src/main/java/org/keycloak/examples/providers/authentication/PropertiesAuthenticationProvider.java @@ -48,7 +48,7 @@ public class PropertiesAuthenticationProvider implements AuthenticationProvider @Override public String registerUser(RealmModel realm, Map configuration, UserModel user) throws AuthenticationProviderException { // Registration ignored - return user.getLoginName(); + return user.getUsername(); } @Override 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 eb6b363382..951ea73b56 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 @@ -211,7 +211,7 @@ public class ModelExporter { this.propertiesManager.setBasicPropertiesFromModel(userModel, userEntity); - userEntity.setLoginName(userModel.getLoginName()); + userEntity.setUsername(userModel.getUsername()); userEntity.setRealmId(realm.getId()); // authentication links 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 6ff1fa911f..c187bb85eb 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 @@ -243,7 +243,7 @@ public class ModelImporter { List users = this.importReader.readEntities(fileName, UserEntity.class); for (UserEntity userEntity : users) { RealmModel realm = session.getRealm(userEntity.getRealmId()); - UserModel user = realm.addUser(userEntity.getId(), userEntity.getLoginName()); + UserModel user = realm.addUser(userEntity.getId(), userEntity.getUsername()); // We need to remove defaultRoles here as realm.addUser is automatically adding them. We may add them later during roles mapping processing for (RoleModel role : user.getRoleMappings()) { diff --git a/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/model/AccountBean.java b/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/model/AccountBean.java index b07d38bed3..06ef6c2ba8 100644 --- a/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/model/AccountBean.java +++ b/forms/account-freemarker/src/main/java/org/keycloak/account/freemarker/model/AccountBean.java @@ -22,7 +22,7 @@ public class AccountBean { } public String getUsername() { - return user.getLoginName(); + return user.getUsername(); } public String getEmail() { diff --git a/model/api/src/main/java/org/keycloak/models/UserModel.java b/model/api/src/main/java/org/keycloak/models/UserModel.java index 3a7bd0b195..08c02a3f3e 100755 --- a/model/api/src/main/java/org/keycloak/models/UserModel.java +++ b/model/api/src/main/java/org/keycloak/models/UserModel.java @@ -16,9 +16,9 @@ public interface UserModel { String getId(); - String getLoginName(); + String getUsername(); - void setLoginName(String loginName); + void setUsername(String username); boolean isEnabled(); diff --git a/model/api/src/main/java/org/keycloak/models/entities/UserEntity.java b/model/api/src/main/java/org/keycloak/models/entities/UserEntity.java index f1248b087d..d1bd1578fc 100644 --- a/model/api/src/main/java/org/keycloak/models/entities/UserEntity.java +++ b/model/api/src/main/java/org/keycloak/models/entities/UserEntity.java @@ -11,7 +11,7 @@ import org.keycloak.models.UserModel; */ public class UserEntity extends AbstractIdentifiableEntity { - private String loginName; + private String username; private String firstName; private String lastName; private String email; @@ -30,12 +30,12 @@ public class UserEntity extends AbstractIdentifiableEntity { private List socialLinks; private AuthenticationLinkEntity authenticationLink; - public String getLoginName() { - return loginName; + public String getUsername() { + return username; } - public void setLoginName(String loginName) { - this.loginName = loginName; + public void setUsername(String username) { + this.username = username; } public String getFirstName() { diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/UserAdapter.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/UserAdapter.java index 727244c0a2..b586df5a7a 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/UserAdapter.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/UserAdapter.java @@ -47,15 +47,15 @@ public class UserAdapter implements UserModel { } @Override - public String getLoginName() { - if (updated != null) return updated.getLoginName(); - return cached.getLoginName(); + public String getUsername() { + if (updated != null) return updated.getUsername(); + return cached.getUsername(); } @Override - public void setLoginName(String loginName) { + public void setUsername(String username) { getDelegateForUpdate(); - updated.setLoginName(loginName); + updated.setUsername(username); } @Override diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedUser.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedUser.java index 465f08fc51..bc2a338ce3 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedUser.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedUser.java @@ -19,7 +19,7 @@ import java.util.Set; */ public class CachedUser { private String id; - private String loginName; + private String username; private String usernameKey; private String firstName; private String lastName; @@ -38,8 +38,8 @@ public class CachedUser { public CachedUser(RealmModel realm, UserModel user) { this.id = user.getId(); - this.loginName = user.getLoginName(); - this.usernameKey = realm.getId() + "." + this.loginName; + this.username = user.getUsername(); + this.usernameKey = realm.getId() + "." + this.username; this.firstName = user.getFirstName(); this.lastName = user.getLastName(); this.attributes.putAll(user.getAttributes()); @@ -63,8 +63,8 @@ public class CachedUser { return id; } - public String getLoginName() { - return loginName; + public String getUsername() { + return username; } public String getUsernameKey() { 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 b750373513..940e0671ff 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 @@ -93,8 +93,8 @@ public class JpaModelProvider implements ModelProvider { @Override public UserModel getUserByUsername(String username, RealmModel realmModel) { - TypedQuery query = em.createNamedQuery("getRealmUserByLoginName", UserEntity.class); - query.setParameter("loginName", username); + TypedQuery query = em.createNamedQuery("getRealmUserByUsername", UserEntity.class); + query.setParameter("username", username); RealmEntity realm = em.getReference(RealmEntity.class, realmModel.getId()); query.setParameter("realm", realm); List results = query.getResultList(); @@ -130,7 +130,7 @@ public class JpaModelProvider implements ModelProvider { } for (UserEntity u : em.createQuery("from UserEntity u where u.realm = :realm", UserEntity.class).setParameter("realm", realm).getResultList()) { - adapter.removeUser(u.getLoginName()); + adapter.removeUser(u.getUsername()); } em.remove(realm); @@ -185,7 +185,7 @@ public class JpaModelProvider implements ModelProvider { @Override public List searchForUser(String search, RealmModel realm) { - TypedQuery query = em.createQuery("select u from UserEntity u where u.realm = :realm and ( lower(u.loginName) like :search or lower(concat(u.firstName, ' ', u.lastName)) like :search or u.email like :search )", UserEntity.class); + TypedQuery query = em.createQuery("select u from UserEntity u where u.realm = :realm and ( lower(u.username) like :search or lower(concat(u.firstName, ' ', u.lastName)) like :search or u.email like :search )", UserEntity.class); RealmEntity realmEntity = em.getReference(RealmEntity.class, realm.getId()); query.setParameter("realm", realmEntity); query.setParameter("search", "%" + search.toLowerCase() + "%"); @@ -202,7 +202,7 @@ public class JpaModelProvider implements ModelProvider { for (Map.Entry entry : attributes.entrySet()) { String attribute = null; if (entry.getKey().equals(UserModel.LOGIN_NAME)) { - attribute = "lower(loginName)"; + attribute = "lower(username)"; } else if (entry.getKey().equalsIgnoreCase(UserModel.FIRST_NAME)) { attribute = "lower(firstName)"; } else if (entry.getKey().equalsIgnoreCase(UserModel.LAST_NAME)) { 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 a985f3d404..ed37b2895e 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 @@ -462,7 +462,7 @@ public class RealmAdapter implements RealmModel { public UserModel addUser(String id, String username) { UserEntity entity = new UserEntity(); entity.setId(id); - entity.setLoginName(username); + entity.setUsername(username); entity.setRealm(realm); em.persist(entity); em.flush(); @@ -483,8 +483,8 @@ public class RealmAdapter implements RealmModel { @Override public boolean removeUser(String name) { - TypedQuery query = em.createNamedQuery("getRealmUserByLoginName", UserEntity.class); - query.setParameter("loginName", name); + TypedQuery query = em.createNamedQuery("getRealmUserByUsername", UserEntity.class); + query.setParameter("username", name); query.setParameter("realm", realm); List results = query.getResultList(); if (results.size() == 0) return false; diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java index e86b42fa73..8e613bb585 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java @@ -53,13 +53,13 @@ public class UserAdapter implements UserModel { } @Override - public String getLoginName() { - return user.getLoginName(); + public String getUsername() { + return user.getUsername(); } @Override - public void setLoginName(String loginName) { - user.setLoginName(loginName); + public void setUsername(String username) { + user.setUsername(username); } @Override diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java index 3b765b4443..075fc8709c 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java @@ -35,21 +35,21 @@ import java.util.Set; */ @NamedQueries({ @NamedQuery(name="getRealmUserById", query="select u from UserEntity u where u.id = :id and u.realm = :realm"), - @NamedQuery(name="getRealmUserByLoginName", query="select u from UserEntity u where u.loginName = :loginName and u.realm = :realm"), + @NamedQuery(name="getRealmUserByUsername", query="select u from UserEntity u where u.username = :username and u.realm = :realm"), @NamedQuery(name="getRealmUserByEmail", query="select u from UserEntity u where u.email = :email and u.realm = :realm"), @NamedQuery(name="getRealmUserByLastName", query="select u from UserEntity u where u.lastName = :lastName and u.realm = :realm"), @NamedQuery(name="getRealmUserByFirstLastName", query="select u from UserEntity u where u.firstName = :first and u.lastName = :last and u.realm = :realm") }) @Entity @Table(uniqueConstraints = { - @UniqueConstraint(columnNames = { "realm", "loginName" }), + @UniqueConstraint(columnNames = { "realm", "username" }), @UniqueConstraint(columnNames = { "realm", "emailConstraint" }) }) public class UserEntity { @Id protected String id; - protected String loginName; + protected String username; protected String firstName; protected String lastName; protected String email; @@ -89,12 +89,12 @@ public class UserEntity { this.id = id; } - public String getLoginName() { - return loginName; + public String getUsername() { + return username; } - public void setLoginName(String loginName) { - this.loginName = loginName; + public void setUsername(String username) { + this.username = username; } public String getFirstName() { 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 739bbc09fd..efc827753f 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 @@ -132,7 +132,7 @@ public class MongoModelProvider implements ModelProvider { @Override public UserModel getUserByUsername(String username, RealmModel realm) { DBObject query = new QueryBuilder() - .and("loginName").is(username) + .and("username").is(username) .and("realmId").is(realm.getId()) .get(); MongoUserEntity user = getMongoStore().loadSingleEntity(MongoUserEntity.class, query, invocationContext); @@ -226,7 +226,7 @@ public class MongoModelProvider implements ModelProvider { QueryBuilder builder = new QueryBuilder().and( new QueryBuilder().and("realmId").is(realm.getId()).get(), new QueryBuilder().or( - new QueryBuilder().put("loginName").regex(caseInsensitivePattern).get(), + new QueryBuilder().put("username").regex(caseInsensitivePattern).get(), new QueryBuilder().put("email").regex(caseInsensitivePattern).get(), nameBuilder.get() @@ -243,7 +243,7 @@ public class MongoModelProvider implements ModelProvider { for (Map.Entry entry : attributes.entrySet()) { if (entry.getKey().equals(UserModel.LOGIN_NAME)) { - queryBuilder.and("loginName").regex(Pattern.compile("(?i:" + entry.getValue() + "$)")); + queryBuilder.and("username").regex(Pattern.compile("(?i:" + entry.getValue() + "$)")); } else if (entry.getKey().equalsIgnoreCase(UserModel.FIRST_NAME)) { queryBuilder.and(UserModel.FIRST_NAME).regex(Pattern.compile("(?i:" + entry.getValue() + "$)")); 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 2602222e17..de64faf866 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 @@ -503,7 +503,7 @@ public class RealmAdapter extends AbstractMongoAdapter impleme protected UserAdapter addUserEntity(String id, String username) { MongoUserEntity userEntity = new MongoUserEntity(); userEntity.setId(id); - userEntity.setLoginName(username); + userEntity.setUsername(username); // Compatibility with JPA model, which has user disabled by default // userEntity.setEnabled(true); userEntity.setRealmId(getId()); @@ -515,7 +515,7 @@ public class RealmAdapter extends AbstractMongoAdapter impleme @Override public boolean removeUser(String name) { DBObject query = new QueryBuilder() - .and("loginName").is(name) + .and("username").is(name) .and("realmId").is(getId()) .get(); return getMongoStore().removeEntities(MongoUserEntity.class, query, invocationContext); diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/UserAdapter.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/UserAdapter.java index e582b9367d..003a7e61b0 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/UserAdapter.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/UserAdapter.java @@ -49,13 +49,13 @@ public class UserAdapter extends AbstractMongoAdapter implement } @Override - public String getLoginName() { - return user.getLoginName(); + public String getUsername() { + return user.getUsername(); } @Override - public void setLoginName(String loginName) { - user.setLoginName(loginName); + public void setUsername(String username) { + user.setUsername(username); updateUser(); } diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoRoleEntity.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoRoleEntity.java index f931a91b9a..afc649f8d0 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoRoleEntity.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoRoleEntity.java @@ -50,7 +50,7 @@ public class MongoRoleEntity extends RoleEntity implements MongoIdentifiableEnti List users = mongoStore.loadEntities(MongoUserEntity.class, query, invContext); for (MongoUserEntity user : users) { - logger.info("Removing role " + getName() + " from user " + user.getLoginName()); + logger.info("Removing role " + getName() + " from user " + user.getUsername()); mongoStore.pullItemFromList(user, "roleIds", getId(), invContext); } @@ -61,7 +61,7 @@ public class MongoRoleEntity extends RoleEntity implements MongoIdentifiableEnti users = mongoStore.loadEntities(MongoUserEntity.class, query, invContext); for (MongoUserEntity user : users) { - logger.info("Removing scope " + getName() + " from user " + user.getLoginName()); + logger.info("Removing scope " + getName() + " from user " + user.getUsername()); mongoStore.pullItemFromList(user, "scopeIds", getId(), invContext); } diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoUserEntity.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoUserEntity.java index 084a15311a..9399ae2841 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoUserEntity.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/entities/MongoUserEntity.java @@ -14,7 +14,7 @@ import org.keycloak.models.mongo.api.context.MongoStoreInvocationContext; */ @MongoCollection(collectionName = "users") @MongoIndexes({ - @MongoIndex(fields = { "realmId", "loginName" }, unique = true), + @MongoIndex(fields = { "realmId", "username" }, unique = true), @MongoIndex(fields = { "emailIndex" }, unique = true, sparse = true), }) public class MongoUserEntity extends UserEntity implements MongoIdentifiableEntity { 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 e3f29ff22e..63962d8243 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 @@ -160,7 +160,7 @@ public class AdapterTest extends AbstractModelTest { RoleModel appRole = app.addRole("test"); user.grantRole(appRole); - SocialLinkModel socialLink = new SocialLinkModel("google", "google1", user.getLoginName()); + SocialLinkModel socialLink = new SocialLinkModel("google", "google1", user.getUsername()); realmModel.addSocialLink(user, socialLink); UserCredentialModel cred = new UserCredentialModel(); @@ -312,7 +312,7 @@ public class AdapterTest extends AbstractModelTest { { ArrayList users = new ArrayList(); for (UserModel u : adapter.searchUsers("ole alver", realmModel)) { - users.add(u.getLoginName()); + users.add(u.getUsername()); } String[] usernames = users.toArray(new String[users.size()]); Arrays.sort(usernames); @@ -625,7 +625,7 @@ public class AdapterTest extends AbstractModelTest { realmManager.getRealmByName("JUGGLER1").addUser("user2"); commit(); try { - realmManager.getRealmByName("JUGGLER1").getUser("user2").setLoginName("user1"); + realmManager.getRealmByName("JUGGLER1").getUser("user2").setUsername("user1"); commit(); Assert.fail("Expected exception"); } catch (ModelDuplicateException e) { diff --git a/model/tests/src/test/java/org/keycloak/model/test/AuthProvidersExternalModelTest.java b/model/tests/src/test/java/org/keycloak/model/test/AuthProvidersExternalModelTest.java index 5b0431687c..f2b3a77235 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/AuthProvidersExternalModelTest.java +++ b/model/tests/src/test/java/org/keycloak/model/test/AuthProvidersExternalModelTest.java @@ -91,7 +91,7 @@ public class AuthProvidersExternalModelTest extends AbstractModelTest { Assert.assertEquals(AuthenticationManager.AuthenticationStatus.SUCCESS, am.authenticateForm(null, realm2, formData)); UserModel john2 = realm2.getUser("john"); Assert.assertNotNull(john2); - Assert.assertEquals("john", john2.getLoginName()); + Assert.assertEquals("john", john2.getUsername()); Assert.assertEquals("John", john2.getFirstName()); Assert.assertEquals("Doe", john2.getLastName()); Assert.assertEquals("john@email.org", john2.getEmail()); diff --git a/model/tests/src/test/java/org/keycloak/model/test/AuthProvidersLDAPTest.java b/model/tests/src/test/java/org/keycloak/model/test/AuthProvidersLDAPTest.java index a39c03385b..10383de15d 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/AuthProvidersLDAPTest.java +++ b/model/tests/src/test/java/org/keycloak/model/test/AuthProvidersLDAPTest.java @@ -89,7 +89,7 @@ public class AuthProvidersLDAPTest extends AbstractModelTest { Assert.assertEquals(AuthenticationManager.AuthenticationStatus.SUCCESS, am.authenticateForm(null, realm, formData)); UserModel john = realm.getUser("johnkeycloak"); Assert.assertNotNull(john); - Assert.assertEquals("johnkeycloak", john.getLoginName()); + Assert.assertEquals("johnkeycloak", john.getUsername()); Assert.assertEquals("John", john.getFirstName()); Assert.assertEquals("Doe", john.getLastName()); Assert.assertEquals("john@email.org", john.getEmail()); diff --git a/model/tests/src/test/java/org/keycloak/model/test/ImportTest.java b/model/tests/src/test/java/org/keycloak/model/test/ImportTest.java index 4264ff713d..dd7d12e63d 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/ImportTest.java +++ b/model/tests/src/test/java/org/keycloak/model/test/ImportTest.java @@ -173,7 +173,7 @@ public class ImportTest extends AbstractModelTest { Assert.assertTrue(facebookFound && twitterFound && googleFound); UserModel foundSocialUser = realm.getUserBySocialLink(new SocialLinkModel("facebook", "facebook1", "fbuser1")); - Assert.assertEquals(foundSocialUser.getLoginName(), socialUser.getLoginName()); + Assert.assertEquals(foundSocialUser.getUsername(), socialUser.getUsername()); Assert.assertNull(realm.getUserBySocialLink(new SocialLinkModel("facebook", "not-existing", "not-existing"))); SocialLinkModel foundSocialLink = realm.getSocialLink(socialUser, "facebook"); 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 4ea8ab260e..a3eda8c0e6 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 @@ -33,7 +33,7 @@ public class MultipleRealmsTest extends AbstractModelTest { public void testUsers() { UserModel r1user1 = realm1.getUser("user1"); UserModel r2user1 = realm2.getUser("user1"); - Assert.assertEquals(r1user1.getLoginName(), r2user1.getLoginName()); + Assert.assertEquals(r1user1.getUsername(), r2user1.getUsername()); Assert.assertNotEquals(r1user1.getId(), r2user1.getId()); // Test password diff --git a/model/tests/src/test/java/org/keycloak/model/test/UserModelTest.java b/model/tests/src/test/java/org/keycloak/model/test/UserModelTest.java index 132e39ce5a..3de0ae2e75 100755 --- a/model/tests/src/test/java/org/keycloak/model/test/UserModelTest.java +++ b/model/tests/src/test/java/org/keycloak/model/test/UserModelTest.java @@ -111,7 +111,7 @@ public class UserModelTest extends AbstractModelTest { } public static void assertEquals(UserModel expected, UserModel actual) { - Assert.assertEquals(expected.getLoginName(), actual.getLoginName()); + Assert.assertEquals(expected.getUsername(), actual.getUsername()); Assert.assertEquals(expected.getFirstName(), actual.getFirstName()); Assert.assertEquals(expected.getLastName(), actual.getLastName()); Assert.assertArrayEquals(expected.getRequiredActions().toArray(), actual.getRequiredActions().toArray()); diff --git a/project-integrations/aerogear-ups/auth-server/src/main/java/org/aerogear/ups/security/UpsSecurityApplication.java b/project-integrations/aerogear-ups/auth-server/src/main/java/org/aerogear/ups/security/UpsSecurityApplication.java index 9f322e1e34..ef7a1efe89 100755 --- a/project-integrations/aerogear-ups/auth-server/src/main/java/org/aerogear/ups/security/UpsSecurityApplication.java +++ b/project-integrations/aerogear-ups/auth-server/src/main/java/org/aerogear/ups/security/UpsSecurityApplication.java @@ -31,7 +31,7 @@ public class UpsSecurityApplication extends KeycloakApplication { RealmManager manager = new RealmManager(session); RealmModel master = manager.getKeycloakAdminstrationRealm(); UserModel admin = master.getUser("admin"); - if (admin != null) master.removeUser(admin.getLoginName()); + if (admin != null) master.removeUser(admin.getUsername()); session.getTransaction().commit(); } finally { session.close(); diff --git a/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java b/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java index 26fe574654..2246803bb8 100755 --- a/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java +++ b/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java @@ -69,7 +69,7 @@ public class AuthenticationManager { if (session == null) return; UserModel user = session.getUser(); - logger.infov("Logging out: {0} ({1})", user.getLoginName(), session.getId()); + logger.infov("Logging out: {0} ({1})", user.getUsername(), session.getId()); realm.removeUserSession(session); expireIdentityCookie(realm, uriInfo); @@ -343,7 +343,7 @@ public class AuthenticationManager { private boolean checkEnabled(UserModel user) { if (!user.isEnabled()) { - logger.warn("AccountProvider is disabled, contact admin. " + user.getLoginName()); + logger.warn("AccountProvider is disabled, contact admin. " + user.getUsername()); return false; } else { return true; diff --git a/services/src/main/java/org/keycloak/services/managers/ModelToRepresentation.java b/services/src/main/java/org/keycloak/services/managers/ModelToRepresentation.java index 49ca015174..4a27c2d6c1 100755 --- a/services/src/main/java/org/keycloak/services/managers/ModelToRepresentation.java +++ b/services/src/main/java/org/keycloak/services/managers/ModelToRepresentation.java @@ -38,7 +38,7 @@ public class ModelToRepresentation { public static UserRepresentation toRepresentation(UserModel user) { UserRepresentation rep = new UserRepresentation(); rep.setId(user.getId()); - rep.setUsername(user.getLoginName()); + rep.setUsername(user.getUsername()); rep.setLastName(user.getLastName()); rep.setFirstName(user.getFirstName()); rep.setEmail(user.getEmail()); @@ -190,7 +190,7 @@ public class ModelToRepresentation { rep.setId(session.getId()); rep.setStart(((long)session.getStarted()) * 1000L); rep.setLastAccess(((long)session.getLastSessionRefresh())* 1000L); - rep.setUser(session.getUser().getLoginName()); + rep.setUser(session.getUser().getUsername()); rep.setIpAddress(session.getIpAddress()); for (ClientModel client : session.getClientAssociations()) { if (client instanceof ApplicationModel) { 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 6fae8e7f3e..aba62335cd 100755 --- a/services/src/main/java/org/keycloak/services/managers/RealmManager.java +++ b/services/src/main/java/org/keycloak/services/managers/RealmManager.java @@ -370,7 +370,7 @@ public class RealmManager { if (rep.getUsers() != null) { for (UserRepresentation userRep : rep.getUsers()) { UserModel user = createUser(newRealm, userRep); - userMap.put(user.getLoginName(), user); + userMap.put(user.getUsername(), user); } } diff --git a/services/src/main/java/org/keycloak/services/managers/ResourceAdminManager.java b/services/src/main/java/org/keycloak/services/managers/ResourceAdminManager.java index 2498eda523..35a118e018 100755 --- a/services/src/main/java/org/keycloak/services/managers/ResourceAdminManager.java +++ b/services/src/main/java/org/keycloak/services/managers/ResourceAdminManager.java @@ -79,7 +79,7 @@ public class ResourceAdminManager { for (Map.Entry entry : stats.getUsers().entrySet()) { UserModel user = realm.getUserById(entry.getKey()); if (user == null) continue; - newUsers.put(user.getLoginName(), entry.getValue()); + newUsers.put(user.getUsername(), entry.getValue()); } stats.setUsers(newUsers); diff --git a/services/src/main/java/org/keycloak/services/managers/TokenManager.java b/services/src/main/java/org/keycloak/services/managers/TokenManager.java index c28825a480..5b3c0000e6 100755 --- a/services/src/main/java/org/keycloak/services/managers/TokenManager.java +++ b/services/src/main/java/org/keycloak/services/managers/TokenManager.java @@ -262,7 +262,7 @@ public class TokenManager { public void initClaims(IDToken token, ClientModel model, UserModel user) { if (ClaimMask.hasUsername(model.getAllowedClaimsMask())) { - token.setPreferredUsername(user.getLoginName()); + token.setPreferredUsername(user.getUsername()); } if (ClaimMask.hasEmail(model.getAllowedClaimsMask())) { token.setEmail(user.getEmail()); @@ -284,7 +284,7 @@ public class TokenManager { token.subject(user.getId()); token.audience(realm.getName()); token.issuedNow(); - token.issuedFor(client.getLoginName()); + token.issuedFor(client.getUsername()); token.issuer(realm.getName()); if (realm.getAccessTokenLifespan() > 0) { token.expiration(Time.currentTime() + realm.getAccessTokenLifespan()); diff --git a/services/src/main/java/org/keycloak/services/resources/AccountService.java b/services/src/main/java/org/keycloak/services/resources/AccountService.java index aa34c27b44..16417579fc 100755 --- a/services/src/main/java/org/keycloak/services/resources/AccountService.java +++ b/services/src/main/java/org/keycloak/services/resources/AccountService.java @@ -514,7 +514,7 @@ public class AccountService { if (realm.getSocialLinks(user).size() > 1 || user.getAuthenticationLink() != null) { realm.removeSocialLink(user, providerId); - logger.debug("Social provider " + providerId + " removed successfully from user " + user.getLoginName()); + logger.debug("Social provider " + providerId + " removed successfully from user " + user.getUsername()); audit.event(EventType.REMOVE_SOCIAL_LINK).client(auth.getClient()).user(auth.getUser()) .detail(Details.USERNAME, link.getSocialUserId() + "@" + link.getSocialProvider()) diff --git a/services/src/main/java/org/keycloak/services/resources/RequiredActionsService.java b/services/src/main/java/org/keycloak/services/resources/RequiredActionsService.java index 269fa51386..64e680f08a 100755 --- a/services/src/main/java/org/keycloak/services/resources/RequiredActionsService.java +++ b/services/src/main/java/org/keycloak/services/resources/RequiredActionsService.java @@ -381,7 +381,7 @@ public class RequiredActionsService { } private UserModel getUser(AccessCodeEntry accessCode) { - return realm.getUser(accessCode.getUser().getLoginName()); + return realm.getUser(accessCode.getUser().getUsername()); } private Response redirectOauth(UserModel user, AccessCodeEntry accessCode) { diff --git a/services/src/main/java/org/keycloak/services/resources/SocialResource.java b/services/src/main/java/org/keycloak/services/resources/SocialResource.java index c59c8f6fe3..2da3ec801f 100755 --- a/services/src/main/java/org/keycloak/services/resources/SocialResource.java +++ b/services/src/main/java/org/keycloak/services/resources/SocialResource.java @@ -212,7 +212,7 @@ public class SocialResource { } realm.addSocialLink(authenticatedUser, socialLink); - logger.debug("Social provider " + provider.getId() + " linked with user " + authenticatedUser.getLoginName()); + logger.debug("Social provider " + provider.getId() + " linked with user " + authenticatedUser.getUsername()); audit.success(); return Response.status(302).location(UriBuilder.fromUri(redirectUri).build()).build(); diff --git a/services/src/main/java/org/keycloak/services/resources/TokenService.java b/services/src/main/java/org/keycloak/services/resources/TokenService.java index 93949d1d55..371d8c465c 100755 --- a/services/src/main/java/org/keycloak/services/resources/TokenService.java +++ b/services/src/main/java/org/keycloak/services/resources/TokenService.java @@ -830,7 +830,7 @@ public class TokenService { UserModel user = authResult.getUser(); UserSessionModel session = authResult.getSession(); - logger.debug(user.getLoginName() + " already logged in."); + logger.debug(user.getUsername() + " already logged in."); audit.user(user).session(session).detail(Details.AUTH_METHOD, "sso"); return oauth.processAccessCode(scopeParam, state, redirect, client, user, session, null, false, "sso", audit); } 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 8976f2b668..a7c2f7d10a 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 @@ -185,7 +185,7 @@ public class AdminConsole { displayName = displayName != null ? displayName + " " + user.getLastName() : user.getLastName(); } } else { - displayName = user.getLoginName(); + displayName = user.getUsername(); } RealmModel masterRealm = getAdminstrationRealm(realmManager); diff --git a/services/src/main/java/org/keycloak/services/resources/admin/AdminRoot.java b/services/src/main/java/org/keycloak/services/resources/admin/AdminRoot.java index deb3513cee..98a29f4049 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/AdminRoot.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/AdminRoot.java @@ -182,7 +182,7 @@ public class AdminRoot { AdminAuth auth = authenticateRealmAdminRequest(headers); if (auth != null) { - logger.info("authenticated admin access for: " + auth.getUser().getLoginName()); + logger.info("authenticated admin access for: " + auth.getUser().getUsername()); } Cors.add(request).allowedOrigins(auth.getToken()).allowedMethods("GET", "PUT", "POST", "DELETE").auth().build(response); diff --git a/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java b/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java index 788cc56f91..bb4a4d81e7 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java @@ -127,7 +127,7 @@ public class UsersResource { UserModel user = realm.addUser(rep.getUsername()); updateUserFromRep(user, rep); - return Response.created(uriInfo.getAbsolutePathBuilder().path(user.getLoginName()).build()).build(); + return Response.created(uriInfo.getAbsolutePathBuilder().path(user.getUsername()).build()).build(); } catch (ModelDuplicateException e) { return Flows.errors().exists("User exists with same username or email"); } 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 32a1fdcae6..b2e8ed1361 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 @@ -98,7 +98,7 @@ public class PerfTools { public void deleteUsers(@PathParam("realm") String realmName) { RealmModel realm = session.getRealmByName(realmName); for (UserModel user : realm.getUsers()) { - realm.removeUser(user.getLoginName()); + realm.removeUser(user.getUsername()); } } From 38857cf2e6a8b8c796e9cce0a52950df962b8a05 Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Mon, 7 Jul 2014 16:23:53 +0100 Subject: [PATCH 2/3] Remove notBefore from users --- .../src/main/java/org/keycloak/models/UserModel.java | 3 --- .../org/keycloak/models/entities/UserEntity.java | 9 --------- .../java/org/keycloak/models/cache/UserAdapter.java | 12 ------------ .../keycloak/models/cache/entities/CachedUser.java | 6 ------ .../java/org/keycloak/models/jpa/UserAdapter.java | 10 ---------- .../org/keycloak/models/jpa/entities/UserEntity.java | 8 -------- .../models/mongo/keycloak/adapters/UserAdapter.java | 10 ---------- .../services/managers/AuthenticationManager.java | 5 ----- .../org/keycloak/services/managers/TokenManager.java | 2 +- .../services/resources/admin/UsersResource.java | 2 -- 10 files changed, 1 insertion(+), 66 deletions(-) diff --git a/model/api/src/main/java/org/keycloak/models/UserModel.java b/model/api/src/main/java/org/keycloak/models/UserModel.java index 08c02a3f3e..621148e09e 100755 --- a/model/api/src/main/java/org/keycloak/models/UserModel.java +++ b/model/api/src/main/java/org/keycloak/models/UserModel.java @@ -58,9 +58,6 @@ public interface UserModel { void setTotp(boolean totp); - int getNotBefore(); - void setNotBefore(int notBefore); - void updateCredential(UserCredentialModel cred); List getCredentialsDirectly(); diff --git a/model/api/src/main/java/org/keycloak/models/entities/UserEntity.java b/model/api/src/main/java/org/keycloak/models/entities/UserEntity.java index d1bd1578fc..2297eed38b 100644 --- a/model/api/src/main/java/org/keycloak/models/entities/UserEntity.java +++ b/model/api/src/main/java/org/keycloak/models/entities/UserEntity.java @@ -18,7 +18,6 @@ public class UserEntity extends AbstractIdentifiableEntity { private boolean emailVerified; private boolean totp; private boolean enabled; - private int notBefore; private String realmId; @@ -86,14 +85,6 @@ public class UserEntity extends AbstractIdentifiableEntity { this.enabled = enabled; } - public int getNotBefore() { - return notBefore; - } - - public void setNotBefore(int notBefore) { - this.notBefore = notBefore; - } - public String getRealmId() { return realmId; } diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/UserAdapter.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/UserAdapter.java index b586df5a7a..b48e1c5d37 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/UserAdapter.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/UserAdapter.java @@ -172,18 +172,6 @@ public class UserAdapter implements UserModel { updated.setTotp(totp); } - @Override - public int getNotBefore() { - if (updated != null) return updated.getNotBefore(); - return cached.getNotBefore(); - } - - @Override - public void setNotBefore(int notBefore) { - getDelegateForUpdate(); - updated.setNotBefore(notBefore); - } - @Override public void updateCredential(UserCredentialModel cred) { getDelegateForUpdate(); diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedUser.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedUser.java index bc2a338ce3..ee0318f662 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedUser.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/entities/CachedUser.java @@ -26,7 +26,6 @@ public class CachedUser { private String email; private String emailKey; private boolean emailVerified; - private int notBefore; private List credentials = new LinkedList(); private boolean enabled; private boolean totp; @@ -48,7 +47,6 @@ public class CachedUser { this.emailKey = realm.getId() + "." + this.email; } this.emailVerified = user.isEmailVerified(); - this.notBefore = user.getNotBefore(); this.credentials.addAll(user.getCredentialsDirectly()); this.enabled = user.isEnabled(); this.totp = user.isTotp(); @@ -91,10 +89,6 @@ public class CachedUser { return emailVerified; } - public int getNotBefore() { - return notBefore; - } - public List getCredentials() { return credentials; } diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java index 8e613bb585..290428430c 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java @@ -173,16 +173,6 @@ public class UserAdapter implements UserModel { user.setTotp(totp); } - @Override - public int getNotBefore() { - return user.getNotBefore(); - } - - @Override - public void setNotBefore(int notBefore) { - user.setNotBefore(notBefore); - } - @Override public void updateCredential(UserCredentialModel cred) { CredentialEntity credentialEntity = getCredentialEntity(user, cred.getType()); diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java index 075fc8709c..75389c94df 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserEntity.java @@ -56,7 +56,6 @@ public class UserEntity { protected boolean enabled; protected boolean totp; protected boolean emailVerified; - protected int notBefore; // Hack just to workaround the fact that on MS-SQL you can't have unique constraint with multiple NULL values TODO: Find better solution (like unique index with 'where' but that's proprietary) protected String emailConstraint = KeycloakModelUtils.generateId(); @@ -194,11 +193,4 @@ public class UserEntity { this.authenticationLink = authenticationLink; } - public int getNotBefore() { - return notBefore; - } - - public void setNotBefore(int notBefore) { - this.notBefore = notBefore; - } } diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/UserAdapter.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/UserAdapter.java index 003a7e61b0..0a1ba68d8d 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/UserAdapter.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/UserAdapter.java @@ -70,16 +70,6 @@ public class UserAdapter extends AbstractMongoAdapter implement updateUser(); } - @Override - public int getNotBefore() { - return user.getNotBefore(); - } - - @Override - public void setNotBefore(int notBefore) { - user.setNotBefore(notBefore); - } - @Override public String getFirstName() { return user.getFirstName(); diff --git a/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java b/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java index 2246803bb8..96aab65529 100755 --- a/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java +++ b/services/src/main/java/org/keycloak/services/managers/AuthenticationManager.java @@ -205,11 +205,6 @@ public class AuthenticationManager { return null; } - if (token.getIssuedAt() < user.getNotBefore()) { - logger.info("Stale cookie"); - return null; - } - UserSessionModel session = realm.getUserSession(token.getSessionState()); if (!isSessionValid(realm, session)) { if (session != null) logout(realm, session, uriInfo); diff --git a/services/src/main/java/org/keycloak/services/managers/TokenManager.java b/services/src/main/java/org/keycloak/services/managers/TokenManager.java index 5b3c0000e6..73270e9128 100755 --- a/services/src/main/java/org/keycloak/services/managers/TokenManager.java +++ b/services/src/main/java/org/keycloak/services/managers/TokenManager.java @@ -155,7 +155,7 @@ public class TokenManager { throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "Unmatching clients", "Unmatching clients"); } - if (refreshToken.getIssuedAt() < client.getNotBefore() || refreshToken.getIssuedAt() < user.getNotBefore()) { + if (refreshToken.getIssuedAt() < client.getNotBefore()) { throw new OAuthErrorException(OAuthErrorException.INVALID_GRANT, "Stale refresh token"); } diff --git a/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java b/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java index bb4a4d81e7..5b503a5ffa 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/UsersResource.java @@ -277,8 +277,6 @@ public class UsersResource { throw new NotFoundException("User not found"); } realm.removeUserSessions(user); - // set notBefore so that user will be forced to log in. - user.setNotBefore(Time.currentTime()); new ResourceAdminManager().logoutUser(uriInfo.getRequestUri(), realm, user.getId(), null); } From 9c8897904021362758d4a92b5f45670fd442697c Mon Sep 17 00:00:00 2001 From: mposolda Date: Wed, 2 Jul 2014 22:25:41 +0200 Subject: [PATCH 3/3] Added UsersJob for generic job related to users. Support for sync/async jobs. Configurable host/port --- .../keycloak/testutils/KeycloakServer.java | 4 + testsuite/performance-web/README.md | 14 +- testsuite/performance-web/pom.xml | 4 +- .../performance/web/KeycloakPerfServer.java | 1 + .../performance/web/OAuthClient.java | 9 +- .../performance/web/PerfAppServlet.java | 5 +- .../src/main/resources/perfrealm.json | 41 ++-- .../test/jmeter/keycloak_web_perf_test.jmx | 30 ++- .../org/keycloak/test/tools/PerfTools.java | 187 +++++++++++++----- .../keycloak/test/tools/jobs/CreateUsers.java | 77 -------- .../test/tools/jobs/CreateUsersJob.java | 50 +++++ .../test/tools/jobs/DeleteUsersJob.java | 38 ++++ .../test/tools/jobs/UpdateUsersJob.java | 54 +++++ .../keycloak/test/tools/jobs/UsersJob.java | 126 ++++++++++++ .../test/tools/jobs/UsersJobInitializer.java | 9 + 15 files changed, 502 insertions(+), 147 deletions(-) delete mode 100644 testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/CreateUsers.java create mode 100644 testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/CreateUsersJob.java create mode 100644 testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/DeleteUsersJob.java create mode 100644 testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/UpdateUsersJob.java create mode 100644 testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/UsersJob.java create mode 100644 testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/UsersJobInitializer.java diff --git a/testsuite/integration/src/main/java/org/keycloak/testutils/KeycloakServer.java b/testsuite/integration/src/main/java/org/keycloak/testutils/KeycloakServer.java index c401f0a349..45bb7cc755 100755 --- a/testsuite/integration/src/main/java/org/keycloak/testutils/KeycloakServer.java +++ b/testsuite/integration/src/main/java/org/keycloak/testutils/KeycloakServer.java @@ -201,6 +201,10 @@ public class KeycloakServer { return server; } + public KeycloakServerConfig getConfig() { + return config; + } + public void importRealm(InputStream realm) { RealmRepresentation rep = loadJson(realm, RealmRepresentation.class); importRealm(rep); diff --git a/testsuite/performance-web/README.md b/testsuite/performance-web/README.md index 950e90cc12..7546378c7e 100644 --- a/testsuite/performance-web/README.md +++ b/testsuite/performance-web/README.md @@ -53,10 +53,20 @@ http://localhost:8081/keycloak-tools/perf/perf-realm/get-users-count?prefix=user For adding 10000 new users into your database (will start from last added user, so you don't need to explicitly check how many users to create are needed: ```shell -http://localhost:8081/keycloak-tools/perf/perf-realm/create-available-users?prefix=user&count=10000&batch=100&roles=user +http://localhost:8081/keycloak-tools/perf/perf-realm/create-available-users?prefix=user&count=10000&batch=100&async=true&roles=role-0,role-1 ```` -Seeing progress of job for creating users +For update role mappings of all users: +```shell +http://localhost:8081/keycloak-tools/perf/perf-realm/update-all-users?prefix=user&async=true&roles=role-3,perf-app:approle-3,perf-app:approle-4 +```` + +For deleting all users: +```shell +http://localhost:8081/keycloak-tools/perf/perf-realm/delete-all-users?prefix=user +```` + +Seeing progress of job for creating/updating/deleting users ```shell http://localhost:8081/keycloak-tools/perf/jobs ```` diff --git a/testsuite/performance-web/pom.xml b/testsuite/performance-web/pom.xml index e2d61db79a..b9fee7c26c 100644 --- a/testsuite/performance-web/pom.xml +++ b/testsuite/performance-web/pom.xml @@ -216,11 +216,11 @@ false - + aggregatedRequests * request - + codes **/perf-app/perf-servlet?code=* diff --git a/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/KeycloakPerfServer.java b/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/KeycloakPerfServer.java index 5dde02ee22..cb9e239fa2 100644 --- a/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/KeycloakPerfServer.java +++ b/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/KeycloakPerfServer.java @@ -85,6 +85,7 @@ public class KeycloakPerfServer { ServletInfo servlet = new ServletInfo("PerfAppServlet", PerfAppServlet.class); servlet.addMapping("/perf-servlet/*"); + servlet.addInitParam(PerfAppServlet.BASE_URL_INIT_PARAM, "http://" + keycloakServer.getConfig().getHost() + ":" + keycloakServer.getConfig().getPort()); deploymentInfo.addServlet(servlet); diff --git a/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/OAuthClient.java b/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/OAuthClient.java index bb6e0c1535..40409ad432 100644 --- a/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/OAuthClient.java +++ b/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/OAuthClient.java @@ -42,7 +42,7 @@ import org.keycloak.util.BasicAuthHelper; */ public class OAuthClient { - private String baseUrl = "http://localhost:8081/auth"; + private String baseUrl; private String realm = "perf-realm"; @@ -52,16 +52,19 @@ public class OAuthClient { private String clientId = "perf-app"; - private String redirectUri = "http://localhost:8081/perf-app/perf-servlet"; + private String redirectUri; private String state = "123"; private PublicKey realmPublicKey; - public OAuthClient() { + public OAuthClient(String baseUrl) { try { JSONObject realmJson = new JSONObject(IOUtils.toString(getClass().getResourceAsStream("/perfrealm.json"))); realmPublicKey = PemUtils.decodePublicKey(realmJson.getString("publicKey")); + + this.baseUrl = (baseUrl != null) ? baseUrl + "/auth" : "http://localhost:8081/auth"; + this.redirectUri = baseUrl + "/perf-app/perf-servlet"; } catch (Exception e) { throw new RuntimeException("Failed to retrieve realm public key", e); } diff --git a/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/PerfAppServlet.java b/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/PerfAppServlet.java index 52cf1fa10b..dbab2dafec 100644 --- a/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/PerfAppServlet.java +++ b/testsuite/performance-web/src/main/java/org/keycloak/testsuite/performance/web/PerfAppServlet.java @@ -25,6 +25,8 @@ import org.keycloak.util.Time; */ public class PerfAppServlet extends HttpServlet { + public static final String BASE_URL_INIT_PARAM = "baseUrl"; + private Template indexTemplate; private OAuthClient oauthClient; @@ -35,7 +37,8 @@ public class PerfAppServlet extends HttpServlet { cfg.setTemplateLoader(new ClassTemplateLoader(getClass(), "/")); indexTemplate = cfg.getTemplate("perf-app-resources/index.ftl"); - oauthClient = new OAuthClient(); + String baseUrl = getInitParameter(BASE_URL_INIT_PARAM); + oauthClient = new OAuthClient(baseUrl); } catch (IOException ioe) { throw new ServletException(ioe); } diff --git a/testsuite/performance-web/src/main/resources/perfrealm.json b/testsuite/performance-web/src/main/resources/perfrealm.json index 1fab9a52b4..78d31ea3e4 100644 --- a/testsuite/performance-web/src/main/resources/perfrealm.json +++ b/testsuite/performance-web/src/main/resources/perfrealm.json @@ -8,7 +8,6 @@ "privateKey": "MIICXAIBAAKBgQCrVrCuTtArbgaZzL1hvh0xtL5mc7o0NqPVnYXkLvgcwiC3BjLGw1tGEGoJaXDuSaRllobm53JBhjx33UNv+5z/UMG4kytBWxheNVKnL6GgqlNabMaFfPLPCF8kAgKnsi79NMo+n6KnSY8YeUmec/p2vjO2NjsSAVcWEQMVhJ31LwIDAQABAoGAfmO8gVhyBxdqlxmIuglbz8bcjQbhXJLR2EoS8ngTXmN1bo2L90M0mUKSdc7qF10LgETBzqL8jYlQIbt+e6TH8fcEpKCjUlyq0Mf/vVbfZSNaVycY13nTzo27iPyWQHK5NLuJzn1xvxxrUeXI6A2WFpGEBLbHjwpx5WQG9A+2scECQQDvdn9NE75HPTVPxBqsEd2z10TKkl9CZxu10Qby3iQQmWLEJ9LNmy3acvKrE3gMiYNWb6xHPKiIqOR1as7L24aTAkEAtyvQOlCvr5kAjVqrEKXalj0Tzewjweuxc0pskvArTI2Oo070h65GpoIKLc9jf+UA69cRtquwP93aZKtW06U8dQJAF2Y44ks/mK5+eyDqik3koCI08qaC8HYq2wVl7G2QkJ6sbAaILtcvD92ToOvyGyeE0flvmDZxMYlvaZnaQ0lcSQJBAKZU6umJi3/xeEbkJqMfeLclD27XGEFoPeNrmdx0q10Azp4NfJAY+Z8KRyQCR2BEG+oNitBOZ+YXF9KCpH3cdmECQHEigJhYg+ykOvr1aiZUMFT72HU0jnmQe2FVekuG+LJUt2Tm7GtMjTFoGpf0JwrVuZN39fOYAlo+nTixgeW7X8Y=", "publicKey": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCrVrCuTtArbgaZzL1hvh0xtL5mc7o0NqPVnYXkLvgcwiC3BjLGw1tGEGoJaXDuSaRllobm53JBhjx33UNv+5z/UMG4kytBWxheNVKnL6GgqlNabMaFfPLPCF8kAgKnsi79NMo+n6KnSY8YeUmec/p2vjO2NjsSAVcWEQMVhJ31LwIDAQAB", "requiredCredentials": [ "password" ], - "defaultRoles": [ "user" ], "smtpServer": { "from": "auto@keycloak.org", "host": "localhost", @@ -30,7 +29,7 @@ "name" : "third-party", "enabled": true, "redirectUris": [ - "http://localhost:8081/app/*" + "/app/*" ], "secret": "password" } @@ -48,41 +47,55 @@ }, { "client": "perf-app", - "roles": ["user"] + "roles": [ "role-0", "role-1", "role-2", "role-3", "role-4" ] } ], "applications": [ { "name": "perf-app", "enabled": true, - "baseUrl": "http://localhost:8081/perf-app", + "baseUrl": "/perf-app", "redirectUris": [ - "http://localhost:8081/perf-app/*" + "/perf-app/*" ], - "adminUrl": "http://localhost:8081/perf-app/perf-servlet", + "adminUrl": "/perf-app/perf-servlet", "secret": "password" } ], "roles" : { "realm" : [ { - "name": "user", - "description": "Have User privileges" + "name": "role-0" }, { - "name": "admin", - "description": "Have Administrator privileges" + "name": "role-1" + }, + { + "name": "role-2" + }, + { + "name": "role-3" + }, + { + "name": "role-4" } ], "application" : { "perf-app" : [ { - "name": "customer-user", - "description": "Have Customer User privileges" + "name": "approle-0" }, { - "name": "customer-admin", - "description": "Have Customer Admin privileges" + "name": "approle-1" + }, + { + "name": "approle-2" + }, + { + "name": "approle-3" + }, + { + "name": "approle-4" } ] } diff --git a/testsuite/performance-web/src/test/jmeter/keycloak_web_perf_test.jmx b/testsuite/performance-web/src/test/jmeter/keycloak_web_perf_test.jmx index 07074aa998..07f16efe7f 100644 --- a/testsuite/performance-web/src/test/jmeter/keycloak_web_perf_test.jmx +++ b/testsuite/performance-web/src/test/jmeter/keycloak_web_perf_test.jmx @@ -1,5 +1,5 @@ - + @@ -166,7 +166,7 @@ - /auth/realms/perf-realm/tokens/auth/request/login?response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8081%2Fperf-app%2Fperf-servlet&state=123&client_id=perf-app + /auth/realms/perf-realm/tokens/auth/request/login?response_type=code&redirect_uri=http%3A%2F%2F${host}%3A${port}%2Fperf-app%2Fperf-servlet&state=123&client_id=perf-app POST true false @@ -322,6 +322,32 @@ true + + + + true + true + true + + true + true + true + true + false + true + true + false + false + false + false + false + false + false + false + 0 + true + + 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 b2e8ed1361..30906545c8 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 @@ -5,8 +5,11 @@ import org.keycloak.exportimport.ExportImportProvider; import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.models.RealmModel; -import org.keycloak.models.UserModel; -import org.keycloak.test.tools.jobs.CreateUsers; +import org.keycloak.test.tools.jobs.CreateUsersJob; +import org.keycloak.test.tools.jobs.DeleteUsersJob; +import org.keycloak.test.tools.jobs.UpdateUsersJob; +import org.keycloak.test.tools.jobs.UsersJob; +import org.keycloak.test.tools.jobs.UsersJobInitializer; import org.keycloak.util.ProviderLoader; import javax.ws.rs.GET; @@ -18,11 +21,11 @@ import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; -import java.util.HashMap; +import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util.List; -import java.util.Map; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicInteger; @@ -41,7 +44,7 @@ public class PerfTools { @Context private KeycloakSession session; - private List jobs = new LinkedList(); + private List jobs = new LinkedList(); public PerfTools(KeycloakSessionFactory sessionFactory) { this.sessionFactory = sessionFactory; @@ -50,16 +53,16 @@ public class PerfTools { @GET @Path("jobs") @Produces("application/json") - public List jobs() { + public List jobs() { return jobs; } @GET @Path("delete-jobs") public void deleteJobs() { - Iterator itr = jobs.iterator(); + Iterator itr = jobs.iterator(); while(itr.hasNext()) { - Job j = itr.next(); + JobRepresentation j = itr.next(); if (j.getError() != null || j.getCount() == j.getTotal()) { itr.remove(); } @@ -68,7 +71,116 @@ public class PerfTools { @GET @Path("{realm}/create-users") - public void createUsers(@PathParam("realm") String realmName, @QueryParam("count") Integer count, @QueryParam("batch") Integer batch, @QueryParam("start") Integer start, @QueryParam("prefix") String prefix, @QueryParam("roles") String roles) throws InterruptedException { + public void createUsers(@PathParam("realm") String realmName, @QueryParam("count") Integer count, + @QueryParam("batch") Integer batch, @QueryParam("start") Integer start, @QueryParam("prefix") String prefix, + @QueryParam("async") Boolean async, @QueryParam("roles") String roles) throws InterruptedException { + final String[] rolesArray = roles != null ? roles.split(",") : new String[0]; + + createAndRunJob(realmName, count, batch, start, prefix, async, "Create users", new UsersJobInitializer() { + + @Override + public UsersJob instantiateJob() { + return new CreateUsersJob(rolesArray); + } + + }); + } + + // Same as createUsers, but dynamically compute "start" (Next available user) + @GET + @Path("{realm}/create-available-users") + public void createAvailableUsers(@PathParam("realm") String realmName, @QueryParam("count") Integer count, + @QueryParam("batch") Integer batch, @QueryParam("prefix") String prefix, + @QueryParam("async") Boolean async, @QueryParam("roles") String roles) throws InterruptedException { + int start = getUsersCount(realmName, prefix); + createUsers(realmName, count, batch, start, prefix, async, roles); + } + + @GET + @Path("{realm}/delete-users") + public void deleteUsers(@PathParam("realm") String realmName, @QueryParam("count") Integer count, + @QueryParam("batch") Integer batch, @QueryParam("start") Integer start, @QueryParam("prefix") String prefix, + @QueryParam("async") Boolean async) throws InterruptedException { + createAndRunJob(realmName, count, batch, start, prefix, async, "Delete users", new UsersJobInitializer() { + + @Override + public UsersJob instantiateJob() { + return new DeleteUsersJob(); + } + + }); + } + + @GET + @Path("{realm}/delete-all-users") + public void deleteUsers(@PathParam("realm") String realmName, @QueryParam("prefix") String prefix, @QueryParam("async") Boolean async) throws InterruptedException { + int count = getUsersCount(realmName, prefix); + if (count == 0) { + return; + } + + int batch = count / 10; + if (batch == 0) { + batch = 1; + } + + deleteUsers(realmName, count, batch, 0, prefix, async); + } + + @GET + @Path("{realm}/update-users") + public void updateUsers(@PathParam("realm") String realmName, @QueryParam("count") Integer count, + @QueryParam("batch") Integer batch, @QueryParam("start") Integer start, @QueryParam("prefix") String prefix, + @QueryParam("async") Boolean async, @QueryParam("roles") String roles) throws InterruptedException { + final String[] rolesArray = roles != null ? roles.split(",") : new String[0]; + + createAndRunJob(realmName, count, batch, start, prefix, async, "Update users", new UsersJobInitializer() { + + @Override + public UsersJob instantiateJob() { + return new UpdateUsersJob(rolesArray); + } + + }); + } + + @GET + @Path("{realm}/update-all-users") + public void updateAllUsers(@PathParam("realm") String realmName, @QueryParam("prefix") String prefix, @QueryParam("async") Boolean async, + @QueryParam("roles") String roles) throws InterruptedException { + int count = getUsersCount(realmName, prefix); + if (count == 0) { + return; + } + + int batch = count / 10; + if (batch == 0) { + batch = 1; + } + + updateUsers(realmName, count, batch, 0, prefix, async, roles); + } + + + @GET + @Path("{realm}/get-users-count") + public Response getUsersCountReq(@PathParam("realm") String realmName, @QueryParam("prefix") String prefix) { + int usersCount = getUsersCount(realmName, prefix); + return Response.ok(String.valueOf(usersCount)).build(); + } + + private int getUsersCount(String realmName, String prefix) { + RealmModel realm = session.getRealmByName(realmName); + + // TODO: method for count on model + if (prefix == null) { + return realm.getUsers().size(); + } else { + return realm.searchForUser(prefix).size(); + } + } + + private void createAndRunJob(String realmName, Integer count, Integer batch, Integer start, String prefix, Boolean async, String jobName, UsersJobInitializer initializer) throws InterruptedException { if (count == null) { count = 1; } @@ -81,51 +193,34 @@ public class PerfTools { if (prefix == null) { prefix = String.valueOf(System.currentTimeMillis()); } + if (async == null) { + async = true; + } - String[] rolesArray = roles != null ? roles.split(",") : new String[0]; + int executorsCount = count / batch; + if (count % batch > 0) { + executorsCount++; + } + CountDownLatch latch = new CountDownLatch(executorsCount); - Job job = new Job("Create users " + prefix + "-" + start + " to " + prefix + "-" + (start + count), count); + JobRepresentation job = new JobRepresentation(jobName + " " + prefix + "-" + start + " to " + prefix + "-" + (start + count), count); jobs.add(job); + List usersJobs = new ArrayList(); for (int s = start; s < (start + count); s += batch) { int c = s + batch <= (start + count) ? batch : (start + count) - s; - executor.submit(new CreateUsers(job, sessionFactory, realmName, s, c, prefix, rolesArray)); + UsersJob usersJob = initializer.instantiateJob(); + usersJob.init(job, sessionFactory, realmName, s, c, prefix, latch); + usersJobs.add(usersJob); } - } - @GET - @Path("{realm}/delete-users") - public void deleteUsers(@PathParam("realm") String realmName) { - RealmModel realm = session.getRealmByName(realmName); - for (UserModel user : realm.getUsers()) { - realm.removeUser(user.getUsername()); + // Run executors once all are initialized + for (UsersJob usersJob : usersJobs) { + executor.submit(usersJob); } - } - - @GET - @Path("{realm}/get-users-count") - public Response getUsersCountReq(@PathParam("realm") String realmName, @QueryParam("prefix") String prefix) { - int usersCount = getUsersCount(realmName, prefix); - return Response.ok(String.valueOf(usersCount)).build(); - } - - // Same as createUsers, but dynamically compute "start" (Next available user) - @GET - @Path("{realm}/create-available-users") - public void createAvailableUsers(@PathParam("realm") String realmName, @QueryParam("count") Integer count, @QueryParam("batch") Integer batch, @QueryParam("prefix") String prefix, @QueryParam("roles") String roles) throws InterruptedException { - int start = getUsersCount(realmName, prefix); - createUsers(realmName, count, batch, start, prefix, roles); - } - - private int getUsersCount(String realmName, String prefix) { - RealmModel realm = session.getRealmByName(realmName); - - // TODO: method for count on model - if (prefix == null) { - return realm.getUsers().size(); - } else { - return realm.searchForUser(prefix).size(); + if (!async) { + latch.await(); } } @@ -146,7 +241,7 @@ public class PerfTools { } } - public class Job { + public static class JobRepresentation { private final String description; private final int total; private AtomicInteger count = new AtomicInteger(); @@ -154,7 +249,7 @@ public class PerfTools { private AtomicLong started = new AtomicLong(); private AtomicLong completed = new AtomicLong(); - public Job(String description, int total) { + public JobRepresentation(String description, int total) { this.description = description; this.total = total; } diff --git a/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/CreateUsers.java b/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/CreateUsers.java deleted file mode 100644 index 87a932aefa..0000000000 --- a/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/CreateUsers.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.keycloak.test.tools.jobs; - -import org.keycloak.models.KeycloakSession; -import org.keycloak.models.KeycloakSessionFactory; -import org.keycloak.models.RealmModel; -import org.keycloak.models.UserCredentialModel; -import org.keycloak.models.UserModel; -import org.keycloak.services.managers.RealmManager; -import org.keycloak.test.tools.PerfTools; - -import java.io.PrintWriter; -import java.io.StringWriter; - -/** - * @author Stian Thorgersen - */ -public class CreateUsers implements Runnable { - - private PerfTools.Job job; - private final KeycloakSessionFactory sessionFactory; - private final String realmName; - private int start; - private int count; - private String prefix; - private String[] roles; - - public CreateUsers(PerfTools.Job job, KeycloakSessionFactory sessionFactory, String realmName, int start, int count, String prefix, String[] roles) { - this.job = job; - this.sessionFactory = sessionFactory; - this.realmName = realmName; - this.start = start; - this.count = count; - this.prefix = prefix; - this.roles = roles; - } - - @Override - public void run() { - job.start(); - - KeycloakSession session = sessionFactory.create(); - try { - session.getTransaction().begin(); - - RealmModel realm = new RealmManager(session).getRealmByName(realmName); - - for (int i = start; i < (start + count); i++) { - UserModel user = realm.addUser(prefix + "-" + i); - user.setEnabled(true); - user.setFirstName("First"); - user.setLastName("Last"); - user.setEmail(prefix + "-" + i + "@localhost"); - - UserCredentialModel password = new UserCredentialModel(); - password.setType(UserCredentialModel.PASSWORD); - password.setValue("password"); - - user.updateCredential(password); - - for (String r : roles) { - user.grantRole(realm.getRole(r)); - } - - job.increment(); - } - - session.getTransaction().commit(); - } catch (Throwable t) { - StringWriter sw = new StringWriter(); - t.printStackTrace(new PrintWriter(sw)); - job.setError(sw.toString()); - } finally { - session.close(); - } - } - -} diff --git a/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/CreateUsersJob.java b/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/CreateUsersJob.java new file mode 100644 index 0000000000..b00f08fbf2 --- /dev/null +++ b/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/CreateUsersJob.java @@ -0,0 +1,50 @@ +package org.keycloak.test.tools.jobs; + +import org.keycloak.models.ApplicationModel; +import org.keycloak.models.KeycloakSession; +import org.keycloak.models.KeycloakSessionFactory; +import org.keycloak.models.RealmModel; +import org.keycloak.models.RoleModel; +import org.keycloak.models.UserCredentialModel; +import org.keycloak.models.UserModel; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CountDownLatch; + +/** + * @author Stian Thorgersen + */ +public class CreateUsersJob extends UsersJob { + + private String[] roles; + + public CreateUsersJob(String[] roles) { + this.roles = roles; + } + + @Override + protected void before(KeycloakSession session) { + } + + @Override + protected void runIteration(RealmModel realm, Map apps, Set realmRoles, Map> appRoles, int counter) { + String username = prefix + "-" + counter; + UserModel user = realm.addUser(username); + user.setEnabled(true); + user.setFirstName("First"); + user.setLastName("Last"); + user.setEmail(username + "@localhost"); + + UserCredentialModel password = new UserCredentialModel(); + password.setType(UserCredentialModel.PASSWORD); + password.setValue("password"); + + user.updateCredential(password); + + for (String r : roles) { + grantRole(user, r, realmRoles, appRoles); + } + } + +} diff --git a/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/DeleteUsersJob.java b/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/DeleteUsersJob.java new file mode 100644 index 0000000000..0f98cb4b93 --- /dev/null +++ b/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/DeleteUsersJob.java @@ -0,0 +1,38 @@ +package org.keycloak.test.tools.jobs; + +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.keycloak.models.ApplicationModel; +import org.keycloak.models.KeycloakSession; +import org.keycloak.models.RealmModel; +import org.keycloak.models.RoleModel; +import org.keycloak.models.UserModel; +import org.keycloak.services.managers.RealmManager; + +/** + * @author Marek Posolda + */ +public class DeleteUsersJob extends UsersJob { + + private Iterator users; + + @Override + protected void before(KeycloakSession session) { + RealmModel realm = new RealmManager(session).getRealmByName(realmName); + + // TODO: pagination + List users = (prefix==null) ? realm.getUsers() : realm.searchForUser(prefix); + users = users.subList(start, start + count); + + this.users = users.iterator(); + } + + @Override + protected void runIteration(RealmModel realm, Map apps, Set realmRoles, Map> appRoles, int counter) { + String username = users.next().getUsername(); + realm.removeUser(username); + } +} diff --git a/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/UpdateUsersJob.java b/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/UpdateUsersJob.java new file mode 100644 index 0000000000..d0a1af99b4 --- /dev/null +++ b/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/UpdateUsersJob.java @@ -0,0 +1,54 @@ +package org.keycloak.test.tools.jobs; + +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.keycloak.models.ApplicationModel; +import org.keycloak.models.KeycloakSession; +import org.keycloak.models.RealmModel; +import org.keycloak.models.RoleModel; +import org.keycloak.models.UserModel; +import org.keycloak.services.managers.RealmManager; + +/** + * @author Marek Posolda + */ +public class UpdateUsersJob extends UsersJob { + + private String[] roles; + private Iterator users; + + public UpdateUsersJob(String[] roles) { + this.roles = roles; + } + + @Override + protected void before(KeycloakSession session) { + RealmModel realm = new RealmManager(session).getRealmByName(realmName); + + // TODO: pagination + List users = (prefix==null) ? realm.getUsers() : realm.searchForUser(prefix); + users = users.subList(start, start + count); + + this.users = users.iterator(); + } + + @Override + protected void runIteration(RealmModel realm, Map apps, Set realmRoles, Map> appRoles, int counter) { + String username = users.next().getUsername(); + + // Remove all role mappings first + UserModel user = realm.getUser(username); + Set currRoles = user.getRoleMappings(); + for (RoleModel role : currRoles) { + user.deleteRoleMapping(role); + } + + // Add new roles now + for (String r : roles) { + grantRole(user, r, realmRoles, appRoles); + } + } +} diff --git a/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/UsersJob.java b/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/UsersJob.java new file mode 100644 index 0000000000..efc598ab55 --- /dev/null +++ b/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/UsersJob.java @@ -0,0 +1,126 @@ +package org.keycloak.test.tools.jobs; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CountDownLatch; + +import org.keycloak.models.ApplicationModel; +import org.keycloak.models.KeycloakSession; +import org.keycloak.models.KeycloakSessionFactory; +import org.keycloak.models.RealmModel; +import org.keycloak.models.RoleModel; +import org.keycloak.models.UserModel; +import org.keycloak.services.managers.RealmManager; +import org.keycloak.test.tools.PerfTools; + +/** + * @author Marek Posolda + */ +public abstract class UsersJob implements Runnable { + + protected PerfTools.JobRepresentation job; + protected KeycloakSessionFactory sessionFactory; + protected String realmName; + protected int start; + protected int count; + protected String prefix; + protected CountDownLatch latch; + + public void init(PerfTools.JobRepresentation job, KeycloakSessionFactory sessionFactory, String realmName, int start, int count, String prefix, CountDownLatch latch) { + this.sessionFactory = sessionFactory; + this.realmName = realmName; + this.start = start; + this.count = count; + this.prefix = prefix; + this.job = job; + this.latch = latch; + + KeycloakSession session = sessionFactory.create(); + try { + session.getTransaction().begin(); + + before(session); + + session.getTransaction().commit(); + } catch (Throwable t) { + handleThrowable(t, session); + } finally { + session.close(); + } + } + + @Override + public void run() { + job.start(); + + KeycloakSession session = sessionFactory.create(); + try { + session.getTransaction().begin(); + + RealmModel realm = new RealmManager(session).getRealmByName(realmName); + Map apps = realm.getApplicationNameMap(); + + Set realmRoles = realm.getRoles(); + Map> appRoles = new HashMap>(); + for (Map.Entry appEntry : apps.entrySet()) { + appRoles.put(appEntry.getKey(), appEntry.getValue().getRoles()); + } + + for (int i = start; i < (start + count); i++) { + runIteration(realm, apps, realmRoles, appRoles, i); + job.increment(); + } + + session.getTransaction().commit(); + } catch (Throwable t) { + handleThrowable(t, session); + } finally { + latch.countDown(); + session.close(); + } + + } + + protected abstract void before(KeycloakSession keycloakSession); + + protected abstract void runIteration(RealmModel realm, Map apps, Set realmRoles, Map> appRoles, int counter); + + protected RoleModel findRole(Set roles, String roleName) { + for (RoleModel role : roles) { + if (role.getName().equals(roleName)) { + return role; + } + } + + return null; + } + + protected void grantRole(UserModel user, String roleName, Set realmRoles, Map> appRoles) { + if (roleName.indexOf(':') == -1) { + // We expect "realmRoleName" + RoleModel realmRole = findRole(realmRoles, roleName); + user.grantRole(realmRole); + } else { + // We expect "appName:appRoleName" + String[] parts = roleName.split(":"); + Set currentAppRoles = appRoles.get(parts[0]); + if (currentAppRoles == null) { + throw new IllegalStateException("Application '" + parts[0] + "' not found"); + } + + RoleModel appRole = findRole(currentAppRoles, parts[1]); + user.grantRole(appRole); + } + } + + private void handleThrowable(Throwable t, KeycloakSession session) { + StringWriter sw = new StringWriter(); + t.printStackTrace(new PrintWriter(sw)); + job.setError(sw.toString()); + session.getTransaction().rollback(); + } + +} diff --git a/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/UsersJobInitializer.java b/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/UsersJobInitializer.java new file mode 100644 index 0000000000..d999856d5d --- /dev/null +++ b/testsuite/tools/src/main/java/org/keycloak/test/tools/jobs/UsersJobInitializer.java @@ -0,0 +1,9 @@ +package org.keycloak.test.tools.jobs; + +/** + * @author Marek Posolda + */ +public interface UsersJobInitializer { + + UsersJob instantiateJob(); +}