diff --git a/app/controllers/noms.js b/app/controllers/noms.js new file mode 100644 index 0000000..134070f --- /dev/null +++ b/app/controllers/noms.js @@ -0,0 +1,43 @@ +var Noms = require('../models/noms'); +var lineReader = require('line-reader'); + +var noms = {} + +noms.get = function (login, cb) { + Noms.find({ + login: login + }, function (err, nom) { + if (err) { + console.error(err) + cb(false) + } else { + if (nom.length >= 1) { + cb(nom.nom) + } else { + found = false + try { + lineReader.eachLine('config/passwd', function (line, last, cbL) { + ex = line.split(':') + // console.log(ex); + if (ex[0] == login) { // Si trouvé + found = true + cb(ex[4]) + cbL(false); + } else { + cbL(); + } + }).then(function () { + if (!found) { + cb(false) + } + }); + } catch (e) { + console.error("Error while fetching name", e) + cb(login.toUpperCase()) + } + } + } + }) +} + +module.exports = noms; \ No newline at end of file diff --git a/app/controllers/sessions.js b/app/controllers/sessions.js index 25883f1..656722c 100644 --- a/app/controllers/sessions.js +++ b/app/controllers/sessions.js @@ -1,7 +1,32 @@ var Session = require('../models/session'); +var noms = require('../controllers/noms'); var sessions = {} +sessions.addData = function (session, cb) { + noms.get(session.login, function (nom) { + if (typeof nom == 'string') { + session.nom = nom + } else { + session.nom = 'Inconnu' + } + cb(session) + }) +} + +sessions.find = function (id, cb) { + _this = this + Session.findById(id).lean().exec(function (err, session) { + if (typeof session == 'object') { + _this.addData(session, function (session) { + cb(err, session) + }) + } else { + cb(err, null) + } + }) +} + sessions.valid = function (session) { return session.started.setSeconds(session.started.getSeconds() + 3600) > new Date() } @@ -14,7 +39,7 @@ sessions.delete = function (id, cb) { sessions.verify = function (id, cb) { _this = this - Session.findById(id, function (err, session) { + _this.find(id, function (err, session) { if (err) { cb('error'); } else { @@ -40,24 +65,31 @@ sessions.create = function (login, cb) { sessions.login = function (data, cb) { // DUMMY - if (data.login == 'cool' && data.pass == 'cool') { - cb(null, true); - } else { - cb(null, false); - } + noms.get(data.login, function (nom) { + if (nom == false) { + cb(null, false) + } else { + if (data.pass == 'cool') { + cb(null, true) + } else { + cb(null, false) + } + } + }) } sessions.open = function (data, cb) { - sessions.login(data, function (err, res) { + _this = this + _this.login(data, function (err, res) { if (err) { cb('error') } else { if (res) { - sessions.create(data.login, function (err, session) { + _this.create(data.login, function (err, session) { if (err) { cb('error'); } else { - cb(session); + _this.find(session._id, cb) } }); } else { diff --git a/app/models/noms.js b/app/models/noms.js new file mode 100644 index 0000000..7d46952 --- /dev/null +++ b/app/models/noms.js @@ -0,0 +1,12 @@ +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' + } +}); \ No newline at end of file diff --git a/app/routes/api.js b/app/routes/api.js index 3ab784d..544b9df 100644 --- a/app/routes/api.js +++ b/app/routes/api.js @@ -21,11 +21,14 @@ api.get('/session', function (req, res) { // Informations sur la session }); api.post('/session', function (req, res) { // Se connecter - sessions.open(req.body, function (status) { - if (typeof status === 'object') { - res.cookie('session', status._id); + sessions.open(req.body, function (err, session) { + if (err) { + res.send(err) + console.error(err) + } else { + res.cookie('session', session._id); + res.send(session) } - res.send(status); }) }) diff --git a/package.json b/package.json index e42aabf..efcd9e2 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "body-parser": "^1.12.0", "cookie-parser": "^1.3.4", "express": "^4.12.2", + "line-reader": "^0.2.4", "method-override": "^2.3.1", "mongoose": "^3.8.25", "morgan": "^1.5.1" diff --git a/public/js/services/SessionServ.js b/public/js/services/SessionServ.js index c66c44e..d4deaa5 100644 --- a/public/js/services/SessionServ.js +++ b/public/js/services/SessionServ.js @@ -1,7 +1,7 @@ angular.module('SessionsServ', []).service('SessionService', ['$http', function ($http) { a = { - name: "Invité", + nom: "Invité", logged: false, status: 0, changeHandlers: [], @@ -17,7 +17,7 @@ angular.module('SessionsServ', []).service('SessionService', ['$http', if (typeof data === 'object') { console.log("Connected") this.logged = true - this.name = data.login + this.nom = data.nom } else { this.logged = false diff --git a/public/views/index.html b/public/views/index.html index 13317ae..ae7be6f 100644 --- a/public/views/index.html +++ b/public/views/index.html @@ -32,7 +32,7 @@
-- libgit2 0.21.2