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 @@ |
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 | 44 | \ No newline at end of file | ... | ... |
app/controllers/sessions.js
1 | 1 | var Session = require('../models/session'); |
2 | +var noms = require('../controllers/noms'); | |
2 | 3 | |
3 | 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 | 30 | sessions.valid = function (session) { |
6 | 31 | return session.started.setSeconds(session.started.getSeconds() + 3600) > new Date() |
7 | 32 | } |
... | ... | @@ -14,7 +39,7 @@ sessions.delete = function (id, cb) { |
14 | 39 | |
15 | 40 | sessions.verify = function (id, cb) { |
16 | 41 | _this = this |
17 | - Session.findById(id, function (err, session) { | |
42 | + _this.find(id, function (err, session) { | |
18 | 43 | if (err) { |
19 | 44 | cb('error'); |
20 | 45 | } else { |
... | ... | @@ -40,24 +65,31 @@ sessions.create = function (login, cb) { |
40 | 65 | |
41 | 66 | sessions.login = function (data, cb) { |
42 | 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 | 81 | sessions.open = function (data, cb) { |
51 | - sessions.login(data, function (err, res) { | |
82 | + _this = this | |
83 | + _this.login(data, function (err, res) { | |
52 | 84 | if (err) { |
53 | 85 | cb('error') |
54 | 86 | } else { |
55 | 87 | if (res) { |
56 | - sessions.create(data.login, function (err, session) { | |
88 | + _this.create(data.login, function (err, session) { | |
57 | 89 | if (err) { |
58 | 90 | cb('error'); |
59 | 91 | } else { |
60 | - cb(session); | |
92 | + _this.find(session._id, cb) | |
61 | 93 | } |
62 | 94 | }); |
63 | 95 | } else { | ... | ... |
... | ... | @@ -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 | 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 | 21 | }); |
22 | 22 | |
23 | 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
public/js/services/SessionServ.js
1 | 1 | angular.module('SessionsServ', []).service('SessionService', ['$http', |
2 | 2 | function ($http) { |
3 | 3 | a = { |
4 | - name: "Invité", | |
4 | + nom: "Invité", | |
5 | 5 | logged: false, |
6 | 6 | status: 0, |
7 | 7 | changeHandlers: [], |
... | ... | @@ -17,7 +17,7 @@ angular.module('SessionsServ', []).service('SessionService', ['$http', |
17 | 17 | if (typeof data === 'object') { |
18 | 18 | console.log("Connected") |
19 | 19 | this.logged = true |
20 | - this.name = data.login | |
20 | + this.nom = data.nom | |
21 | 21 | } else { |
22 | 22 | |
23 | 23 | this.logged = false | ... | ... |
public/views/index.html
... | ... | @@ -32,7 +32,7 @@ |
32 | 32 | <ul class="nav navbar-nav navbar-right" ng-controller="SessionController"> |
33 | 33 | <li> |
34 | 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 | 36 | </li> |
37 | 37 | </ul> |
38 | 38 | </div> | ... | ... |