From 71af7f6d0e5ecf29ebb975dabdb4274a7cbf9b3c Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Fri, 25 Jan 2019 17:05:53 +0100 Subject: [PATCH] #981 Login fixes --- front/core/lib/module-loader.js | 2 +- front/core/services/auth.js | 2 +- front/core/services/locale/en.yml | 2 +- front/core/services/locale/es.yml | 2 +- front/salix/index.ejs | 4 +- front/salix/module.js | 2 +- loopback/client/index.ejs | 2 + .../methods/schema/model-info.js} | 101 +++++++++++------- loopback/common/models/schema.js | 4 + loopback/common/models/schema.json | 13 +++ loopback/server/boot/salix.js | 8 ++ loopback/server/model-config.json | 3 + 12 files changed, 98 insertions(+), 47 deletions(-) rename loopback/{server/boot/validations.js => common/methods/schema/model-info.js} (70%) create mode 100644 loopback/common/models/schema.js create mode 100644 loopback/common/models/schema.json diff --git a/front/core/lib/module-loader.js b/front/core/lib/module-loader.js index b4f393d77..4ecc179b4 100644 --- a/front/core/lib/module-loader.js +++ b/front/core/lib/module-loader.js @@ -41,7 +41,7 @@ export function factory($http, $window, $ocLazyLoad, $translatePartialLoader, $t if (validations) { promises.push(new Promise(resolve => { - $http.get(`/${moduleName}/validations`).then( + $http.get(`/${moduleName}/api/modelInfo`).then( json => this.onValidationsReady(json, resolve), () => resolve() ); diff --git a/front/core/services/auth.js b/front/core/services/auth.js index 3cdf21055..44d9f16d9 100644 --- a/front/core/services/auth.js +++ b/front/core/services/auth.js @@ -68,7 +68,7 @@ export default class Auth { logout() { let promise = this.$http.post('/api/Accounts/logout', null, { headers: {Authorization: this.vnToken.token} - }); + }).catch(() => {}); this.vnToken.unset(); this.loggedIn = false; diff --git a/front/core/services/locale/en.yml b/front/core/services/locale/en.yml index eab8860be..2be73e696 100644 --- a/front/core/services/locale/en.yml +++ b/front/core/services/locale/en.yml @@ -2,5 +2,5 @@ Invalid login: Invalid login, remember that distinction is made between uppercas Could not contact the server: Could not contact the server, make sure you have a connection to the network Please enter your username: Please enter your username It seems that the server has fall down: It seems that the server has fall down, wait a few minutes and try again -Your session has expired: Your session has expired +Session has expired: Your session has expired, please login again Access denied: Access denied \ No newline at end of file diff --git a/front/core/services/locale/es.yml b/front/core/services/locale/es.yml index 78c911459..cf8801b52 100644 --- a/front/core/services/locale/es.yml +++ b/front/core/services/locale/es.yml @@ -2,5 +2,5 @@ Invalid login: Usuario o contraseña incorrectos, recuerda que se hace distinci Could not contact the server: No se ha podido contactar con el servidor, asegurate de que dispones de conexión con la red Please enter your username: Por favor introduce tu nombre de usuario It seems that the server has fall down: Parece que el servidor se ha caído, espera unos minutos e inténtalo de nuevo -Your session has expired: Tu sesión ha expirado +Session has expired: Tu sesión ha expirado, por favor vuelve a iniciar sesión Access denied: Acción no permitida \ No newline at end of file diff --git a/front/salix/index.ejs b/front/salix/index.ejs index 8eaafd041..078be4cfd 100644 --- a/front/salix/index.ejs +++ b/front/salix/index.ejs @@ -1,7 +1,9 @@ - + + + diff --git a/front/salix/module.js b/front/salix/module.js index fc734e1fb..b46a07fdb 100644 --- a/front/salix/module.js +++ b/front/salix/module.js @@ -83,7 +83,7 @@ function $exceptionHandler(vnApp, $window, $state, $injector) { switch (exception.status) { case 401: if ($state.current.name != 'login') { - messageT = 'Your session has expired'; + messageT = 'Session has expired'; let params = {continue: $window.location.hash}; $state.go('login', params); } else diff --git a/loopback/client/index.ejs b/loopback/client/index.ejs index 316179c17..c68339fa8 100644 --- a/loopback/client/index.ejs +++ b/loopback/client/index.ejs @@ -2,6 +2,8 @@ + + diff --git a/loopback/server/boot/validations.js b/loopback/common/methods/schema/model-info.js similarity index 70% rename from loopback/server/boot/validations.js rename to loopback/common/methods/schema/model-info.js index caf01e6f7..a95b60627 100644 --- a/loopback/server/boot/validations.js +++ b/loopback/common/methods/schema/model-info.js @@ -1,4 +1,63 @@ -module.exports = function(app) { + +module.exports = Self => { + Self.remoteMethod('modelInfo', { + description: 'Gets all models information', + accepts: [ + { + arg: 'ctx', + type: 'Object', + http: {source: 'context'} + } + ], + returns: { + type: 'Object', + root: true + }, + http: { + path: `/modelInfo`, + verb: 'GET' + } + }); + + Self.modelInfo = async function(ctx) { + let json = {}; + let models = Self.app.models; + + for (let modelName in models) { + let model = models[modelName]; + let validations = model.validations; + let jsonValidations = {}; + + for (let fieldName in validations) { + let jsonField = []; + + for (let validation of validations[fieldName]) { + let options = validation.options; + + if ((options && options.async) || + (validation.validation == 'custom' && !validation.isExportable)) + continue; + + let validationCp = Object.assign({}, validation); + + if (validationCp.message) + validationCp.message = ctx.req.__(validationCp.message); + + jsonField.push(toJson(validationCp)); + } + + jsonValidations[fieldName] = jsonField; + } + + json[modelName] = { + properties: model.definition.rawProperties, + validations: jsonValidations + }; + } + + return json; + }; + function toJson(object) { let json = {}; @@ -20,44 +79,4 @@ module.exports = function(app) { return json; } - - app.get('/validations', function(req, res) { - let json = {}; - let models = app.models; - - for (let modelName in models) { - let model = models[modelName]; - let validations = model.validations; - let jsonValidations = {}; - - for (let fieldName in validations) { - let jsonField = []; - - for (let validation of validations[fieldName]) { - let options = validation.options; - - if ((options && options.async) || - (validation.validation == 'custom' && !validation.isExportable)) - continue; - - let validationCp = Object.assign({}, validation); - - if (validationCp.message) - validationCp.message = req.__(validationCp.message); - - jsonField.push(toJson(validationCp)); - } - - jsonValidations[fieldName] = jsonField; - } - - json[modelName] = { - properties: model.definition.rawProperties, - validations: jsonValidations - }; - } - - res.set('Content-Type', 'application/json'); - res.send(JSON.stringify(json)); - }); }; diff --git a/loopback/common/models/schema.js b/loopback/common/models/schema.js new file mode 100644 index 000000000..f88cc895c --- /dev/null +++ b/loopback/common/models/schema.js @@ -0,0 +1,4 @@ + +module.exports = function(Self) { + require('../methods/schema/model-info')(Self); +}; diff --git a/loopback/common/models/schema.json b/loopback/common/models/schema.json new file mode 100644 index 000000000..face506ff --- /dev/null +++ b/loopback/common/models/schema.json @@ -0,0 +1,13 @@ +{ + "name": "Schema", + "base": "PersistedModel", + "acls": [ + { + "property": "validations", + "accessType": "EXECUTE", + "principalType": "ROLE", + "principalId": "$everyone", + "permission": "ALLOW" + } + ] +} diff --git a/loopback/server/boot/salix.js b/loopback/server/boot/salix.js index e9ee7f9a0..d90da3415 100644 --- a/loopback/server/boot/salix.js +++ b/loopback/server/boot/salix.js @@ -7,4 +7,12 @@ module.exports = function(app) { version: bootTimestamp }); }); + + // FIXME: Fix until the original can be used + app.get('/api/modelInfo', function(req, res) { + app.models.Schema.modelInfo({req}).then(json => { + res.set('Content-Type', 'application/json'); + res.send(JSON.stringify(json)); + }); + }); }; diff --git a/loopback/server/model-config.json b/loopback/server/model-config.json index ae2aaceb7..530229c41 100644 --- a/loopback/server/model-config.json +++ b/loopback/server/model-config.json @@ -43,5 +43,8 @@ }, "user": { "dataSource": "vn" + }, + "Schema": { + "dataSource": "vn" } } \ No newline at end of file