From aba180119362485233d96f6adab4fe5a542066e9 Mon Sep 17 00:00:00 2001 From: Will Browne Date: Mon, 31 May 2021 18:42:36 +0200 Subject: [PATCH] Plugins: Check for Grafana admin instead of Org admin (#35000) * check for grafana admin where necessary * fix --- .../src/components/InstallControls.tsx | 38 +++++++++---------- .../internal/plugin-admin-app/src/helpers.ts | 19 +--------- 2 files changed, 20 insertions(+), 37 deletions(-) diff --git a/plugins-bundled/internal/plugin-admin-app/src/components/InstallControls.tsx b/plugins-bundled/internal/plugin-admin-app/src/components/InstallControls.tsx index 54676297dc8..7bbf150e41d 100644 --- a/plugins-bundled/internal/plugin-admin-app/src/components/InstallControls.tsx +++ b/plugins-bundled/internal/plugin-admin-app/src/components/InstallControls.tsx @@ -4,15 +4,15 @@ import { gt, satisfies } from 'semver'; import { config } from '@grafana/runtime'; import { Button, HorizontalGroup, Icon, LinkButton, useStyles2 } from '@grafana/ui'; -import { AppEvents, GrafanaTheme2, OrgRole } from '@grafana/data'; +import { AppEvents, GrafanaTheme2 } from '@grafana/data'; import { Metadata, Plugin } from '../types'; -import { hasRole } from '../helpers'; import { api } from '../api'; // This isn't exported in the sdk yet // @ts-ignore import appEvents from 'grafana/app/core/app_events'; +import { isGrafanaAdmin } from '../helpers'; interface Props { localPlugin?: Metadata; @@ -76,7 +76,7 @@ export const InstallControls = ({ localPlugin, remotePlugin }: Props) => { const isDevelopmentBuild = Boolean(localPlugin?.dev); const isEnterprise = remotePlugin?.status === 'enterprise'; - const hasPermission = hasRole(OrgRole.Admin); + const hasPermission = isGrafanaAdmin(); if (isEnterprise) { return ( @@ -97,7 +97,7 @@ export const InstallControls = ({ localPlugin, remotePlugin }: Props) => { {shouldUpdate && (isExternallyManaged ? ( - + {'Update via grafana.com'} ) : ( @@ -107,21 +107,17 @@ export const InstallControls = ({ localPlugin, remotePlugin }: Props) => { ))} {isExternallyManaged ? ( - + {'Uninstall via grafana.com'} ) : ( - + <> + + {!hasPermission &&
You need admin privileges to manage this plugin.
} + )} - {!hasPermission &&
You need admin privileges to manage this plugin.
}
); } @@ -138,15 +134,17 @@ export const InstallControls = ({ localPlugin, remotePlugin }: Props) => { return ( {isExternallyManaged ? ( - + {'Install via grafana.com'} ) : ( - + <> + + {!hasPermission &&
You need admin privileges to install this plugin.
} + )} - {!hasPermission &&
You need admin privileges to install this plugin.
}
); }; diff --git a/plugins-bundled/internal/plugin-admin-app/src/helpers.ts b/plugins-bundled/internal/plugin-admin-app/src/helpers.ts index 7a8c0631c0a..f353ab03050 100644 --- a/plugins-bundled/internal/plugin-admin-app/src/helpers.ts +++ b/plugins-bundled/internal/plugin-admin-app/src/helpers.ts @@ -1,20 +1,5 @@ -import { OrgRole } from '@grafana/data'; import { config } from '@grafana/runtime'; -export function hasRole(requiredRole: OrgRole): boolean { - const user = config.bootData.user; - switch (requiredRole) { - case OrgRole.Admin: { - return user.orgRole === OrgRole.Admin; - } - case OrgRole.Editor: { - return user.orgRole === OrgRole.Admin || user.orgRole === OrgRole.Editor; - } - case OrgRole.Viewer: { - return user.orgRole === OrgRole.Admin || user.orgRole === OrgRole.Editor || user.orgRole === OrgRole.Viewer; - } - default: { - return false; - } - } +export function isGrafanaAdmin(): boolean { + return config.bootData.user.isGrafanaAdmin; }