Access control: expose permissions to the frontend (#32954)

* Expose user permissions to the frontend

* Do not include empty scope

* Extend ContextSrv with hasPermission() method

* Add access control types

* Fix type error (make permissions optional)

* Fallback if access control disabled

* Move UserPermission to types

* Simplify hasPermission()
This commit is contained in:
Alexander Zobnin
2021-04-16 16:02:16 +03:00
committed by GitHub
parent 6ae73eaa22
commit 8b843eb0a6
8 changed files with 101 additions and 16 deletions
+12
View File
@@ -2,6 +2,7 @@ import config from '../../core/config';
import _ 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;
@@ -17,6 +18,7 @@ export class User {
lightTheme: boolean;
hasEditPermissionInFolders: boolean;
email?: string;
permissions?: UserPermission;
constructor() {
this.id = 0;
@@ -74,6 +76,16 @@ export class ContextSrv {
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');
}
+38
View File
@@ -0,0 +1,38 @@
/**
* UserPermission is a map storing permissions in a form of
* {
* action: { scope: scope }
* }
*/
export type UserPermission = {
[key: string]: { [key: string]: string };
};
export interface AccessControlPermission {
action: AccessControlAction;
scope?: AccessControlScope;
}
// Permission actions
export enum AccessControlAction {
UsersRead = 'users:read',
UsersWrite = 'users:write',
UsersTeamRead = 'users.teams:read',
UsersAuthTokenList = 'users.authtoken:list',
UsersAuthTokenUpdate = 'users.authtoken:update',
UsersPasswordUpdate = 'users.password.update',
UsersDelete = 'users:delete',
UsersCreate = 'users:create',
UsersEnable = 'users:enable',
UsersDisable = 'users:disable',
UsersPermissionsUpdate = 'users.permissions.update',
UsersLogout = 'users:logout',
UsersQuotasList = 'users.quotas:list',
UsersQuotasUpdate = 'users.quotas:update',
}
// Global Scopes
export enum AccessControlScope {
UsersAll = 'users:*',
UsersSelf = 'users:self',
}
+1
View File
@@ -17,6 +17,7 @@ export * from './appEvent';
export * from './angular';
export * from './query';
export * from './preferences';
export * from './accessControl';
import * as CoreEvents from './events';
export { CoreEvents };