Skip to content

Commit

Permalink
Update empty pages and add permission checks for the create button
Browse files Browse the repository at this point in the history
Add rbac checks for the Event stream actions and the edit page
  • Loading branch information
lgalis committed Aug 26, 2024
1 parent ad35330 commit e91d3ab
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function useMapContentTypeToDisplayName() {
decisionenvironment: options?.isTitleCase
? t('Decision Environment')
: t('decision environment'),
eventstream: options?.isTitleCase ? t('Event Stream') : t('event stream'),
auditrule: options?.isTitleCase ? t('Rule Audit') : t('rule audit'),
team: options?.isTitleCase ? t('Team') : t('team'),
organization: options?.isTitleCase ? t('Organization') : t('organization'),
Expand Down
52 changes: 39 additions & 13 deletions frontend/eda/event-streams/EventStreamForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import { useFormContext, useWatch } from 'react-hook-form';
import { EdaCredentialType } from '../interfaces/EdaCredentialType';
import { useEffect } from 'react';
import { PageFormHidden } from '../../../framework/PageForm/Utils/PageFormHidden';
import { useOptions } from '../../common/crud/useOptions';
import { ActionsResponse, OptionsResponse } from '../interfaces/OptionsResponse';
import { Alert } from '@patternfly/react-core';
import { EventStreamDetails } from './EventStreamPage/EventStreamDetails';

// eslint-disable-next-line react/prop-types
function EventStreamInputs() {
Expand Down Expand Up @@ -211,6 +215,8 @@ export function EditEventStream() {
const navigate = useNavigate();
const params = useParams<{ id?: string }>();
const id = Number(params.id);
const { data } = useOptions<OptionsResponse<ActionsResponse>>(edaAPI`/event-streams/`);
const canEditEventStream = Boolean(data && data.actions && data.actions['PATCH']);
const { data: eventStream } = useGet<EdaEventStream>(edaAPI`/event-streams/${id.toString()}/`);

const { cache } = useSWRConfig();
Expand Down Expand Up @@ -245,19 +251,39 @@ export function EditEventStream() {
{ label: `${t('Edit')} ${eventStream?.name || t('event stream')}` },
]}
/>
<EdaPageForm
submitText={t('Save event stream')}
onSubmit={onSubmit}
cancelText={t('Cancel')}
onCancel={onCancel}
defaultValue={{
...eventStream,
organization_id: eventStream.organization?.id,
eda_credential_id: eventStream?.eda_credential?.id,
}}
>
<EventStreamEditInputs />
</EdaPageForm>
{!canEditEventStream ? (
<>
<Alert
variant={'warning'}
isInline
style={{
marginLeft: '24px',
marginRight: '24px',
marginTop: '24px',
paddingLeft: '24px',
paddingTop: '16px',
}}
title={t(
'You do not have permissions to edit this credential. Please contact your organization administrator if there is an issue with your access.'
)}
/>
<EventStreamDetails />
</>
) : (
<EdaPageForm
submitText={t('Save event stream')}
onSubmit={onSubmit}
cancelText={t('Cancel')}
onCancel={onCancel}
defaultValue={{
...eventStream,
organization_id: eventStream.organization?.id,
eda_credential_id: eventStream?.eda_credential?.id,
}}
>
<EventStreamEditInputs />
</EdaPageForm>
)}
</PageLayout>
);
}
Expand Down
22 changes: 21 additions & 1 deletion frontend/eda/event-streams/EventStreamPage/EventStreamPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ import { EdaEventStream } from '../../interfaces/EdaEventStream';
import { EdaRoute } from '../../main/EdaRoutes';
import { useDeleteEventStreams } from '../hooks/useDeleteEventStreams';
import { usePatchRequest } from '../../../common/crud/usePatchRequest';
import { useOptions } from '../../../common/crud/useOptions';
import { ActionsResponse, OptionsResponse } from '../../interfaces/OptionsResponse';

export function EventStreamPage() {
const { t } = useTranslation();
const params = useParams<{ id: string }>();
const pageNavigate = usePageNavigate();
const { data } = useOptions<OptionsResponse<ActionsResponse>>(edaAPI`/event-streams/`);
const canEditEventStream = Boolean(data && data.actions && data.actions['PATCH']);
const { data: eventStream } = useGet<EdaEventStream>(edaAPI`/event-streams/${params.id ?? ''}/`);
const patchRequest = usePatchRequest();
const alertToaster = usePageAlertToaster();
Expand Down Expand Up @@ -71,6 +75,10 @@ export function EventStreamPage() {
icon: PencilAltIcon,
isPinned: true,
label: t('Edit event stream'),
isDisabled: () =>
canEditEventStream
? ''
: t(`The event stream cannot be edited due to insufficient permission`),
onClick: (eventStream: EdaEventStream) =>
pageNavigate(EdaRoute.EditEventStream, { params: { id: eventStream.id } }),
},
Expand All @@ -79,6 +87,10 @@ export function EventStreamPage() {
selection: PageActionSelection.Single,
icon: TaskIcon,
label: t('Switch to test mode'),
isDisabled: () =>
canEditEventStream
? ''
: t(`The event stream cannot be updated due to insufficient permission`),
isHidden: (eventStream: EdaEventStream) => !!eventStream?.test_mode,
onClick: (eventStream: EdaEventStream) => toggleEventStreamMode(true, eventStream),
},
Expand All @@ -87,6 +99,10 @@ export function EventStreamPage() {
selection: PageActionSelection.Single,
icon: DatabaseIcon,
label: t('Switch to production mode'),
isDisabled: () =>
canEditEventStream
? ''
: t(`The event stream cannot be updated due to insufficient permission`),
isHidden: (eventStream: EdaEventStream) => !eventStream?.test_mode,
onClick: (eventStream: EdaEventStream) => toggleEventStreamMode(false, eventStream),
},
Expand All @@ -98,11 +114,15 @@ export function EventStreamPage() {
selection: PageActionSelection.Single,
icon: TrashIcon,
label: t('Delete event stream'),
isDisabled: () =>
canEditEventStream
? ''
: t(`The event stream cannot be deleted due to insufficient permission`),
onClick: (eventStream: EdaEventStream) => deleteEventStreams([eventStream]),
isDanger: true,
},
],
[deleteEventStreams, pageNavigate, t, toggleEventStreamMode]
[canEditEventStream, deleteEventStreams, pageNavigate, t, toggleEventStreamMode]
);

