Backend api updates

This commit is contained in:
Jamie Curnow 2018-06-20 16:50:51 +10:00
parent 08fe46311d
commit 8942b99372
10 changed files with 95 additions and 52 deletions

4
bin/build-dev Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
sudo /usr/local/bin/docker-compose run --no-deps --rm app npm run-script dev
exit $?

View File

@ -252,34 +252,19 @@ const internalUser = {
* All users * All users
* *
* @param {Access} access * @param {Access} access
* @param {Integer} [start]
* @param {Integer} [limit]
* @param {Array} [sort]
* @param {Array} [expand] * @param {Array} [expand]
* @param {String} [search_query] * @param {String} [search_query]
* @returns {Promise} * @returns {Promise}
*/ */
getAll: (access, start, limit, sort, expand, search_query) => { getAll: (access, expand, search_query) => {
return access.can('users:list') return access.can('users:list')
.then(() => { .then(() => {
let query = userModel let query = userModel
.query() .query()
.where('is_deleted', 0) .where('is_deleted', 0)
.groupBy('id') .groupBy('id')
.limit(limit ? limit : 100) .omit(['is_deleted'])
.omit(['is_deleted']); .orderBy('name', 'ASC');
if (typeof start !== 'undefined' && start !== null) {
query.offset(start);
}
if (typeof sort !== 'undefined' && sort !== null) {
_.map(sort, (item) => {
query.orderBy(item.field, item.dir);
});
} else {
query.orderBy('name', 'DESC');
}
// Query is used for searching // Query is used for searching
if (typeof search_query === 'string') { if (typeof search_query === 'string') {

View File

@ -0,0 +1,7 @@
{
"anyOf": [
{
"$ref": "roles#/definitions/admin"
}
]
}

View File

@ -0,0 +1,7 @@
{
"anyOf": [
{
"$ref": "roles#/definitions/admin"
}
]
}

View File

@ -0,0 +1,7 @@
{
"anyOf": [
{
"$ref": "roles#/definitions/admin"
}
]
}

View File

@ -0,0 +1,23 @@
{
"anyOf": [
{
"$ref": "roles#/definitions/admin"
},
{
"type": "object",
"required": ["data", "scope"],
"properties": {
"data": {
"$ref": "objects#/properties/users"
},
"scope": {
"type": "array",
"contains": {
"type": "string",
"pattern": "^user$"
}
}
}
}
]
}

View File

@ -0,0 +1,26 @@
{
"anyOf": [
{
"$ref": "roles#/definitions/admin"
},
{
"type": "object",
"required": [
"data",
"scope"
],
"properties": {
"data": {
"$ref": "objects#/properties/users"
},
"scope": {
"type": "array",
"contains": {
"type": "string",
"pattern": "^user$"
}
}
}
}
]
}

View File

@ -34,8 +34,6 @@ function validator (schema, payload) {
if (valid && !validate.errors) { if (valid && !validate.errors) {
resolve(_.cloneDeep(payload)); resolve(_.cloneDeep(payload));
} else { } else {
//console.log('Validation failed:', schema, payload);
let message = ajv.errorsText(validate.errors); let message = ajv.errorsText(validate.errors);
reject(new error.InternalValidationError(message)); reject(new error.InternalValidationError(message));
} }

View File

@ -29,14 +29,10 @@ router
* *
* Retrieve all users * Retrieve all users
*/ */
.get(pagination('name', 0, 50, 300), (req, res, next) => { .get((req, res, next) => {
validator({ validator({
additionalProperties: false, additionalProperties: false,
required: ['sort'],
properties: { properties: {
sort: {
$ref: 'definitions#/definitions/sort'
},
expand: { expand: {
$ref: 'definitions#/definitions/expand' $ref: 'definitions#/definitions/expand'
}, },
@ -45,23 +41,13 @@ router
} }
} }
}, { }, {
sort: req.query.sort,
expand: (typeof req.query.expand === 'string' ? req.query.expand.split(',') : null), expand: (typeof req.query.expand === 'string' ? req.query.expand.split(',') : null),
query: (typeof req.query.query === 'string' ? req.query.query : null) query: (typeof req.query.query === 'string' ? req.query.query : null)
}) })
.then((data) => { .then(data => {
return Promise.all([ return internalUser.getAll(res.locals.access, data.expand, data.query);
internalUser.getCount(res.locals.access, data.query),
internalUser.getAll(res.locals.access, req.query.offset, req.query.limit, data.sort, data.expand, data.query)
]);
}) })
.then((data) => { .then(users => {
res.setHeader('X-Dataset-Total', data.shift());
res.setHeader('X-Dataset-Offset', req.query.offset);
res.setHeader('X-Dataset-Limit', req.query.limit);
return data.shift();
})
.then((users) => {
res.status(200) res.status(200)
.send(users); .send(users);
}) })
@ -75,10 +61,10 @@ router
*/ */
.post((req, res, next) => { .post((req, res, next) => {
apiValidator({$ref: 'endpoints/users#/links/1/schema'}, req.body) apiValidator({$ref: 'endpoints/users#/links/1/schema'}, req.body)
.then((payload) => { .then(payload => {
return internalUser.create(res.locals.access, payload); return internalUser.create(res.locals.access, payload);
}) })
.then((result) => { .then(result => {
res.status(201) res.status(201)
.send(result); .send(result);
}) })
@ -119,14 +105,14 @@ router
user_id: req.params.user_id, user_id: req.params.user_id,
expand: (typeof req.query.expand === 'string' ? req.query.expand.split(',') : null) expand: (typeof req.query.expand === 'string' ? req.query.expand.split(',') : null)
}) })
.then((data) => { .then(data => {
return internalUser.get(res.locals.access, { return internalUser.get(res.locals.access, {
id: data.user_id, id: data.user_id,
expand: data.expand, expand: data.expand,
omit: internalUser.getUserOmisionsByAccess(res.locals.access, data.user_id) omit: internalUser.getUserOmisionsByAccess(res.locals.access, data.user_id)
}); });
}) })
.then((user) => { .then(user => {
res.status(200) res.status(200)
.send(user); .send(user);
}) })
@ -140,11 +126,11 @@ router
*/ */
.put((req, res, next) => { .put((req, res, next) => {
apiValidator({$ref: 'endpoints/users#/links/2/schema'}, req.body) apiValidator({$ref: 'endpoints/users#/links/2/schema'}, req.body)
.then((payload) => { .then(payload => {
payload.id = req.params.user_id; payload.id = req.params.user_id;
return internalUser.update(res.locals.access, payload); return internalUser.update(res.locals.access, payload);
}) })
.then((result) => { .then(result => {
res.status(200) res.status(200)
.send(result); .send(result);
}) })
@ -158,7 +144,7 @@ router
*/ */
.delete((req, res, next) => { .delete((req, res, next) => {
internalUser.delete(res.locals.access, {id: req.params.user_id}) internalUser.delete(res.locals.access, {id: req.params.user_id})
.then((result) => { .then(result => {
res.status(200) res.status(200)
.send(result); .send(result);
}) })
@ -216,11 +202,11 @@ router
*/ */
.post((req, res, next) => { .post((req, res, next) => {
apiValidator({$ref: 'endpoints/users#/links/5/schema'}, req.body) apiValidator({$ref: 'endpoints/users#/links/5/schema'}, req.body)
.then((payload) => { .then(payload => {
payload.id = req.params.user_id; payload.id = req.params.user_id;
return internalUser.setServiceSettings(res.locals.access, payload); return internalUser.setServiceSettings(res.locals.access, payload);
}) })
.then((result) => { .then(result => {
res.status(200) res.status(200)
.send(result); .send(result);
}) })

View File

@ -1,7 +1,7 @@
<% var title = 'Nginx Proxy Manager' %> <% var title = 'Nginx Proxy Manager' %>
<%- include partials/header.ejs %> <%- include partials/header.ejs %>
<div id="app"> <div id="app" class="page">
<span class="loader"></span> <span class="loader"></span>
</div> </div>