Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: require permissions for create integration #1273

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 91 additions & 75 deletions src/components/IntegrationsDropdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,43 @@ import AddSourceWizard from './addSourceWizard';
import { CLOUD_VENDOR, COMMUNICATIONS, REDHAT_VENDOR, REPORTING, WEBHOOKS } from '../utilities/constants';
import { checkPropTypes } from 'prop-types';
import { useFlag } from '@unleash/proxy-client-react';
import { useSelector } from 'react-redux';
import PermissionsChecker from './PermissionsChecker';

const dropdownItems = (isPagerDutyEnabled) => [
{
title: 'Cloud',
description: 'Amazon Web Services, Google Cloud Platform, Microsoft Azure, Oracle Cloud Platform',
value: CLOUD_VENDOR,
},
{
title: 'Red Hat',
description: 'Red Hat OpenShift Container Platform, Red Hat Satellite, Red Hat Ansible Automation Platform',
value: REDHAT_VENDOR,
},
{
title: 'Communications',
description: 'Google Chat, Microsoft Office Teams, Slack',
value: COMMUNICATIONS,
},
{
title: 'Reporting & Automation',
description: `Event-Driven Ansible, ${isPagerDutyEnabled ? 'PagerDuty, ' : ''}ServiceNow, Splunk`,
value: REPORTING,
},
{
title: 'Webhooks',
description: '',
value: WEBHOOKS,
},
const dropdownItems = (isPagerDutyEnabled, hasSourcesPermissions, hasIntegrationsPermissions) => [
...(hasSourcesPermissions
? [
{
title: 'Cloud',
description: 'Amazon Web Services, Google Cloud Platform, Microsoft Azure, Oracle Cloud Platform',
value: CLOUD_VENDOR,
},
{
title: 'Red Hat',
description: 'Red Hat OpenShift Container Platform, Red Hat Satellite, Red Hat Ansible Automation Platform',
value: REDHAT_VENDOR,
},
]
: []),
...(hasIntegrationsPermissions
? [
{
title: 'Communications',
description: 'Google Chat, Microsoft Office Teams, Slack',
value: COMMUNICATIONS,
},
{
title: 'Reporting & Automation',
description: `Event-Driven Ansible, ${isPagerDutyEnabled ? 'PagerDuty, ' : ''}ServiceNow, Splunk`,
value: REPORTING,
},
{
title: 'Webhooks',
description: '',
value: WEBHOOKS,
},
]
: []),
];

const IntegrationsDropdown = (props) => {
Expand All @@ -40,6 +50,8 @@ const IntegrationsDropdown = (props) => {
const [isSourcesWizardOpen, setIsSourcesWizardOpen] = useState(false);
const [selectedIntegration, setSelectedIntegration] = useState(null);
const isPagerDutyEnabled = useFlag('platform.integrations.pager-duty');
const hasSourcesPermissions = useSelector(({ user }) => user?.writePermissions);
const hasIntegrationsPermissions = useSelector(({ user }) => user?.integrationsEndpointsPermissions);

const handleSelect = (_event, value) => {
setIsOpen(false);
Expand All @@ -52,56 +64,60 @@ const IntegrationsDropdown = (props) => {
};

return (
<div className="integrations-dropdown">
{[REDHAT_VENDOR, CLOUD_VENDOR].includes(selectedIntegration) && (
<AddSourceWizard
isOpen={isSourcesWizardOpen}
onClose={() => {
setIsSourcesWizardOpen(false);
setSelectedIntegration(null);
}}
activeCategory={selectedIntegration}
/>
)}
{[COMMUNICATIONS, REPORTING, WEBHOOKS].includes(selectedIntegration) && (
<AsyncComponent
appName="notifications"
module="./IntegrationsWizard"
isOpen={isIntegrationsWizardOpen}
category={selectedIntegration}
closeModal={() => {
setIsIntegrationsWizardOpen(false);
setSelectedIntegration(null);
}}
fallback={<div id="fallback-modal" />}
/>
)}
<Dropdown
isOpen={isOpen}
onSelect={handleSelect}
onOpenChange={setIsOpen}
toggle={(toggleRef) => (
<MenuToggle
ref={toggleRef}
onClick={() => setIsOpen(!isOpen)}
isExpanded={isOpen}
isDisabled={props.isDisabled}
variant="primary"
>
Create Integration
</MenuToggle>
<PermissionsChecker>
<div className="integrations-dropdown">
{[REDHAT_VENDOR, CLOUD_VENDOR].includes(selectedIntegration) && (
<AddSourceWizard
isOpen={isSourcesWizardOpen}
onClose={() => {
setIsSourcesWizardOpen(false);
setSelectedIntegration(null);
}}
activeCategory={selectedIntegration}
/>
)}
{...props}
>
<DropdownList>
{dropdownItems(isPagerDutyEnabled).map(({ title, value, description }) => (
<DropdownItem key={title} value={value} description={description}>
{title}
</DropdownItem>
))}
</DropdownList>
</Dropdown>
</div>
{[COMMUNICATIONS, REPORTING, WEBHOOKS].includes(selectedIntegration) && (
<AsyncComponent
appName="notifications"
module="./IntegrationsWizard"
isOpen={isIntegrationsWizardOpen}
category={selectedIntegration}
closeModal={() => {
setIsIntegrationsWizardOpen(false);
setSelectedIntegration(null);
}}
fallback={<div id="fallback-modal" />}
/>
)}
<Dropdown
isOpen={isOpen}
onSelect={handleSelect}
onOpenChange={setIsOpen}
toggle={(toggleRef) => (
<MenuToggle
ref={toggleRef}
onClick={() => setIsOpen(!isOpen)}
isExpanded={isOpen}
isDisabled={props.isDisabled || (!hasSourcesPermissions && !hasIntegrationsPermissions)}
variant="primary"
>
Create Integration
</MenuToggle>
)}
{...props}
>
<DropdownList>
{dropdownItems(isPagerDutyEnabled, hasSourcesPermissions, hasIntegrationsPermissions).map(
({ title, value, description }) => (
<DropdownItem key={title} value={value} description={description}>
{title}
</DropdownItem>
),
)}
</DropdownList>
</Dropdown>
</div>
</PermissionsChecker>
);
};

Expand Down