Skip to content

Commit d0194c6

Browse files
Add permission checks for connection picker (#2089)
<!-- Ensure the title clearly reflects what was changed. Provide a clear and concise description of the changes made. The PR should only contain the changes related to the issue, and no other unrelated changes. --> Fixes OPS-3856
1 parent 5c0e70a commit d0194c6

1 file changed

Lines changed: 53 additions & 21 deletions

File tree

packages/react-ui/src/app/features/builder/step-settings/block-settings/connection-select.tsx

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
} from '@openops/components/ui';
1616
import {
1717
addConnectionBrackets,
18+
Permission,
1819
removeConnectionBrackets,
1920
} from '@openops/shared';
2021
import { t } from 'i18next';
@@ -23,6 +24,7 @@ import type React from 'react';
2324
import { memo, useCallback, useState } from 'react';
2425
import { ControllerRenderProps, useFormContext } from 'react-hook-form';
2526

27+
import { useAuthorization } from '@/app/common/hooks/authorization-hooks';
2628
import { AutoFormFieldWrapper } from '@/app/features/builder/block-properties/auto-form-field-wrapper';
2729
import { DynamicFormValidationProvider } from '@/app/features/builder/dynamic-form-validation/dynamic-form-validation-context';
2830

@@ -50,6 +52,9 @@ const ConnectionSelect = memo((params: ConnectionSelectProps) => {
5052
string | null
5153
>(null);
5254

55+
const { checkAccess } = useAuthorization();
56+
const canWriteConnection = checkAccess(Permission.WRITE_APP_CONNECTION);
57+
5358
const form = useFormContext();
5459
const {
5560
data: connectionsPage,
@@ -188,7 +193,7 @@ const ConnectionSelect = memo((params: ConnectionSelectProps) => {
188193

189194
{field.value && !field.disabled && !params.disabled && (
190195
<div className="shrink-0 flex items-center gap-1">
191-
{selectConnectionOpen && (
196+
{selectConnectionOpen && canWriteConnection && (
192197
<Button
193198
type="button"
194199
variant="ghost"
@@ -229,30 +234,15 @@ const ConnectionSelect = memo((params: ConnectionSelectProps) => {
229234
)}
230235
</SelectTrigger>
231236
<SelectContent>
232-
<SelectAction
233-
onClick={() => {
237+
<ConnectionSelectContent
238+
canWriteConnection={canWriteConnection}
239+
connections={connectionsPage?.data ?? []}
240+
onCreateNew={() => {
234241
setReconnectConnectionId(null);
235242
setSelectConnectionOpen(false);
236243
setConnectionDialogOpen(true);
237244
}}
238-
>
239-
<span className="flex items-center gap-1 text-primary w-full">
240-
<Plus size={16} />
241-
{t('Create new connection')}
242-
</span>
243-
</SelectAction>
244-
245-
{connectionsPage?.data &&
246-
connectionsPage.data.map((connection) => {
247-
return (
248-
<SelectItem
249-
value={addConnectionBrackets(connection.name)}
250-
key={connection.name}
251-
>
252-
{connection.name}
253-
</SelectItem>
254-
);
255-
})}
245+
/>
256246
</SelectContent>
257247
</Select>
258248
</AutoFormFieldWrapper>
@@ -263,5 +253,47 @@ const ConnectionSelect = memo((params: ConnectionSelectProps) => {
263253
);
264254
});
265255

256+
type ConnectionSelectContentProps = {
257+
canWriteConnection: boolean;
258+
connections: { name: string }[];
259+
onCreateNew: () => void;
260+
};
261+
262+
const ConnectionSelectContent = ({
263+
canWriteConnection,
264+
connections,
265+
onCreateNew,
266+
}: ConnectionSelectContentProps) => {
267+
if (connections.length === 0 && !canWriteConnection) {
268+
return (
269+
<div className="px-3 py-2 text-sm text-muted-foreground">
270+
{t('No available connections')}
271+
</div>
272+
);
273+
}
274+
275+
return (
276+
<>
277+
{canWriteConnection && (
278+
<SelectAction onClick={onCreateNew}>
279+
<span className="flex items-center gap-1 text-primary w-full">
280+
<Plus size={16} />
281+
{t('Create new connection')}
282+
</span>
283+
</SelectAction>
284+
)}
285+
286+
{connections.map((connection) => (
287+
<SelectItem
288+
value={addConnectionBrackets(connection.name)}
289+
key={connection.name}
290+
>
291+
{connection.name}
292+
</SelectItem>
293+
))}
294+
</>
295+
);
296+
};
297+
266298
ConnectionSelect.displayName = 'ConnectionSelect';
267299
export { ConnectionSelect };

0 commit comments

Comments
 (0)