export-import/export-import-api/src/main/java/org/keycloak/exportimport/util/ExportUtils.java
/**
     * Full export of user (including role mappings and credentials)
     *
     * @param user
     * @return fully exported user representation
     */
Full export of user (including role mappings and credentials)
@param user
@return fully exported user representation
Full export of user ( including role mappings and credentials ) @param user @return fully exported user representation
public static UserRepresentation exportUser(KeycloakSession session, RealmModel realm, UserModel user) {
        UserRepresentation userRep = ModelToRepresentation.toRepresentation(user);
        Set<FederatedIdentityModel> socialLinks = session.users().getFederatedIdentities(user, realm);
        List<FederatedIdentityRepresentation> socialLinkReps = new ArrayList<FederatedIdentityRepresentation>();
        for (FederatedIdentityModel socialLink : socialLinks) {
            FederatedIdentityRepresentation socialLinkRep = exportSocialLink(socialLink);
            socialLinkReps.add(socialLinkRep);
        }
        if (socialLinkReps.size() > 0) {
            userRep.setFederatedIdentities(socialLinkReps);
        }
        Set<RoleModel> roles = user.getRoleMappings();
        List<String> realmRoleNames = new ArrayList<>();
        Map<String, List<String>> clientRoleNames = new HashMap<>();
        for (RoleModel role : roles) {
            if (role.getContainer() instanceof RealmModel) {
                realmRoleNames.add(role.getName());
            } else {
                ClientModel client = (ClientModel)role.getContainer();
                String clientId = client.getClientId();
                List<String> currentClientRoles = clientRoleNames.get(clientId);
                if (currentClientRoles == null) {
                    currentClientRoles = new ArrayList<>();
                    clientRoleNames.put(clientId, currentClientRoles);
                }
                currentClientRoles.add(role.getName());
            }
        }
        if (realmRoleNames.size() > 0) {
            userRep.setRealmRoles(realmRoleNames);
        }
        if (clientRoleNames.size() > 0) {
            userRep.setClientRoles(clientRoleNames);
        }
        List<UserCredentialValueModel> creds = user.getCredentialsDirectly();
        List<CredentialRepresentation> credReps = new ArrayList<CredentialRepresentation>();
        for (UserCredentialValueModel cred : creds) {
            CredentialRepresentation credRep = exportCredential(cred);
            credReps.add(credRep);
        }
        userRep.setCredentials(credReps);
        userRep.setFederationLink(user.getFederationLink());
        List<UserConsentModel> consents = user.getConsents();
        LinkedList<UserConsentRepresentation> consentReps = new LinkedList<UserConsentRepresentation>();
        for (UserConsentModel consent : consents) {
            UserConsentRepresentation consentRep = ModelToRepresentation.toRepresentation(consent);
            consentReps.add(consentRep);
        }
        if (consentReps.size() > 0) {
            userRep.setClientConsents(consentReps);
        }
        return userRep;
    }
public static UserRepresentation exportUser ( KeycloakSession session , RealmModel realm , UserModel user ) { UserRepresentation userRep = ModelToRepresentation . toRepresentation ( user ) ; Set < FederatedIdentityModel > socialLinks = session . users ( ) . getFederatedIdentities ( user , realm ) ; List < FederatedIdentityRepresentation > socialLinkReps = new ArrayList < FederatedIdentityRepresentation > ( ) ; for ( FederatedIdentityModel socialLink : socialLinks ) { FederatedIdentityRepresentation socialLinkRep = exportSocialLink ( socialLink ) ; socialLinkReps . add ( socialLinkRep ) ; } if ( socialLinkReps . size ( ) > 0 ) { userRep . setFederatedIdentities ( socialLinkReps ) ; } Set < RoleModel > roles = user . getRoleMappings ( ) ; List < String > realmRoleNames = new ArrayList < > ( ) ; Map < String , List < String > > clientRoleNames = new HashMap < > ( ) ; for ( RoleModel role : roles ) { if ( role . getContainer ( ) instanceof RealmModel ) { realmRoleNames . add ( role . getName ( ) ) ; } else { ClientModel client = ( ClientModel ) role . getContainer ( ) ; String clientId = client . getClientId ( ) ; List < String > currentClientRoles = clientRoleNames . get ( clientId ) ; if ( currentClientRoles == null ) { currentClientRoles = new ArrayList < > ( ) ; clientRoleNames . put ( clientId , currentClientRoles ) ; } currentClientRoles . add ( role . getName ( ) ) ; } } if ( realmRoleNames . size ( ) > 0 ) { userRep . setRealmRoles ( realmRoleNames ) ; } if ( clientRoleNames . size ( ) > 0 ) { userRep . setClientRoles ( clientRoleNames ) ; } List < UserCredentialValueModel > creds = user . getCredentialsDirectly ( ) ; List < CredentialRepresentation > credReps = new ArrayList < CredentialRepresentation > ( ) ; for ( UserCredentialValueModel cred : creds ) { CredentialRepresentation credRep = exportCredential ( cred ) ; credReps . add ( credRep ) ; } userRep . setCredentials ( credReps ) ; userRep . setFederationLink ( user . getFederationLink ( ) ) ; List < UserConsentModel > consents = user . getConsents ( ) ; LinkedList < UserConsentRepresentation > consentReps = new LinkedList < UserConsentRepresentation > ( ) ; for ( UserConsentModel consent : consents ) { UserConsentRepresentation consentRep = ModelToRepresentation . toRepresentation ( consent ) ; consentReps . add ( consentRep ) ; } if ( consentReps . size ( ) > 0 ) { userRep . setClientConsents ( consentReps ) ; } return userRep ; }
Full export of user (including role mappings and credentials)
 @param user
 @return fully exported user representation
Full export of user ( including role mappings and credentials ) @param user @return fully exported user representation
// Social links // Role mappings // Credentials // Grants