Skip to content

Commit

Permalink
fix: refactor getUserPermissions function
Browse files Browse the repository at this point in the history
  • Loading branch information
VikaCep committed Nov 26, 2024
1 parent e7eb75e commit d11f6d4
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 81 deletions.
2 changes: 1 addition & 1 deletion src/components/CheckForm/CheckForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { DataTestIds } from 'test/dataTestIds';

import { Check, CheckFormPageParams, CheckFormValues, CheckType } from 'types';
import { AdHocCheckResponse } from 'datasource/responses.types';
import { getUserPermissions } from 'data/permissions';
import { useCheckTypeOptions } from 'hooks/useCheckTypeOptions';
import { useCanReadLogs } from 'hooks/useDSPermission';
import { useLimits } from 'hooks/useLimits';
import { getUserPermissions } from 'hooks/useUserPermissions';
import { toFormValues } from 'components/CheckEditor/checkFormTransformations';
import { CheckJobName } from 'components/CheckEditor/FormComponents/CheckJobName';
import { ChooseCheckType } from 'components/CheckEditor/FormComponents/ChooseCheckType';
Expand Down
2 changes: 1 addition & 1 deletion src/components/CheckList/AddNewCheckButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React from 'react';
import { Button } from '@grafana/ui';

import { ROUTES } from 'types';
import { getUserPermissions } from 'data/permissions';
import { useNavigation } from 'hooks/useNavigation';
import { getUserPermissions } from 'hooks/useUserPermissions';

export function AddNewCheckButton() {
const navigate = useNavigation();
Expand Down
2 changes: 1 addition & 1 deletion src/components/CheckList/BulkActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Button, ButtonCascader, ConfirmModal, useStyles2 } from '@grafana/ui';
import { css } from '@emotion/css';

import { Check } from 'types';
import { getUserPermissions } from 'data/permissions';
import { useBulkDeleteChecks, useBulkUpdateChecks } from 'data/useChecks';
import { getUserPermissions } from 'hooks/useUserPermissions';
import { BulkEditModal } from 'components/BulkEditModal';

