-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add readonly view to user management (#143438)
Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
1 parent
6c5d816
commit 53f3034
Showing
15 changed files
with
548 additions
and
175 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
x-pack/plugins/security/public/components/use_badge.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { renderHook } from '@testing-library/react-hooks'; | ||
import React from 'react'; | ||
|
||
import { coreMock } from '@kbn/core/public/mocks'; | ||
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public'; | ||
|
||
import type { ChromeBadge } from './use_badge'; | ||
import { useBadge } from './use_badge'; | ||
|
||
describe('useBadge', () => { | ||
it('should add badge to chrome', async () => { | ||
const coreStart = coreMock.createStart(); | ||
const badge: ChromeBadge = { | ||
text: 'text', | ||
tooltip: 'text', | ||
}; | ||
renderHook(useBadge, { | ||
initialProps: badge, | ||
wrapper: ({ children }) => ( | ||
<KibanaContextProvider services={coreStart}>{children}</KibanaContextProvider> | ||
), | ||
}); | ||
|
||
expect(coreStart.chrome.setBadge).toHaveBeenLastCalledWith(badge); | ||
}); | ||
|
||
it('should remove badge from chrome on unmount', async () => { | ||
const coreStart = coreMock.createStart(); | ||
const badge: ChromeBadge = { | ||
text: 'text', | ||
tooltip: 'text', | ||
}; | ||
const { unmount } = renderHook(useBadge, { | ||
initialProps: badge, | ||
wrapper: ({ children }) => ( | ||
<KibanaContextProvider services={coreStart}>{children}</KibanaContextProvider> | ||
), | ||
}); | ||
|
||
expect(coreStart.chrome.setBadge).toHaveBeenLastCalledWith(badge); | ||
|
||
unmount(); | ||
|
||
expect(coreStart.chrome.setBadge).toHaveBeenLastCalledWith(); | ||
}); | ||
|
||
it('should update chrome when badge changes', async () => { | ||
const coreStart = coreMock.createStart(); | ||
const badge1: ChromeBadge = { | ||
text: 'text', | ||
tooltip: 'text', | ||
}; | ||
const { rerender } = renderHook(useBadge, { | ||
initialProps: badge1, | ||
wrapper: ({ children }) => ( | ||
<KibanaContextProvider services={coreStart}>{children}</KibanaContextProvider> | ||
), | ||
}); | ||
|
||
expect(coreStart.chrome.setBadge).toHaveBeenLastCalledWith(badge1); | ||
|
||
const badge2: ChromeBadge = { | ||
text: 'text2', | ||
tooltip: 'text2', | ||
}; | ||
rerender(badge2); | ||
|
||
expect(coreStart.chrome.setBadge).toHaveBeenLastCalledWith(badge2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { DependencyList } from 'react'; | ||
import { useEffect } from 'react'; | ||
|
||
import type { ChromeBadge } from '@kbn/core-chrome-browser'; | ||
import type { CoreStart } from '@kbn/core/public'; | ||
import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
|
||
export type { ChromeBadge }; | ||
|
||
/** | ||
* Renders a badge in the Kibana chrome. | ||
* @param badge Params of the badge or `undefined` to render no badge. | ||
* @param badge.iconType Icon type of the badge shown in the Kibana chrome. | ||
* @param badge.text Title of tooltip displayed when hovering the badge. | ||
* @param badge.tooltip Description of tooltip displayed when hovering the badge. | ||
* @param deps If present, badge will be updated or removed if the values in the list change. | ||
*/ | ||
export function useBadge( | ||
badge: ChromeBadge | undefined, | ||
deps: DependencyList = [badge?.iconType, badge?.text, badge?.tooltip] | ||
) { | ||
const { services } = useKibana<CoreStart>(); | ||
|
||
useEffect(() => { | ||
if (badge) { | ||
services.chrome.setBadge(badge); | ||
return () => services.chrome.setBadge(); | ||
} | ||
}, deps); // eslint-disable-line react-hooks/exhaustive-deps | ||
} |
47 changes: 47 additions & 0 deletions
47
x-pack/plugins/security/public/components/use_capabilities.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { renderHook } from '@testing-library/react-hooks'; | ||
import React from 'react'; | ||
|
||
import { coreMock } from '@kbn/core/public/mocks'; | ||
import { KibanaContextProvider } from '@kbn/kibana-react-plugin/public'; | ||
|
||
import { useCapabilities } from './use_capabilities'; | ||
|
||
describe('useCapabilities', () => { | ||
it('should return capabilities', async () => { | ||
const coreStart = coreMock.createStart(); | ||
|
||
const { result } = renderHook(useCapabilities, { | ||
wrapper: ({ children }) => ( | ||
<KibanaContextProvider services={coreStart}>{children}</KibanaContextProvider> | ||
), | ||
}); | ||
|
||
expect(result.current).toEqual(coreStart.application.capabilities); | ||
}); | ||
|
||
it('should return capabilities scoped by feature', async () => { | ||
const coreStart = coreMock.createStart(); | ||
coreStart.application.capabilities = { | ||
...coreStart.application.capabilities, | ||
users: { | ||
save: true, | ||
}, | ||
}; | ||
|
||
const { result } = renderHook(useCapabilities, { | ||
initialProps: 'users', | ||
wrapper: ({ children }) => ( | ||
<KibanaContextProvider services={coreStart}>{children}</KibanaContextProvider> | ||
), | ||
}); | ||
|
||
expect(result.current).toEqual({ save: true }); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
x-pack/plugins/security/public/components/use_capabilities.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { Capabilities } from '@kbn/core-capabilities-common'; | ||
import type { CoreStart } from '@kbn/core/public'; | ||
import { useKibana } from '@kbn/kibana-react-plugin/public'; | ||
|
||
type FeatureCapabilities = Capabilities[string]; | ||
|
||
/** | ||
* Returns capabilities for a specific feature, or alternatively the entire capabilities object. | ||
* @param featureId ID of feature | ||
*/ | ||
export function useCapabilities(): Capabilities; | ||
export function useCapabilities<T extends FeatureCapabilities = FeatureCapabilities>( | ||
featureId: string | ||
): T; | ||
export function useCapabilities<T extends FeatureCapabilities = FeatureCapabilities>( | ||
featureId?: string | ||
) { | ||
const { services } = useKibana<CoreStart>(); | ||
|
||
if (featureId) { | ||
return services.application.capabilities[featureId] as T; | ||
} | ||
|
||
return services.application.capabilities; | ||
} |
33 changes: 33 additions & 0 deletions
33
x-pack/plugins/security/public/management/badges/readonly_badge.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
|
||
import { useBadge } from '../../components/use_badge'; | ||
import { useCapabilities } from '../../components/use_capabilities'; | ||
|
||
export interface ReadonlyBadgeProps { | ||
featureId: string; | ||
tooltip: string; | ||
} | ||
|
||
export const ReadonlyBadge = ({ featureId, tooltip }: ReadonlyBadgeProps) => { | ||
const { save } = useCapabilities(featureId); | ||
useBadge( | ||
save | ||
? undefined | ||
: { | ||
iconType: 'glasses', | ||
text: i18n.translate('xpack.security.management.readonlyBadge.text', { | ||
defaultMessage: 'Read only', | ||
}), | ||
tooltip, | ||
}, | ||
[save] | ||
); | ||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.