import { Capabilities , CapabilitiesProvider , CapabilitiesSwitcher } from './types' ; import { CoreContext } from '../core_context' ; import { Logger } from '../logging' ; import { InternalHttpServicePreboot , InternalHttpServiceSetup , KibanaRequest } from '../http' ; import { mergeCapabilities } from './merge_capabilities' ; import { getCapabilitiesResolver , CapabilitiesResolver } from './resolve_capabilities' ; import { registerRoutes } from './routes' ; /**
 * APIs to manage the  Capabilities that will be used by the application.
 *
 * Plugins relying on capabilities to toggle some of their features should register them during the setup phase
 * using the `registerProvider` method.
 *
 * Plugins having the responsibility to restrict capabilities depending on a given context should register
 * their capabilities switcher using the `registerSwitcher` method.
 *
 * Refers to the methods documentation for complete description and examples.
 *
 *  public export interface CapabilitiesSetup { /**
     * Register a  CapabilitiesProvider to be used to provide  Capabilities * when resolving them.
     *
     *  example registerProvider ( provider ) : void ; /**
     * Register a  CapabilitiesSwitcher to be used to change the default state
     * of the  Capabilities entries when resolving them.
     *
     * A capabilities switcher can only change the state of existing capabilities.
     * Capabilities added or removed when invoking the switcher will be ignored.
     *
     *  example registerSwitcher ( switcher ) : void ; } /**
 * Defines a set of additional options for the `resolveCapabilities` method of  CapabilitiesStart .
 *
 *  public export interface ResolveCapabilitiesOptions { /**
     * Indicates if capability switchers are supposed to return a default set of capabilities.
     */ useDefaultCapabilities : boolean ; } /**
 * APIs to access the application  Capabilities .
 *
 *  public export interface CapabilitiesStart { /**
     * Resolve the  Capabilities to be used for given request
      resolveCapabilities ( request , options ? ) : Promise < Capabilities > ; } interface PrebootSetupDeps { http : InternalHttpServicePreboot ; } interface SetupDeps { http : InternalHttpServiceSetup ; } const defaultCapabilities = { navLinks : { } , management : { } , catalogue : { } , } ; internal export class CapabilitiesService { private readonly logger : Logger ; private readonly capabilitiesProviders : CapabilitiesProvider [ ] = [ ] ; private readonly capabilitiesSwitchers : CapabilitiesSwitcher [ ] = [ ] ; private readonly resolveCapabilities : CapabilitiesResolver ; constructor ( core ) { this . logger = core . logger . get ( 'capabilities-service' ) ; this . resolveCapabilities = getCapabilitiesResolver ( ( ) => mergeCapabilities ( defaultCapabilities , ... this . capabilitiesProviders . map ( ( provider ) => provider ( ) ) ) , ( ) => this . capabilitiesSwitchers ) ; } public preboot ( prebootDeps ) { this . logger . debug ( 'Prebooting capabilities service' ) ; prebootDeps . http . registerRoutes ( '' , ( router ) => { registerRoutes ( router , async ( ) => defaultCapabilities ) ; } ) ; } public setup ( setupDeps ) { this . logger . debug ( 'Setting up capabilities service' ) ; registerRoutes ( setupDeps . http . createRouter ( '' ) , this . resolveCapabilities ) ; return { registerProvider : ( provider ) => { this . capabilitiesProviders . push ( provider ) ; } , registerSwitcher : ( switcher ) => { this . capabilitiesSwitchers . push ( switcher ) ; } , } ; } public start ( ) { return { resolveCapabilities : ( request , options ) => this . resolveCapabilities ( request , [ ] , options ?. useDefaultCapabilities ?? false ) , } ; } }