Commit ff51658d by Pablo Molina

Añadida comprobación de los valores de meta_stu en la creación de un estudiante

parent 16ef6ddc
Showing with 37 additions and 51 deletions
/* global sails */ /* global sails, MetaStu */
const bcrypt = require('bcrypt-nodejs')
/** /**
* student.js * student.js
...@@ -95,6 +97,7 @@ module.exports = { ...@@ -95,6 +97,7 @@ module.exports = {
collection: "StuPicto", collection: "StuPicto",
via: "student" via: "student"
}, },
},
toJSON: function () { toJSON: function () {
var student = this.toObject(); var student = this.toObject();
...@@ -102,60 +105,43 @@ module.exports = { ...@@ -102,60 +105,43 @@ module.exports = {
delete student.password; delete student.password;
return student; return student;
}, },
},
//
// Model hook for storing default configuration adding a new account
//
beforeCreate: function(attrs, next) {
MetaStu.find().exec(function(err, metaStu) {
if (err || !metaStu || metaStu.length == 0)
return next(err);
/* Default Configuration:
var configuration = {
"categories":"on",
"input_feedback":{"vibration":true,"click":false,"read":false,"highlight":false},
"input_selection":"drag",
"pictogram_size":"normal",
"tts_engine":"IVONA Text-to-Speech HQ",
"tts_voice":"child", -- NO USADO
"picto_select":"enlarge", -- NO USADO
"legend":"yes",
"legendsize":"50",
"animation":"yes", -- NO USADO
"tts_options":{phrase":true},
"picto_background":"#000000",
"phrase_background":"#ff0000"};
/**
* Checks the given attributes before creating a new Student
* @param {Object} attrs All student properties to be stored
* @param {Function} next Function to be executed when the check process
* has been completed (an error object will be passed
* to the function if necesary)
*/ */
var configuration = []; beforeCreate: function (attrs, next) {
// The default configuration is built by meta_stu fields and its default values MetaStu.find().then(function (metaAttributes) {
for (var i = 0; i < metaStu.length; i++) { var attributeValue;
configuration.push('"' + metaStu[i].name + '":' + metaStu[i].default_val); var attributeDomains = {};
} var validAttributes = {};
attrs.attributes = JSON.parse("{" + configuration.toString() + "}"); metaAttributes.forEach((metaAttribute) => {
//attrs.attributes = "{" + configuration.toString() + "}"; validAttributes[metaAttribute.name] = metaAttribute.default_val;
//console.log(attrs.attributes); attributeDomains[metaAttribute.name] = metaAttribute.domain.split(',');
//
// Encrypt password before insertion
//
var bcrypt = require('bcrypt-nodejs');
bcrypt.genSalt(10, function(err, salt) {
if (err)
return next(err);
bcrypt.hash(attrs.password, salt, null, function(err, hash) {
if (err)
return next(err);
attrs.password = hash;
next();
}); });
attrs.attributes.keys().forEach((attributeKey) => {
attributeValue = attrs.attributes[attributeKey];
if (attributeDomains[attributeKey] &&
attributeDomains[attributeKey].indexOf(attributeValue) !== -1) {
validAttributes[attributeKey] = attributeValue;
} else {
sails.log.error(`Received invalid attribute \
${attributeKey} with value "${attributeValue}`);
}
}); });
attrs.password = bcrypt.hashSync(attrs.password, bcrypt.genSaltSync());
}).catch((error) => {
if (error) {
next(error);
} else {
next(new Error());
}
}); });
}, },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment