From 54abfa4859bf3ffa60f771507792c6a442610abf Mon Sep 17 00:00:00 2001 From: Stian Thorgersen Date: Mon, 25 Jan 2016 14:22:18 +0100 Subject: [PATCH] KEYCLOAK-2237 Offer the possibility to add own locale to login/registration dialogs --- .../info/ServerInfoRepresentation.java | 7 +-- .../info/ThemeInfoRepresentation.java | 29 +++++++++++ .../admin/info/ServerInfoAdminResource.java | 26 ++++++++-- .../resources/META-INF/keycloak-themes.json | 2 +- .../theme/base/account/theme.properties | 1 + .../admin/resources/js/controllers/realm.js | 52 ++++++++++++++++--- .../partials/realm-theme-settings.html | 8 +-- .../theme/base/admin/theme.properties | 3 +- .../email/html/email-verification.ftl | 0 .../email/html/event-login_error.ftl | 0 .../email/html/event-remove_totp.ftl | 0 .../email/html/event-update_password.ftl | 0 .../email/html/event-update_totp.ftl | 0 .../email/html/executeActions.ftl | 0 .../email/html/identity-provider-link.ftl | 0 .../email/html/password-reset.ftl | 0 .../email/messages/messages_ca.properties | 0 .../email/messages/messages_de.properties | 0 .../email/messages/messages_en.properties | 0 .../email/messages/messages_es.properties | 0 .../email/messages/messages_fr.properties | 0 .../email/messages/messages_pt_BR.properties | 0 .../email/text/email-verification.ftl | 0 .../email/text/event-login_error.ftl | 0 .../email/text/event-remove_totp.ftl | 0 .../email/text/event-update_password.ftl | 0 .../email/text/event-update_totp.ftl | 0 .../email/text/executeActions.ftl | 0 .../email/text/identity-provider-link.ftl | 0 .../email/text/password-reset.ftl | 0 .../theme/base/email/theme.properties | 1 + .../theme/base/login/theme.properties | 1 + .../theme/keycloak/email/theme.properties | 1 + 33 files changed, 113 insertions(+), 18 deletions(-) create mode 100644 core/src/main/java/org/keycloak/representations/info/ThemeInfoRepresentation.java create mode 100644 themes/src/main/resources/theme/base/account/theme.properties rename themes/src/main/resources/theme/{keycloak => base}/email/html/email-verification.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/html/event-login_error.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/html/event-remove_totp.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/html/event-update_password.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/html/event-update_totp.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/html/executeActions.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/html/identity-provider-link.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/html/password-reset.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/messages/messages_ca.properties (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/messages/messages_de.properties (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/messages/messages_en.properties (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/messages/messages_es.properties (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/messages/messages_fr.properties (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/messages/messages_pt_BR.properties (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/text/email-verification.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/text/event-login_error.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/text/event-remove_totp.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/text/event-update_password.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/text/event-update_totp.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/text/executeActions.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/text/identity-provider-link.ftl (100%) rename themes/src/main/resources/theme/{keycloak => base}/email/text/password-reset.ftl (100%) create mode 100644 themes/src/main/resources/theme/base/email/theme.properties create mode 100644 themes/src/main/resources/theme/base/login/theme.properties create mode 100644 themes/src/main/resources/theme/keycloak/email/theme.properties diff --git a/core/src/main/java/org/keycloak/representations/info/ServerInfoRepresentation.java b/core/src/main/java/org/keycloak/representations/info/ServerInfoRepresentation.java index 640f3f6119..935c441d35 100755 --- a/core/src/main/java/org/keycloak/representations/info/ServerInfoRepresentation.java +++ b/core/src/main/java/org/keycloak/representations/info/ServerInfoRepresentation.java @@ -14,7 +14,7 @@ public class ServerInfoRepresentation { private SystemInfoRepresentation systemInfo; private MemoryInfoRepresentation memoryInfo; - private Map> themes; + private Map> themes; private List> socialProviders; private List> identityProviders; @@ -43,11 +43,12 @@ public class ServerInfoRepresentation { public void setMemoryInfo(MemoryInfoRepresentation memoryInfo) { this.memoryInfo = memoryInfo; } - public Map> getThemes() { + + public Map> getThemes() { return themes; } - public void setThemes(Map> themes) { + public void setThemes(Map> themes) { this.themes = themes; } diff --git a/core/src/main/java/org/keycloak/representations/info/ThemeInfoRepresentation.java b/core/src/main/java/org/keycloak/representations/info/ThemeInfoRepresentation.java new file mode 100644 index 0000000000..4607045598 --- /dev/null +++ b/core/src/main/java/org/keycloak/representations/info/ThemeInfoRepresentation.java @@ -0,0 +1,29 @@ +/* + */ + +package org.keycloak.representations.info; + +/** + * @author Stian Thorgersen + */ +public class ThemeInfoRepresentation { + + private String name; + private String[] locales; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String[] getLocales() { + return locales; + } + + public void setLocales(String[] locales) { + this.locales = locales; + } +} diff --git a/services/src/main/java/org/keycloak/services/resources/admin/info/ServerInfoAdminResource.java b/services/src/main/java/org/keycloak/services/resources/admin/info/ServerInfoAdminResource.java index cd288b7e18..35e1d5678a 100755 --- a/services/src/main/java/org/keycloak/services/resources/admin/info/ServerInfoAdminResource.java +++ b/services/src/main/java/org/keycloak/services/resources/admin/info/ServerInfoAdminResource.java @@ -1,5 +1,6 @@ package org.keycloak.services.resources.admin.info; +import java.io.IOException; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; @@ -10,6 +11,7 @@ import java.util.Map; import java.util.ServiceLoader; import javax.ws.rs.GET; +import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Context; import org.keycloak.broker.provider.IdentityProvider; @@ -109,13 +111,31 @@ public class ServerInfoAdminResource { private void setThemes(ServerInfoRepresentation info) { ThemeProvider themeProvider = session.getProvider(ThemeProvider.class, "extending"); - info.setThemes(new HashMap>()); + info.setThemes(new HashMap>()); for (Theme.Type type : Theme.Type.values()) { - List themes = new LinkedList(themeProvider.nameSet(type)); - Collections.sort(themes); + List themeNames = new LinkedList<>(themeProvider.nameSet(type)); + Collections.sort(themeNames); + List themes = new LinkedList<>(); info.getThemes().put(type.toString().toLowerCase(), themes); + + for (String name : themeNames) { + try { + Theme theme = themeProvider.getTheme(name, type); + ThemeInfoRepresentation ti = new ThemeInfoRepresentation(); + ti.setName(name); + + String locales = theme.getProperties().getProperty("locales"); + if (locales != null) { + ti.setLocales(locales.replaceAll(" ", "").split(",")); + } + + themes.add(ti); + } catch (IOException e) { + throw new WebApplicationException("Failed to load themes", e); + } + } } } diff --git a/themes/src/main/resources/META-INF/keycloak-themes.json b/themes/src/main/resources/META-INF/keycloak-themes.json index 56322d3176..12711eae54 100755 --- a/themes/src/main/resources/META-INF/keycloak-themes.json +++ b/themes/src/main/resources/META-INF/keycloak-themes.json @@ -1,7 +1,7 @@ { "themes": [{ "name" : "base", - "types": [ "admin", "account", "login" ] + "types": [ "admin", "account", "login", "email" ] }, { "name" : "keycloak", "types": [ "admin", "account", "login", "common", "email", "welcome" ] diff --git a/themes/src/main/resources/theme/base/account/theme.properties b/themes/src/main/resources/theme/base/account/theme.properties new file mode 100644 index 0000000000..29af932354 --- /dev/null +++ b/themes/src/main/resources/theme/base/account/theme.properties @@ -0,0 +1 @@ +locales=ca,de,en,es,fr,it,pt_BR \ No newline at end of file diff --git a/themes/src/main/resources/theme/base/admin/resources/js/controllers/realm.js b/themes/src/main/resources/theme/base/admin/resources/js/controllers/realm.js index f58004d57c..f300616aa4 100755 --- a/themes/src/main/resources/theme/base/admin/resources/js/controllers/realm.js +++ b/themes/src/main/resources/theme/base/admin/resources/js/controllers/realm.js @@ -338,15 +338,55 @@ module.controller('RealmThemeCtrl', function($scope, Current, Realm, realm, serv $scope.supportedLocalesOptions = { 'multiple' : true, - 'simple_tags' : true, - 'tags' : ['en', 'de', 'pt-BR', 'it', 'es', 'ca'] + 'simple_tags' : true }; - $scope.$watch('realm.supportedLocales', function(oldVal, newVal) { - if ($scope.realm.defaultLocale && newVal && newVal.indexOf($scope.realm.defaultLocale) == -1) { - $scope.realm.defaultLocale = null; + function localeForTheme(type, name) { + name = name || 'base'; + for (var i = 0; i < serverInfo.themes[type].length; i++) { + if (serverInfo.themes[type][i].name == name) { + return serverInfo.themes[type][i].locales; + } } - }, true); + } + + function updateSupported() { + if ($scope.realm.internationalizationEnabled) { + var accountLocales = localeForTheme('account', $scope.realm.loginTheme); + var adminLocales = localeForTheme('admin', $scope.realm.loginTheme); + var loginLocales = localeForTheme('login', $scope.realm.loginTheme); + var emailLocales = localeForTheme('email', $scope.realm.loginTheme); + + var supportedLocales = []; + for (var i = 0; i < accountLocales.length; i++) { + var l = accountLocales[i]; + if (adminLocales.indexOf(l) >= 0 && loginLocales.indexOf(l) >= 0 && emailLocales.indexOf(l) >= 0) { + supportedLocales.push(l); + } + } + + $scope.supportedLocalesOptions.tags = supportedLocales; + + if (!$scope.realm.supportedLocales) { + $scope.realm.supportedLocales = supportedLocales; + } else { + for (var i = 0; i < $scope.realm.supportedLocales.length; i++) { + if ($scope.realm.supportedLocales.indexOf($scope.realm.supportedLocales[i]) == -1) { + $scope.realm.supportedLocales = supportedLocales; + } + } + } + + if (!$scope.realm.defaultLocale || supportedLocales.indexOf($scope.realm.defaultLocale) == -1) { + $scope.realm.defaultLocale = 'en'; + } + } + } + + $scope.$watch('realm.loginTheme', updateSupported); + $scope.$watch('realm.accountTheme', updateSupported); + $scope.$watch('realm.emailTheme', updateSupported); + $scope.$watch('realm.internationalizationEnabled', updateSupported); }); module.controller('RealmCacheCtrl', function($scope, realm, RealmClearUserCache, RealmClearRealmCache, Notifications) { diff --git a/themes/src/main/resources/theme/base/admin/resources/partials/realm-theme-settings.html b/themes/src/main/resources/theme/base/admin/resources/partials/realm-theme-settings.html index 0c36f93c64..2011c08fec 100755 --- a/themes/src/main/resources/theme/base/admin/resources/partials/realm-theme-settings.html +++ b/themes/src/main/resources/theme/base/admin/resources/partials/realm-theme-settings.html @@ -9,7 +9,7 @@
@@ -22,7 +22,7 @@
@@ -35,7 +35,7 @@
@@ -48,7 +48,7 @@
diff --git a/themes/src/main/resources/theme/base/admin/theme.properties b/themes/src/main/resources/theme/base/admin/theme.properties index 5eb8ef9e03..9f605c700a 100644 --- a/themes/src/main/resources/theme/base/admin/theme.properties +++ b/themes/src/main/resources/theme/base/admin/theme.properties @@ -1 +1,2 @@ -import=common/keycloak \ No newline at end of file +import=common/keycloak +locales=ca,de,en,es,fr \ No newline at end of file diff --git a/themes/src/main/resources/theme/keycloak/email/html/email-verification.ftl b/themes/src/main/resources/theme/base/email/html/email-verification.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/html/email-verification.ftl rename to themes/src/main/resources/theme/base/email/html/email-verification.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/html/event-login_error.ftl b/themes/src/main/resources/theme/base/email/html/event-login_error.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/html/event-login_error.ftl rename to themes/src/main/resources/theme/base/email/html/event-login_error.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/html/event-remove_totp.ftl b/themes/src/main/resources/theme/base/email/html/event-remove_totp.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/html/event-remove_totp.ftl rename to themes/src/main/resources/theme/base/email/html/event-remove_totp.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/html/event-update_password.ftl b/themes/src/main/resources/theme/base/email/html/event-update_password.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/html/event-update_password.ftl rename to themes/src/main/resources/theme/base/email/html/event-update_password.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/html/event-update_totp.ftl b/themes/src/main/resources/theme/base/email/html/event-update_totp.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/html/event-update_totp.ftl rename to themes/src/main/resources/theme/base/email/html/event-update_totp.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/html/executeActions.ftl b/themes/src/main/resources/theme/base/email/html/executeActions.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/html/executeActions.ftl rename to themes/src/main/resources/theme/base/email/html/executeActions.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/html/identity-provider-link.ftl b/themes/src/main/resources/theme/base/email/html/identity-provider-link.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/html/identity-provider-link.ftl rename to themes/src/main/resources/theme/base/email/html/identity-provider-link.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/html/password-reset.ftl b/themes/src/main/resources/theme/base/email/html/password-reset.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/html/password-reset.ftl rename to themes/src/main/resources/theme/base/email/html/password-reset.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/messages/messages_ca.properties b/themes/src/main/resources/theme/base/email/messages/messages_ca.properties similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/messages/messages_ca.properties rename to themes/src/main/resources/theme/base/email/messages/messages_ca.properties diff --git a/themes/src/main/resources/theme/keycloak/email/messages/messages_de.properties b/themes/src/main/resources/theme/base/email/messages/messages_de.properties similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/messages/messages_de.properties rename to themes/src/main/resources/theme/base/email/messages/messages_de.properties diff --git a/themes/src/main/resources/theme/keycloak/email/messages/messages_en.properties b/themes/src/main/resources/theme/base/email/messages/messages_en.properties similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/messages/messages_en.properties rename to themes/src/main/resources/theme/base/email/messages/messages_en.properties diff --git a/themes/src/main/resources/theme/keycloak/email/messages/messages_es.properties b/themes/src/main/resources/theme/base/email/messages/messages_es.properties similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/messages/messages_es.properties rename to themes/src/main/resources/theme/base/email/messages/messages_es.properties diff --git a/themes/src/main/resources/theme/keycloak/email/messages/messages_fr.properties b/themes/src/main/resources/theme/base/email/messages/messages_fr.properties similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/messages/messages_fr.properties rename to themes/src/main/resources/theme/base/email/messages/messages_fr.properties diff --git a/themes/src/main/resources/theme/keycloak/email/messages/messages_pt_BR.properties b/themes/src/main/resources/theme/base/email/messages/messages_pt_BR.properties similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/messages/messages_pt_BR.properties rename to themes/src/main/resources/theme/base/email/messages/messages_pt_BR.properties diff --git a/themes/src/main/resources/theme/keycloak/email/text/email-verification.ftl b/themes/src/main/resources/theme/base/email/text/email-verification.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/text/email-verification.ftl rename to themes/src/main/resources/theme/base/email/text/email-verification.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/text/event-login_error.ftl b/themes/src/main/resources/theme/base/email/text/event-login_error.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/text/event-login_error.ftl rename to themes/src/main/resources/theme/base/email/text/event-login_error.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/text/event-remove_totp.ftl b/themes/src/main/resources/theme/base/email/text/event-remove_totp.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/text/event-remove_totp.ftl rename to themes/src/main/resources/theme/base/email/text/event-remove_totp.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/text/event-update_password.ftl b/themes/src/main/resources/theme/base/email/text/event-update_password.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/text/event-update_password.ftl rename to themes/src/main/resources/theme/base/email/text/event-update_password.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/text/event-update_totp.ftl b/themes/src/main/resources/theme/base/email/text/event-update_totp.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/text/event-update_totp.ftl rename to themes/src/main/resources/theme/base/email/text/event-update_totp.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/text/executeActions.ftl b/themes/src/main/resources/theme/base/email/text/executeActions.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/text/executeActions.ftl rename to themes/src/main/resources/theme/base/email/text/executeActions.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/text/identity-provider-link.ftl b/themes/src/main/resources/theme/base/email/text/identity-provider-link.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/text/identity-provider-link.ftl rename to themes/src/main/resources/theme/base/email/text/identity-provider-link.ftl diff --git a/themes/src/main/resources/theme/keycloak/email/text/password-reset.ftl b/themes/src/main/resources/theme/base/email/text/password-reset.ftl similarity index 100% rename from themes/src/main/resources/theme/keycloak/email/text/password-reset.ftl rename to themes/src/main/resources/theme/base/email/text/password-reset.ftl diff --git a/themes/src/main/resources/theme/base/email/theme.properties b/themes/src/main/resources/theme/base/email/theme.properties new file mode 100644 index 0000000000..29af932354 --- /dev/null +++ b/themes/src/main/resources/theme/base/email/theme.properties @@ -0,0 +1 @@ +locales=ca,de,en,es,fr,it,pt_BR \ No newline at end of file diff --git a/themes/src/main/resources/theme/base/login/theme.properties b/themes/src/main/resources/theme/base/login/theme.properties new file mode 100644 index 0000000000..29af932354 --- /dev/null +++ b/themes/src/main/resources/theme/base/login/theme.properties @@ -0,0 +1 @@ +locales=ca,de,en,es,fr,it,pt_BR \ No newline at end of file diff --git a/themes/src/main/resources/theme/keycloak/email/theme.properties b/themes/src/main/resources/theme/keycloak/email/theme.properties new file mode 100644 index 0000000000..f1dbb7215d --- /dev/null +++ b/themes/src/main/resources/theme/keycloak/email/theme.properties @@ -0,0 +1 @@ +parent=base \ No newline at end of file