use of org.keycloak.storage.federated.UserFederatedStorageProvider in project keycloak by keycloak.
the class UserStorageManager method query.
protected Stream<UserModel> query(PaginatedQuery pagedQuery, CountQuery countQuery, RealmModel realm, Integer firstResult, Integer maxResults) {
if (maxResults != null && maxResults == 0)
return Stream.empty();
Stream<Object> providersStream = Stream.concat(Stream.of((Object) localStorage()), getEnabledStorageProviders(realm, UserQueryProvider.class));
UserFederatedStorageProvider federatedStorageProvider = getFederatedStorage();
if (federatedStorageProvider != null) {
providersStream = Stream.concat(providersStream, Stream.of(federatedStorageProvider));
}
final AtomicInteger currentFirst;
if (firstResult == null || firstResult <= 0) {
// We don't want to skip any users so we don't need to do firstResult filtering
currentFirst = new AtomicInteger(0);
} else {
AtomicBoolean droppingProviders = new AtomicBoolean(true);
currentFirst = new AtomicInteger(firstResult);
providersStream = providersStream.filter(provider -> {
// We have already gathered enough users to pass firstResult number in previous providers, we can take all following providers
if (!droppingProviders.get())
return true;
// check how many users we can obtain from this provider
long expectedNumberOfUsersForProvider = countQuery.query(provider, 0, currentFirst.get() + 1);
if (expectedNumberOfUsersForProvider == currentFirst.get()) {
// This provider provides exactly the amount of users we need for passing firstResult, we can set currentFirst to 0 and drop this provider
currentFirst.set(0);
droppingProviders.set(false);
return false;
}
if (expectedNumberOfUsersForProvider > currentFirst.get()) {
// If we can obtain enough enough users from this provider to fulfill our need we can stop dropping providers
droppingProviders.set(false);
// don't filter out this provider because we are going to return some users from it
return true;
}
// This provider cannot provide enough users to pass firstResult so we are going to filter it out and change firstResult for next provider
currentFirst.set((int) (currentFirst.get() - expectedNumberOfUsersForProvider));
return false;
});
}
// Actual user querying
if (maxResults == null || maxResults < 0) {
// No maxResult set, we want all users
return providersStream.flatMap(provider -> pagedQuery.query(provider, currentFirst.getAndSet(0), null));
} else {
final AtomicInteger currentMax = new AtomicInteger(maxResults);
// Query users with currentMax variable counting how many users we return
return providersStream.filter(// If we reach currentMax == 0, we can skip querying all following providers
provider -> currentMax.get() != 0).flatMap(provider -> pagedQuery.query(provider, currentFirst.getAndSet(0), currentMax.get())).peek(userModel -> {
currentMax.updateAndGet(i -> i > 0 ? i - 1 : i);
});
}
}
use of org.keycloak.storage.federated.UserFederatedStorageProvider in project keycloak by keycloak.
the class RepresentationToModel method importFederatedUser.
public static void importFederatedUser(KeycloakSession session, RealmModel newRealm, UserRepresentation userRep) {
UserFederatedStorageProvider federatedStorage = session.userFederatedStorage();
if (userRep.getAttributes() != null) {
for (Map.Entry<String, List<String>> entry : userRep.getAttributes().entrySet()) {
String key = entry.getKey();
List<String> value = entry.getValue();
if (value != null) {
federatedStorage.setAttribute(newRealm, userRep.getId(), key, new LinkedList<>(value));
}
}
}
if (userRep.getRequiredActions() != null) {
for (String action : userRep.getRequiredActions()) {
federatedStorage.addRequiredAction(newRealm, userRep.getId(), action);
}
}
if (userRep.getCredentials() != null) {
for (CredentialRepresentation cred : userRep.getCredentials()) {
federatedStorage.createCredential(newRealm, userRep.getId(), toModel(cred));
}
}
createFederatedRoleMappings(federatedStorage, userRep, newRealm);
if (userRep.getGroups() != null) {
for (String path : userRep.getGroups()) {
GroupModel group = KeycloakModelUtils.findGroupByPath(newRealm, path);
if (group == null) {
throw new RuntimeException("Unable to find group specified by path: " + path);
}
federatedStorage.joinGroup(newRealm, userRep.getId(), group);
}
}
if (userRep.getFederatedIdentities() != null) {
for (FederatedIdentityRepresentation identity : userRep.getFederatedIdentities()) {
FederatedIdentityModel mappingModel = new FederatedIdentityModel(identity.getIdentityProvider(), identity.getUserId(), identity.getUserName());
federatedStorage.addFederatedIdentity(newRealm, userRep.getId(), mappingModel);
}
}
if (userRep.getClientConsents() != null) {
for (UserConsentRepresentation consentRep : userRep.getClientConsents()) {
UserConsentModel consentModel = toModel(newRealm, consentRep);
federatedStorage.addConsent(newRealm, userRep.getId(), consentModel);
}
}
if (userRep.getNotBefore() != null) {
federatedStorage.setNotBeforeForUser(newRealm, userRep.getId(), userRep.getNotBefore());
}
}
Aggregations