Commit a470afda542fd8ff6240de72d7dbc019e7dcf9a8
1 parent
ba3a9e89
Simplification des permissions
Vous pouvez mainteant éditer VOS messages. Fix #12
Showing
7 changed files
with
127 additions
and
65 deletions
Show diff stats
app/routes/ApiRtes.js
... | ... | @@ -2,8 +2,11 @@ var MembresServ = require('../services/MembresServ'); |
2 | 2 | var PolyUserServ = require('../services/PolyUserServ'); |
3 | 3 | var DecryptServ = require('../services/DecryptServ'); |
4 | 4 | var DosssServ = require('../services/DosssServ'); |
5 | +var DossModl = require('../models/DossModl'); | |
5 | 6 | var ConvsServ = require('../services/ConvsServ'); |
7 | +var ConvModl = require('../models/ConvModl'); | |
6 | 8 | var MessServ = require('../services/MessServ'); |
9 | +var MessModl = require('../models/MessModl'); // TODO Unfier ce bazar / supprimer Serv | |
7 | 10 | var fs = require('fs'); |
8 | 11 | var mongoose = require('mongoose'); |
9 | 12 | var express = require('express'); |
... | ... | @@ -44,9 +47,21 @@ reqVerified = function (verify) { // Assert mais pour les droits (d'où le 403) |
44 | 47 | }; |
45 | 48 | }; |
46 | 49 | |
47 | -reqPerm = function (perm) { | |
50 | +reqOwn = function (objName) { | |
48 | 51 | return reqVerified(function (req, res, cb) { |
49 | - cb(null, req.session.data[perm]); | |
52 | + cb(null, req.session.data.bureau || req[objName].login == req.session.data.login); | |
53 | + }); | |
54 | +}; | |
55 | + | |
56 | +reqMembre = function () { | |
57 | + return reqVerified(function (req, res, cb) { | |
58 | + cb(null, req.session.data.membre); | |
59 | + }); | |
60 | +}; | |
61 | + | |
62 | +reqBureau = function () { | |
63 | + return reqVerified(function (req, res, cb) { | |
64 | + cb(null, req.session.data.bureau); | |
50 | 65 | }); |
51 | 66 | }; |
52 | 67 | |
... | ... | @@ -86,19 +101,12 @@ sessionData = function (session, cb) { |
86 | 101 | // Nom |
87 | 102 | session.nom = nom.nom; |
88 | 103 | session.section = nom.section; |
89 | - MembresServ.estBureau(session.login, function (bureau) { | |
90 | - session.bureau = bureau; | |
91 | - // Permissions | |
92 | - session.canAddMembre = session.bureau; | |
93 | - session.canDelMembre = session.bureau; | |
94 | - session.canAddDoss = session.bureau; | |
95 | - session.canDelDoss = session.bureau; | |
96 | - session.canAddConv = true; | |
97 | - session.canDelConv = session.bureau; | |
98 | - session.canAddMess = true; | |
99 | - session.canEditMess = session.bureau; | |
100 | - session.canDelMess = session.bureau; | |
101 | - cb(session); | |
104 | + MembresServ.estMembre(session.login, function (membre) { // TODO Asyc | |
105 | + session.membre = membre; | |
106 | + MembresServ.estBureau(session.login, function (bureau) { | |
107 | + session.bureau = bureau; | |
108 | + cb(session); | |
109 | + }); | |
102 | 110 | }); |
103 | 111 | }); |
104 | 112 | }; |
... | ... | @@ -133,7 +141,7 @@ api.post('/session', decrypt(), assert(function (req, res, cb) { |
133 | 141 | req.session.data = session; |
134 | 142 | req.session.save(function (err) { |
135 | 143 | if (err) { |
136 | - res.status(500).end("Sauvegarde session"); | |
144 | + res.status(500).end(err); | |
137 | 145 | } else { |
138 | 146 | res.status(201).send(session); |
139 | 147 | } |
... | ... | @@ -142,7 +150,7 @@ api.post('/session', decrypt(), assert(function (req, res, cb) { |
142 | 150 | } else { |
143 | 151 | req.session.destroy(function (err) { |
144 | 152 | if (err) { |
145 | - res.status(500).end("Suppression de la session"); | |
153 | + res.status(500).end(err); | |
146 | 154 | } else { |
147 | 155 | res.status(401).end(); |
148 | 156 | } |
... | ... | @@ -170,7 +178,7 @@ api.get('/membres', function (req, res) { // Liste des membres |
170 | 178 | |
171 | 179 | api.post('/membres', assert(function (req, res, cb) { |
172 | 180 | cb(null, typeof req.body.login == 'string' && req.body.login !== ''); |
173 | -}), reqPerm('canAddMembre'), function (req, res) { // Ajout d'un membre | |
181 | +}), reqBureau(), function (req, res) { // Ajout d'un membre | |
174 | 182 | MembresServ.add(req.body, function (err, membre) { |
175 | 183 | if (err) |
176 | 184 | res.status(500).send(err); |
... | ... | @@ -179,7 +187,7 @@ api.post('/membres', assert(function (req, res, cb) { |
179 | 187 | }); |
180 | 188 | }); |
181 | 189 | |
182 | -api.delete('/membres/:membre_id', reqPerm('canDelMembre'), function (req, res) { // Supression d'un membre | |
190 | +api.delete('/membres/:membre_id', reqBureau(), function (req, res) { // Supression d'un membre | |
183 | 191 | MembresServ.remove(req.params.membre_id, function (err, membre) { |
184 | 192 | if (err) |
185 | 193 | res.status(500).send(err); |
... | ... | @@ -189,14 +197,14 @@ api.delete('/membres/:membre_id', reqPerm('canDelMembre'), function (req, res) { |
189 | 197 | }); |
190 | 198 | |
191 | 199 | // Dossiers |
192 | -api.get('/dosss/:doss_id', function (req, res) { // Un doss | |
200 | +api.get('/dosss/:doss_id', reqAuth(), function (req, res) { // Un doss | |
193 | 201 | // TODO Assertion 404 existe, transformer req.body.id avec la vraie id (ou redirect) |
194 | 202 | // TODO Requêtes séparées ? |
195 | 203 | DosssServ.get(req.params.doss_id, function (err, doss) { // TODO Async |
196 | 204 | if (err) { |
197 | 205 | res.status(500).send(err); |
198 | 206 | } else if (!doss) { |
199 | - res.status(404); | |
207 | + res.status(404).end(); | |
200 | 208 | } else { |
201 | 209 | DosssServ.children(doss._id, function (err, dosss) { |
202 | 210 | if (err) { |
... | ... | @@ -217,7 +225,7 @@ api.get('/dosss/:doss_id', function (req, res) { // Un doss |
217 | 225 | }); |
218 | 226 | }); |
219 | 227 | |
220 | -api.post('/dosss', reqPerm('canAddDoss'), function (req, res) { // Ajout d'un doss | |
228 | +api.post('/dosss', reqMembre(), function (req, res) { // Ajout d'un doss | |
221 | 229 | // TODO Assertion 404 existe, transformer req.body.id avec la vraie id (ou redirect) |
222 | 230 | DosssServ.getId(req.body.parent, function (parent) { // TODO Async |
223 | 231 | req.body.parent = parent; |
... | ... | @@ -230,7 +238,7 @@ api.post('/dosss', reqPerm('canAddDoss'), function (req, res) { // Ajout d'un do |
230 | 238 | }); |
231 | 239 | }); |
232 | 240 | |
233 | -api.delete('/dosss/:doss_id', reqPerm('canDelDoss'), function (req, res) { // Supression d'un doss | |
241 | +api.delete('/dosss/:doss_id', reqBureau(), function (req, res) { // Supression d'un doss | |
234 | 242 | DosssServ.remove(req.params.doss_id, function (err, doss) { |
235 | 243 | if (err) |
236 | 244 | res.status(500).send(err); |
... | ... | @@ -240,16 +248,27 @@ api.delete('/dosss/:doss_id', reqPerm('canDelDoss'), function (req, res) { // Su |
240 | 248 | }); |
241 | 249 | |
242 | 250 | // Conversations |
243 | -api.get('/convs/:conv_id', function (req, res) { // Une conv | |
244 | - ConvsServ.get(req.params.conv_id, function (err, conv) { | |
245 | - if (err) | |
246 | - res.status(500).send(err); | |
247 | - else | |
248 | - res.json(conv); | |
251 | + | |
252 | +getConv = function (req, res, next) { | |
253 | + ConvModl.findById(req.params.conv_id, function (err, conv) { | |
254 | + if (err) { | |
255 | + res.status(500).json(err); | |
256 | + } else { | |
257 | + if (conv) { | |
258 | + req.conv = conv; | |
259 | + next(); | |
260 | + } else { | |
261 | + res.status(404).end(); | |
262 | + } | |
263 | + } | |
249 | 264 | }); |
265 | +}; | |
266 | + | |
267 | +api.get('/convs/:conv_id', reqAuth(), getConv, function (req, res) { // Une conv | |
268 | + res.json(req.conv); | |
250 | 269 | }); |
251 | 270 | |
252 | -api.post('/convs', reqPerm('canAddConv'), function (req, res) { // Ajout d'un conv | |
271 | +api.post('/convs', reqMembre(), function (req, res) { // Ajout d'un conv | |
253 | 272 | // TODO Assertion 404 existe, transformer req.body.id avec la vraie id (ou redirect) |
254 | 273 | DosssServ.getId(req.body.parent, function (parent) { // TODO Async |
255 | 274 | req.body.parent = parent; |
... | ... | @@ -262,12 +281,12 @@ api.post('/convs', reqPerm('canAddConv'), function (req, res) { // Ajout d'un co |
262 | 281 | }); |
263 | 282 | }); |
264 | 283 | |
265 | -api.delete('/convs/:conv_id', reqPerm('canDelConv'), function (req, res) { // Supression d'un conv | |
266 | - ConvsServ.remove(req.params.conv_id, function (err, conv) { | |
267 | - if (err) | |
284 | +api.delete('/convs/:conv_id', reqBureau(), getConv, function (req, res) { // Supression d'un conv | |
285 | + req.conv.remove(function (err) { | |
286 | + if (err) // TODO Fonction propre | |
268 | 287 | res.status(500).send(err); |
269 | 288 | else |
270 | - res.json(null); | |
289 | + res.status(205).end(); | |
271 | 290 | }); |
272 | 291 | }); |
273 | 292 | |
... | ... | @@ -281,16 +300,16 @@ api.get('/messs/:conv_id', reqAuth(), function (req, res) { // Liste des messs |
281 | 300 | }); |
282 | 301 | }); |
283 | 302 | |
284 | -api.get('/messs/:mess_id', reqAuth(), function (req, res) { // Une mess | |
285 | - MessServ.get(req.params.mess_id, function (err, mess) { | |
286 | - if (err) | |
287 | - res.status(500).send(err); | |
288 | - else | |
289 | - res.json(mess); | |
290 | - }); | |
291 | -}); | |
303 | +// api.get('/messs/:mess_id', reqAuth(), function (req, res) { // Une mess | |
304 | +// MessServ.get(req.params.mess_id, function (err, mess) { | |
305 | +// if (err) | |
306 | +// res.status(500).send(err); | |
307 | +// else | |
308 | +// res.json(mess); | |
309 | +// }); | |
310 | +// }); | |
292 | 311 | |
293 | -api.post('/messs', reqPerm('canAddMess'), function (req, res) { // Ajout d'un mess | |
312 | +api.post('/messs', reqMembre(), function (req, res) { // Ajout d'un mess | |
294 | 313 | data = req.body; |
295 | 314 | data.login = req.session.data.login; |
296 | 315 | MessServ.add(data, function (err, mess) { |
... | ... | @@ -301,27 +320,50 @@ api.post('/messs', reqPerm('canAddMess'), function (req, res) { // Ajout d'un me |
301 | 320 | }); |
302 | 321 | }); |
303 | 322 | |
304 | -api.put('/messs', reqPerm('canEditMess'), function (req, res) { // Édition d'un message | |
305 | - MessServ.edit(req.body, function (err, mess) { | |
306 | - console.log('CALLED', err, mess); | |
323 | +api.put('/messs', reqMembre(), function (req, res, next) { // Édition d'un mess | |
324 | + MessModl.findById(req.body._id, function (err, mess) { // TODO Fonction propre | |
325 | + // TODO Utiliser req.params | |
307 | 326 | if (err) { |
308 | - if (err == 'notfound') { | |
309 | - res.status(404).end(); | |
327 | + res.status(500).json(err); | |
328 | + } else { | |
329 | + if (mess) { | |
330 | + req.mess = mess; | |
331 | + next(); | |
310 | 332 | } else { |
311 | - res.status(500).json(err); | |
333 | + res.status(404).end(); | |
312 | 334 | } |
313 | - } else { | |
314 | - res.status(201).json(mess); | |
315 | 335 | } |
316 | 336 | }); |
337 | +}, reqOwn('mess'), function (req, res) { | |
338 | + req.mess.content = req.body.content; | |
339 | + // TODO Edit date | |
340 | + req.mess.save(function (err, mess) { | |
341 | + if (err) // TODO Fonction propre | |
342 | + res.status(500).send(err); | |
343 | + else | |
344 | + res.json(mess); | |
345 | + }); | |
317 | 346 | }); |
318 | 347 | |
319 | -api.delete('/messs/:mess_id', reqPerm('canDelMess'), function (req, res) { // Supression d'un mess | |
320 | - MessServ.remove(req.params.mess_id, function (err, mess) { | |
321 | - if (err) | |
348 | +api.delete('/messs/:mess_id', reqMembre(), function (req, res, next) { // Supression d'un mess | |
349 | + MessModl.findById(req.params.mess_id, function (err, mess) { // TODO Fonction propre | |
350 | + if (err) { | |
351 | + res.status(500).json(err); | |
352 | + } else { | |
353 | + if (mess) { | |
354 | + req.mess = mess; | |
355 | + next(); | |
356 | + } else { | |
357 | + res.status(404).end(); | |
358 | + } | |
359 | + } | |
360 | + }); | |
361 | +}, reqOwn('mess'), function (req, res) { | |
362 | + req.mess.remove(function (err) { | |
363 | + if (err) // TODO Fonction propre | |
322 | 364 | res.status(500).send(err); |
323 | 365 | else |
324 | - res.json(null); | |
366 | + res.status(205).end(); | |
325 | 367 | }); |
326 | 368 | }); |
327 | 369 | ... | ... |
app/services/MembresServ.js
... | ... | @@ -46,6 +46,18 @@ MembresServ.remove = function (id, cb) { |
46 | 46 | }, cb); |
47 | 47 | }; |
48 | 48 | |
49 | +MembresServ.estMembre = function (login, cb) { | |
50 | + MembreModl.findOne({ | |
51 | + login: login | |
52 | + }, function (err, data) { | |
53 | + if (!err && data) { | |
54 | + cb(true); | |
55 | + } else { | |
56 | + cb(false); | |
57 | + } | |
58 | + }); | |
59 | +}; | |
60 | + | |
49 | 61 | MembresServ.estBureau = function (login, cb) { |
50 | 62 | MembreModl.findOne({ |
51 | 63 | login: login | ... | ... |
public/js/controllers/ForumConvCtrl.js
public/js/controllers/ForumDirCtrl.js
public/js/services/ApiServ.js
... | ... | @@ -13,7 +13,7 @@ angular.module('ApiServ', ['NotifyServ']) |
13 | 13 | }; |
14 | 14 | } |
15 | 15 | |
16 | - if (method == 'get' || method == 'delete') { | |
16 | + if (method == 'get' || method == 'delete') { // TODO url & data en même temps | |
17 | 17 | for (arg = 3; arg < arglen; arg++) { |
18 | 18 | link += '/' + arguments[arg]; |
19 | 19 | } | ... | ... |
public/views/forumConv.html
... | ... | @@ -28,16 +28,16 @@ |
28 | 28 | <td mess-edit></td> |
29 | 29 | <td> |
30 | 30 | <mess-viewsource></mess-viewsource> |
31 | - <button type="button" class="btn btn-primary" ng-show="session.canEditMess" ng-click="editButton($index)"> | |
31 | + <button type="button" class="btn btn-primary" ng-show="mine(mess)" ng-click="editButton($index)"> | |
32 | 32 | <span class="glyphicon glyphicon-edit" aria-hidden="true" ng-hide="mess.editMode"></span> |
33 | 33 | <span class="glyphicon glyphicon-ok" aria-hidden="true" ng-show="mess.editMode"></span> |
34 | 34 | </button> |
35 | - <button type="button" class="btn btn-danger" ng-show="session.canDelMess" ng-click="delMess($index)"> | |
35 | + <button type="button" class="btn btn-danger" ng-show="mine(mess)" ng-click="delMess($index)"> | |
36 | 36 | <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> |
37 | 37 | </button> |
38 | 38 | </td> |
39 | 39 | </tr> |
40 | - <tr ng-show="session.canAddMess"> | |
40 | + <tr ng-show="session.membre"> | |
41 | 41 | <form ng-init="mess = {content: '', editMode: true, viewSource: true}"> |
42 | 42 | <td> |
43 | 43 | <label for="nvMessTitre">Nouveau message</label> | ... | ... |
public/views/forumDir.html
... | ... | @@ -31,16 +31,16 @@ |
31 | 31 | {{ doss.date }} |
32 | 32 | </td> |
33 | 33 | <td> |
34 | - <button type="button" class="btn btn-default" ng-show="session.canAbonner"> | |
34 | + <button type="button" class="btn btn-default" ng-show="session.membre && implemented"> | |
35 | 35 | <span class="glyphicon glyphicon-eye-open" aria-hidden="true" ng-show="doss.abonne"></span> |
36 | 36 | <span class="glyphicon glyphicon-eye-close" aria-hidden="true" ng-hide="doss.abonne"></span> |
37 | 37 | </button> |
38 | - <button type="button" class="btn btn-danger" ng-show="session.canDelDoss" ng-click="delDoss($index)"> | |
38 | + <button type="button" class="btn btn-danger" ng-show="mine(doss)" ng-click="delDoss($index)"> | |
39 | 39 | <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> |
40 | 40 | </button> |
41 | 41 | </td> |
42 | 42 | </tr> |
43 | - <tr ng-show="session.canAddDoss"> | |
43 | + <tr ng-show="session.membre"> | |
44 | 44 | <form> |
45 | 45 | <td class="form-group"> |
46 | 46 | <label for="nvDossTitre">Nouveau dossier</label> |
... | ... | @@ -73,16 +73,16 @@ |
73 | 73 | {{ conv.date }} |
74 | 74 | </td> |
75 | 75 | <td> |
76 | - <button type="button" class="btn btn-default" ng-show="session.canAbonner"> | |
76 | + <button type="button" class="btn btn-default" ng-show="session.membre && implemented"> | |
77 | 77 | <span class="glyphicon glyphicon-eye-open" aria-hidden="true" ng-show="conv.abonne"></span> |
78 | 78 | <span class="glyphicon glyphicon-eye-close" aria-hidden="true" ng-hide="conv.abonne"></span> |
79 | 79 | </button> |
80 | - <button type="button" class="btn btn-danger" ng-show="session.canDelConv" ng-click="delConv($index)"> | |
80 | + <button type="button" class="btn btn-danger" ng-show="mine(conv)" ng-click="delConv($index)"> | |
81 | 81 | <span class="glyphicon glyphicon-remove" aria-hidden="true"></span> |
82 | 82 | </button> |
83 | 83 | </td> |
84 | 84 | </tr> |
85 | - <tr ng-show="session.canAddConv"> | |
85 | + <tr ng-show="session.membre"> | |
86 | 86 | <form> |
87 | 87 | <td class="form-group"> |
88 | 88 | <label for="nvConvTitre">Nouvelle conversation</label> | ... | ... |