|
1 | 1 | 'use strict';
|
2 | 2 |
|
3 |
| -const { BadRequestError, UnprocessableEntityError, InvalidArgumentError } = require('restify-errors'), |
| 3 | +const { BadRequestError, UnprocessableEntityError, InvalidArgumentError, ForbiddenError } = require('restify-errors'), |
4 | 4 | { utils: { parseAndValidateTimestamp }, db: { mongoose }, decoding: { validators: { transformAndValidateCoords } } } = require('@sensebox/opensensemap-api-models'),
|
5 | 5 | moment = require('moment'),
|
6 | 6 | isemail = require('isemail'),
|
7 | 7 | handleModelError = require('./errorHandler'),
|
8 |
| - area = require('@turf/area'); |
9 |
| - |
| 8 | + area = require('@turf/area'), |
| 9 | + config = require('config'); |
10 | 10 |
|
11 | 11 | const decodeBase64Image = function (dataString) {
|
12 | 12 | const matches = dataString.match(/^data:(?:image\/(jpeg|png|gif));base64,(.+)$/m);
|
@@ -489,20 +489,28 @@ const parseAndValidateTimeParamsForFindAllBoxes = function parseAndValidateTimeP
|
489 | 489 | next();
|
490 | 490 | };
|
491 | 491 |
|
492 |
| -const checkBoxIdOwner = function checkBoxIdOwner (req, res, next) { |
493 |
| - try { |
494 |
| - req.user.checkBoxOwner(req._userParams.boxId); |
495 |
| - |
| 492 | +const checkPrivilege = function checkPrivilege (req, res, next) { |
| 493 | + if (req.user && req.user.role === config.get('management_role')) { |
496 | 494 | return next();
|
497 |
| - } catch (err) { |
498 |
| - handleModelError(err, next); |
499 | 495 | }
|
| 496 | + |
| 497 | + if (req._userParams.boxId) { |
| 498 | + try { |
| 499 | + req.user.checkBoxOwner(req._userParams.boxId); |
| 500 | + |
| 501 | + return next(); |
| 502 | + } catch (err) { |
| 503 | + return handleModelError(err, next); |
| 504 | + } |
| 505 | + } |
| 506 | + |
| 507 | + return next(new ForbiddenError('Not signed in or not authorized to access.')); |
500 | 508 | };
|
501 | 509 |
|
502 | 510 | module.exports = {
|
503 | 511 | validateFromToTimeParams,
|
504 | 512 | retrieveParameters,
|
505 | 513 | initUserParams,
|
506 | 514 | parseAndValidateTimeParamsForFindAllBoxes,
|
507 |
| - checkBoxIdOwner |
| 515 | + checkPrivilege |
508 | 516 | };
|
0 commit comments