type BulkActionsProps = {
Expand Down
2 changes: 1 addition & 1 deletion src/components/CheckList/CheckListHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { Button, Checkbox, Icon, Select, Tooltip, useStyles2 } from '@grafana/ui
import { css } from '@emotion/css';

import { Check, CheckFiltersType, CheckListViewType, CheckSort } from 'types';
import { getUserPermissions } from 'data/permissions';
import { FilterType } from 'hooks/useCheckFilters';
import { getUserPermissions } from 'hooks/useUserPermissions';
import { CheckFilters } from 'components/CheckFilters';
import { CHECK_LIST_SORT_OPTIONS } from 'components/constants';

Expand Down
2 changes: 1 addition & 1 deletion src/components/CheckListItem/CheckItemActionButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { css } from '@emotion/css';

import { Check, ROUTES } from 'types';
import { getCheckType, getCheckTypeGroup } from 'utils';
import { getUserPermissions } from 'data/permissions';
import { useDeleteCheck } from 'data/useChecks';
import { getUserPermissions } from 'hooks/useUserPermissions';
import { getRoute } from 'components/Routing.utils';

const getStyles = (theme: GrafanaTheme2) => ({
Expand Down
4 changes: 2 additions & 2 deletions src/components/ProgrammaticManagement.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import React from 'react';

import { getUserPermissions } from 'data/permissions';
import { useInitialised } from 'hooks/useInitialised';
import { useUserPermissions } from 'hooks/useUserPermissions';
import { AccessToken } from 'components/AccessToken';
import { TerraformConfig } from 'components/TerraformConfig';

export const ProgrammaticManagement = () => {
const initialised = useInitialised();
const { canWriteTokens } = useUserPermissions();
const { canWriteTokens } = getUserPermissions();

return (
<div>
Expand Down
55 changes: 55 additions & 0 deletions src/data/permissions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { OrgRole } from '@grafana/data';
import { config } from '@grafana/runtime';

import { PluginPermissions } from 'types';

const roleHierarchy: Record<OrgRole, OrgRole[]> = {
[OrgRole.Viewer]: [OrgRole.Viewer, OrgRole.Editor, OrgRole.Admin],
[OrgRole.Editor]: [OrgRole.Editor, OrgRole.Admin],
[OrgRole.Admin]: [OrgRole.Admin],
[OrgRole.None]: [],
};

const hasMinFallbackRole = (fallbackOrgRole: OrgRole) => {
const { orgRole } = config.bootData.user;

if (!orgRole) {
return false;
}

return roleHierarchy[fallbackOrgRole]?.includes(orgRole) || false;
};

const isUserActionAllowed = (permission: PluginPermissions, fallbackOrgRole: OrgRole): boolean => {
const { permissions: userPermissions } = config.bootData.user;

if (config.featureToggles.accessControlOnCall) {
return Boolean(userPermissions?.[permission]);
}

return hasMinFallbackRole(fallbackOrgRole);
};

export const getUserPermissions = () => ({
canReadChecks: isUserActionAllowed('grafana-synthetic-monitoring-app.checks:read', OrgRole.Viewer),
canWriteChecks: isUserActionAllowed('grafana-synthetic-monitoring-app.checks:write', OrgRole.Editor),
canDeleteChecks: isUserActionAllowed('grafana-synthetic-monitoring-app.checks:delete', OrgRole.Editor),

canReadProbes: isUserActionAllowed('grafana-synthetic-monitoring-app.probes:read', OrgRole.Viewer),
canWriteProbes: isUserActionAllowed('grafana-synthetic-monitoring-app.probes:write', OrgRole.Editor),
canDeleteProbes: isUserActionAllowed('grafana-synthetic-monitoring-app.probes:delete', OrgRole.Editor),

canReadAlerts: isUserActionAllowed('grafana-synthetic-monitoring-app.alerts:read', OrgRole.Viewer),
canWriteAlerts: isUserActionAllowed('grafana-synthetic-monitoring-app.alerts:write', OrgRole.Editor),
canDeleteAlerts: isUserActionAllowed('grafana-synthetic-monitoring-app.alerts:delete', OrgRole.Editor),

canReadThresholds: isUserActionAllowed('grafana-synthetic-monitoring-app.thresholds:read', OrgRole.Viewer),
canWriteThresholds: isUserActionAllowed('grafana-synthetic-monitoring-app.thresholds:write', OrgRole.Editor),

canReadTokens: isUserActionAllowed('grafana-synthetic-monitoring-app.access-tokens:read', OrgRole.Admin),
canWriteTokens: isUserActionAllowed('grafana-synthetic-monitoring-app.access-tokens:write', OrgRole.Admin),
canDeleteTokens: isUserActionAllowed('grafana-synthetic-monitoring-app.access-tokens:delete', OrgRole.Admin),

canEnablePlugin: isUserActionAllowed('grafana-synthetic-monitoring-app.plugin:enable', OrgRole.Admin),
canDisablePlugin: isUserActionAllowed('grafana-synthetic-monitoring-app.plugin:disable', OrgRole.Admin),
});
3 changes: 2 additions & 1 deletion src/hooks/useAlertPermissions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getUserPermissions } from 'data/permissions';

import { useDSPermission } from './useDSPermission';
import { getUserPermissions } from './useUserPermissions';

export function useAlertPermissions() {
const { canReadAlerts, canWriteAlerts, canDeleteAlerts } = getUserPermissions();
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/useCanEditProbe.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Probe } from 'types';

import { getUserPermissions } from './useUserPermissions';
import { getUserPermissions } from 'data/permissions';

export function useCanEditProbe(probe?: Probe) {
const { canWriteProbes, canDeleteProbes } = getUserPermissions();
Expand Down
5 changes: 2 additions & 3 deletions src/hooks/usePluginPermissions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { hasGlobalPermission } from 'utils';

import { useUserPermissions } from './useUserPermissions';
import { getUserPermissions } from 'data/permissions';

export function usePluginPermissions() {
const canEditPlugin = hasGlobalPermission(`plugins:write`);
const { canEnablePlugin, canDisablePlugin } = useUserPermissions();
const { canEnablePlugin, canDisablePlugin } = getUserPermissions();

return {
canEnablePlugin: canEditPlugin && canEnablePlugin,
Expand Down
59 changes: 0 additions & 59 deletions src/hooks/useUserPermissions.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/page/AppInitializer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import { css } from '@emotion/css';

import { ROUTES } from 'types';
import { hasGlobalPermission } from 'utils';
import { getUserPermissions } from 'data/permissions';
import { useAppInitializer } from 'hooks/useAppInitializer';
import { useMeta } from 'hooks/useMeta';
import { useUserPermissions } from 'hooks/useUserPermissions';
import { MismatchedDatasourceModal } from 'components/MismatchedDatasourceModal';

import { ContactAdminAlert } from './ContactAdminAlert';
Expand All @@ -20,7 +20,7 @@ interface Props {
export const AppInitializer = ({ redirectTo, buttonText }: PropsWithChildren<Props>) => {
const { jsonData } = useMeta();
const styles = useStyles2(getStyles);
const { canEnablePlugin } = useUserPermissions();
const { canEnablePlugin } = getUserPermissions();

const meetsMinPermissions = hasGlobalPermission(`datasources:read`);
const canInitialize = canEnablePlugin && hasGlobalPermission(`datasources:create`);
Expand Down
2 changes: 1 addition & 1 deletion src/page/CheckRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import React from 'react';
import { Redirect, Route, Switch, useRouteMatch } from 'react-router-dom';

import { CheckType } from 'types';
import { getUserPermissions } from 'data/permissions';
import { CHECK_TYPE_OPTIONS } from 'hooks/useCheckTypeOptions';
import { getUserPermissions } from 'hooks/useUserPermissions';
import { CheckList } from 'components/CheckList';
import { ChooseCheckGroup } from 'components/ChooseCheckGroup';

Expand Down
4 changes: 2 additions & 2 deletions src/page/ProbeRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React from 'react';
import { Route, Switch, useRouteMatch } from 'react-router-dom';

import { useUserPermissions } from 'hooks/useUserPermissions';
import { getUserPermissions } from 'data/permissions';
import { EditProbe } from 'page/EditProbe';
import { NewProbe } from 'page/NewProbe';
import { Probes } from 'page/Probes';

export const ProbeRouter = () => {
const { path } = useRouteMatch();
const { canWriteProbes } = useUserPermissions();
const { canWriteProbes } = getUserPermissions();

return (
<Switch>
Expand Down
4 changes: 2 additions & 2 deletions src/page/Probes/Probes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { css } from '@emotion/css';
import { DataTestIds } from 'test/dataTestIds';

import { ExtendedProbe, ROUTES } from 'types';
import { getUserPermissions } from 'data/permissions';
import { useExtendedProbes } from 'data/useProbes';
import { useUserPermissions } from 'hooks/useUserPermissions';
import { CenteredSpinner } from 'components/CenteredSpinner';
import { DocsLink } from 'components/DocsLink';
import { ProbeList } from 'components/ProbeList';
Expand All @@ -33,7 +33,7 @@ export const Probes = () => {
};

const Actions = () => {
const { canWriteProbes } = useUserPermissions();
const { canWriteProbes } = getUserPermissions();
if (!canWriteProbes) {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/scenes/Common/editButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { LinkButton } from '@grafana/ui';

import { Check, ROUTES } from 'types';
import { getCheckType, getCheckTypeGroup } from 'utils';
import { getUserPermissions } from 'data/permissions';
import { useChecks } from 'data/useChecks';
import { useUserPermissions } from 'hooks/useUserPermissions';
import { getRoute } from 'components/Routing.utils';

interface Props {
Expand All @@ -16,7 +16,7 @@ interface Props {
function EditCheckButton({ job, instance }: Props) {
const { data: checks = [], isLoading } = useChecks();
const url = getUrl(checks, instance.getValue(), job.getValue());
const { canWriteChecks } = useUserPermissions();
const { canWriteChecks } = getUserPermissions();

return (
<LinkButton
Expand Down

0 comments on commit d11f6d4

Please sign in to comment.