forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TIP] Restrict content visibility for non-enterprise users (elastic#1…
…38097) (elastic#138358) (cherry picked from commit 335d63a) Co-authored-by: Luke Gmys <[email protected]>
- Loading branch information
1 parent
d831a81
commit ffda8c0
Showing
9 changed files
with
134 additions
and
14 deletions.
There are no files selected for viewing
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
55 changes: 55 additions & 0 deletions
55
.../plugins/threat_intelligence/public/containers/enterprise_guard/enterprise_guard.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,55 @@ | ||
/* | ||
* 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 { render, screen } from '@testing-library/react'; | ||
import React from 'react'; | ||
import { ThreatIntelligenceSecuritySolutionContext } from '../../types'; | ||
import { SecuritySolutionContext } from '../security_solution_context'; | ||
import { EnterpriseGuard } from './enterprise_guard'; | ||
|
||
describe('<EnterpriseGuard />', () => { | ||
describe('when on enterprise plan', () => { | ||
it('should render specified children', () => { | ||
render( | ||
<SecuritySolutionContext.Provider | ||
value={ | ||
{ | ||
licenseService: { isEnterprise: jest.fn().mockReturnValue(true) }, | ||
} as unknown as ThreatIntelligenceSecuritySolutionContext | ||
} | ||
> | ||
<EnterpriseGuard> | ||
<div>enterprise only content</div> | ||
</EnterpriseGuard> | ||
</SecuritySolutionContext.Provider> | ||
); | ||
|
||
expect(screen.queryByText('enterprise only content')).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
describe('when not on enterprise plan', () => { | ||
it('should render specified children', () => { | ||
render( | ||
<SecuritySolutionContext.Provider | ||
value={ | ||
{ | ||
licenseService: { isEnterprise: jest.fn().mockReturnValue(false) }, | ||
} as unknown as ThreatIntelligenceSecuritySolutionContext | ||
} | ||
> | ||
<EnterpriseGuard fallback={<div>fallback for non enterprise</div>}> | ||
<div>enterprise only content</div> | ||
</EnterpriseGuard> | ||
</SecuritySolutionContext.Provider> | ||
); | ||
|
||
expect(screen.queryByText('enterprise only content')).not.toBeInTheDocument(); | ||
expect(screen.queryByText('fallback for non enterprise')).toBeInTheDocument(); | ||
}); | ||
}); | ||
}); |
24 changes: 24 additions & 0 deletions
24
x-pack/plugins/threat_intelligence/public/containers/enterprise_guard/enterprise_guard.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,24 @@ | ||
/* | ||
* 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 React, { ReactElement } from 'react'; | ||
import { FC } from 'react'; | ||
import { useSecurityContext } from '../../hooks/use_security_context'; | ||
|
||
interface EnterpriseGuardProps { | ||
fallback?: ReactElement; | ||
} | ||
|
||
export const EnterpriseGuard: FC<EnterpriseGuardProps> = ({ children, fallback = null }) => { | ||
const { licenseService } = useSecurityContext(); | ||
|
||
if (licenseService.isEnterprise()) { | ||
return <>{children}</>; | ||
} | ||
|
||
return fallback; | ||
}; |
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/threat_intelligence/public/containers/enterprise_guard/index.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,8 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export * from './enterprise_guard'; |
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
22 changes: 22 additions & 0 deletions
22
x-pack/plugins/threat_intelligence/public/hooks/use_security_context.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,22 @@ | ||
/* | ||
* 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 { useContext } from 'react'; | ||
import { SecuritySolutionContext } from '../containers/security_solution_context'; | ||
import { ThreatIntelligenceSecuritySolutionContext } from '../types'; | ||
|
||
export const useSecurityContext = (): ThreatIntelligenceSecuritySolutionContext => { | ||
const contextValue = useContext(SecuritySolutionContext); | ||
|
||
if (!contextValue) { | ||
throw new Error( | ||
'SecuritySolutionContext can only be used within SecuritySolutionContext provider' | ||
); | ||
} | ||
|
||
return contextValue; | ||
}; |
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