/* Copyright © 2016 Stefano Pigozzi, Emiliano Maccaferri * Released under the GPL v3 license */ var db = require("./connection"); module.exports.flushSession = function(sessionid){ console.log("Pulizia sessione " + sessionid); //Un po' più lento di truncate, ma vabbeh... Tanto è async ;) db.query("DELETE FROM sessions WHERE session_id = ?", [sessionid], function(err, success){ if(err) throw err; console.log("Sessione " + sessionid + " cancellata!"); }); } module.exports.getClass = function(classname, response, callback){ db.query("SELECT username, firstname, lastname, class FROM users WHERE class = ?", [classname], function(err, rows){ if(rows.length == 0){ console.log("Nessuna classe trovata!"); response = { success: false, error: "class_not_found" } callback(null, response); } if(err){ console.log("Errore fatale!"); response = { success: false, error: "fatal" } callback(err, response); throw err; } response = { success: true, list: rows } callback(null, response); }); } module.exports.getProfClasses = function(username, response, callback){ db.query("SELECT classes FROM prof_classes WHERE username = ?", [username], function(err, row){ if(row.length == 0){ console.log("Nessuna classe trovata!"); response = { success: false, errordesc: 'no_classes_found' } callback(null, response); } if(row.length == 1){ var jRow = JSON.stringify(row); var parsed = JSON.parse(jRow); var classes = parsed[0].classes; var classesArray = classes.split(","); console.log(classesArray); response = { success: true, username: username, classes: classesArray } callback(null, response); } if(err){ throw err; response = { sucess: false, errordesc: 'fatal' } callback(err, response); } }); } module.exports.checkSession = function(sessionid, callback){ var error; if(!sessionid || sessionid == ""){ error = new Error("Sessionid mancante!"); return false; } db.query("SELECT username,rank FROM sessions WHERE session_id = ?", [sessionid], function(queryError, row){ if(queryError){ throw queryError; response = { success: false, errordesc: "fatal" } callback(null, response); return false; } if(row.length == 0){ response = { success: false, errordesc: "invalid_sessionid" } callback(null, response); } if(row.length == 1){ callback(row, null); } }); } module.exports.checkProfSession = function(sessionid, callback){ var error; if(!sessionid || sessionid == ""){ error = new Error("Sessionid mancante!"); return false; } db.query("SELECT username FROM sessions WHERE session_id = ? AND rank = 'prof'", [sessionid], function(queryError, row){ console.log(row); if(queryError){ throw queryError; response = { success: false, errordesc: "fatal" } callback(null, response); return false; } if(row.length == 0){ response = { success: false, errordesc: "invalid_sessionid" } callback(null, response); } if(row.length == 1){ callback(row, null); } }); } module.exports.checkIfProfInClass = function(username, classname, callback){ db.query("SELECT classes FROM prof_classes WHERE username = ?", [username], function(err, row){ if(err){ throw err; response = { success: false, errordesc: "fatal" } callback(null, response); } if(row.length == 0){ response = { success: false, errordesc: "no_prof_found" } callback(null, response); } if(row.length == 1){ var cArray = module.exports.rowToString(row, "classes"); var classes = cArray.split(","); if(classes.contains(classname)){ console.log("Professore autorizzato!"); callback(row, null); console.log("OK!"); return true; } response = { success: false, errordesc: "not_of_this_class" } callback(null, response); return false; } }); } module.exports.rowToString = function(row, value){ var jsonObject = JSON.stringify(row); var parsedObj = JSON.parse(jsonObject); return parsedObj[0][value]; } Array.prototype.contains = function ( needle ) { for (i in this) { if (this[i] == needle) return true; } return false; }