diff --git a/app/models/NomsModl.js b/app/models/NomsModl.js deleted file mode 100644 index 10a4b74..0000000 --- a/app/models/NomsModl.js +++ /dev/null @@ -1,16 +0,0 @@ -var mongoose = require('mongoose'); - -module.exports = mongoose.model('Noms', { - login: { // On récupèrera le nom via les passwd - type: String, - default: 'login' - }, - nom: { - type: String, - default: 'Nom' - }, - section: { - type: String, - default: '' - } -}); diff --git a/app/models/PolyUserModl.js b/app/models/PolyUserModl.js new file mode 100644 index 0000000..3ee9e88 --- /dev/null +++ b/app/models/PolyUserModl.js @@ -0,0 +1,16 @@ +var mongoose = require('mongoose'); + +module.exports = mongoose.model('PolyUser', { + login: { + type: String, + default: 'login' + }, + nom: { + type: String, + default: 'Nom' + }, + section: { + type: String, + default: '' + } +}); diff --git a/app/services/ConvsServ.js b/app/services/ConvsServ.js index a421137..813b71d 100644 --- a/app/services/ConvsServ.js +++ b/app/services/ConvsServ.js @@ -1,11 +1,11 @@ var ConvModl = require('../models/ConvModl'); -// var NomsServ = require('../services/NomsServ'); +// var PolyUserServ = require('../services/PolyUserServ'); var async = require('async'); var ConvsServ = {}; ConvsServ.addData = function (conv, cb) { - // NomsServ.get(Conv.login, function (nom) { + // PolyUserServ.get(Conv.login, function (err, nom) { // if (nom) { // Conv.nom = nom; // } else { diff --git a/app/services/MembresServ.js b/app/services/MembresServ.js index f7fa136..b7632cf 100644 --- a/app/services/MembresServ.js +++ b/app/services/MembresServ.js @@ -1,11 +1,11 @@ var MembreModl = require('../models/MembreModl'); -var NomsServ = require('../services/NomsServ'); +var PolyUserServ = require('../services/PolyUserServ'); var async = require('async'); var MembresServ = {}; MembresServ.addData = function (membre, cb) { - NomsServ.get(membre.login, function (nom) { + PolyUserServ.get(membre.login, function (err, nom) { membre.nom = nom.nom; membre.section = nom.section; cb(null, membre); diff --git a/app/services/MessServ.js b/app/services/MessServ.js index 10673c8..d6e4b89 100644 --- a/app/services/MessServ.js +++ b/app/services/MessServ.js @@ -1,12 +1,12 @@ var MessModl = require('../models/MessModl'); -// var NomsServ = require('../services/NomsServ'); +// var PolyUserServ = require('../services/PolyUserServ'); var ConvsServ = require('../services/ConvsServ'); var async = require('async'); var MesssServ = {}; MesssServ.addData = function (mess, cb) { - // NomsServ.get(Mess.login, function (nom) { + // PolyUserServ.get(Mess.login, function (err, nom) { // if (nom) { // Mess.nom = nom; // } else { diff --git a/app/services/NomsServ.js b/app/services/NomsServ.js deleted file mode 100644 index 89bc4d8..0000000 --- a/app/services/NomsServ.js +++ /dev/null @@ -1,129 +0,0 @@ -var NomsModl = require('../models/NomsModl'); -var LineTransform = require('node-line-reader').LineTransform; -var fs = require('fs'); - -var noms = {}; - -noms.readPasswd = function (login, cb) { - passwdF = 'config/passwd'; - fs.exists(passwdF, function (exists) { - found = false; - if (exists) { - stream = fs.createReadStream(passwdF); - transform = new LineTransform(); - stream.pipe(transform); - transform.on('data', function (line) { - ex = line.split(':'); - if (ex[0] == login) { // Si trouvé - stream.close(); - cb({ - 'username': ex[0], - 'password': ex[1], - 'UID': ex[2], - 'GID': ex[3], - 'GECOS': ex[4], - 'home': ex[5], - 'shell': ex[6] - }); - found = true; - } - }); - transform.on('end', function () { - if (!found) { - cb(false); - } - }); - } else { - cb(undefined); - } - }); -}; - -noms.readGroup = function (gid, cb) { - groupF = 'config/group'; - fs.exists(groupF, function (exists) { - found = false; - if (exists) { - stream = fs.createReadStream(groupF); - transform = new LineTransform(); - stream.pipe(transform); - transform.on('data', function (line) { - ex = line.split(':'); - if (ex[2] == gid) { // Si trouvé - stream.close(); - cb({ - 'name': ex[0], - 'password': ex[1], - 'GID': ex[2], - 'list': ex[3] - }); - found = true; - } - }); - transform.on('end', function () { - if (!found) { - cb(false); - } - }); - } else { - cb(undefined); - } - }); -}; - -noms.get = function (login, cb) { - NomsModl.findOne({ - login: login - }, { - nom: 1, - section: 1, - _id: 0 - }, function (err, nom) { - if (nom && !err && nom.nom && nom.section) { - cb(nom); - } else { - noms.readPasswd(login, function (passwd) { - if (passwd) { - noms.readGroup(passwd.GID, function (group) { - if (group) { - cb({ - nom: passwd.GECOS, - section: group.name.toUpperCase() - }); - NomsModl.create({ - login: login, - nom: passwd.GECOS, - section: group.name.toUpperCase() - }); - } else { - if (group === undefined) { - console.error("Impossible d'ouvrir le fichier des groupes."); - } else { - console.error("Impossible d'obtenir le groupe de " + passwd.GID + "."); - } - cb({ - nom: passwd.GECOS, - section: passwd.GID - }); - } - }); - } else { - if (passwd === undefined) { - console.error("Impossible d'ouvrir le fichier des noms."); - } else { - console.error("Impossible d'obtenir le nom de " + login + "."); - } - if (!login) { - login = 'Inconnu'; - } - cb({ - nom: login.toUpperCase(), - section: 'Inconnue' - }); - } - }); - } - }); -}; - -module.exports = noms; diff --git a/app/services/PolyUserServ.js b/app/services/PolyUserServ.js new file mode 100644 index 0000000..2bd70b8 --- /dev/null +++ b/app/services/PolyUserServ.js @@ -0,0 +1,184 @@ +var PolyUserModl = require('../models/PolyUserModl'); +var LineTransform = require('node-line-reader').LineTransform; +var fs = require('fs'); +var Client = require('ssh2').Client; +var creds = require('../../config/sshAuth'); + +var PolyUserServ = {}; + +PolyUserServ.readPasswd = function (login, cb) { + passwdF = 'config/passwd'; + fs.exists(passwdF, function (exists) { + found = false; + if (exists) { + stream = fs.createReadStream(passwdF); + transform = new LineTransform(); + stream.pipe(transform); + transform.on('data', function (line) { + ex = line.split(':'); + if (ex[0] == login) { // Si trouvé + stream.close(); + cb({ + 'username': ex[0], + 'password': ex[1], + 'UID': ex[2], + 'GID': ex[3], + 'GECOS': ex[4], + 'home': ex[5], + 'shell': ex[6] + }); + found = true; + } + }); + transform.on('end', function () { + if (!found) { + cb(false); + } + }); + } else { + cb(undefined); + } + }); +}; + +PolyUserServ.readGroup = function (gid, cb) { + groupF = 'config/group'; + fs.exists(groupF, function (exists) { + found = false; + if (exists) { + stream = fs.createReadStream(groupF); + transform = new LineTransform(); + stream.pipe(transform); + transform.on('data', function (line) { + ex = line.split(':'); + if (ex[2] == gid) { // Si trouvé + stream.close(); + cb({ + 'name': ex[0], + 'password': ex[1], + 'GID': ex[2], + 'list': ex[3] + }); + found = true; + } + }); + transform.on('end', function () { + if (!found) { + cb(false); + } + }); + } else { + cb(undefined); + } + }); +}; + +PolyUserServ.grabInfos = function (login, cb) { + PolyUserServ.readPasswd(login, function (passwd) { + if (passwd) { + PolyUserServ.readGroup(passwd.GID, function (group) { + if (group) { + cb({ + nom: passwd.GECOS, + section: group.name.toUpperCase() + }); + PolyUserModl.create({ + login: login, + nom: passwd.GECOS, + section: group.name.toUpperCase() + }); + } else { + if (group === undefined) { + console.error("Impossible d'ouvrir le fichier des groupes."); + } else { + console.error("Impossible d'obtenir le groupe de " + passwd.GID + "."); + } + cb({ + nom: passwd.GECOS, + section: passwd.GID + }); + } + }); + } else { + if (passwd === undefined) { + console.error("Impossible d'ouvrir le fichier des noms."); + } else { + console.error("Impossible d'obtenir le nom de " + login + "."); + } + if (!login) { + login = 'Inconnu'; + } + cb({ + nom: login.toUpperCase(), + section: 'Inconnue' + }); + } + }); +}; + +PolyUserServ.addData = function (polyuser, cb) { + cb(null, polyuser); +}; + +PolyUserServ.obtain = function (login, cb) { + PolyUserModl.findOne({ + login: login + }, { + nom: 1, + section: 1, + _id: 0 + }, function (err, polyuser) { + if (err) { + cb(err); + } else { + PolyUserServ.addData(polyuser, cb); + } + }); +}; + +PolyUserServ.add = function (login, cb) { + PolyUserServ.grabInfos(login, function (data) { + data.login = login; + PolyUserModl.create(data, function (err, polyuser) { + PolyUserServ.obtain(login, cb); + }); + }); +}; + +// PolyUserServ.remove = function (login, cb) { +// PolyUserModl.remove({ +// login: login +// }, cb); +// }; + +PolyUserServ.get = function (login, cb) { + PolyUserModl.findOne({ + login: login + }, function (err, polyuser) { + if (polyuser && !err && polyuser.polyuser && polyuser.section) { + PolyUserServ.obtain(login, cb); + } else { + PolyUserServ.add(login, cb); + } + }); +}; + +PolyUserServ.verify = function (login, pass, cb) { + var conn = new Client(); + conn.on('ready', function () { + cb(null, true); + }).on('error', function (err) { + if (err.level === 'client-authentication') { + cb(null, false); + } else { + cb(err); + } + }).connect({ + host: creds.host, + port: creds.port, + username: login, + password: pass + }); +}; + +module.exports = PolyUserServ; diff --git a/app/services/SessionsServ.js b/app/services/SessionsServ.js index d3eeebf..2763422 100644 --- a/app/services/SessionsServ.js +++ b/app/services/SessionsServ.js @@ -1,14 +1,13 @@ var SessionModl = require('../models/SessionModl'); -var NomsServ = require('../services/NomsServ'); +var PolyUserServ = require('../services/PolyUserServ'); var MembresServ = require('../services/MembresServ'); -var SshAuthServ = require('../services/SshAuthServ'); var sessions = {}; sessions.cur = false; sessions.addData = function (session, cb) { - NomsServ.get(session.login, function (nom) { + PolyUserServ.get(session.login, function (err, nom) { // Nom session.nom = nom.nom; session.section = nom.section; @@ -87,7 +86,7 @@ sessions.create = function (login, cb) { }; sessions.login = function (data, cb) { - SshAuthServ.verify(data.login, data.pass, cb); + PolyUserServ.verify(data.login, data.pass, cb); }; sessions.open = function (data, cb) { diff --git a/app/services/SshAuthServ.js b/app/services/SshAuthServ.js deleted file mode 100644 index 2c53ad4..0000000 --- a/app/services/SshAuthServ.js +++ /dev/null @@ -1,25 +0,0 @@ -var Client = require('ssh2').Client; - -var creds = require('../../config/sshAuth'); - -var SshAuth = {}; - -SshAuth.verify = function (login, pass, cb) { - var conn = new Client(); - conn.on('ready', function () { - cb(null, true); - }).on('error', function(err) { - if (err.level === 'client-authentication') { - cb(null, false); - } else { - cb(err); - } - }).connect({ - host: creds.host, - port: creds.port, - username: login, - password: pass - }); -}; - -module.exports = SshAuth; diff --git a/package.json b/package.json index 641182e..1cadc75 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,16 @@ { - "name": "ci-site", - "main": "server.js", - "repository": "https://github.com/ClubInfoPolytechLille/ci-site", - "dependencies": { - "async": "^0.9.0", - "body-parser": "^1.12.2", - "cookie-parser": "^1.3.4", - "express": "^4.12.3", - "mongoose": "^4.0.1", - "node-line-reader": "0.0.2", - "ssh2": "^0.4.6", - "ursa": "^0.8.4" - } + "name": "ci-site", + "main": "server.js", + "repository": "https://github.com/ClubInfoPolytechLille/ci-site", + "dependencies": { + "async": "^0.9.0", + "body-parser": "^1.12.2", + "cookie-parser": "^1.3.4", + "express": "^4.12.3", + "express-http-proxy": "^0.5.0", + "mongoose": "^4.0.1", + "node-line-reader": "0.0.2", + "ssh2": "^0.4.6", + "ursa": "^0.8.4" + } } -- libgit2 0.21.2