-
Notifications
You must be signed in to change notification settings - Fork 48
/
ssoconfig.js
227 lines (210 loc) · 6.84 KB
/
ssoconfig.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* 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/ssoconfig.js
*/
'use strict'; // eslint-disable-line strict
const helper = require('../helpers/nouns/ssoconfig');
const httpStatus = require('../constants').httpStatus;
const u = require('../helpers/verbs/utils');
const apiErrors = require('../apiErrors');
/**
* Builds the API links to send back in the response.
*
* @param {String} key - The record id or name (or subject.absolutePath)
* @param {Object} props - The helpers/nouns module for the given DB model
* @param {String} method - The method name
* @returns {Object} the API links to send back in the response
*/
function getApiLinks(key, props, method) {
// If this was a DELETE method, only include a POST link...
if (/delete/i.test(method)) {
return [
{
href: props.baseUrl,
method: 'POST',
rel: props.apiLinks.POST,
},
];
}
// Otherwise include all the methods specified for this resource
return Object.keys(props.apiLinks).map((i) => ({
href: props.baseUrl,
method: i,
rel: props.apiLinks[i],
}));
} // getApiLinks
/**
* Prepares the object to be sent back in the response ("cleans" the object,
* strips out nulls, adds API links).
*
* @param {Instance|Array} rec - The record or records to return in the
* response
* @param {Object} props - The helpers/nouns module for the given DB model
* @param {String} method - The request method, used to help build the API
* links
* @returns {Object} the "responsified" cleaned up object to send back in
* the response
*/
function responsify(rec, props, method) {
const o = u.cleanAndStripNulls(rec);
o.apiLinks = getApiLinks(o.id, props, method);
return o;
}// responsify
module.exports = {
/**
* DELETE /ssoconfig
*
* Deletes the ssoconfig and sends it back in the response.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
deleteSSOConfig(req, res, next) {
helper.model.findOne()
.then((o) => {
if (o) {
o.destroy()
.then((destroyedObj) => {
res.status(httpStatus.OK)
.json(responsify(destroyedObj, helper, req.method));
});
} else {
const err = new apiErrors.ResourceNotFoundError();
err.info = 'There is no sso config to delete.';
u.handleError(next, err, helper.modelName);
}
})
.catch((err) => u.handleError(next, err, helper.modelName));
},
/**
* GET /ssoconfig
*
* Retrieves the ssoconfig and sends it back in the response.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
getSSOConfig(req, res, next) {
helper.model.findOne()
.then((o) => {
if (o) {
res.status(httpStatus.OK).json(responsify(o, helper, req.method));
} else {
const err = new apiErrors.ResourceNotFoundError({
explanation: 'There is no sso config.',
});
u.handleError(next, err, helper.modelName);
}
})
.catch((err) => {
u.handleError(next, err, helper.modelName);
});
},
/**
* PATCH /ssoconfig
*
* Updates the ssoconfig and sends it back in the response. PATCH will only
* update the attributes of the user provided in the body of the request.
* Other attributes will not be updated.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
patchSSOConfig(req, res, next) {
helper.model.findOne()
.then((o) => {
if (o) {
const requestBody = req.swagger.params.queryBody.value;
// if patching with value,
// force db to update value, regardless of whether it changed
if (Object.keys(requestBody).indexOf('value') >= 0) {
o.changed('value', true);
}
o.update(requestBody)
.then((updatedObj) => {
res.status(httpStatus.OK)
.json(responsify(updatedObj, helper, req.method));
});
} else {
const err = new apiErrors.ResourceNotFoundError({
explanation: 'There is no sso config.',
});
u.handleError(next, err, helper.modelName);
}
})
.catch((err) => u.handleError(next, err, helper.modelName));
},
/**
* POST /ssoconfig
*
* Creates a new ssoconfig and sends it back in the response.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
postSSOConfig(req, res, next) {
const toPost = req.swagger.params.queryBody.value;
const assocToCreate = u.includeAssocToCreate(toPost, helper);
helper.model.create(toPost, assocToCreate)
.then((o) =>
res.status(httpStatus.CREATED).json(responsify(o, helper, req.method))
)
.catch((err) => u.handleError(next, err, helper.modelName));
},
/**
* PUT /ssoconfig
*
* Updates ssoconfig and sends it back in the response. If any attributes are
* missing from the body of the request, those attributes are cleared.
*
* @param {IncomingMessage} req - The request object
* @param {ServerResponse} res - The response object
* @param {Function} next - The next middleware function in the stack
*/
putSSOConfig(req, res, next) {
const toPut = req.swagger.params.queryBody.value;
const puttableFields =
req.swagger.params.queryBody.schema.schema.properties;
helper.model.findOne()
.then((o) => {
if (o) {
const keys = Object.keys(puttableFields);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
if (toPut[key] === undefined) {
let nullish = null;
if (puttableFields[key].type === 'boolean') {
nullish = false;
} else if (puttableFields[key].enum) {
nullish = puttableFields[key].default;
}
o.set(key, nullish);
} else {
o.set(key, toPut[key]);
}
}
o.save()
.then((savedObj) => {
res.status(httpStatus.OK)
.json(responsify(savedObj, helper, req.method));
});
} else {
const err = new apiErrors.ResourceNotFoundError({
explanation: 'There is no sso config.',
});
u.handleError(next, err, helper.modelName);
}
})
.catch((err) => u.handleError(next, err, helper.modelName));
},
}; // exports