diff --git a/src/api/session.js b/src/api/session.js index e5ff93359..c58fbbbbe 100644 --- a/src/api/session.js +++ b/src/api/session.js @@ -1,4 +1,4 @@ -import {deprecate} from 'util' +import { deprecate, getUserPublicProperties } from 'util' import {InvalidCredential, AlreadyAuthenticated} from '../api-errors' @@ -15,7 +15,7 @@ export async function signIn (credentials) { } this.session.set('user_id', user.id) - return this.getUserPublicProperties(user) + return getUserPublicProperties(user) } signIn.description = 'sign in' @@ -55,7 +55,7 @@ export async function getUser () { return userId === undefined ? null - : this.getUserPublicProperties(await this.getUser(userId)) + : getUserPublicProperties(await this.getUser(userId)) } getUser.description = 'return the currently connected user' diff --git a/src/api/user.js b/src/api/user.js index 3f70b8ccc..541724f97 100644 --- a/src/api/user.js +++ b/src/api/user.js @@ -1,5 +1,5 @@ import {InvalidParameters} from '../api-errors' -import { mapToArray } from '../utils' +import { getUserPublicProperties, mapToArray } from '../utils' // =================================================================== @@ -48,7 +48,7 @@ export async function getAll () { const users = await this.getAllUsers() // Filters out private properties. - return mapToArray(users, this.getUserPublicProperties) + return mapToArray(users, getUserPublicProperties) } getAll.description = 'returns all the existing users' diff --git a/src/api/vm.coffee b/src/api/vm.coffee index db98c4ae7..1f90a963a 100644 --- a/src/api/vm.coffee +++ b/src/api/vm.coffee @@ -819,8 +819,7 @@ stop = $coroutine ({vm, force}) -> yield xapi.call 'VM.clean_shutdown', vm._xapiRef catch error if error.code is 'VM_MISSING_PV_DRIVERS' or error.code is 'VM_LACKS_FEATURE_SHUTDOWN' - # TODO: Improve reporting: this message is unclear. - @throw 'INVALID_PARAMS' + throw new InvalidParameters('clean shutdown requires PV drivers') else throw error @@ -1116,10 +1115,7 @@ setBootOrder = $coroutine ({vm, order}) -> yield xapi.call 'VM.set_HVM_boot_params', vm._xapiRef, order return true - @throw( - 'INVALID_PARAMS' - 'You can only set the boot order on a HVM guest' - ) + throw new InvalidParameters('You can only set the boot order on a HVM guest') setBootOrder.params = { vm: { type: 'string' }, diff --git a/src/index.js b/src/index.js index 9bd79b1a6..c6a11ac05 100644 --- a/src/index.js +++ b/src/index.js @@ -9,7 +9,6 @@ import eventToPromise from 'event-to-promise' import has from 'lodash/has' import helmet from 'helmet' import includes from 'lodash/includes' -import pick from 'lodash/pick' import proxyConsole from './proxy-console' import serveStatic from 'serve-static' import startsWith from 'lodash/startsWith' @@ -18,14 +17,8 @@ import { compile as compilePug } from 'pug' import { createServer as createProxyServer } from 'http-proxy' import { join as joinPath } from 'path' -import { - AlreadyAuthenticated, - InvalidCredential, - InvalidParameters, - NoSuchObject, - NotImplemented -} from './api-errors' import JsonRpcPeer from 'json-rpc-peer' +import { InvalidCredential } from './api-errors' import { readFile, readdir @@ -407,27 +400,6 @@ const setUpStaticFiles = (express, opts) => { // =================================================================== -const errorClasses = { - ALREADY_AUTHENTICATED: AlreadyAuthenticated, - INVALID_CREDENTIAL: InvalidCredential, - INVALID_PARAMS: InvalidParameters, - NO_SUCH_OBJECT: NoSuchObject, - NOT_IMPLEMENTED: NotImplemented -} - -const apiHelpers = { - getUserPublicProperties (user) { - // Handles both properties and wrapped models. - const properties = user.properties || user - - return pick(properties, 'id', 'email', 'groups', 'permission', 'preferences', 'provider') - }, - - throw (errorId, data) { - throw new (errorClasses[errorId])(data) - } -} - const setUpApi = (webServer, xo, verboseLogsOnErrors) => { const webSocketServer = new WebSocket.Server({ server: webServer, @@ -437,7 +409,7 @@ const setUpApi = (webServer, xo, verboseLogsOnErrors) => { // FIXME: it can cause issues if there any property assignments in // XO methods called from the API. - const context = { __proto__: xo, ...apiHelpers } + const context = { __proto__: xo } const api = new Api({ context, diff --git a/src/utils.js b/src/utils.js index 0e7102404..d015158f8 100644 --- a/src/utils.js +++ b/src/utils.js @@ -11,6 +11,7 @@ import isString from 'lodash/isString' import keys from 'lodash/keys' import kindOf from 'kindof' import multiKeyHashInt from 'multikey-hash' +import pick from 'lodash/pick' import xml2js from 'xml2js' // Moment timezone can be loaded only one time, it's a workaround to load @@ -177,6 +178,13 @@ export function extractProperty (obj, prop) { // ------------------------------------------------------------------- +export const getUserPublicProperties = user => pick( + user.properties || user, + 'id', 'email', 'groups', 'permission', 'preferences', 'provider' +) + +// ------------------------------------------------------------------- + export const getPseudoRandomBytes = n => { const bytes = new Buffer(n)