2020-02-28 21:32:01 +08:00
|
|
|
import config from '../../core/config';
|
2017-12-20 19:33:33 +08:00
|
|
|
import _ from 'lodash';
|
|
|
|
|
import coreModule from 'app/core/core_module';
|
2020-02-28 21:32:01 +08:00
|
|
|
import kbn from '../utils/kbn';
|
2016-04-03 20:27:35 +08:00
|
|
|
|
|
|
|
|
export class User {
|
2019-03-12 20:46:52 +08:00
|
|
|
id: number;
|
2016-04-03 20:27:35 +08:00
|
|
|
isGrafanaAdmin: any;
|
|
|
|
|
isSignedIn: any;
|
|
|
|
|
orgRole: any;
|
2017-08-16 21:37:13 +08:00
|
|
|
orgId: number;
|
2018-09-04 23:24:08 +08:00
|
|
|
orgName: string;
|
2019-12-05 15:30:39 +08:00
|
|
|
login: string;
|
2018-09-04 23:24:08 +08:00
|
|
|
orgCount: number;
|
2016-09-19 17:34:08 +08:00
|
|
|
timezone: string;
|
2016-11-09 17:41:39 +08:00
|
|
|
helpFlags1: number;
|
2017-03-31 23:12:50 +08:00
|
|
|
lightTheme: boolean;
|
2018-04-30 21:38:46 +08:00
|
|
|
hasEditPermissionInFolders: boolean;
|
2016-04-03 20:27:35 +08:00
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
if (config.bootData.user) {
|
|
|
|
|
_.extend(this, config.bootData.user);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class ContextSrv {
|
|
|
|
|
pinned: any;
|
|
|
|
|
version: any;
|
|
|
|
|
user: User;
|
|
|
|
|
isSignedIn: any;
|
|
|
|
|
isGrafanaAdmin: any;
|
|
|
|
|
isEditor: any;
|
2017-12-04 19:37:00 +08:00
|
|
|
sidemenuSmallBreakpoint = false;
|
2018-04-30 21:38:46 +08:00
|
|
|
hasEditPermissionInFolders: boolean;
|
2020-02-28 21:32:01 +08:00
|
|
|
minRefreshInterval: string;
|
2016-04-03 20:27:35 +08:00
|
|
|
|
|
|
|
|
constructor() {
|
2016-04-03 22:12:43 +08:00
|
|
|
if (!config.bootData) {
|
2017-12-19 23:06:54 +08:00
|
|
|
config.bootData = { user: {}, settings: {} };
|
2016-04-03 22:12:43 +08:00
|
|
|
}
|
|
|
|
|
|
2016-04-03 20:27:35 +08:00
|
|
|
this.user = new User();
|
|
|
|
|
this.isSignedIn = this.user.isSignedIn;
|
|
|
|
|
this.isGrafanaAdmin = this.user.isGrafanaAdmin;
|
2017-12-20 19:33:33 +08:00
|
|
|
this.isEditor = this.hasRole('Editor') || this.hasRole('Admin');
|
2018-04-30 21:38:46 +08:00
|
|
|
this.hasEditPermissionInFolders = this.user.hasEditPermissionInFolders;
|
2020-02-28 21:32:01 +08:00
|
|
|
this.minRefreshInterval = config.minRefreshInterval;
|
2016-04-03 20:27:35 +08:00
|
|
|
}
|
|
|
|
|
|
2019-04-28 15:58:12 +08:00
|
|
|
hasRole(role: string) {
|
2016-04-03 20:27:35 +08:00
|
|
|
return this.user.orgRole === role;
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-12 02:46:34 +08:00
|
|
|
isGrafanaVisible() {
|
2017-12-21 15:39:31 +08:00
|
|
|
return !!(document.visibilityState === undefined || document.visibilityState === 'visible');
|
2017-01-12 02:46:34 +08:00
|
|
|
}
|
|
|
|
|
|
2020-02-28 21:32:01 +08:00
|
|
|
// checks whether the passed interval is longer than the configured minimum refresh rate
|
|
|
|
|
isAllowedInterval(interval: string) {
|
|
|
|
|
if (!config.minRefreshInterval) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return kbn.interval_to_ms(interval) >= kbn.interval_to_ms(config.minRefreshInterval);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getValidInterval(interval: string) {
|
|
|
|
|
if (!this.isAllowedInterval(interval)) {
|
|
|
|
|
return config.minRefreshInterval;
|
|
|
|
|
}
|
|
|
|
|
return interval;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-21 17:50:34 +08:00
|
|
|
hasAccessToExplore() {
|
|
|
|
|
return (this.isEditor || config.viewersCanEdit) && config.exploreEnabled;
|
|
|
|
|
}
|
2016-04-03 20:27:35 +08:00
|
|
|
}
|
|
|
|
|
|
2018-08-29 20:26:50 +08:00
|
|
|
const contextSrv = new ContextSrv();
|
2017-12-19 23:06:54 +08:00
|
|
|
export { contextSrv };
|
2016-04-03 20:27:35 +08:00
|
|
|
|
2018-09-04 23:02:32 +08:00
|
|
|
coreModule.factory('contextSrv', () => {
|
2016-04-03 20:27:35 +08:00
|
|
|
return contextSrv;
|
|
|
|
|
});
|