import { i18n } from '@kbn/i18n' ; import { hasLicenseExpired } from '../license' ; import { MlCapabilities , getDefaultCapabilities } from '../../../common/types/capabilities' ; import { getCapabilities , getManageMlCapabilities } from './get_capabilities' ; let _capabilities = getDefaultCapabilities ( ) ; export function checkGetManagementMlJobsResolver ( ) { return new Promise < { mlFeatureEnabledInSpace : boolean ; } > ( ( resolve , reject ) => { getManageMlCapabilities ( ) . then ( ( { capabilities , isPlatinumOrTrialLicense , mlFeatureEnabledInSpace } ) => { _capabilities = capabilities ; const isManageML = Object . values ( _capabilities ) . every ( ( p ) => p === true ) ; if ( isManageML === true && isPlatinumOrTrialLicense === true ) { return resolve ( { mlFeatureEnabledInSpace } ) ; } else { return reject ( { capabilities , isPlatinumOrTrialLicense , mlFeatureEnabledInSpace } ) ; } } ) . catch ( ( e ) => { return reject ( ) ; } ) ; } ) ; } export function checkGetJobsCapabilitiesResolver ( redirectToMlAccessDeniedPage ) { return new Promise ( ( resolve , reject ) => { getCapabilities ( ) . then ( async ( { capabilities , isPlatinumOrTrialLicense } ) => { _capabilities = capabilities ; if ( _capabilities . canGetJobs || isPlatinumOrTrialLicense === false ) { return resolve ( _capabilities ) ; } else { await redirectToMlAccessDeniedPage ( ) ; return reject ( ) ; } } ) . catch ( async ( e ) => { await redirectToMlAccessDeniedPage ( ) ; return reject ( ) ; } ) ; } ) ; } export function checkCreateJobsCapabilitiesResolver ( redirectToJobsManagementPage ) { return new Promise ( ( resolve , reject ) => { getCapabilities ( ) . then ( async ( { capabilities , isPlatinumOrTrialLicense } ) => { _capabilities = capabilities ; if ( _capabilities . canCreateJob || isPlatinumOrTrialLicense === false ) { return resolve ( _capabilities ) ; } else { await redirectToJobsManagementPage ( ) ; return reject ( ) ; } } ) . catch ( async ( e ) => { await redirectToJobsManagementPage ( ) ; return reject ( ) ; } ) ; } ) ; } export function checkFindFileStructurePrivilegeResolver ( redirectToMlAccessDeniedPage ) { return new Promise ( ( resolve , reject ) => { getCapabilities ( ) . then ( async ( { capabilities } ) => { _capabilities = capabilities ; if ( _capabilities . canFindFileStructure ) { return resolve ( _capabilities ) ; } else { await redirectToMlAccessDeniedPage ( ) ; return reject ( ) ; } } ) . catch ( async ( e ) => { await redirectToMlAccessDeniedPage ( ) ; return reject ( ) ; } ) ; } ) ; } export function checkPermission ( capability ) { const licenseHasExpired = hasLicenseExpired ( ) ; return _capabilities [ capability ] === true && licenseHasExpired !== true ; } export function createPermissionFailureMessage ( privilegeType ) { let message = '' ; const licenseHasExpired = hasLicenseExpired ( ) ; if ( licenseHasExpired ) { message = i18n . translate ( 'xpack.ml.privilege.licenseHasExpiredTooltip' , { defaultMessage : 'Your license has expired.' , } ) ; } else if ( privilegeType === 'canCreateJob' ) { message = i18n . translate ( 'xpack.ml.privilege.noPermission.createMLJobsTooltip' , { defaultMessage : 'You do not have permission to create Machine Learning jobs.' , } ) ; } else if ( privilegeType === 'canStartStopDatafeed' ) { message = i18n . translate ( 'xpack.ml.privilege.noPermission.startOrStopDatafeedsTooltip' , { defaultMessage : 'You do not have permission to start or stop datafeeds.' , } ) ; } else if ( privilegeType === 'canUpdateJob' ) { message = i18n . translate ( 'xpack.ml.privilege.noPermission.editJobsTooltip' , { defaultMessage : 'You do not have permission to edit jobs.' , } ) ; } else if ( privilegeType === 'canDeleteJob' ) { message = i18n . translate ( 'xpack.ml.privilege.noPermission.deleteJobsTooltip' , { defaultMessage : 'You do not have permission to delete jobs.' , } ) ; } else if ( privilegeType === 'canCreateCalendar' ) { message = i18n . translate ( 'xpack.ml.privilege.noPermission.createCalendarsTooltip' , { defaultMessage : 'You do not have permission to create calendars.' , } ) ; } else if ( privilegeType === 'canDeleteCalendar' ) { message = i18n . translate ( 'xpack.ml.privilege.noPermission.deleteCalendarsTooltip' , { defaultMessage : 'You do not have permission to delete calendars.' , } ) ; } else if ( privilegeType === 'canForecastJob' ) { message = i18n . translate ( 'xpack.ml.privilege.noPermission.runForecastsTooltip' , { defaultMessage : 'You do not have permission to run forecasts.' , } ) ; } return i18n . translate ( 'xpack.ml.privilege.pleaseContactAdministratorTooltip' , { defaultMessage : '{message} Please contact your administrator.' , values : { message , } , } ) ; }