issue #440 fixed, to be tested

parent 12be7529
......@@ -5,5 +5,5 @@
# Changes for new grid system
- Lanzar desde MySQL el archivo roles/database/files/new-grid-system-adapt2.sql
- Lanzar desde MySQL el archivo roles/database/files/new-grid-system-adapt.sql
- Reactivar triggers
......@@ -61,15 +61,10 @@ module.exports = {
.then(function (student) {
if (student) {
if (bcrypt.compareSync(req.body.password, student.password)) {
student.isStudent = true;
if (!student.license || !student.license[0]) {
sails.log.error('Tried to login with non valid license ${req.body.username}');
res.unauthorized("Student has an invalid license");
} else {
var hasExpired = student.license[0].hasExpired();
student = student.toObject(); // to enable overwrite license field
student.license = student.license[0];
student.license.expired = hasExpired;
return res.ok({
user: student,
token: sailsTokenAuth.issueToken(student, sails.config.jwt.expiresInMinutes),
......@@ -143,10 +138,16 @@ module.exports = {
// Promisify asynchronous call to Student.supervisors
var supervisors = new Promise(function (resolve, reject) {
Student.validSupervisors(student.id, req.token.id, function (err, ss) {
if (err) return reject(err);
return resolve(ss);
});
if (req.token.isStudent)
Student.allSupervisors(student.id, function(err, ss) {
if (err) return reject(err);
return resolve(ss);
});
else
Student.validSupervisors(student.id, req.token.id, function (err, ss) {
if (err) return reject(err);
return resolve(ss);
});
});
return [student, supervisors];
......
......@@ -113,6 +113,7 @@ module.exports = {
if (student.license && student.license[0]) {
student.license = student.license[0];
student.license.isValid = !License.hasExpired(student.license);
student.license.expired = !student.license.isValid;
student.license.isTrial = student.license.type == 'trial';
student.license.isOfficial = student.license.type == 'official';
} else student.license = null;
......@@ -121,15 +122,19 @@ module.exports = {
if (!student.surname || student.surname.length == 0)
student.surname = sails.__({phrase: 'no_surname', locale: student.lang});
student.attributes = Student.getValidAttributes(student.attributes);
if (student.lastInstruction && student.lastInstruction[0] && student.lastInstruction[0].met_name) {
student.current_method = student.lastInstruction[0].met_name;
student.current_instruction = student.lastInstruction[0].ins_name;
}
else {
student.current_method = "no_method";
student.current_instruction = "no_instruction";
}
if (student.lastInstruction && student.lastInstruction[0] && student.lastInstruction[0].met_name) {
student.current_method = student.lastInstruction[0].met_name;
student.current_instruction = student.lastInstruction[0].ins_name;
}
else {
student.current_method = "no_method";
student.current_instruction = "no_instruction";
}
student.isTutor = false;
student.isTherapist = false;
student.isOffice = false;
student.isAdmin = false;
student.isStudent = true;
delete student.password;
return student;
},
......@@ -312,8 +317,10 @@ module.exports = {
},
//
// Class method for getting the list of supervisors associated to a given
// student and where the requester is related office
// Returns a callback(err, supervisors) where supervisors (when no errors)
// is the list of supervisors associated to a given student and they all
// belong to the same office
//
validSupervisors: function(id_stu, id_sup, callback) {
// Get all supervisors
......@@ -327,14 +334,13 @@ module.exports = {
return ([stuSups, SupOff.find({office: id_sup}).populate('supervisor')]);
})
.spread((stuSups, supOffs) => {
if (!supOffs || supOffs.length == 0) {
// if we get here, only the requester being directly related to the
// student is valid
var supIdx = stuSups.findIndex(x => x.supervisor && x.supervisor.id == id_sup);
if (supIdx >= 0)
return ([stuSups[supIdx].supervisor]); // break "then" chain
throw new Error("No supervisors related");
return ([stuSups[supIdx].supervisor]); // return only the supervisor herself and break "then" chain
throw new Error("Your are not associated to the student");
}
// else (continue then chain)
......
......@@ -130,6 +130,7 @@ module.exports = {
supervisor.isTherapist = supervisor.role == 'therapist';
supervisor.isOffice = supervisor.role == 'office';
supervisor.isAdmin = supervisor.role == 'admin';
supervisor.isStudent = false;
if (!supervisor.name || supervisor.name.length == 0)
supervisor.name = sails.__({phrase: 'no_name', locale: supervisor.lang});
if (!supervisor.surname || supervisor.surname.length == 0)
......
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