grafana/public/app/core/services/context_srv.ts

67 lines
1.5 KiB
TypeScript
Raw Normal View History

2017-12-20 05:33:33 -06:00
import config from 'app/core/config';
import _ from 'lodash';
import coreModule from 'app/core/core_module';
export class User {
id: number;
isGrafanaAdmin: any;
isSignedIn: any;
orgRole: any;
orgId: number;
orgName: string;
login: string;
orgCount: number;
timezone: string;
helpFlags1: number;
lightTheme: boolean;
hasEditPermissionInFolders: boolean;
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;
sidemenuSmallBreakpoint = false;
hasEditPermissionInFolders: boolean;
constructor() {
2016-04-03 09:12:43 -05:00
if (!config.bootData) {
config.bootData = { user: {}, settings: {} };
2016-04-03 09:12:43 -05:00
}
this.user = new User();
this.isSignedIn = this.user.isSignedIn;
this.isGrafanaAdmin = this.user.isGrafanaAdmin;
2017-12-20 05:33:33 -06:00
this.isEditor = this.hasRole('Editor') || this.hasRole('Admin');
this.hasEditPermissionInFolders = this.user.hasEditPermissionInFolders;
}
hasRole(role: string) {
return this.user.orgRole === role;
}
isGrafanaVisible() {
return !!(document.visibilityState === undefined || document.visibilityState === 'visible');
}
hasAccessToExplore() {
return (this.isEditor || config.viewersCanEdit) && config.exploreEnabled;
}
}
2018-08-29 07:26:50 -05:00
const contextSrv = new ContextSrv();
export { contextSrv };
coreModule.factory('contextSrv', () => {
return contextSrv;
});