Search in sources :

Example 31 with UserConsentModel

use of org.keycloak.models.UserConsentModel in project keycloak by keycloak.

the class RepresentationToModel method createUser.

// Users
public static UserModel createUser(KeycloakSession session, RealmModel newRealm, UserRepresentation userRep) {
    convertDeprecatedSocialProviders(userRep);
    // Import users just to user storage. Don't federate
    UserModel user = session.userLocalStorage().addUser(newRealm, userRep.getId(), userRep.getUsername(), false, false);
    user.setEnabled(userRep.isEnabled() != null && userRep.isEnabled());
    user.setCreatedTimestamp(userRep.getCreatedTimestamp());
    user.setEmail(userRep.getEmail());
    if (userRep.isEmailVerified() != null)
        user.setEmailVerified(userRep.isEmailVerified());
    user.setFirstName(userRep.getFirstName());
    user.setLastName(userRep.getLastName());
    user.setFederationLink(userRep.getFederationLink());
    if (userRep.getAttributes() != null) {
        for (Map.Entry<String, List<String>> entry : userRep.getAttributes().entrySet()) {
            List<String> value = entry.getValue();
            if (value != null) {
                user.setAttribute(entry.getKey(), new ArrayList<>(value));
            }
        }
    }
    if (userRep.getRequiredActions() != null) {
        for (String requiredAction : userRep.getRequiredActions()) {
            try {
                user.addRequiredAction(UserModel.RequiredAction.valueOf(requiredAction.toUpperCase()));
            } catch (IllegalArgumentException iae) {
                user.addRequiredAction(requiredAction);
            }
        }
    }
    createCredentials(userRep, session, newRealm, user, false);
    createFederatedIdentities(userRep, session, newRealm, user);
    createRoleMappings(userRep, user, newRealm);
    if (userRep.getClientConsents() != null) {
        for (UserConsentRepresentation consentRep : userRep.getClientConsents()) {
            UserConsentModel consentModel = toModel(newRealm, consentRep);
            session.users().addConsent(newRealm, user.getId(), consentModel);
        }
    }
    if (userRep.getNotBefore() != null) {
        session.users().setNotBeforeForUser(newRealm, user, userRep.getNotBefore());
    }
    if (userRep.getServiceAccountClientId() != null) {
        String clientId = userRep.getServiceAccountClientId();
        ClientModel client = newRealm.getClientByClientId(clientId);
        if (client == null) {
            throw new RuntimeException("Unable to find client specified for service account link. Client: " + clientId);
        }
        user.setServiceAccountClientLink(client.getId());
    }
    createGroups(userRep, newRealm, user);
    return user;
}
Also used : UserModel(org.keycloak.models.UserModel) ClientModel(org.keycloak.models.ClientModel) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) Map(java.util.Map) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) HashMap(java.util.HashMap) UserConsentModel(org.keycloak.models.UserConsentModel) UserConsentRepresentation(org.keycloak.representations.idm.UserConsentRepresentation)

Example 32 with UserConsentModel

use of org.keycloak.models.UserConsentModel 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());
    }
}
Also used : FederatedIdentityModel(org.keycloak.models.FederatedIdentityModel) GroupModel(org.keycloak.models.GroupModel) ArtifactBindingUtils.computeArtifactBindingIdentifierString(org.keycloak.protocol.saml.util.ArtifactBindingUtils.computeArtifactBindingIdentifierString) UserConsentModel(org.keycloak.models.UserConsentModel) UserConsentRepresentation(org.keycloak.representations.idm.UserConsentRepresentation) CredentialRepresentation(org.keycloak.representations.idm.CredentialRepresentation) UserFederatedStorageProvider(org.keycloak.storage.federated.UserFederatedStorageProvider) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) Map(java.util.Map) MultivaluedHashMap(org.keycloak.common.util.MultivaluedHashMap) HashMap(java.util.HashMap) FederatedIdentityRepresentation(org.keycloak.representations.idm.FederatedIdentityRepresentation)

Aggregations

UserConsentModel (org.keycloak.models.UserConsentModel)32 ClientModel (org.keycloak.models.ClientModel)26 UserModel (org.keycloak.models.UserModel)20 RealmModel (org.keycloak.models.RealmModel)17 ClientScopeModel (org.keycloak.models.ClientScopeModel)16 KeycloakSession (org.keycloak.models.KeycloakSession)15 Test (org.junit.Test)10 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)10 ModelTest (org.keycloak.testsuite.arquillian.annotation.ModelTest)10 ArrayList (java.util.ArrayList)5 LinkedList (java.util.LinkedList)5 List (java.util.List)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 MultivaluedHashMap (org.keycloak.common.util.MultivaluedHashMap)4 ModelException (org.keycloak.models.ModelException)4 StorageId (org.keycloak.storage.StorageId)4 Objects (java.util.Objects)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3