chore(xo-server/api): uniformize session → connection

This commit is contained in:
Julien Fontanet
2022-03-15 14:58:36 +01:00
parent bc987b2dda
commit e69944eaaf
10 changed files with 31 additions and 31 deletions

View File

@@ -9,7 +9,7 @@ get.description = 'get existing ACLs'
// -------------------------------------------------------------------
export async function getCurrentPermissions() {
return /* await */ this.getPermissionsForUser(this.session.get('user_id'))
return /* await */ this.getPermissionsForUser(this.connection.get('user_id'))
}
getCurrentPermissions.description = 'get (explicit) permissions by object for the current user'

View File

@@ -26,7 +26,7 @@ get.params = {
export async function create({ job }) {
if (!job.userId) {
job.userId = this.session.get('user_id')
job.userId = this.connection.get('user_id')
}
return (await this.createJob(job)).id

View File

@@ -24,7 +24,7 @@ export function create({ cron, enabled, jobId, name, timezone }) {
jobId,
name,
timezone,
userId: this.session.get('user_id'),
userId: this.connection.get('user_id'),
})
}

View File

@@ -5,18 +5,18 @@ import { getUserPublicProperties } from '../utils.mjs'
// ===================================================================
export async function signIn(credentials) {
const { session } = this
const { connection } = this
const { user, expiration } = await this.authenticateUser(credentials, {
ip: session.get('user_ip', undefined),
ip: connection.get('user_ip', undefined),
})
session.set('user_id', user.id)
connection.set('user_id', user.id)
if (expiration === undefined) {
session.unset('expiration')
connection.unset('expiration')
} else {
session.set('expiration', expiration)
connection.set('expiration', expiration)
}
return getUserPublicProperties(user)
@@ -47,7 +47,7 @@ signInWithToken.permission = null // user does not need to be authenticated
// -------------------------------------------------------------------
export function signOut() {
this.session.unset('user_id')
this.connection.unset('user_id')
}
signOut.description = 'sign out the user from the current session'
@@ -55,7 +55,7 @@ signOut.description = 'sign out the user from the current session'
// -------------------------------------------------------------------
export async function getUser() {
const userId = this.session.get('user_id')
const userId = this.connection.get('user_id')
return userId === undefined ? null : getUserPublicProperties(await this.getUser(userId))
}

View File

@@ -4,7 +4,7 @@ export async function create({ expiresIn }) {
return (
await this.createAuthenticationToken({
expiresIn,
userId: this.session.get('user_id'),
userId: this.connection.get('user_id'),
})
).id
}
@@ -40,7 +40,7 @@ delete_.params = {
export async function deleteAll({ except }) {
await this.deleteAuthenticationTokens({
filter: {
user_id: this.session.get('user_id'),
user_id: this.connection.get('user_id'),
id: {
__not: except,
},

View File

@@ -22,7 +22,7 @@ create.params = {
// Deletes an existing user.
async function delete_({ id }) {
if (id === this.session.get('user_id')) {
if (id === this.connection.get('user_id')) {
throw invalidParameters('a user cannot delete itself')
}
@@ -61,7 +61,7 @@ getAll.permission = 'admin'
export async function set({ id, email, password, permission, preferences }) {
const isAdmin = this.user && this.user.permission === 'admin'
if (isAdmin) {
if (permission && id === this.session.get('user_id')) {
if (permission && id === this.connection.get('user_id')) {
throw invalidParameters('a user cannot change its own permission')
}
} else if (email || password || permission) {

View File

@@ -34,7 +34,7 @@ function checkPermissionOnSrs(vm, permission = 'operate') {
return permissions.push([this.getObject(vdiId, ['VDI', 'VDI-snapshot']).$SR, permission])
})
return this.checkPermissions(this.session.get('user_id'), permissions)
return this.checkPermissions(this.connection.get('user_id'), permissions)
}
// ===================================================================
@@ -1122,7 +1122,7 @@ async function import_({ data, sr, type = 'xva', url }) {
return {
$sendTo: await this.registerApiHttpRequest(
'vm.import',
this.session,
this.connection,
handleVmImport,
{ data, srId, type, xapi },
{ exposeAllErrors: true }

View File

@@ -225,7 +225,7 @@ export default class Api {
return remove
}
async callApiMethod(session, name, params = {}) {
async callApiMethod(connection, name, params = {}) {
const app = this._app
const startTime = Date.now()
@@ -241,8 +241,8 @@ export default class Api {
// Used by system.*().
value: this,
},
session: {
value: session,
connection: {
value: connection,
},
}
@@ -259,7 +259,7 @@ export default class Api {
})()
// Fetch and inject the current user.
const userId = session.get('user_id', undefined)
const userId = connection.get('user_id', undefined)
context.user = userId && (await app.getUser(userId))
const userName = context.user ? context.user.email : '(unknown user)'
@@ -267,7 +267,7 @@ export default class Api {
callId: Math.random().toString(36).slice(2),
userId,
userName,
userIp: session.get('user_ip', undefined),
userIp: connection.get('user_ip', undefined),
method: name,
params: sensitiveValues.replace(params, '* obfuscated *'),
timestamp: Date.now(),
@@ -323,7 +323,7 @@ export default class Api {
// it's a special case in which the user is defined at the end of the call
if (data.method === 'session.signIn') {
const { id, email } = await app.getUser(session.get('user_id'))
const { id, email } = await app.getUser(connection.get('user_id'))
data.userId = id
data.userName = email
}
@@ -405,7 +405,7 @@ export default class Api {
return connection
}
registerApiHttpRequest(method, session, fn, data, { exposeAllErrors = false, ...opts } = {}) {
registerApiHttpRequest(method, connection, fn, data, { exposeAllErrors = false, ...opts } = {}) {
const app = this._app
const logger = this._logger
return app.registerHttpRequest(
@@ -414,13 +414,13 @@ export default class Api {
try {
return await fn.apply(this, arguments)
} catch (error) {
const userId = session.get('user_id', undefined)
const userId = connection.get('user_id', undefined)
const user = userId && (await app.getUser(userId))
logger.error(`handleVmImport =!> ${error}`, {
callId: Math.random().toString(36).slice(2),
// userId,
userName: user?.email ?? '(unknown user)',
userIp: session.get('user_ip', undefined),
userIp: connection.get('user_ip', undefined),
method: `HTTP handler of ${method}`,
timestamp,
duration: Date.now() - timestamp,

View File

@@ -47,7 +47,7 @@ export function resolveParamsVector(paramsVector) {
// ===================================================================
export default async function executeJobCall({ app, job, logger, runJobId, schedule, session }) {
export default async function executeJobCall({ app, job, logger, runJobId, schedule, connection }) {
const { paramsVector } = job
const paramsFlatVector = paramsVector ? resolveParamsVector.call(app, paramsVector) : [{}] // One call with no parameters
@@ -71,7 +71,7 @@ export default async function executeJobCall({ app, job, logger, runJobId, sched
params,
start: Date.now(),
})
let promise = app.callApiMethod(session, job.method, Object.assign({}, params))
let promise = app.callApiMethod(connection, job.method, Object.assign({}, params))
if (job.timeout) {
promise = promise::timeout(job.timeout)
}

View File

@@ -280,9 +280,9 @@ export default class Jobs {
})(executor)
}
const session = app.createUserConnection()
$defer.call(session, 'close')
session.set('user_id', job.userId)
const connection = app.createUserConnection()
$defer.call(connection, 'close')
connection.set('user_id', job.userId)
const { cancel, token } = CancelToken.source()
@@ -293,12 +293,12 @@ export default class Jobs {
const status = await executor({
app,
cancelToken: token,
connection,
data: data_,
job,
logger,
runJobId,
schedule,
session,
})
await logger.notice(