mirror of https://github.com/grafana/grafana
Plugins: Expose functions to plugins for checking RBAC permissions (#89047)
* feat(grafana-data): create rbac functions for checking permissions * feat(grafana-runtime): pass current user to runtime * feat(grafana-runtime): expose rbac functions to check permissions against current user * refactor(contextsrv): use functions from grafana/data to check rbac permissions against user * Apply suggestions from code review Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com> * chore(rbac): fix missing types imports * refactor(rbac): make exposed functions return boolean --------- Co-authored-by: Marcus Andersson <marcus.andersson@grafana.com>pull/89795/head
parent
7f4faaa45b
commit
40207c53ae
@ -0,0 +1,19 @@ |
||||
import { CurrentUserDTO, WithAccessControlMetadata } from '../types'; |
||||
|
||||
export interface CurrentUser extends Omit<CurrentUserDTO, 'lightTheme'> {} |
||||
|
||||
export function userHasPermission(action: string, user: CurrentUser): boolean { |
||||
return !!user.permissions?.[action]; |
||||
} |
||||
|
||||
export function userHasPermissionInMetadata(action: string, object: WithAccessControlMetadata): boolean { |
||||
return !!object.accessControl?.[action]; |
||||
} |
||||
|
||||
export function userHasAllPermissions(actions: string[], user: CurrentUser) { |
||||
return actions.every((action) => userHasPermission(action, user)); |
||||
} |
||||
|
||||
export function userHasAnyPermission(actions: string[], user: CurrentUser) { |
||||
return actions.some((action) => userHasPermission(action, user)); |
||||
} |
@ -0,0 +1,29 @@ |
||||
import { CurrentUser } from '@grafana/data'; |
||||
|
||||
let singletonInstance: CurrentUser | null = null; |
||||
|
||||
/** |
||||
* Used during startup by Grafana to set the current user so it is available |
||||
* for rbac checks. |
||||
* |
||||
* @internal |
||||
*/ |
||||
export function setCurrentUser(instance: CurrentUser) { |
||||
if (singletonInstance) { |
||||
throw new Error('User should only be set once, when Grafana is starting.'); |
||||
} |
||||
singletonInstance = instance; |
||||
} |
||||
|
||||
/** |
||||
* Used to retrieve the current user. |
||||
* |
||||
* @internal |
||||
* |
||||
*/ |
||||
export function getCurrentUser(): CurrentUser { |
||||
if (!singletonInstance) { |
||||
throw new Error('User can only be used after Grafana instance has started.'); |
||||
} |
||||
return singletonInstance; |
||||
} |
@ -0,0 +1,18 @@ |
||||
import { |
||||
userHasPermission, |
||||
userHasPermissionInMetadata, |
||||
userHasAllPermissions, |
||||
userHasAnyPermission, |
||||
WithAccessControlMetadata, |
||||
} from '@grafana/data'; |
||||
|
||||
import { getCurrentUser } from '../services/user'; |
||||
|
||||
export const hasPermission = (action: string) => userHasPermission(action, getCurrentUser()); |
||||
|
||||
export const hasPermissionInMetadata = (action: string, object: WithAccessControlMetadata) => |
||||
userHasPermissionInMetadata(action, object); |
||||
|
||||
export const hasAllPermissions = (actions: string[]) => userHasAllPermissions(actions, getCurrentUser()); |
||||
|
||||
export const hasAnyPermission = (actions: string[]) => userHasAnyPermission(actions, getCurrentUser()); |
Loading…
Reference in new issue