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
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 7 additions & 4 deletions src/components/Widget/IntegrationsWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
Tile,
} from '@patternfly/react-core';
import React, { FunctionComponent, useEffect, useState } from 'react';
import { Provider } from 'react-redux';
import { Provider, useSelector } from 'react-redux';
import IntegrationsDropdown from '../IntegrationsDropdown';
import { CLOUD_VENDOR, COMMUNICATIONS, REDHAT_VENDOR, REPORTING, WEBHOOKS } from '../../utilities/constants';
import { getProdStore } from '../../utilities/store';
Expand All @@ -25,6 +25,7 @@ import { fetchIntegrations } from './services/fetchIntegrations';
import { fetchRedHatSources } from './services/fetchRedHatSources';
import { createIntegrationsData } from './consts/widgetData';
import { useFlag } from '@unleash/proxy-client-react';
import PermissionsChecker from '../PermissionsChecker';

const IntegrationsWidget: FunctionComponent = () => {
const [isLoading, setIsLoading] = useState(false);
Expand All @@ -35,8 +36,10 @@ const IntegrationsWidget: FunctionComponent = () => {
const [isIntegrationsWizardOpen, setIsIntegrationsWizardOpen] = useState(false);
const [isSourcesWizardOpen, setIsSourcesWizardOpen] = useState(false);

const hasSourcesPermissions = useSelector(({ user }) => user?.writePermissions);
const hasIntegrationsPermissions = useSelector(({ user }) => user?.integrationsEndpointsPermissions);
const isPagerDutyEnabled = useFlag('platform.integrations.pager-duty');
const integrationsData = createIntegrationsData(isPagerDutyEnabled);
const integrationsData = createIntegrationsData(isPagerDutyEnabled, hasSourcesPermissions, hasIntegrationsPermissions);

const handleTileClick = (value: string) => {
setSelectedTileValue(value);
Expand Down Expand Up @@ -97,7 +100,7 @@ const IntegrationsWidget: FunctionComponent = () => {
const isEmptyState = Object.values(integrationCounts).reduce((total, count) => total + count, 0) === 0;

return (
<>
<PermissionsChecker>
{isLoading ? (
<Spinner />
) : isEmptyState ? (
Expand Down Expand Up @@ -178,7 +181,7 @@ const IntegrationsWidget: FunctionComponent = () => {
</CardFooter>
</Card>
)}{' '}
</>
</PermissionsChecker>
);
};

Expand Down
Loading