From 9bd5849b5df436a34e10b8b4399786e778c10d55 Mon Sep 17 00:00:00 2001 From: Geoffrey Frogeye Date: Sun, 26 Apr 2015 13:12:13 +0200 Subject: [PATCH] Amelioré PolyUserServ --- app/services/PolyUserServ.js | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------------------------------------------------- package.json | 1 - 2 files changed, 52 insertions(+), 71 deletions(-) diff --git a/app/services/PolyUserServ.js b/app/services/PolyUserServ.js index 4804b44..14b0f90 100644 --- a/app/services/PolyUserServ.js +++ b/app/services/PolyUserServ.js @@ -1,5 +1,5 @@ -var LineTransform = require('node-line-reader').LineTransform; var fs = require('fs'); +var async = require('async'); var Client = require('ssh2').Client; var creds = require('../../config/sshAuth'); var NodeCache = require("node-cache"); @@ -12,17 +12,18 @@ 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) { + fs.readFile(passwdF, function (err, file) { + if (err) { + cb(err); + } else { + lines = file.toString('utf8').split('\n'); + async.detect(lines, function (line, cba) { ex = line.split(':'); - if (ex[0] == login) { // Si trouvé - stream.close(); - cb({ + cba(ex[0] == login); + }, function (res) { + if (res) { + ex = res.split(':'); + cb(null, { 'username': ex[0], 'password': ex[1], 'UID': ex[2], @@ -31,94 +32,75 @@ PolyUserServ.readPasswd = function (login, cb) { 'home': ex[5], 'shell': ex[6] }); - found = true; - } - }); - transform.on('end', function () { - if (!found) { - cb(false); + } else { + cb(null, null); } }); - } 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) { + fs.readFile(groupF, function (err, file) { + if (err) { + cb(err); + } else { + lines = file.toString('utf8').split('\n'); + async.detect(lines, function (line, cba) { ex = line.split(':'); - if (ex[2] == gid) { // Si trouvé - stream.close(); - cb({ + cba(ex[2] == gid); + }, function (res) { + if (res) { + ex = res.split(':'); + cb(null, { 'name': ex[0], 'password': ex[1], 'GID': ex[2], 'list': ex[3] }); - found = true; - } - }); - transform.on('end', function () { - if (!found) { - cb(false); + } else { + cb(null, null); } }); - } 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() - }); - } 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."); + async.waterfall([ + function (cba) { + PolyUserServ.readPasswd(login, cba); + }, + function (passwd, cba) { + if (passwd && passwd.GID) { + PolyUserServ.readGroup(passwd.GID, function (err, group) { + cba(err, passwd, group); + }); } else { - console.error("Impossible d'obtenir le nom de " + login + "."); - } - if (!login) { - login = 'Inconnu'; + cba(null, passwd, null); } - cb({ - nom: login.toUpperCase(), - section: 'Inconnue' + } + ], function (err, passwd, group) { + if (err) { + cb(err); + } else { + cb(null, { + nom: (passwd && passwd.GECOS) ? passwd.GECOS : login.toUpperCase(), + section: (group && group.name) ? group.name.toUpperCase() : ((passwd && passwd.GID) ? passwd.GID : 'Inconnu') }); } }); }; PolyUserServ.add = function (login, cb) { - PolyUserServ.grabInfos(login, function (data) { - cb(null, data); - cache.set(login, data); + PolyUserServ.grabInfos(login, function (err, data) { + if (err) { + cb(err); + } else { + cb(null, data); + cache.set(login, data); + } }); }; diff --git a/package.json b/package.json index 3b995f6..17a74a8 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,6 @@ "express-session": "^1.11.1", "mongoose": "^4.0.2", "node-cache": "^2.1.1", - "node-line-reader": "0.0.2", "serve-favicon": "^2.2.0", "ssh2": "^0.4.6", "ursa": "^0.8.4" -- libgit2 0.21.2