"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.RouteGuard = void 0; var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _mlRouteUtils = require("@kbn/ml-route-utils"); var _app = require("../../common/constants/app"); var _saved_objects = require("../saved_objects"); var _ml_client = require("./ml_client"); var _data_views_utils = require("./data_views_utils"); /* * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one * or more contributor license agreements. Licensed under the Elastic License * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ class RouteGuard { constructor(mlLicense, getSavedObject, getInternalSavedObject, spacesPlugin, authorization, isMlReady, getDataViews, getStartServices) { (0, _defineProperty2.default)(this, "_mlLicense", void 0); (0, _defineProperty2.default)(this, "_getMlSavedObjectClient", void 0); (0, _defineProperty2.default)(this, "_getInternalSavedObjectClient", void 0); (0, _defineProperty2.default)(this, "_spacesPlugin", void 0); (0, _defineProperty2.default)(this, "_authorization", void 0); (0, _defineProperty2.default)(this, "_isMlReady", void 0); (0, _defineProperty2.default)(this, "_getDataViews", void 0); (0, _defineProperty2.default)(this, "_getStartServices", void 0); this._mlLicense = mlLicense; this._getMlSavedObjectClient = getSavedObject; this._getInternalSavedObjectClient = getInternalSavedObject; this._spacesPlugin = spacesPlugin; this._authorization = authorization; this._isMlReady = isMlReady; this._getDataViews = getDataViews; this._getStartServices = getStartServices; } fullLicenseAPIGuard(handler) { return this._guard(() => this._mlLicense.isFullLicense(), handler); } basicLicenseAPIGuard(handler) { return this._guard(() => this._mlLicense.isMinimumLicense(), handler); } _guard(check, handler) { return async (context, request, response) => { if (check() === false) { return response.forbidden(); } const { elasticsearch: { client }, savedObjects: { client: savedObjectClient } } = await context.core; const mlSavedObjectClient = this._getMlSavedObjectClient(request); const internalSavedObjectsClient = this._getInternalSavedObjectClient(); if (mlSavedObjectClient === null || internalSavedObjectsClient === null) { return response.badRequest({ body: { message: 'saved object client has not been initialized' } }); } const mlSavedObjectService = (0, _saved_objects.mlSavedObjectServiceFactory)(mlSavedObjectClient, internalSavedObjectsClient, this._spacesPlugin !== undefined, this._authorization, client, this._isMlReady); const [coreStart] = await this._getStartServices(); const executionContext = (0, _mlRouteUtils.createExecutionContext)(coreStart, _app.PLUGIN_ID, request.route.path); return await coreStart.executionContext.withContext(executionContext, () => handler({ client, request, response, context, mlSavedObjectService, mlClient: (0, _ml_client.getMlClient)(client, mlSavedObjectService), getDataViewsService: (0, _data_views_utils.getDataViewsServiceFactory)(this._getDataViews, savedObjectClient, client, request) })); }; } } exports.RouteGuard = RouteGuard;