Merge pull request #2189 from stianst/master

KEYCLOAK-2430 Load resource bundle before bootstrapping Angular
This commit is contained in:
Stian Thorgersen 2016-02-08 19:20:16 +01:00
commit f28f87b050
2 changed files with 39 additions and 18 deletions

View file

@ -831,6 +831,7 @@ additional-grants=Additional Grants
revoke=Revoke revoke=Revoke
new-password=New Password new-password=New Password
password-confirmation=Password Confirmation password-confirmation=Password Confirmation
reset-password=Reset Password
credentials.temporary.tooltip=If enabled user is required to change password on next login credentials.temporary.tooltip=If enabled user is required to change password on next login
remove-totp=Remove TOTP remove-totp=Remove TOTP
credentials.remove-totp.tooltip=Remove one time password generator for user. credentials.remove-totp.tooltip=Remove one time password generator for user.

View file

@ -6,6 +6,8 @@ consoleBaseUrl = consoleBaseUrl + "/console";
var configUrl = consoleBaseUrl + "/config"; var configUrl = consoleBaseUrl + "/config";
var auth = {}; var auth = {};
var resourceBundle;
var locale = 'en';
var module = angular.module('keycloak', [ 'keycloak.services', 'keycloak.loaders', 'ui.bootstrap', 'ui.select2', 'angularFileUpload', 'angularTreeview', 'pascalprecht.translate', 'ngCookies', 'ngSanitize']); var module = angular.module('keycloak', [ 'keycloak.services', 'keycloak.loaders', 'ui.bootstrap', 'ui.select2', 'angularFileUpload', 'angularTreeview', 'pascalprecht.translate', 'ngCookies', 'ngSanitize']);
var resourceRequests = 0; var resourceRequests = 0;
@ -34,6 +36,25 @@ angular.element(document).ready(function () {
req.send(); req.send();
} }
function loadResourceBundle(success, error) {
var req = new XMLHttpRequest();
req.open('GET', consoleBaseUrl + '/messages.json?lang=' + locale, true);
req.setRequestHeader('Accept', 'application/json');
req.onreadystatechange = function () {
if (req.readyState == 4) {
if (req.status == 200) {
var data = JSON.parse(req.responseText);
success && success(data);
} else {
error && error();
}
}
}
req.send();
}
function hasAnyAccess(user) { function hasAnyAccess(user) {
return user && user['realm_access']; return user && user['realm_access'];
} }
@ -45,6 +66,10 @@ angular.element(document).ready(function () {
keycloakAuth.init({ onLoad: 'login-required' }).success(function () { keycloakAuth.init({ onLoad: 'login-required' }).success(function () {
auth.authz = keycloakAuth; auth.authz = keycloakAuth;
if (auth.authz.idTokenParsed.locale) {
locale = auth.authz.idTokenParsed.locale;
}
auth.refreshPermissions = function(success, error) { auth.refreshPermissions = function(success, error) {
whoAmI(function(data) { whoAmI(function(data) {
auth.user = data; auth.user = data;
@ -57,17 +82,19 @@ angular.element(document).ready(function () {
}); });
}; };
auth.refreshPermissions(function() { loadResourceBundle(function(data) {
module.factory('Auth', function() { resourceBundle = data;
return auth;
});
var injector = angular.bootstrap(document, ["keycloak"]);
injector.get('$translate')('consoleTitle').then(function(consoleTitle) { auth.refreshPermissions(function () {
document.title=consoleTitle; module.factory('Auth', function () {
return auth;
});
var injector = angular.bootstrap(document, ["keycloak"]);
injector.get('$translate')('consoleTitle').then(function (consoleTitle) {
document.title = consoleTitle;
});
}); });
}, function() {
window.location.reload();
}); });
}).error(function () { }).error(function () {
window.location.reload(); window.location.reload();
@ -99,15 +126,8 @@ module.factory('authInterceptor', function($q, Auth) {
module.config(['$translateProvider', function($translateProvider) { module.config(['$translateProvider', function($translateProvider) {
$translateProvider.useSanitizeValueStrategy('sanitizeParameters'); $translateProvider.useSanitizeValueStrategy('sanitizeParameters');
$translateProvider.preferredLanguage(locale);
var locale = auth.authz.idTokenParsed.locale; $translateProvider.translations(locale, resourceBundle);
if (locale !== undefined) {
$translateProvider.preferredLanguage(locale);
} else {
$translateProvider.preferredLanguage('en');
}
$translateProvider.useUrlLoader('messages.json');
}]); }]);
module.config([ '$routeProvider', function($routeProvider) { module.config([ '$routeProvider', function($routeProvider) {