Skip to content
Snippets Groups Projects
Commit 925b11a8 authored by nicolas.paschoud's avatar nicolas.paschoud
Browse files

Update the login check

parent 8944a022
No related branches found
No related tags found
No related merge requests found
...@@ -253,6 +253,8 @@ app.get('/download/:file_id', (req, res) => { ...@@ -253,6 +253,8 @@ app.get('/download/:file_id', (req, res) => {
* param * param
*/ */
app.get('/change-path*', (req, res) => { app.get('/change-path*', (req, res) => {
sql.changeDirectory('a', req.params['0']);
res.send(`Request for a change path (${req.params['0']})`) res.send(`Request for a change path (${req.params['0']})`)
}) })
......
...@@ -14,22 +14,21 @@ var con = mysql.createConnection({ ...@@ -14,22 +14,21 @@ var con = mysql.createConnection({
* @param {string} login This is the login of the user * @param {string} login This is the login of the user
* @param {string} pass This is the password of the user * @param {string} pass This is the password of the user
*/ */
function userExist(login, pass){ function userExist(login, pass, callback){
const q = `SELECT login, passwd FROM Users WHERE login='${login}' AND passwd='${pass}';`; const q = `SELECT login, passwd FROM Users WHERE login='${login}' AND passwd='${pass}';`;
return new Promise (() => {
con.query(q, function (err, result) { con.query(q, function (err, result) {
if (err) return false; if (err) return false;
if (result.length > 0) { if (result.length > 0) {
console.log("user exists"); console.log("user already exists");
return { return callback({
login: result[0]['login'], login: result[0]['login'],
passwd: result[0]['passwd'] passwd: result[0]['passwd']
}; });
} else { } else {
console.log("user don't exists"); console.log("user don't exists");
return callback(false);
} }
}); });
});
} }
/** /**
...@@ -37,16 +36,17 @@ function userExist(login, pass){ ...@@ -37,16 +36,17 @@ function userExist(login, pass){
* @param {string} login This is the login of the user * @param {string} login This is the login of the user
* @param {string} pass This is the password of the user * @param {string} pass This is the password of the user
*/ */
async function insertUser(login, passwd) { function insertUser(login, passwd, callback){
let q = `INSERT INTO Users VALUES ('${login}', '${passwd}');`; let q = `INSERT INTO Users VALUES ('${login}', '${passwd}');`;
return new Promise( () => { userExist(login, passwd, (userExist) => {
userExist(login, passwd).then( if (!userExist){
con.query(q, (err, res) => { con.query(q, (err, res) => {
if (err) return false; if (err) return callback(false);
console.log("User", login, "inserted in the db") if (!res) {console.log(res);return callback(false);}
return true; console.log("User", login, "inserted in the db");
}) return callback(true);
) });
}
}); });
} }
...@@ -56,18 +56,16 @@ async function insertUser(login, passwd) { ...@@ -56,18 +56,16 @@ async function insertUser(login, passwd) {
* @param {string} login User's path * @param {string} login User's path
* @param {string} parent Parent of the new folder * @param {string} parent Parent of the new folder
*/ */
function addPath(path, login, parent){ function addPath(path, login, parent, callback){
let q = `INSERT INTO Paths VALUES ('${path}', '${login}', ${parent});`; let q = `INSERT INTO Paths VALUES ('${path}', '${login}', ${parent});`;
return new Promise(() => {
con.query(q, function(err, res) { con.query(q, function(err, res) {
if (err) { if (err) {
console.log("Error while adding a new path"); console.log("Error while adding a new path");
console.log(err); console.log(err);
return false; return callback(false);
} }
console.log("New path", path, "added succesfully !"); console.log("New path", path, "added succesfully !");
return true; return callback(true);
});
}); });
} }
...@@ -77,13 +75,18 @@ function addPath(path, login, parent){ ...@@ -77,13 +75,18 @@ function addPath(path, login, parent){
* @param {string} login This is the login of the user * @param {string} login This is the login of the user
* @param {string} pass This is the password of the user * @param {string} pass This is the password of the user
*/ */
async function addUser(login, passwd){ function addUser(login, passwd) {
return new Promise(async () => {
await insertUser(login, passwd).then( insertUser(login, passwd, (insertOk) => {
addPath('/'+login, login, null)); console.log(insertOk);
if (insertOk) {
addPath('/'+login, login, null, (res)=>{});
console.log("Add ok");
}
}); });
} }
/** /**
+----------------+-------+-----------+---------+-----------+ +----------------+-------+-----------+---------+-----------+
| paths | login | parent | file_id | file_name | | paths | login | parent | file_id | file_name |
...@@ -93,20 +96,33 @@ async function addUser(login, passwd){ ...@@ -93,20 +96,33 @@ async function addUser(login, passwd){
| /a/coucou/test | a | /a/coucou | NULL | NULL | | /a/coucou/test | a | /a/coucou | NULL | NULL |
+----------------+-------+-----------+---------+-----------+ +----------------+-------+-----------+---------+-----------+
*/ */
async function changePath(login, path){ async function changeDirectory(login, path, callback){
q = ` q = `
SELECT Paths.paths, login, parent, Files.file_id, Files.file_name SELECT Paths.paths, login, parent, Files.file_id, Files.file_name
FROM Paths FROM Paths
LEFT JOIN Files ON Files.paths = Paths.paths LEFT JOIN Files ON Files.paths = Paths.paths
WHERE Files.file_id IS NOT NULL WHERE Paths.login='${login}'
AND login='${login}' AND Paths.paths='${path}'`; AND Paths.paths='${path}'
OR Paths.parent='${path}';`;
con.query(q, (err, res) => { con.query(q, (err, res) => {
if (err) { if (err) {
console.log("Error while loading the path"); console.log("Error while loading the path");
return false; return callback(false, err);
}
let content = [];
for (let i in res){
content.push({
paths: res[i].paths,
login: res[i].login,
parent: res[i].parent,
file_id: res[i].file_id,
file_name: res[i].file_name
});
} }
console.log(res);
console.log(content);
}); });
return [ return [
...@@ -134,6 +150,6 @@ async function changePath(login, path){ ...@@ -134,6 +150,6 @@ async function changePath(login, path){
]; ];
} }
exports.userExist = userExist;
exports.addUser = addUser; exports.addUser = addUser;
exports.addPath = addPath; exports.addPath = addPath;
exports.changeDirectory = changeDirectory;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment