feat(xo-server/rest-api): add users collection

This commit is contained in:
Julien Fontanet
2023-11-07 12:51:12 +01:00
parent 0d00c1c45f
commit 8f2cfebda6
2 changed files with 31 additions and 0 deletions
+4
View File
@@ -7,6 +7,8 @@
> Users must be able to say: “Nice enhancement, I'm eager to test it” > Users must be able to say: “Nice enhancement, I'm eager to test it”
- [REST API] Add `users` collection
### Bug fixes ### Bug fixes
> Users must be able to say: “I had this issue, happy to know it's fixed” > Users must be able to say: “I had this issue, happy to know it's fixed”
@@ -27,4 +29,6 @@
<!--packages-start--> <!--packages-start-->
- xo-server minor
<!--packages-end--> <!--packages-end-->
@@ -10,6 +10,8 @@ import pick from 'lodash/pick.js'
import * as CM from 'complex-matcher' import * as CM from 'complex-matcher'
import { VDI_FORMAT_RAW, VDI_FORMAT_VHD } from '@xen-orchestra/xapi' import { VDI_FORMAT_RAW, VDI_FORMAT_VHD } from '@xen-orchestra/xapi'
import { getUserPublicProperties } from '../utils.mjs'
const { join } = path.posix const { join } = path.posix
const noop = Function.prototype const noop = Function.prototype
@@ -176,6 +178,7 @@ export default class RestApi {
collections.backups = { id: 'backups' } collections.backups = { id: 'backups' }
collections.restore = { id: 'restore' } collections.restore = { id: 'restore' }
collections.tasks = { id: 'tasks' } collections.tasks = { id: 'tasks' }
collections.users = { id: 'users' }
collections.hosts.routes = { collections.hosts.routes = {
__proto__: null, __proto__: null,
@@ -389,6 +392,30 @@ export default class RestApi {
}, true) }, true)
) )
api
.get(
'/users',
wrap(async (req, res) => {
let users = await app.getAllUsers()
const { filter, limit } = req.query
if (filter !== undefined) {
users = users.filter(CM.parse(filter).createPredicate())
}
if (limit < users.length) {
users.length = limit
}
sendObjects(users.map(getUserPublicProperties), req, res)
})
)
.get(
'/users/:id',
wrap(async (req, res) => {
res.json(getUserPublicProperties(await app.getUser(req.params.id)))
})
)
api.get( api.get(
'/:collection', '/:collection',
wrap(async (req, res) => { wrap(async (req, res) => {