@@ -3,6 +3,11 @@ import express from 'express';
33import restify from 'express-restify-mongoose' ;
44import git from 'git-rev' ;
55import Promise from 'bluebird' ;
6+ import { omit , findIndex } from 'lodash' ;
7+ import getAuthFromRequest from 'lib/helpers/getAuthFromRequest' ;
8+ import getScopesFromRequest from 'lib/services/auth/authInfoSelectors/getScopesFromAuthInfo' ;
9+ import getUserIdFromAuthInfo from 'lib/services/auth/authInfoSelectors/getUserIdFromAuthInfo' ;
10+ import { SITE_ADMIN } from 'lib/constants/scopes' ;
611import { jsonSuccess , serverError } from 'api/utils/responses' ;
712import passport from 'api/auth/passport' ;
813import {
@@ -193,13 +198,40 @@ router.get(
193198 * REST APIS
194199 */
195200restify . defaults ( RESTIFY_DEFAULTS ) ;
196- restify . serve ( router , Organisation ) ;
201+ restify . serve ( router , Organisation , {
202+ preUpdate : ( req , res , next ) => {
203+ const authInfo = getAuthFromRequest ( req ) ;
204+ const scopes = getScopesFromRequest ( authInfo ) ;
205+ if (
206+ findIndex ( scopes , item => item === SITE_ADMIN ) < 0
207+ ) {
208+ req . body = omit ( req . body , 'expiration' ) ;
209+ }
210+ next ( ) ;
211+ }
212+ } ) ;
197213restify . serve ( router , Stream ) ;
198214restify . serve ( router , Export ) ;
199215restify . serve ( router , Download ) ;
200216restify . serve ( router , Query ) ;
201217restify . serve ( router , ImportCsv ) ;
202- restify . serve ( router , User ) ;
218+ restify . serve ( router , User , {
219+ preUpdate : ( req , res , next ) => {
220+ const authInfo = getAuthFromRequest ( req ) ;
221+ const scopes = getScopesFromRequest ( authInfo ) ;
222+
223+ if ( findIndex ( scopes , item => item === SITE_ADMIN ) < 0 ) {
224+ // remove scope changes
225+ req . body = omit ( req . body , 'scopes' ) ;
226+ if ( req . body . _id !== getUserIdFromAuthInfo ( authInfo ) . toString ( ) ) {
227+ // Don't allow changing of passwords
228+ req . body = omit ( req . body , 'password' ) ;
229+ }
230+ }
231+
232+ next ( ) ;
233+ }
234+ } ) ;
203235restify . serve ( router , Client ) ;
204236restify . serve ( router , Visualisation ) ;
205237restify . serve ( router , Dashboard ) ;
0 commit comments