feat(xo-server/api): split token.delete to token.deleteOwn

So that the behavior is more consistent.
This commit is contained in:
Julien Fontanet
2023-09-01 15:41:03 +02:00
parent df0ed5e794
commit 025e671989
3 changed files with 28 additions and 8 deletions

View File

@@ -27,4 +27,6 @@
<!--packages-start-->
- xo-server minor
<!--packages-end-->

View File

@@ -31,13 +31,37 @@ async function delete_({ pattern, tokens }) {
export { delete_ as delete }
delete_.description = 'delete an existing authentication token'
delete_.description = 'delete matching authentication tokens for all users'
delete_.params = {
tokens: { type: 'array', optional: true, items: { type: 'string' } },
pattern: { type: 'object', optional: true },
}
delete_.permission = 'admin'
// -------------------------------------------------------------------
export async function deleteOwn({ pattern, tokens }) {
await this.deleteAuthenticationTokens({
filter: {
__and: [
{
user_id: this.apiContext.user.id,
},
pattern ?? { id: { __or: tokens } },
],
},
})
}
deleteOwn.description = 'delete matching authentication tokens for the current user'
deleteOwn.params = {
tokens: { type: 'array', optional: true, items: { type: 'string' } },
pattern: { type: 'object', optional: true },
}
// -------------------------------------------------------------------
export async function set({ id, ...props }) {

View File

@@ -210,14 +210,8 @@ export default class {
}
async deleteAuthenticationTokens({ filter }) {
let predicate
const { apiContext } = this._app
if (apiContext !== undefined && apiContext.permission !== 'admin') {
predicate = { user_id: apiContext.user.id }
}
const db = this._tokens
return db.remove((await db.get(predicate)).filter(createPredicate(filter)).map(({ id }) => id))
await db.remove((await db.get()).filter(createPredicate(filter)).map(({ id }) => id))
}
async _getAuthenticationToken(id, properties) {