Commit dafb4eeb55a5880f4ff725e83681c484db1b8260
1 parent
ac04356e
Affichage des noms complets via passwd
Authentification via identifiants TODO: Cache via db
Showing
7 changed files
with
107 additions
and
16 deletions
Show diff stats
@@ -0,0 +1,43 @@ | @@ -0,0 +1,43 @@ | ||
1 | +var Noms = require('../models/noms'); | ||
2 | +var lineReader = require('line-reader'); | ||
3 | + | ||
4 | +var noms = {} | ||
5 | + | ||
6 | +noms.get = function (login, cb) { | ||
7 | + Noms.find({ | ||
8 | + login: login | ||
9 | + }, function (err, nom) { | ||
10 | + if (err) { | ||
11 | + console.error(err) | ||
12 | + cb(false) | ||
13 | + } else { | ||
14 | + if (nom.length >= 1) { | ||
15 | + cb(nom.nom) | ||
16 | + } else { | ||
17 | + found = false | ||
18 | + try { | ||
19 | + lineReader.eachLine('config/passwd', function (line, last, cbL) { | ||
20 | + ex = line.split(':') | ||
21 | + // console.log(ex); | ||
22 | + if (ex[0] == login) { // Si trouvé | ||
23 | + found = true | ||
24 | + cb(ex[4]) | ||
25 | + cbL(false); | ||
26 | + } else { | ||
27 | + cbL(); | ||
28 | + } | ||
29 | + }).then(function () { | ||
30 | + if (!found) { | ||
31 | + cb(false) | ||
32 | + } | ||
33 | + }); | ||
34 | + } catch (e) { | ||
35 | + console.error("Error while fetching name", e) | ||
36 | + cb(login.toUpperCase()) | ||
37 | + } | ||
38 | + } | ||
39 | + } | ||
40 | + }) | ||
41 | +} | ||
42 | + | ||
43 | +module.exports = noms; | ||
0 | \ No newline at end of file | 44 | \ No newline at end of file |
app/controllers/sessions.js
1 | var Session = require('../models/session'); | 1 | var Session = require('../models/session'); |
2 | +var noms = require('../controllers/noms'); | ||
2 | 3 | ||
3 | var sessions = {} | 4 | var sessions = {} |
4 | 5 | ||
6 | +sessions.addData = function (session, cb) { | ||
7 | + noms.get(session.login, function (nom) { | ||
8 | + if (typeof nom == 'string') { | ||
9 | + session.nom = nom | ||
10 | + } else { | ||
11 | + session.nom = 'Inconnu' | ||
12 | + } | ||
13 | + cb(session) | ||
14 | + }) | ||
15 | +} | ||
16 | + | ||
17 | +sessions.find = function (id, cb) { | ||
18 | + _this = this | ||
19 | + Session.findById(id).lean().exec(function (err, session) { | ||
20 | + if (typeof session == 'object') { | ||
21 | + _this.addData(session, function (session) { | ||
22 | + cb(err, session) | ||
23 | + }) | ||
24 | + } else { | ||
25 | + cb(err, null) | ||
26 | + } | ||
27 | + }) | ||
28 | +} | ||
29 | + | ||
5 | sessions.valid = function (session) { | 30 | sessions.valid = function (session) { |
6 | return session.started.setSeconds(session.started.getSeconds() + 3600) > new Date() | 31 | return session.started.setSeconds(session.started.getSeconds() + 3600) > new Date() |
7 | } | 32 | } |
@@ -14,7 +39,7 @@ sessions.delete = function (id, cb) { | @@ -14,7 +39,7 @@ sessions.delete = function (id, cb) { | ||
14 | 39 | ||
15 | sessions.verify = function (id, cb) { | 40 | sessions.verify = function (id, cb) { |
16 | _this = this | 41 | _this = this |
17 | - Session.findById(id, function (err, session) { | 42 | + _this.find(id, function (err, session) { |
18 | if (err) { | 43 | if (err) { |
19 | cb('error'); | 44 | cb('error'); |
20 | } else { | 45 | } else { |
@@ -40,24 +65,31 @@ sessions.create = function (login, cb) { | @@ -40,24 +65,31 @@ sessions.create = function (login, cb) { | ||
40 | 65 | ||
41 | sessions.login = function (data, cb) { | 66 | sessions.login = function (data, cb) { |
42 | // DUMMY | 67 | // DUMMY |
43 | - if (data.login == 'cool' && data.pass == 'cool') { | ||
44 | - cb(null, true); | ||
45 | - } else { | ||
46 | - cb(null, false); | ||
47 | - } | 68 | + noms.get(data.login, function (nom) { |
69 | + if (nom == false) { | ||
70 | + cb(null, false) | ||
71 | + } else { | ||
72 | + if (data.pass == 'cool') { | ||
73 | + cb(null, true) | ||
74 | + } else { | ||
75 | + cb(null, false) | ||
76 | + } | ||
77 | + } | ||
78 | + }) | ||
48 | } | 79 | } |
49 | 80 | ||
50 | sessions.open = function (data, cb) { | 81 | sessions.open = function (data, cb) { |
51 | - sessions.login(data, function (err, res) { | 82 | + _this = this |
83 | + _this.login(data, function (err, res) { | ||
52 | if (err) { | 84 | if (err) { |
53 | cb('error') | 85 | cb('error') |
54 | } else { | 86 | } else { |
55 | if (res) { | 87 | if (res) { |
56 | - sessions.create(data.login, function (err, session) { | 88 | + _this.create(data.login, function (err, session) { |
57 | if (err) { | 89 | if (err) { |
58 | cb('error'); | 90 | cb('error'); |
59 | } else { | 91 | } else { |
60 | - cb(session); | 92 | + _this.find(session._id, cb) |
61 | } | 93 | } |
62 | }); | 94 | }); |
63 | } else { | 95 | } else { |
@@ -0,0 +1,12 @@ | @@ -0,0 +1,12 @@ | ||
1 | +var mongoose = require('mongoose'); | ||
2 | + | ||
3 | +module.exports = mongoose.model('Noms', { | ||
4 | + login: { // On récupèrera le nom via les passwd | ||
5 | + type: String, | ||
6 | + default: 'login' | ||
7 | + }, | ||
8 | + nom: { | ||
9 | + type: String, | ||
10 | + default: 'Nom' | ||
11 | + } | ||
12 | +}); | ||
0 | \ No newline at end of file | 13 | \ No newline at end of file |
app/routes/api.js
@@ -21,11 +21,14 @@ api.get('/session', function (req, res) { // Informations sur la session | @@ -21,11 +21,14 @@ api.get('/session', function (req, res) { // Informations sur la session | ||
21 | }); | 21 | }); |
22 | 22 | ||
23 | api.post('/session', function (req, res) { // Se connecter | 23 | api.post('/session', function (req, res) { // Se connecter |
24 | - sessions.open(req.body, function (status) { | ||
25 | - if (typeof status === 'object') { | ||
26 | - res.cookie('session', status._id); | 24 | + sessions.open(req.body, function (err, session) { |
25 | + if (err) { | ||
26 | + res.send(err) | ||
27 | + console.error(err) | ||
28 | + } else { | ||
29 | + res.cookie('session', session._id); | ||
30 | + res.send(session) | ||
27 | } | 31 | } |
28 | - res.send(status); | ||
29 | }) | 32 | }) |
30 | }) | 33 | }) |
31 | 34 |
package.json
@@ -5,6 +5,7 @@ | @@ -5,6 +5,7 @@ | ||
5 | "body-parser": "^1.12.0", | 5 | "body-parser": "^1.12.0", |
6 | "cookie-parser": "^1.3.4", | 6 | "cookie-parser": "^1.3.4", |
7 | "express": "^4.12.2", | 7 | "express": "^4.12.2", |
8 | + "line-reader": "^0.2.4", | ||
8 | "method-override": "^2.3.1", | 9 | "method-override": "^2.3.1", |
9 | "mongoose": "^3.8.25", | 10 | "mongoose": "^3.8.25", |
10 | "morgan": "^1.5.1" | 11 | "morgan": "^1.5.1" |
public/js/services/SessionServ.js
1 | angular.module('SessionsServ', []).service('SessionService', ['$http', | 1 | angular.module('SessionsServ', []).service('SessionService', ['$http', |
2 | function ($http) { | 2 | function ($http) { |
3 | a = { | 3 | a = { |
4 | - name: "Invité", | 4 | + nom: "Invité", |
5 | logged: false, | 5 | logged: false, |
6 | status: 0, | 6 | status: 0, |
7 | changeHandlers: [], | 7 | changeHandlers: [], |
@@ -17,7 +17,7 @@ angular.module('SessionsServ', []).service('SessionService', ['$http', | @@ -17,7 +17,7 @@ angular.module('SessionsServ', []).service('SessionService', ['$http', | ||
17 | if (typeof data === 'object') { | 17 | if (typeof data === 'object') { |
18 | console.log("Connected") | 18 | console.log("Connected") |
19 | this.logged = true | 19 | this.logged = true |
20 | - this.name = data.login | 20 | + this.nom = data.nom |
21 | } else { | 21 | } else { |
22 | 22 | ||
23 | this.logged = false | 23 | this.logged = false |
public/views/index.html
@@ -32,7 +32,7 @@ | @@ -32,7 +32,7 @@ | ||
32 | <ul class="nav navbar-nav navbar-right" ng-controller="SessionController"> | 32 | <ul class="nav navbar-nav navbar-right" ng-controller="SessionController"> |
33 | <li> | 33 | <li> |
34 | <a ng-hide="session.logged" href="/connect">Se connecter</a> | 34 | <a ng-hide="session.logged" href="/connect">Se connecter</a> |
35 | - <a ng-show="session.logged" href="#" ng-click="session.disconnect()">{{ session.name }} <span class="glyphicon glyphicon-off"></span></a> | 35 | + <a ng-show="session.logged" href="#" ng-click="session.disconnect()">{{ session.nom }} <span class="glyphicon glyphicon-off"></span></a> |
36 | </li> | 36 | </li> |
37 | </ul> | 37 | </ul> |
38 | </div> | 38 | </div> |