mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 21:52:43 +08:00

* Performance: Standardize lodash imports to use destructured members Changes lodash imports of the form `import x from 'lodash/x'` to `import { x } from 'lodash'` to reduce bundle size. * Remove unnecessary _ import from Graph component * Enforce lodash import style * Fix remaining lodash imports
126 lines
3.2 KiB
TypeScript
126 lines
3.2 KiB
TypeScript
import config from '../../core/config';
|
|
import { extend } from 'lodash';
|
|
import coreModule from 'app/core/core_module';
|
|
import { rangeUtil } from '@grafana/data';
|
|
import { AccessControlAction, AccessControlScope, UserPermission } from 'app/types';
|
|
|
|
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;
|
|
email?: string;
|
|
permissions?: UserPermission;
|
|
|
|
constructor() {
|
|
this.id = 0;
|
|
this.isGrafanaAdmin = false;
|
|
this.isSignedIn = false;
|
|
this.orgRole = '';
|
|
this.orgId = 0;
|
|
this.orgName = '';
|
|
this.login = '';
|
|
this.orgCount = 0;
|
|
this.timezone = '';
|
|
this.helpFlags1 = 0;
|
|
this.lightTheme = false;
|
|
this.hasEditPermissionInFolders = false;
|
|
this.email = undefined;
|
|
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;
|
|
minRefreshInterval: string;
|
|
|
|
constructor() {
|
|
if (!config.bootData) {
|
|
config.bootData = { user: {}, settings: {} };
|
|
}
|
|
|
|
this.user = new User();
|
|
this.isSignedIn = this.user.isSignedIn;
|
|
this.isGrafanaAdmin = this.user.isGrafanaAdmin;
|
|
this.isEditor = this.hasRole('Editor') || this.hasRole('Admin');
|
|
this.hasEditPermissionInFolders = this.user.hasEditPermissionInFolders;
|
|
this.minRefreshInterval = config.minRefreshInterval;
|
|
}
|
|
|
|
/**
|
|
* Indicate the user has been logged out
|
|
*/
|
|
setLoggedOut() {
|
|
this.user.isSignedIn = false;
|
|
this.isSignedIn = false;
|
|
}
|
|
|
|
hasRole(role: string) {
|
|
return this.user.orgRole === role;
|
|
}
|
|
|
|
// Checks whether user has required permission
|
|
hasPermission(action: AccessControlAction, scope?: AccessControlScope): boolean {
|
|
// Fallback if access control disabled
|
|
if (!config.featureToggles['accesscontrol']) {
|
|
return true;
|
|
}
|
|
|
|
return !!(this.user.permissions?.[action] && (scope ? this.user.permissions[action][scope] : true));
|
|
}
|
|
|
|
isGrafanaVisible() {
|
|
return !!(document.visibilityState === undefined || document.visibilityState === 'visible');
|
|
}
|
|
|
|
// checks whether the passed interval is longer than the configured minimum refresh rate
|
|
isAllowedInterval(interval: string) {
|
|
if (!config.minRefreshInterval) {
|
|
return true;
|
|
}
|
|
return rangeUtil.intervalToMs(interval) >= rangeUtil.intervalToMs(config.minRefreshInterval);
|
|
}
|
|
|
|
getValidInterval(interval: string) {
|
|
if (!this.isAllowedInterval(interval)) {
|
|
return config.minRefreshInterval;
|
|
}
|
|
return interval;
|
|
}
|
|
|
|
hasAccessToExplore() {
|
|
return (this.isEditor || config.viewersCanEdit) && config.exploreEnabled;
|
|
}
|
|
}
|
|
|
|
let contextSrv = new ContextSrv();
|
|
export { contextSrv };
|
|
|
|
export const setContextSrv = (override: ContextSrv) => {
|
|
if (process.env.NODE_ENV !== 'test') {
|
|
throw new Error('contextSrv can be only overriden in test environment');
|
|
}
|
|
contextSrv = override;
|
|
};
|
|
|
|
coreModule.factory('contextSrv', () => {
|
|
return contextSrv;
|
|
});
|