From 5e14d93530fb8fd55f2bde26195f1bec5dc71868 Mon Sep 17 00:00:00 2001 From: Ieva Date: Tue, 10 Oct 2023 12:04:07 +0100 Subject: [PATCH] Chore: remove hasAccess and hasAcessInMetadata (#76237) * remove hasAccess and hasAcessInMetadata in favour of hasPermission and hasPermissionInMetadata * test fixes * more test fixes --- public/app/core/services/context_srv.ts | 8 -------- public/app/features/admin/ServerStats.test.tsx | 1 - .../contact-points/ContactPoints.v2.tsx | 2 +- .../unified/hooks/useIsRuleEditable.ts | 5 ++--- .../EmailSharingConfiguration.tsx | 3 +-- public/app/features/explore/Explore.test.tsx | 2 +- .../explore/spec/datasourceState.test.tsx | 1 - .../explore/spec/interpolation.test.tsx | 2 +- .../explore/spec/queryHistory.test.tsx | 1 - .../app/features/explore/spec/split.test.tsx | 1 - .../GetStartedWithPlugin/GetStartedWithApp.tsx | 3 +-- .../admin/hooks/usePluginDetailsTabs.tsx | 6 ++---- .../plugins/admin/pages/PluginDetails.test.tsx | 3 +-- .../components/ManageDashboards.test.tsx | 4 ---- .../search/components/ManageDashboardsNew.tsx | 3 +-- .../ServiceAccountPage.test.tsx | 3 +-- .../serviceaccounts/ServiceAccountPage.tsx | 5 ++--- public/app/features/teams/TeamList.tsx | 5 ++--- .../app/features/users/UsersActionBar.test.tsx | 18 ++++-------------- public/app/features/users/UsersActionBar.tsx | 4 +--- .../app/features/users/UsersListPage.test.tsx | 2 +- public/app/features/users/state/reducers.ts | 1 - public/app/types/user.ts | 1 - 23 files changed, 22 insertions(+), 62 deletions(-) diff --git a/public/app/core/services/context_srv.ts b/public/app/core/services/context_srv.ts index f76ca6b5c00..6b49da8f97e 100644 --- a/public/app/core/services/context_srv.ts +++ b/public/app/core/services/context_srv.ts @@ -167,14 +167,6 @@ export class ContextSrv { return this.hasPermission(AccessControlAction.DataSourcesExplore) && config.exploreEnabled; } - hasAccess(action: string, fallBack: boolean): boolean { - return this.hasPermission(action); - } - - hasAccessInMetadata(action: string, object: WithAccessControlMetadata, fallBack: boolean): boolean { - return this.hasPermissionInMetadata(action, object); - } - // evaluates access control permissions, granting access if the user has any of them evaluatePermission(actions: string[]) { if (actions.some((action) => this.hasPermission(action))) { diff --git a/public/app/features/admin/ServerStats.test.tsx b/public/app/features/admin/ServerStats.test.tsx index a6d586c4222..3bae8e74e0b 100644 --- a/public/app/features/admin/ServerStats.test.tsx +++ b/public/app/features/admin/ServerStats.test.tsx @@ -29,7 +29,6 @@ jest.mock('./state/apis', () => ({ })); jest.mock('../../core/services/context_srv', () => ({ contextSrv: { - hasAccess: () => true, hasPermission: () => true, }, })); diff --git a/public/app/features/alerting/unified/components/contact-points/ContactPoints.v2.tsx b/public/app/features/alerting/unified/components/contact-points/ContactPoints.v2.tsx index f0bbc011ce9..0576fdffc92 100644 --- a/public/app/features/alerting/unified/components/contact-points/ContactPoints.v2.tsx +++ b/public/app/features/alerting/unified/components/contact-points/ContactPoints.v2.tsx @@ -272,7 +272,7 @@ const ContactPointHeader = (props: ContactPointHeaderProps) => { // we make a distinction here becase for "canExport" we show the menu item, if not we hide it const canExport = isGranaManagedAlertmanager; - const allowedToExport = contextSrv.hasAccess(permissions.provisioning.read, isOrgAdmin()); + const allowedToExport = contextSrv.hasPermission(permissions.provisioning.read); return (
diff --git a/public/app/features/alerting/unified/hooks/useIsRuleEditable.ts b/public/app/features/alerting/unified/hooks/useIsRuleEditable.ts index 4625678903d..44008d00d1e 100644 --- a/public/app/features/alerting/unified/hooks/useIsRuleEditable.ts +++ b/public/app/features/alerting/unified/hooks/useIsRuleEditable.ts @@ -47,10 +47,9 @@ export function useIsRuleEditable(rulesSourceName: string, rule?: RulerRuleDTO): loading, }; } - const rbacDisabledFallback = folder.canSave; - const canEditGrafanaRules = contextSrv.hasAccessInMetadata(rulePermission.update, folder, rbacDisabledFallback); - const canRemoveGrafanaRules = contextSrv.hasAccessInMetadata(rulePermission.delete, folder, rbacDisabledFallback); + const canEditGrafanaRules = contextSrv.hasPermissionInMetadata(rulePermission.update, folder); + const canRemoveGrafanaRules = contextSrv.hasPermissionInMetadata(rulePermission.delete, folder); return { isEditable: canEditGrafanaRules, diff --git a/public/app/features/dashboard/components/ShareModal/SharePublicDashboard/ConfigPublicDashboard/EmailSharingConfiguration.tsx b/public/app/features/dashboard/components/ShareModal/SharePublicDashboard/ConfigPublicDashboard/EmailSharingConfiguration.tsx index 4a25e6c9b11..5260cae46b8 100644 --- a/public/app/features/dashboard/components/ShareModal/SharePublicDashboard/ConfigPublicDashboard/EmailSharingConfiguration.tsx +++ b/public/app/features/dashboard/components/ShareModal/SharePublicDashboard/ConfigPublicDashboard/EmailSharingConfiguration.tsx @@ -24,7 +24,6 @@ import { useReshareAccessToRecipientMutation, useUpdatePublicDashboardMutation, } from 'app/features/dashboard/api/publicDashboardApi'; -import { isOrgAdmin } from 'app/features/plugins/admin/permissions'; import { AccessControlAction, useSelector } from 'app/types'; import { trackDashboardSharingActionPerType } from '../../analytics'; @@ -121,7 +120,7 @@ export const EmailSharingConfiguration = () => { const [updateShareType] = useUpdatePublicDashboardMutation(); const [addEmail, { isLoading: isAddEmailLoading }] = useAddRecipientMutation(); - const hasWritePermissions = contextSrv.hasAccess(AccessControlAction.DashboardsPublicWrite, isOrgAdmin()); + const hasWritePermissions = contextSrv.hasPermission(AccessControlAction.DashboardsPublicWrite); const { register, diff --git a/public/app/features/explore/Explore.test.tsx b/public/app/features/explore/Explore.test.tsx index 4cb63ae2049..f76ed10569f 100644 --- a/public/app/features/explore/Explore.test.tsx +++ b/public/app/features/explore/Explore.test.tsx @@ -111,7 +111,7 @@ jest.mock('@grafana/runtime/src/services/dataSourceSrv', () => { jest.mock('app/core/core', () => ({ contextSrv: { - hasAccess: () => true, + hasPermission: () => true, getValidIntervals: (defaultIntervals: string[]) => defaultIntervals, }, })); diff --git a/public/app/features/explore/spec/datasourceState.test.tsx b/public/app/features/explore/spec/datasourceState.test.tsx index 032fa054ae7..158af770bd8 100644 --- a/public/app/features/explore/spec/datasourceState.test.tsx +++ b/public/app/features/explore/spec/datasourceState.test.tsx @@ -15,7 +15,6 @@ jest.mock('@grafana/runtime', () => ({ jest.mock('app/core/core', () => ({ contextSrv: { - hasAccess: () => true, hasPermission: () => true, getValidIntervals: (defaultIntervals: string[]) => defaultIntervals, }, diff --git a/public/app/features/explore/spec/interpolation.test.tsx b/public/app/features/explore/spec/interpolation.test.tsx index d910cd0656e..969f41f424a 100644 --- a/public/app/features/explore/spec/interpolation.test.tsx +++ b/public/app/features/explore/spec/interpolation.test.tsx @@ -17,7 +17,7 @@ jest.mock('@grafana/runtime', () => ({ jest.mock('app/core/core', () => ({ contextSrv: { - hasAccess: () => true, + hasPermission: () => true, getValidIntervals: (defaultIntervals: string[]) => defaultIntervals, }, })); diff --git a/public/app/features/explore/spec/queryHistory.test.tsx b/public/app/features/explore/spec/queryHistory.test.tsx index 69df5461c40..5c7c93abb13 100644 --- a/public/app/features/explore/spec/queryHistory.test.tsx +++ b/public/app/features/explore/spec/queryHistory.test.tsx @@ -50,7 +50,6 @@ jest.mock('@grafana/runtime', () => ({ jest.mock('app/core/core', () => ({ contextSrv: { hasPermission: () => true, - hasAccess: () => true, isSignedIn: true, getValidIntervals: (defaultIntervals: string[]) => defaultIntervals, }, diff --git a/public/app/features/explore/spec/split.test.tsx b/public/app/features/explore/spec/split.test.tsx index 54775223820..fc39d8eba96 100644 --- a/public/app/features/explore/spec/split.test.tsx +++ b/public/app/features/explore/spec/split.test.tsx @@ -16,7 +16,6 @@ jest.mock('app/core/core', () => { return { contextSrv: { hasPermission: () => true, - hasAccess: () => true, getValidIntervals: (defaultIntervals: string[]) => defaultIntervals, }, }; diff --git a/public/app/features/plugins/admin/components/GetStartedWithPlugin/GetStartedWithApp.tsx b/public/app/features/plugins/admin/components/GetStartedWithPlugin/GetStartedWithApp.tsx index c63a6c53c17..ea95689b71f 100644 --- a/public/app/features/plugins/admin/components/GetStartedWithPlugin/GetStartedWithApp.tsx +++ b/public/app/features/plugins/admin/components/GetStartedWithPlugin/GetStartedWithApp.tsx @@ -7,7 +7,6 @@ import { AccessControlAction } from 'app/types'; import { updatePluginSettings } from '../../api'; import { usePluginConfig } from '../../hooks/usePluginConfig'; -import { isOrgAdmin } from '../../permissions'; import { CatalogPlugin } from '../../types'; type Props = { @@ -22,7 +21,7 @@ export function GetStartedWithApp({ plugin }: Props): React.ReactElement | null } // Enforce RBAC - if (!contextSrv.hasAccessInMetadata(AccessControlAction.PluginsWrite, plugin, isOrgAdmin())) { + if (!contextSrv.hasPermissionInMetadata(AccessControlAction.PluginsWrite, plugin)) { return null; } diff --git a/public/app/features/plugins/admin/hooks/usePluginDetailsTabs.tsx b/public/app/features/plugins/admin/hooks/usePluginDetailsTabs.tsx index 79cc6a2fa88..283d52abdfe 100644 --- a/public/app/features/plugins/admin/hooks/usePluginDetailsTabs.tsx +++ b/public/app/features/plugins/admin/hooks/usePluginDetailsTabs.tsx @@ -7,7 +7,6 @@ import { contextSrv } from 'app/core/core'; import { AccessControlAction } from 'app/types'; import { usePluginConfig } from '../hooks/usePluginConfig'; -import { isOrgAdmin } from '../permissions'; import { CatalogPlugin, PluginTabIds, PluginTabLabels } from '../types'; type ReturnType = { @@ -25,8 +24,7 @@ export const usePluginDetailsTabs = (plugin?: CatalogPlugin, pageId?: PluginTabI const currentPageId = pageId || defaultTab; const navModelChildren = useMemo(() => { - const canConfigurePlugins = - plugin && contextSrv.hasAccessInMetadata(AccessControlAction.PluginsWrite, plugin, isOrgAdmin()); + const canConfigurePlugins = plugin && contextSrv.hasPermissionInMetadata(AccessControlAction.PluginsWrite, plugin); const navModelChildren: NavModelItem[] = []; if (isPublished) { navModelChildren.push({ @@ -122,7 +120,7 @@ function useDefaultPage(plugin: CatalogPlugin | undefined, pluginConfig: Grafana return PluginTabIds.OVERVIEW; } - const hasAccess = contextSrv.hasAccessInMetadata(AccessControlAction.PluginsWrite, plugin, isOrgAdmin()); + const hasAccess = contextSrv.hasPermissionInMetadata(AccessControlAction.PluginsWrite, plugin); if (!hasAccess || pluginConfig.meta.type !== PluginType.app) { return PluginTabIds.OVERVIEW; diff --git a/public/app/features/plugins/admin/pages/PluginDetails.test.tsx b/public/app/features/plugins/admin/pages/PluginDetails.test.tsx index bb4aa5ed385..92c7410c208 100644 --- a/public/app/features/plugins/admin/pages/PluginDetails.test.tsx +++ b/public/app/features/plugins/admin/pages/PluginDetails.test.tsx @@ -52,9 +52,8 @@ jest.mock('../helpers.ts', () => ({ jest.mock('app/core/core', () => ({ contextSrv: { - hasAccess: (action: string, fallBack: boolean) => true, hasPermission: (action: string) => true, - hasAccessInMetadata: (action: string, object: WithAccessControlMetadata, fallBack: boolean) => true, + hasPermissionInMetadata: (action: string, object: WithAccessControlMetadata) => true, }, })); diff --git a/public/app/features/search/components/ManageDashboards.test.tsx b/public/app/features/search/components/ManageDashboards.test.tsx index 52a062dd6b5..e07cd425f10 100644 --- a/public/app/features/search/components/ManageDashboards.test.tsx +++ b/public/app/features/search/components/ManageDashboards.test.tsx @@ -14,7 +14,6 @@ jest.mock('app/core/services/context_srv', () => { contextSrv: { ...originMock.context_srv, user: {}, - hasAccess: jest.fn(() => false), hasPermission: jest.fn(() => false), }, }; @@ -32,19 +31,16 @@ jest.spyOn(console, 'error').mockImplementation(); describe('ManageDashboards', () => { beforeEach(() => { - (contextSrv.hasAccess as jest.Mock).mockClear(); (contextSrv.hasPermission as jest.Mock).mockClear(); }); it("should hide and show dashboard actions based on user's permissions", async () => { - (contextSrv.hasAccess as jest.Mock).mockReturnValue(false); (contextSrv.hasPermission as jest.Mock).mockReturnValue(false); const { rerender } = await setup(); expect(screen.queryByRole('button', { name: /new/i })).not.toBeInTheDocument(); - (contextSrv.hasAccess as jest.Mock).mockReturnValue(true); (contextSrv.hasPermission as jest.Mock).mockReturnValue(true); await waitFor(() => rerender()); diff --git a/public/app/features/search/components/ManageDashboardsNew.tsx b/public/app/features/search/components/ManageDashboardsNew.tsx index 3f9363be520..6dd4643276e 100644 --- a/public/app/features/search/components/ManageDashboardsNew.tsx +++ b/public/app/features/search/components/ManageDashboardsNew.tsx @@ -31,9 +31,8 @@ export const ManageDashboardsNew = React.memo(({ folder }: Props) => { const { isEditor } = contextSrv; const hasEditPermissionInFolders = folder ? canSave : contextSrv.hasEditPermissionInFolders; const canCreateFolders = contextSrv.hasPermission(AccessControlAction.FoldersCreate); - const canCreateDashboardsFallback = hasEditPermissionInFolders || !!canSave; const canCreateDashboards = folderUid - ? contextSrv.hasAccessInMetadata(AccessControlAction.DashboardsCreate, folder, canCreateDashboardsFallback) + ? contextSrv.hasPermissionInMetadata(AccessControlAction.DashboardsCreate, folder) : contextSrv.hasPermission(AccessControlAction.DashboardsCreate); const viewActions = (folder === undefined && canCreateFolders) || canCreateDashboards; diff --git a/public/app/features/serviceaccounts/ServiceAccountPage.test.tsx b/public/app/features/serviceaccounts/ServiceAccountPage.test.tsx index 66ab605e509..3be5cf73cfb 100644 --- a/public/app/features/serviceaccounts/ServiceAccountPage.test.tsx +++ b/public/app/features/serviceaccounts/ServiceAccountPage.test.tsx @@ -13,8 +13,7 @@ jest.mock('app/core/core', () => ({ contextSrv: { licensedAccessControlEnabled: () => false, hasPermission: () => true, - hasPermissionInMetadata: () => true, - hasAccessInMetadata: () => false, + hasPermissionInMetadata: () => false, }, })); diff --git a/public/app/features/serviceaccounts/ServiceAccountPage.tsx b/public/app/features/serviceaccounts/ServiceAccountPage.tsx index 4ae6e309d0f..7456b16a932 100644 --- a/public/app/features/serviceaccounts/ServiceAccountPage.tsx +++ b/public/app/features/serviceaccounts/ServiceAccountPage.tsx @@ -73,10 +73,9 @@ export const ServiceAccountPageUnconnected = ({ !contextSrv.hasPermission(AccessControlAction.ServiceAccountsWrite) || serviceAccount.isDisabled; const ableToWrite = contextSrv.hasPermission(AccessControlAction.ServiceAccountsWrite); - const canReadPermissions = contextSrv.hasAccessInMetadata( + const canReadPermissions = contextSrv.hasPermissionInMetadata( AccessControlAction.ServiceAccountsPermissionsRead, - serviceAccount!, - false + serviceAccount! ); const pageNav: NavModelItem = { diff --git a/public/app/features/teams/TeamList.tsx b/public/app/features/teams/TeamList.tsx index 5b2a2d1c60d..85d54da7bed 100644 --- a/public/app/features/teams/TeamList.tsx +++ b/public/app/features/teams/TeamList.tsx @@ -96,10 +96,9 @@ export const TeamList = ({ id: 'role', header: 'Role', cell: ({ cell: { value }, row: { original } }: Cell<'memberCount'>) => { - const canSeeTeamRoles = contextSrv.hasAccessInMetadata( + const canSeeTeamRoles = contextSrv.hasPermissionInMetadata( AccessControlAction.ActionTeamsRolesList, - original, - false + original ); return canSeeTeamRoles && ; }, diff --git a/public/app/features/users/UsersActionBar.test.tsx b/public/app/features/users/UsersActionBar.test.tsx index eee54c936d7..e9b527d5d4c 100644 --- a/public/app/features/users/UsersActionBar.test.tsx +++ b/public/app/features/users/UsersActionBar.test.tsx @@ -10,7 +10,6 @@ import { searchQueryChanged } from './state/reducers'; jest.mock('app/core/core', () => ({ contextSrv: { hasPermission: () => true, - hasAccess: () => true, }, })); @@ -20,7 +19,6 @@ const setup = (propOverrides?: object) => { changeSearchQuery: mockToolkitActionCreator(searchQueryChanged), onShowInvites: jest.fn(), pendingInvitesCount: 0, - canInvite: false, externalUserMngLinkUrl: '', externalUserMngLinkName: '', showInvites: false, @@ -49,9 +47,7 @@ describe('Render', () => { }); it('should show invite button', () => { - setup({ - canInvite: true, - }); + setup(); expect(screen.getByRole('link', { name: 'Invite' })).toHaveAttribute('href', 'org/users/invite'); }); @@ -70,9 +66,7 @@ describe('Render', () => { config.externalUserMngInfo = 'truthy'; config.disableLoginForm = true; - setup({ - canInvite: true, - }); + setup(); expect(screen.queryByRole('link', { name: 'Invite' })).not.toBeInTheDocument(); // Reset the disableLoginForm mock to its original value @@ -83,9 +77,7 @@ describe('Render', () => { config.externalUserMngInfo = ''; config.disableLoginForm = true; - setup({ - canInvite: true, - }); + setup(); expect(screen.getByRole('link', { name: 'Invite' })).toHaveAttribute('href', 'org/users/invite'); // Reset the disableLoginForm mock to its original value @@ -96,9 +88,7 @@ describe('Render', () => { const originalExternalUserMngInfo = config.externalUserMngInfo; config.externalUserMngInfo = 'truthy'; - setup({ - canInvite: true, - }); + setup(); expect(screen.getByRole('link', { name: 'Invite' })).toHaveAttribute('href', 'org/users/invite'); // Reset the disableLoginForm mock to its original value diff --git a/public/app/features/users/UsersActionBar.tsx b/public/app/features/users/UsersActionBar.tsx index 4a97fb7de15..c5b69c379a9 100644 --- a/public/app/features/users/UsersActionBar.tsx +++ b/public/app/features/users/UsersActionBar.tsx @@ -22,7 +22,6 @@ function mapStateToProps(state: StoreState) { pendingInvitesCount: selectTotal(state.invites), externalUserMngLinkName: state.users.externalUserMngLinkName, externalUserMngLinkUrl: state.users.externalUserMngLinkUrl, - canInvite: state.users.canInvite, }; } @@ -35,7 +34,6 @@ const connector = connect(mapStateToProps, mapDispatchToProps); export type Props = ConnectedProps & OwnProps; export const UsersActionBarUnconnected = ({ - canInvite, externalUserMngLinkName, externalUserMngLinkUrl, searchQuery, @@ -48,7 +46,7 @@ export const UsersActionBarUnconnected = ({ { label: 'Users', value: 'users' }, { label: `Pending Invites (${pendingInvitesCount})`, value: 'invites' }, ]; - const canAddToOrg: boolean = contextSrv.hasAccess(AccessControlAction.OrgUsersAdd, canInvite); + const canAddToOrg: boolean = contextSrv.hasPermission(AccessControlAction.OrgUsersAdd); // Show invite button in the following cases: // 1) the instance is not a hosted Grafana instance (!config.externalUserMngInfo) // 2) new basic auth users can be created for this instance (!config.disableLoginForm). diff --git a/public/app/features/users/UsersListPage.test.tsx b/public/app/features/users/UsersListPage.test.tsx index 95e92ac482f..6b1e231166b 100644 --- a/public/app/features/users/UsersListPage.test.tsx +++ b/public/app/features/users/UsersListPage.test.tsx @@ -16,7 +16,7 @@ jest.mock('../../core/app_events', () => ({ jest.mock('app/core/core', () => ({ contextSrv: { user: { orgId: 1 }, - hasAccess: () => false, + hasPermission: () => false, licensedAccessControlEnabled: () => false, }, })); diff --git a/public/app/features/users/state/reducers.ts b/public/app/features/users/state/reducers.ts index ccc2269a439..8b1a987a96e 100644 --- a/public/app/features/users/state/reducers.ts +++ b/public/app/features/users/state/reducers.ts @@ -9,7 +9,6 @@ export const initialState: UsersState = { page: 0, perPage: 30, totalPages: 1, - canInvite: !config.externalUserMngLinkName, externalUserMngInfo: config.externalUserMngInfo, externalUserMngLinkName: config.externalUserMngLinkName, externalUserMngLinkUrl: config.externalUserMngLinkUrl, diff --git a/public/app/types/user.ts b/public/app/types/user.ts index 6c31393b1bb..66107cd1f27 100644 --- a/public/app/types/user.ts +++ b/public/app/types/user.ts @@ -72,7 +72,6 @@ export interface Invitee { export interface UsersState { users: OrgUser[]; searchQuery: string; - canInvite: boolean; externalUserMngLinkUrl: string; externalUserMngLinkName: string; externalUserMngInfo: string;