Merge pull request #1578 from automatisch/AUT-620

feat: improve UI display depending on user permissions
This commit is contained in:
Ali BARIN
2024-06-20 12:04:31 +02:00
committed by GitHub
5 changed files with 145 additions and 76 deletions

View File

@@ -8,6 +8,7 @@ import * as URLS from 'config/urls';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import { ConnectionPropType } from 'propTypes/propTypes'; import { ConnectionPropType } from 'propTypes/propTypes';
import { useQueryClient } from '@tanstack/react-query'; import { useQueryClient } from '@tanstack/react-query';
import Can from 'components/Can';
function ContextMenu(props) { function ContextMenu(props) {
const { const {
@@ -44,21 +45,35 @@ function ContextMenu(props) {
hideBackdrop={false} hideBackdrop={false}
anchorEl={anchorEl} anchorEl={anchorEl}
> >
<Can I="read" a="Flow" passThrough>
{(allowed) => (
<MenuItem <MenuItem
component={Link} component={Link}
to={URLS.APP_FLOWS_FOR_CONNECTION(appKey, connection.id)} to={URLS.APP_FLOWS_FOR_CONNECTION(appKey, connection.id)}
onClick={createActionHandler({ type: 'viewFlows' })} onClick={createActionHandler({ type: 'viewFlows' })}
disabled={!allowed}
> >
{formatMessage('connection.viewFlows')} {formatMessage('connection.viewFlows')}
</MenuItem> </MenuItem>
)}
</Can>
<MenuItem onClick={createActionHandler({ type: 'test' })}> <Can I="update" a="Connection" passThrough>
{(allowed) => (
<MenuItem
onClick={createActionHandler({ type: 'test' })}
disabled={!allowed}
>
{formatMessage('connection.testConnection')} {formatMessage('connection.testConnection')}
</MenuItem> </MenuItem>
)}
</Can>
<Can I="create" a="Connection" passThrough>
{(allowed) => (
<MenuItem <MenuItem
component={Link} component={Link}
disabled={disableReconnection} disabled={!allowed || disableReconnection}
to={URLS.APP_RECONNECT_CONNECTION( to={URLS.APP_RECONNECT_CONNECTION(
appKey, appKey,
connection.id, connection.id,
@@ -68,10 +83,19 @@ function ContextMenu(props) {
> >
{formatMessage('connection.reconnect')} {formatMessage('connection.reconnect')}
</MenuItem> </MenuItem>
)}
</Can>
<MenuItem onClick={createActionHandler({ type: 'delete' })}> <Can I="delete" a="Connection" passThrough>
{(allowed) => (
<MenuItem
onClick={createActionHandler({ type: 'delete' })}
disabled={!allowed}
>
{formatMessage('connection.delete')} {formatMessage('connection.delete')}
</MenuItem> </MenuItem>
)}
</Can>
</Menu> </Menu>
); );
} }

View File

@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import AppConnectionRow from 'components/AppConnectionRow'; import AppConnectionRow from 'components/AppConnectionRow';
import NoResultFound from 'components/NoResultFound'; import NoResultFound from 'components/NoResultFound';
import Can from 'components/Can';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import * as URLS from 'config/urls'; import * as URLS from 'config/urls';
import useAppConnections from 'hooks/useAppConnections'; import useAppConnections from 'hooks/useAppConnections';
@@ -16,11 +17,15 @@ function AppConnections(props) {
if (!hasConnections) { if (!hasConnections) {
return ( return (
<Can I="create" a="Connection" passThrough>
{(allowed) => (
<NoResultFound <NoResultFound
to={URLS.APP_ADD_CONNECTION(appKey)}
text={formatMessage('app.noConnections')} text={formatMessage('app.noConnections')}
data-test="connections-no-results" data-test="connections-no-results"
{...(allowed && { to: URLS.APP_ADD_CONNECTION(appKey) })}
/> />
)}
</Can>
); );
} }

View File

@@ -5,6 +5,7 @@ import PaginationItem from '@mui/material/PaginationItem';
import * as URLS from 'config/urls'; import * as URLS from 'config/urls';
import AppFlowRow from 'components/FlowRow'; import AppFlowRow from 'components/FlowRow';
import Can from 'components/Can';
import NoResultFound from 'components/NoResultFound'; import NoResultFound from 'components/NoResultFound';
import useFormatMessage from 'hooks/useFormatMessage'; import useFormatMessage from 'hooks/useFormatMessage';
import useConnectionFlows from 'hooks/useConnectionFlows'; import useConnectionFlows from 'hooks/useConnectionFlows';
@@ -36,11 +37,20 @@ function AppFlows(props) {
if (!hasFlows) { if (!hasFlows) {
return ( return (
<Can I="create" a="Flow" passThrough>
{(allowed) => (
<NoResultFound <NoResultFound
to={URLS.CREATE_FLOW_WITH_APP_AND_CONNECTION(appKey, connectionId)}
text={formatMessage('app.noFlows')} text={formatMessage('app.noFlows')}
data-test="flows-no-results" data-test="flows-no-results"
{...(allowed && {
to: URLS.CREATE_FLOW_WITH_APP_AND_CONNECTION(
appKey,
connectionId
),
})}
/> />
)}
</Can>
); );
} }

View File

@@ -30,6 +30,7 @@ import AppIcon from 'components/AppIcon';
import Container from 'components/Container'; import Container from 'components/Container';
import PageTitle from 'components/PageTitle'; import PageTitle from 'components/PageTitle';
import useApp from 'hooks/useApp'; import useApp from 'hooks/useApp';
import Can from 'components/Can';
const ReconnectConnection = (props) => { const ReconnectConnection = (props) => {
const { application, onClose } = props; const { application, onClose } = props;
@@ -92,7 +93,7 @@ export default function Application() {
} }
return options; return options;
}, [appKey, appConfig?.data, currentUserAbility]); }, [appKey, appConfig?.data, currentUserAbility, formatMessage]);
if (loading) return null; if (loading) return null;
@@ -118,6 +119,8 @@ export default function Application() {
<Route <Route
path={`${URLS.FLOWS}/*`} path={`${URLS.FLOWS}/*`}
element={ element={
<Can I="create" a="Flow" passThrough>
{(allowed) => (
<ConditionalIconButton <ConditionalIconButton
type="submit" type="submit"
variant="contained" variant="contained"
@@ -130,18 +133,23 @@ export default function Application() {
)} )}
fullWidth fullWidth
icon={<AddIcon />} icon={<AddIcon />}
disabled={!currentUserAbility.can('create', 'Flow')} disabled={!allowed}
> >
{formatMessage('app.createFlow')} {formatMessage('app.createFlow')}
</ConditionalIconButton> </ConditionalIconButton>
)}
</Can>
} }
/> />
<Route <Route
path={`${URLS.CONNECTIONS}/*`} path={`${URLS.CONNECTIONS}/*`}
element={ element={
<Can I="create" a="Connection" passThrough>
{(allowed) => (
<SplitButton <SplitButton
disabled={ disabled={
!allowed ||
(appConfig?.data && (appConfig?.data &&
!appConfig?.data?.canConnect && !appConfig?.data?.canConnect &&
!appConfig?.data?.canCustomConnect) || !appConfig?.data?.canCustomConnect) ||
@@ -149,6 +157,8 @@ export default function Application() {
} }
options={connectionOptions} options={connectionOptions}
/> />
)}
</Can>
} }
/> />
</Routes> </Routes>
@@ -169,17 +179,20 @@ export default function Application() {
label={formatMessage('app.connections')} label={formatMessage('app.connections')}
to={URLS.APP_CONNECTIONS(appKey)} to={URLS.APP_CONNECTIONS(appKey)}
value={URLS.APP_CONNECTIONS_PATTERN} value={URLS.APP_CONNECTIONS_PATTERN}
disabled={!app.supportsConnections} disabled={
!currentUserAbility.can('read', 'Connection') ||
!app.supportsConnections
}
component={Link} component={Link}
data-test="connections-tab" data-test="connections-tab"
/> />
<Tab <Tab
label={formatMessage('app.flows')} label={formatMessage('app.flows')}
to={URLS.APP_FLOWS(appKey)} to={URLS.APP_FLOWS(appKey)}
value={URLS.APP_FLOWS_PATTERN} value={URLS.APP_FLOWS_PATTERN}
component={Link} component={Link}
data-test="flows-tab" data-test="flows-tab"
disabled={!currentUserAbility.can('read', 'Flow')}
/> />
</Tabs> </Tabs>
</Box> </Box>
@@ -187,14 +200,20 @@ export default function Application() {
<Routes> <Routes>
<Route <Route
path={`${URLS.FLOWS}/*`} path={`${URLS.FLOWS}/*`}
element={<AppFlows appKey={appKey} />} element={
<Can I="read" a="Flow">
<AppFlows appKey={appKey} />
</Can>
}
/> />
<Route <Route
path={`${URLS.CONNECTIONS}/*`} path={`${URLS.CONNECTIONS}/*`}
element={<AppConnections appKey={appKey} />} element={
<Can I="read" a="Connection">
<AppConnections appKey={appKey} />
</Can>
}
/> />
<Route <Route
path="/" path="/"
element={ element={
@@ -218,17 +237,24 @@ export default function Application() {
<Route <Route
path="/connections/add" path="/connections/add"
element={ element={
<AddAppConnection onClose={goToApplicationPage} application={app} /> <Can I="create" a="Connection">
<AddAppConnection
onClose={goToApplicationPage}
application={app}
/>
</Can>
} }
/> />
<Route <Route
path="/connections/:connectionId/reconnect" path="/connections/:connectionId/reconnect"
element={ element={
<Can I="create" a="Connection">
<ReconnectConnection <ReconnectConnection
application={app} application={app}
onClose={goToApplicationPage} onClose={goToApplicationPage}
/> />
</Can>
} }
/> />
</Routes> </Routes>

View File

@@ -84,11 +84,15 @@ export default function Applications() {
)} )}
{!isLoading && !hasApps && ( {!isLoading && !hasApps && (
<Can I="create" a="Connection" passThrough>
{(allowed) => (
<NoResultFound <NoResultFound
text={formatMessage('apps.noConnections')} text={formatMessage('apps.noConnections')}
to={URLS.NEW_APP_CONNECTION} {...(allowed && { to: URLS.NEW_APP_CONNECTION })}
/> />
)} )}
</Can>
)}
{!isLoading && {!isLoading &&
apps?.map((app) => ( apps?.map((app) => (