-
Notifications
You must be signed in to change notification settings - Fork 48
/
globalconfig.js
154 lines (143 loc) · 4.16 KB
/
globalconfig.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Copyright (c) 2016, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or
* https://opensource.org/licenses/BSD-3-Clause
*/
/**
* api/v1/controllers/globalconfig.js
*/
'use strict';
const helper = require('../helpers/nouns/globalconfig');
const Profile = require('../helpers/nouns/profiles').model;
const User = require('../helpers/nouns/users').model;
const doDelete = require('../helpers/verbs/doDelete');
const doPatch = require('../helpers/verbs/doPatch');
const doPost = require('../helpers/verbs/doPost');
const doGet = require('../helpers/verbs/doGet');
const doFind = require('../helpers/verbs/doFind');
const apiErrors = require('../apiErrors');
const u = require('../helpers/verbs/utils');
const jwtUtil = require('../../../utils/jwtUtil');
function isAdmin(req) {
return new Promise((resolve, reject) => {
if (req.headers.authorization) {
// use the token
jwtUtil.getUsernameFromToken(req)
.then((username) => User.findOne({ where: { name: username } }))
.then((user) => {
resolve(Profile.isAdmin(user.profileId));
})
.catch((err) => reject(err));
} else if (req.user) {
// try to use the logged-in user
resolve(Profile.isAdmin(req.user.profileId));
} else {
resolve(false);
}
});
} // isAdmin
function forbidden(next) {
const err = new apiErrors.ForbiddenError({
explanation: 'Forbidden.',
});
u.handleError(next, err, helper.modelName);
} // forbidden
module.exports = {
/**
* DELETE /globalconfig/{key}
*
* Deletes the global config item and sends it back in the response. Only
* available to users with Admin profile.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
deleteGlobalConfig(req, res, next) {
isAdmin(req)
.then((ok) => {
if (ok) {
doDelete(req, res, next, helper);
} else {
forbidden(next);
}
})
.catch((err) => {
forbidden(next);
});
},
/**
* GET /globalconfig
*
* Retrieves all global config items and sends them back in the response.
* Available to all users.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
findGlobalConfig(req, res, next) {
doFind(req, res, next, helper);
},
/**
* GET /globalconfig/{key}
*
* Retrieves the global config item from the global config cache and sends it
* back in the response. Available to all users.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
getGlobalConfig(req, res, next) {
doGet(req, res, next, helper);
},
/**
* PATCH /globalconfig/{key}
*
* Updates the global config item and sends it back in the response. Only
* available to users with Admin profile.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
patchGlobalConfig(req, res, next) {
isAdmin(req)
.then((ok) => {
if (ok) {
doPatch(req, res, next, helper);
} else {
forbidden(next);
}
})
.catch((err) => {
forbidden(next);
});
},
/**
* POST /globalconfig
*
* Creates a new global config item and sends it back in the response. Only
* available to users with Admin profile.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
postGlobalConfig(req, res, next) {
isAdmin(req)
.then((ok) => {
if (ok) {
doPost(req, res, next, helper);
} else {
forbidden(next);
}
})
.catch((err) => {
forbidden(next);
});
},
}; // exports