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 42 additions and 56 deletions
/* global sails */
/* global sails, MetaStu */
const bcrypt = require('bcrypt-nodejs')
/**
* student.js
......@@ -95,67 +97,51 @@ module.exports = {
collection: "StuPicto",
via: "student"
},
toJSON: function () {
var student = this.toObject();
student.pic = sails.config.pictogram.urls.getStudentAvatarUrl(student.pic);
delete student.password;
return student;
},
},
toJSON: function () {
var student = this.toObject();
student.pic = sails.config.pictogram.urls.getStudentAvatarUrl(student.pic);
delete student.password;
return student;
},
//
// Model hook for storing default configuration adding a new account
//
beforeCreate: function(attrs, next) {
MetaStu.find().exec(function(err, metaStu) {
/**
* 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)
*/
beforeCreate: function (attrs, next) {
MetaStu.find().then(function (metaAttributes) {
var attributeValue;
var attributeDomains = {};
var validAttributes = {};
metaAttributes.forEach((metaAttribute) => {
validAttributes[metaAttribute.name] = metaAttribute.default_val;
attributeDomains[metaAttribute.name] = metaAttribute.domain.split(',');
});
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"};
*/
var configuration = [];
// The default configuration is built by meta_stu fields and its default values
for (var i = 0; i < metaStu.length; i++) {
configuration.push('"' + metaStu[i].name + '":' + metaStu[i].default_val);
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.attributes = JSON.parse("{" + configuration.toString() + "}");
//attrs.attributes = "{" + configuration.toString() + "}";
//console.log(attrs.attributes);
//
// 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.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