97 RealmRepresentation rep = ModelToRepresentation.toRepresentation(realm,
internal);
98 ModelToRepresentation.exportAuthenticationFlows(realm, rep);
99 ModelToRepresentation.exportRequiredActions(realm, rep);
102 rep.setKeycloakVersion(Version.VERSION);
105 List<ClientScopeModel> clientScopeModels = realm.getClientScopes();
106 List<ClientScopeRepresentation> clientScopesReps =
new ArrayList<>();
107 for (ClientScopeModel app : clientScopeModels) {
108 ClientScopeRepresentation clientRep = ModelToRepresentation.toRepresentation(app);
109 clientScopesReps.add(clientRep);
111 rep.setClientScopes(clientScopesReps);
113 List<String> defaultClientScopeNames = realm.getDefaultClientScopes(
true).stream().map((ClientScopeModel clientScope) -> {
114 return clientScope.getName();
115 }).collect(Collectors.toList());
116 rep.setDefaultDefaultClientScopes(defaultClientScopeNames);
118 List<String> optionalClientScopeNames = realm.getDefaultClientScopes(
false).stream().map((ClientScopeModel clientScope) -> {
119 return clientScope.getName();
120 }).collect(Collectors.toList());
121 rep.setDefaultOptionalClientScopes(optionalClientScopeNames);
124 List<ClientModel> clients = Collections.emptyList();
126 if (options.isClientsIncluded()) {
127 clients = realm.getClients();
128 List<ClientRepresentation> clientReps =
new ArrayList<>();
129 for (ClientModel app : clients) {
130 ClientRepresentation clientRep =
exportClient(session, app);
131 clientReps.add(clientRep);
133 rep.setClients(clientReps);
137 if (options.isGroupsAndRolesIncluded()) {
138 ModelToRepresentation.exportGroups(realm, rep);
140 List<RoleRepresentation> realmRoleReps = null;
141 Map<String, List<RoleRepresentation>> clientRolesReps =
new HashMap<>();
143 Set<RoleModel> realmRoles = realm.getRoles();
144 if (realmRoles != null && realmRoles.size() > 0) {
148 RolesRepresentation rolesRep =
new RolesRepresentation();
149 if (realmRoleReps != null) {
150 rolesRep.setRealm(realmRoleReps);
153 if (options.isClientsIncluded()) {
154 for (ClientModel client : clients) {
155 Set<RoleModel> currentAppRoles = client.getRoles();
156 List<RoleRepresentation> currentAppRoleReps =
exportRoles(currentAppRoles);
157 clientRolesReps.put(client.getClientId(), currentAppRoleReps);
159 if (clientRolesReps.size() > 0) {
160 rolesRep.setClient(clientRolesReps);
163 rep.setRoles(rolesRep);
167 Map<String, List<ScopeMappingRepresentation>> clientScopeReps =
new HashMap<>();
169 if (options.isClientsIncluded()) {
170 List<ClientModel> allClients =
new ArrayList<>(clients);
173 for (ClientModel client : allClients) {
174 Set<RoleModel> clientScopes = client.getScopeMappings();
175 ScopeMappingRepresentation scopeMappingRep = null;
176 for (RoleModel scope : clientScopes) {
177 if (scope.getContainer() instanceof RealmModel) {
178 if (scopeMappingRep == null) {
179 scopeMappingRep = rep.clientScopeMapping(client.getClientId());
181 scopeMappingRep.role(scope.getName());
183 ClientModel app = (ClientModel) scope.getContainer();
184 String appName = app.getClientId();
185 List<ScopeMappingRepresentation> currentAppScopes = clientScopeReps.get(appName);
186 if (currentAppScopes == null) {
187 currentAppScopes =
new ArrayList<>();
188 clientScopeReps.put(appName, currentAppScopes);
191 ScopeMappingRepresentation currentClientScope = null;
192 for (ScopeMappingRepresentation scopeMapping : currentAppScopes) {
193 if (client.getClientId().equals(scopeMapping.getClient())) {
194 currentClientScope = scopeMapping;
198 if (currentClientScope == null) {
199 currentClientScope =
new ScopeMappingRepresentation();
200 currentClientScope.setClient(client.getClientId());
201 currentAppScopes.add(currentClientScope);
203 currentClientScope.role(scope.getName());
210 for (ClientScopeModel clientScope : realm.getClientScopes()) {
211 Set<RoleModel> clientScopes = clientScope.getScopeMappings();
212 ScopeMappingRepresentation scopeMappingRep = null;
213 for (RoleModel scope : clientScopes) {
214 if (scope.getContainer() instanceof RealmModel) {
215 if (scopeMappingRep == null) {
216 scopeMappingRep = rep.clientScopeScopeMapping(clientScope.getName());
218 scopeMappingRep.role(scope.getName());
220 ClientModel app = (ClientModel)scope.getContainer();
221 String appName = app.getClientId();
222 List<ScopeMappingRepresentation> currentAppScopes = clientScopeReps.get(appName);
223 if (currentAppScopes == null) {
224 currentAppScopes =
new ArrayList<>();
225 clientScopeReps.put(appName, currentAppScopes);
228 ScopeMappingRepresentation currentClientTemplateScope = null;
229 for (ScopeMappingRepresentation scopeMapping : currentAppScopes) {
230 if (clientScope.getName().equals(scopeMapping.getClientScope())) {
231 currentClientTemplateScope = scopeMapping;
235 if (currentClientTemplateScope == null) {
236 currentClientTemplateScope =
new ScopeMappingRepresentation();
237 currentClientTemplateScope.setClientScope(clientScope.getName());
238 currentAppScopes.add(currentClientTemplateScope);
240 currentClientTemplateScope.role(scope.getName());
245 if (clientScopeReps.size() > 0) {
246 rep.setClientScopeMappings(clientScopeReps);
250 if (options.isUsersIncluded()) {
251 List<UserModel> allUsers = session.users().getUsers(realm,
true);
252 List<UserRepresentation> users =
new LinkedList<>();
253 for (UserModel user : allUsers) {
254 UserRepresentation userRep =
exportUser(session, realm, user, options);
258 if (users.size() > 0) {
262 List<UserRepresentation> federatedUsers =
new LinkedList<>();
263 for (String userId : session.userFederatedStorage().getStoredUsers(realm, 0, -1)) {
265 federatedUsers.add(userRep);
267 if (federatedUsers.size() > 0) {
268 rep.setFederatedUsers(federatedUsers);
274 MultivaluedHashMap<String, ComponentExportRepresentation> components =
exportComponents(realm, realm.getId());
275 rep.setComponents(components);
static ClientRepresentation exportClient(KeycloakSession session, ClientModel client)
Definition: ExportUtils.java:301
static List< RoleRepresentation > exportRoles(Collection< RoleModel > roles)
Definition: ExportUtils.java:403
static UserRepresentation exportFederatedUser(KeycloakSession session, RealmModel realm, String id, ExportOptions options)
Definition: ExportUtils.java:653
static UserRepresentation exportUser(KeycloakSession session, RealmModel realm, UserModel user, ExportOptions options)
Definition: ExportUtils.java:478
static MultivaluedHashMap< String, ComponentExportRepresentation > exportComponents(RealmModel realm, String parentId)
Definition: ExportUtils.java:280