Skip to content

Commit

Permalink
Merge pull request #674 from pencilblue/feature/509
Browse files Browse the repository at this point in the history
Feature/509
  • Loading branch information
brianhyder committed Jul 5, 2015
2 parents 147614d + ac1867c commit 1f53a18
Show file tree
Hide file tree
Showing 20 changed files with 721 additions and 191 deletions.
3 changes: 2 additions & 1 deletion controllers/base_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ module.exports = function BaseControllerModule(pb) {
BaseController.prototype.formError = function(message, redirectLocation, cb) {

this.session.error = message;
cb(pb.RequestHandler.generateRedirect(pb.config.siteRoot + redirectLocation));
var uri = pb.UrlService.createSystemUrl(redirectLocation);
cb(pb.RequestHandler.generateRedirect(uri));
};

/**
Expand Down
2 changes: 1 addition & 1 deletion controllers/delete_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ module.exports = function(pb) {
* @return {String}
*/
DeleteController.prototype.getSuccessRedirect = function() {
return pb.config.siteRoot;
return pb.UrlService.createSystemUrl('/');
};

/**
Expand Down
5 changes: 5 additions & 0 deletions include/access_management.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ module.exports = function(pb) {
session.authentication.user = user;
session.authentication.user_id = user[pb.DAO.getIdField()].toString();
session.authentication.admin_level = user.admin;

//set locale if no preference already indicated for the session
if (!session.locale) {
session.locale = user.locale;
}
cb(null, user);
});
};
Expand Down
30 changes: 24 additions & 6 deletions include/http/request_handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,9 @@ module.exports = function RequestHandlerModule(pb) {
*/
RequestHandler.prototype.handleRequest = function(){

//get locale preference
this.localizationService = new pb.Localization(this.req);

//fist things first check for public resource
if (RequestHandler.isPublicRoute(this.url.pathname)) {
this.servePublicContent();
return;
return this.servePublicContent();
}

//check for session cookie
Expand All @@ -421,6 +417,12 @@ module.exports = function RequestHandlerModule(pb) {
//open session
var self = this;
pb.session.open(this.req, function(err, session){
if (util.isError(err)) {
return self.serveError(err);
}

//get locale preference
self.localizationService = self.deriveLocalization(session);

//set the session id when no session has started or the current one has
//expired.
Expand All @@ -435,6 +437,22 @@ module.exports = function RequestHandlerModule(pb) {
self.onSessionRetrieved(err, session);
});
};

/**
* Derives the locale and localization instance.
* @method deriveLocalization
*/
RequestHandler.prototype.deriveLocalization = function(session) {

var userPreferredLocale = session.locale;
var browserIndicated = this.req.headers[pb.Localization.ACCEPT_LANG_HEADER];
if (browserIndicated) {
browserIndicated = ',' + browserIndicated;
}

//get locale preference
return new pb.Localization(userPreferredLocale + '' + browserIndicated);
};

/**
* Serves up public content from an absolute file path
Expand Down Expand Up @@ -1039,7 +1057,7 @@ module.exports = function RequestHandlerModule(pb) {
if (self.session.authentication.user_id == null || self.session.authentication.user_id == undefined) {
result.success = false;
result.redirect = RequestHandler.isAdminURL(self.url.href) ? '/admin/login' : '/user/login';
self.session.on_login = self.req.method.toLowerCase() === 'get' ? self.url.href : pb.UrlService.urlJoin(pb.config.siteRoot, '/admin');
self.session.on_login = self.req.method.toLowerCase() === 'get' ? self.url.href : pb.UrlService.createSystemUrl('/admin');
callback(result, result);
return;
}
Expand Down
37 changes: 37 additions & 0 deletions include/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,5 +352,42 @@ module.exports = function LocalizationModule(pb) {
return config.localization.defaultLocale || 'en-US';
};

/**
* Retrieves the supported locales
* @static
* @method getSupported
* @return {Array}
*/
Localization.getSupported = function() {
return util.clone(Localization.supported);
};

/**
* Retrieves the supported locales as an array where each item in the array
* contains a value (locale) and a name (locale specific representation of
* the locale).
* @static
* @method getSupportedWithDisplay
* @return {Array}
*/
Localization.getSupportedWithDisplay = function() {
var locales = [];
var supported = Localization.getSupported();
supported.forEach(function(locale) {

var package = Localization.getLocalizationPackage(locale);
if (!util.isObject(package)) {
return;
}

var kv = {
value: locale,
name: package.generic.LOCALE_DISPLAY
};
locales.push(kv);
});
return locales;
};

return Localization;
};
Loading

0 comments on commit 1f53a18

Please sign in to comment.