From c52d16da6d98843193b87d6f92cb487a7e807e18 Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Thu, 11 Jun 2015 15:18:09 -0400 Subject: [PATCH] default required actions in model --- .../org/keycloak/models/UserFederationManager.java | 4 ++-- .../main/java/org/keycloak/models/UserProvider.java | 2 +- .../keycloak/models/utils/RepresentationToModel.java | 2 +- .../org/keycloak/models/file/FileUserProvider.java | 11 +++++++++-- .../models/cache/DefaultCacheUserProvider.java | 4 ++-- .../keycloak/models/cache/NoCacheUserProvider.java | 4 ++-- .../java/org/keycloak/models/jpa/JpaUserProvider.java | 9 +++++++-- .../mongo/keycloak/adapters/MongoUserProvider.java | 11 +++++++++-- 8 files changed, 33 insertions(+), 14 deletions(-) diff --git a/model/api/src/main/java/org/keycloak/models/UserFederationManager.java b/model/api/src/main/java/org/keycloak/models/UserFederationManager.java index 86355fb481..945b8d81ae 100755 --- a/model/api/src/main/java/org/keycloak/models/UserFederationManager.java +++ b/model/api/src/main/java/org/keycloak/models/UserFederationManager.java @@ -28,8 +28,8 @@ public class UserFederationManager implements UserProvider { } @Override - public UserModel addUser(RealmModel realm, String id, String username, boolean addDefaultRoles) { - UserModel user = session.userStorage().addUser(realm, id, username.toLowerCase(), addDefaultRoles); + public UserModel addUser(RealmModel realm, String id, String username, boolean addDefaultRoles, boolean addDefaultRequiredActions) { + UserModel user = session.userStorage().addUser(realm, id, username.toLowerCase(), addDefaultRoles, addDefaultRequiredActions); return registerWithFederation(realm, user); } diff --git a/model/api/src/main/java/org/keycloak/models/UserProvider.java b/model/api/src/main/java/org/keycloak/models/UserProvider.java index 5e78cd1784..b9a0079d1b 100755 --- a/model/api/src/main/java/org/keycloak/models/UserProvider.java +++ b/model/api/src/main/java/org/keycloak/models/UserProvider.java @@ -13,7 +13,7 @@ import java.util.Set; public interface UserProvider extends Provider { // Note: The reason there are so many query methods here is for layering a cache on top of an persistent KeycloakSession - UserModel addUser(RealmModel realm, String id, String username, boolean addDefaultRoles); + UserModel addUser(RealmModel realm, String id, String username, boolean addDefaultRoles, boolean addDefaultRequiredActions); UserModel addUser(RealmModel realm, String username); boolean removeUser(RealmModel realm, UserModel user); diff --git a/model/api/src/main/java/org/keycloak/models/utils/RepresentationToModel.java b/model/api/src/main/java/org/keycloak/models/utils/RepresentationToModel.java index c9611af143..245e6c6e8b 100755 --- a/model/api/src/main/java/org/keycloak/models/utils/RepresentationToModel.java +++ b/model/api/src/main/java/org/keycloak/models/utils/RepresentationToModel.java @@ -796,7 +796,7 @@ public class RepresentationToModel { convertDeprecatedSocialProviders(userRep); // Import users just to user storage. Don't federate - UserModel user = session.userStorage().addUser(newRealm, userRep.getId(), userRep.getUsername(), false); + UserModel user = session.userStorage().addUser(newRealm, userRep.getId(), userRep.getUsername(), false, false); user.setEnabled(userRep.isEnabled()); user.setEmail(userRep.getEmail()); user.setEmailVerified(userRep.isEmailVerified()); diff --git a/model/file/src/main/java/org/keycloak/models/file/FileUserProvider.java b/model/file/src/main/java/org/keycloak/models/file/FileUserProvider.java index 8dd783d985..cf2f21dea6 100755 --- a/model/file/src/main/java/org/keycloak/models/file/FileUserProvider.java +++ b/model/file/src/main/java/org/keycloak/models/file/FileUserProvider.java @@ -266,7 +266,7 @@ public class FileUserProvider implements UserProvider { } @Override - public UserAdapter addUser(RealmModel realm, String id, String username, boolean addDefaultRoles) { + public UserAdapter addUser(RealmModel realm, String id, String username, boolean addDefaultRoles, boolean addDefaultRequiredActions) { if (inMemoryModel.hasUserWithUsername(realm.getId(), username.toLowerCase())) throw new ModelDuplicateException("User with username " + username + " already exists in realm."); @@ -284,6 +284,13 @@ public class FileUserProvider implements UserProvider { } } + if (addDefaultRequiredActions) { + for (String r : realm.getDefaultRequiredActions()) { + userModel.addRequiredAction(r); + } + } + + return userModel; } @@ -358,7 +365,7 @@ public class FileUserProvider implements UserProvider { @Override public UserModel addUser(RealmModel realm, String username) { - return this.addUser(realm, KeycloakModelUtils.generateId(), username.toLowerCase(), true); + return this.addUser(realm, KeycloakModelUtils.generateId(), username.toLowerCase(), true, true); } @Override diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheUserProvider.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheUserProvider.java index a37e2a4e47..750eebe91d 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheUserProvider.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/DefaultCacheUserProvider.java @@ -250,8 +250,8 @@ public class DefaultCacheUserProvider implements CacheUserProvider { } @Override - public UserModel addUser(RealmModel realm, String id, String username, boolean addDefaultRoles) { - return getDelegate().addUser(realm, id, username, addDefaultRoles); + public UserModel addUser(RealmModel realm, String id, String username, boolean addDefaultRoles, boolean addDefaultRequiredActions) { + return getDelegate().addUser(realm, id, username, addDefaultRoles, addDefaultRequiredActions); } @Override diff --git a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/NoCacheUserProvider.java b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/NoCacheUserProvider.java index 5cf96ecb66..aca466b543 100755 --- a/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/NoCacheUserProvider.java +++ b/model/invalidation-cache/model-adapters/src/main/java/org/keycloak/models/cache/NoCacheUserProvider.java @@ -119,8 +119,8 @@ public class NoCacheUserProvider implements CacheUserProvider { } @Override - public UserModel addUser(RealmModel realm, String id, String username, boolean addDefaultRoles) { - return getDelegate().addUser(realm, id, username, addDefaultRoles); + public UserModel addUser(RealmModel realm, String id, String username, boolean addDefaultRoles, boolean addDefaultRequiredActions) { + return getDelegate().addUser(realm, id, username, addDefaultRoles, addDefaultRequiredActions); } @Override diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java index dee433e966..1c45bde919 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaUserProvider.java @@ -44,7 +44,7 @@ public class JpaUserProvider implements UserProvider { } @Override - public UserModel addUser(RealmModel realm, String id, String username, boolean addDefaultRoles) { + public UserModel addUser(RealmModel realm, String id, String username, boolean addDefaultRoles, boolean addDefaultRequiredActions) { if (id == null) { id = KeycloakModelUtils.generateId(); } @@ -68,13 +68,18 @@ public class JpaUserProvider implements UserProvider { } } } + if (addDefaultRequiredActions) { + for (String r : realm.getDefaultRequiredActions()) { + userModel.addRequiredAction(r); + } + } return userModel; } @Override public UserModel addUser(RealmModel realm, String username) { - return addUser(realm, KeycloakModelUtils.generateId(), username.toLowerCase(), true); + return addUser(realm, KeycloakModelUtils.generateId(), username.toLowerCase(), true, true); } @Override diff --git a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoUserProvider.java b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoUserProvider.java index 39d647ddcc..abf8121a23 100755 --- a/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoUserProvider.java +++ b/model/mongo/src/main/java/org/keycloak/models/mongo/keycloak/adapters/MongoUserProvider.java @@ -242,7 +242,7 @@ public class MongoUserProvider implements UserProvider { } @Override - public UserAdapter addUser(RealmModel realm, String id, String username, boolean addDefaultRoles) { + public UserAdapter addUser(RealmModel realm, String id, String username, boolean addDefaultRoles, boolean addDefaultRequiredActions) { UserAdapter userModel = addUserEntity(realm, id, username.toLowerCase()); if (addDefaultRoles) { @@ -257,6 +257,13 @@ public class MongoUserProvider implements UserProvider { } } + if (addDefaultRequiredActions) { + for (String r : realm.getDefaultRequiredActions()) { + userModel.addRequiredAction(r); + } + } + + return userModel; } @@ -327,7 +334,7 @@ public class MongoUserProvider implements UserProvider { @Override public UserModel addUser(RealmModel realm, String username) { - return this.addUser(realm, null, username, true); + return this.addUser(realm, null, username, true, true); } @Override