const getPageUrl = useGetPageUrl();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export function EventStreamUserAccess() {
<UserAccess
service="eda"
id={params.id || ''}
type={'project'}
type={'eventstream'}
addRolesRoute={EdaRoute.EventStreamAddUsers}
/>
);
Expand Down
27 changes: 22 additions & 5 deletions frontend/eda/event-streams/EventStreams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import { useEventStreamActions } from './hooks/useEventStreamActions';
import { useEventStreamColumns } from './hooks/useEventStreamColumns';
import { useEventStreamFilters } from './hooks/useEventStreamFilters';
import { useEventStreamsActions } from './hooks/useEventStreamsActions';
import { PlusCircleIcon } from '@patternfly/react-icons';
import { CubesIcon, PlusCircleIcon } from '@patternfly/react-icons';
import { useOptions } from '../../common/crud/useOptions';
import { ActionsResponse, OptionsResponse } from '../interfaces/OptionsResponse';

export function EventStreams() {
const { t } = useTranslation();
Expand All @@ -21,6 +23,8 @@ export function EventStreams() {
tableColumns,
});
const toolbarActions = useEventStreamsActions(view);
const { data } = useOptions<OptionsResponse<ActionsResponse>>(edaAPI`/event-streams/`);
const canCreateEventStream = Boolean(data && data.actions && data.actions['POST']);
const rowActions = useEventStreamActions(view);
return (
<PageLayout>
Expand All @@ -38,11 +42,24 @@ export function EventStreams() {
toolbarFilters={toolbarFilters}
rowActions={rowActions}
errorStateTitle={t('Error loading event streams')}
emptyStateTitle={t('There are currently no event streams created for your organization.')}
emptyStateDescription={t('Please create an event stream by using the button below.')}
emptyStateTitle={
canCreateEventStream
? t('There are currently no event streams created for your organization.')
: t('You do not have permission to create an event stream.')
}
emptyStateDescription={
canCreateEventStream
? t('Please create an event stream by using the button below.')
: t(
'Please contact your organization administrator if there is an issue with your access.'
)
}
emptyStateIcon={canCreateEventStream ? undefined : CubesIcon}
emptyStateButtonIcon={<PlusCircleIcon />}
emptyStateButtonText={t('Create event stream')}
emptyStateButtonClick={() => pageNavigate(EdaRoute.CreateEventStream)}
emptyStateButtonText={canCreateEventStream ? t('Create event stream') : undefined}
emptyStateButtonClick={
canCreateEventStream ? () => pageNavigate(EdaRoute.CreateEventStream) : undefined
}
{...view}
defaultSubtitle={t('Event stream')}
/>
Expand Down
12 changes: 11 additions & 1 deletion frontend/eda/event-streams/hooks/useEventStreamsActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ import { IEdaView } from '../../common/useEventDrivenView';
import { EdaEventStream } from '../../interfaces/EdaEventStream';
import { EdaRoute } from '../../main/EdaRoutes';
import { useDeleteEventStreams } from './useDeleteEventStreams';
import { useOptions } from '../../../common/crud/useOptions';
import { ActionsResponse, OptionsResponse } from '../../interfaces/OptionsResponse';
import { edaAPI } from '../../common/eda-utils';

export function useEventStreamsActions(view: IEdaView<EdaEventStream>) {
const { t } = useTranslation();
const pageNavigate = usePageNavigate();
const deleteEventStreams = useDeleteEventStreams(view.unselectItemsAndRefresh);
const { data } = useOptions<OptionsResponse<ActionsResponse>>(edaAPI`/event-streams/`);
const canCreateEventStream = Boolean(data && data.actions && data.actions['POST']);
return useMemo<IPageAction<EdaEventStream>[]>(
() => [
{
Expand All @@ -26,6 +31,11 @@ export function useEventStreamsActions(view: IEdaView<EdaEventStream>) {
isPinned: true,
icon: PlusCircleIcon,
label: t('Create event stream'),
isDisabled: canCreateEventStream
? undefined
: t(
'You do not have permission to create a project. Please contact your organization administrator if there is an issue with your access.'
),
onClick: () => pageNavigate(EdaRoute.CreateEventStream),
},
{
Expand All @@ -37,6 +47,6 @@ export function useEventStreamsActions(view: IEdaView<EdaEventStream>) {
isDanger: true,
},
],
[deleteEventStreams, pageNavigate, t]
[canCreateEventStream, deleteEventStreams, pageNavigate, t]
);
}

0 comments on commit e91d3ab

Please sign in to comment.