Commit e4fff96cf5cfac9ccba3393d0052d84356adec4a
1 parent
f22cd7f3
Récupération des sections via /etc/group
Showing
6 changed files
with
122 additions
and
58 deletions
Show diff stats
app/models/MembreModl.js
... | ... | @@ -5,14 +5,6 @@ module.exports = mongoose.model('Membre', { |
5 | 5 | type: String, |
6 | 6 | default: 'login' |
7 | 7 | }, |
8 | - section: { // TODO From /etc/groups | |
9 | - type: String, | |
10 | - default: 'IMA' | |
11 | - }, | |
12 | - promo: { // Nécessaire pour calculer le numéro de section | |
13 | - type: Number, | |
14 | - default: 2017 | |
15 | - }, | |
16 | 8 | role: { |
17 | 9 | type: String, |
18 | 10 | default: 'Membre' | ... | ... |
app/models/NomsModl.js
app/services/MembresServ.js
... | ... | @@ -6,16 +6,13 @@ var MembresServ = {}; |
6 | 6 | |
7 | 7 | MembresServ.addData = function (membre, cb) { |
8 | 8 | NomsServ.get(membre.login, function (nom) { |
9 | - if (nom) { | |
10 | - membre.nom = nom; | |
11 | - } else { | |
12 | - membre.nom = membre.login; | |
13 | - } | |
9 | + membre.nom = nom.nom; | |
10 | + membre.section = nom.section; | |
14 | 11 | cb(null, membre); |
15 | 12 | }); |
16 | 13 | }; |
17 | 14 | |
18 | -MembresServ.get = function(id, cb) { | |
15 | +MembresServ.get = function (id, cb) { | |
19 | 16 | MembreModl.findById(id).lean().exec(function (err, membre) { |
20 | 17 | if (err) |
21 | 18 | cb(err); |
... | ... | @@ -35,7 +32,7 @@ MembresServ.add = function (data, cb) { |
35 | 32 | login: data.login, |
36 | 33 | role: data.role, |
37 | 34 | section: data.section, |
38 | - }, function(err, membre) { | |
35 | + }, function (err, membre) { | |
39 | 36 | MembresServ.get(membre._id, cb); |
40 | 37 | }); |
41 | 38 | }; | ... | ... |
app/services/NomsServ.js
... | ... | @@ -4,47 +4,121 @@ var fs = require('fs'); |
4 | 4 | |
5 | 5 | var noms = {}; |
6 | 6 | |
7 | +noms.readPasswd = function (login, cb) { | |
8 | + passwdF = 'config/passwd'; | |
9 | + fs.exists(passwdF, function (exists) { | |
10 | + found = false; | |
11 | + if (exists) { | |
12 | + stream = fs.createReadStream(passwdF); | |
13 | + transform = new LineTransform(); | |
14 | + stream.pipe(transform); | |
15 | + transform.on('data', function (line) { | |
16 | + ex = line.split(':'); | |
17 | + if (ex[0] == login) { // Si trouvé | |
18 | + stream.close(); | |
19 | + cb({ | |
20 | + 'username': ex[0], | |
21 | + 'password': ex[1], | |
22 | + 'UID': ex[2], | |
23 | + 'GID': ex[3], | |
24 | + 'GECOS': ex[4], | |
25 | + 'home': ex[5], | |
26 | + 'shell': ex[6] | |
27 | + }); | |
28 | + found = true; | |
29 | + } | |
30 | + }); | |
31 | + transform.on('end', function () { | |
32 | + if (!found) { | |
33 | + cb(false); | |
34 | + } | |
35 | + }); | |
36 | + } else { | |
37 | + cb(undefined); | |
38 | + } | |
39 | + }); | |
40 | +}; | |
41 | + | |
42 | +noms.readGroup = function (gid, cb) { | |
43 | + groupF = 'config/group'; | |
44 | + fs.exists(groupF, function (exists) { | |
45 | + found = false; | |
46 | + if (exists) { | |
47 | + stream = fs.createReadStream(groupF); | |
48 | + transform = new LineTransform(); | |
49 | + stream.pipe(transform); | |
50 | + transform.on('data', function (line) { | |
51 | + ex = line.split(':'); | |
52 | + if (ex[2] == gid) { // Si trouvé | |
53 | + stream.close(); | |
54 | + cb({ | |
55 | + 'name': ex[0], | |
56 | + 'password': ex[1], | |
57 | + 'GID': ex[2], | |
58 | + 'list': ex[3] | |
59 | + }); | |
60 | + found = true; | |
61 | + } | |
62 | + }); | |
63 | + transform.on('end', function () { | |
64 | + if (!found) { | |
65 | + cb(false); | |
66 | + } | |
67 | + }); | |
68 | + } else { | |
69 | + cb(undefined); | |
70 | + } | |
71 | + }); | |
72 | +}; | |
73 | + | |
7 | 74 | noms.get = function (login, cb) { |
8 | 75 | NomsModl.findOne({ |
9 | 76 | login: login |
77 | + }, { | |
78 | + nom: 1, | |
79 | + section: 1, | |
80 | + _id: 0 | |
10 | 81 | }, function (err, nom) { |
11 | - if (err) { | |
12 | - console.error(err); | |
13 | - cb(false); | |
82 | + if (nom && !err && nom.nom && nom.section) { | |
83 | + cb(nom); | |
14 | 84 | } else { |
15 | - if (nom) { | |
16 | - cb(nom.nom); | |
17 | - } else { | |
18 | - passwdF = 'config/passwd'; | |
19 | - fs.exists(passwdF, function (exists) { | |
20 | - found = false; | |
21 | - if (exists) { | |
22 | - stream = fs.createReadStream(passwdF); | |
23 | - transform = new LineTransform(); | |
24 | - stream.pipe(transform); | |
25 | - transform.on('data', function (line) { | |
26 | - ex = line.split(':'); | |
27 | - if (ex[0] == login) { // Si trouvé | |
28 | - stream.close(); | |
29 | - cb(ex[4]); | |
30 | - found = true; | |
31 | - NomsModl.create({ | |
32 | - login: login, | |
33 | - nom: ex[4] | |
34 | - }); | |
35 | - } | |
36 | - }); | |
37 | - transform.on('end', function () { | |
38 | - if (!found) { | |
39 | - cb(false); | |
85 | + noms.readPasswd(login, function (passwd) { | |
86 | + if (passwd) { | |
87 | + noms.readGroup(passwd.GID, function (group) { | |
88 | + if (group) { | |
89 | + cb({ | |
90 | + nom: passwd.GECOS, | |
91 | + section: group.name.toUpperCase() | |
92 | + }); | |
93 | + NomsModl.create({ | |
94 | + login: login, | |
95 | + nom: passwd.GECOS, | |
96 | + section: group.name.toUpperCase() | |
97 | + }); | |
98 | + } else { | |
99 | + if (group === undefined) { | |
100 | + console.error("Impossible d'ouvrir le fichier des groupes."); | |
101 | + } else { | |
102 | + console.error("Impossible d'obtenir le groupe de " + passwd.GID + "."); | |
40 | 103 | } |
41 | - }); | |
104 | + cb({ | |
105 | + nom: passwd.GECOS, | |
106 | + section: passwd.GID | |
107 | + }); | |
108 | + } | |
109 | + }); | |
110 | + } else { | |
111 | + if (passwd === undefined) { | |
112 | + console.error("Impossible d'ouvrir le fichier des noms."); | |
42 | 113 | } else { |
43 | - console.error("Impossible de trouver le fichier passwd"); | |
44 | - cb(login.toUpperCase()); | |
114 | + console.error("Impossible d'obtenir le nom de " + login + "."); | |
45 | 115 | } |
46 | - }); | |
47 | - } | |
116 | + cb({ | |
117 | + nom: login.toUpperCase(), | |
118 | + section: 'Inconnue' | |
119 | + }); | |
120 | + } | |
121 | + }); | |
48 | 122 | } |
49 | 123 | }); |
50 | 124 | }; | ... | ... |
app/services/SessionsServ.js
... | ... | @@ -8,11 +8,10 @@ sessions.cur = false; |
8 | 8 | |
9 | 9 | sessions.addData = function (session, cb) { |
10 | 10 | NomsServ.get(session.login, function (nom) { |
11 | - if (typeof nom == 'string') { | |
12 | - session.nom = nom; | |
13 | - } else { | |
14 | - session.nom = 'Inconnu'; | |
15 | - } | |
11 | + // Nom | |
12 | + session.nom = nom.nom; | |
13 | + session.section = nom.section; | |
14 | + // Permissions | |
16 | 15 | session.canAddMembre = session.login == 'gbontoux'; |
17 | 16 | session.canDelMembre = session.login == 'gbontoux'; |
18 | 17 | session.canAddConv = true; | ... | ... |
public/views/membres.html
... | ... | @@ -28,11 +28,9 @@ |
28 | 28 | <tfoot id="membre-form" ng-if="session.canAddMembre"> |
29 | 29 | <tr class="form-group"> |
30 | 30 | <td> |
31 | - <input type="text" class="form-control input-lg" placeholder="Login" ng-model="formData.login"> | |
31 | + <input type="text" class="form-control input-lg" placeholder="Login Polytech" ng-model="formData.login"> | |
32 | 32 | </td> |
33 | - <td class="form-inline"> | |
34 | - <input type="text" class="form-control input-lg" placeholder="Section" ng-model="formData.section"> | |
35 | - <input type="number" class="form-control input-lg" placeholder="2015" ng-model="formData.promo"> | |
33 | + <td> | |
36 | 34 | </td> |
37 | 35 | <td> |
38 | 36 | <input type="text" class="form-control input-lg" placeholder="Rôle" ng-model="formData.role"> |
... | ... | @@ -45,4 +43,4 @@ |
45 | 43 | </tr> |
46 | 44 | </tfoot> |
47 | 45 | </table> |
48 | 46 | -</div> |
47 | +</div> | |
49 | 48 | \ No newline at end of file | ... | ... |