Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Refactor webhooks and add abac

Joshen Lim48dd810dec8f
diff --git a/studio/components/interfaces/Database/Hooks/CreateHook/TableSelection.tsx b/studio/components/interfaces/Database/Hooks/CreateHook/TableSelection.tsx
index 4c27577dd1b..b0864f98226 100644
--- a/studio/components/interfaces/Database/Hooks/CreateHook/TableSelection.tsx
+++ b/studio/components/interfaces/Database/Hooks/CreateHook/TableSelection.tsx
@@ -84,11 +84,9 @@ const TableSelection: FC = observer(({}) => {
</div>
)}
>
- <div className="flex flex-row items-center space-x-1">
- <Typography.Text>{x.name}</Typography.Text>
- <Typography.Text type="secondary" className="opacity-50" small>
- {x.schema}
- </Typography.Text>
+ <div className="flex flex-row items-center space-x-2">
+ <p>{x.name}</p>
+ <p className="text-scale-1100">{x.schema}</p>
</div>
</Listbox.Option>
)
diff --git a/studio/components/interfaces/Database/Hooks/HooksList/HookList.tsx b/studio/components/interfaces/Database/Hooks/HooksList/HookList.tsx
new file mode 100644
index 00000000000..726fa3e0206
--- /dev/null
+++ b/studio/components/interfaces/Database/Hooks/HooksList/HookList.tsx
@@ -0,0 +1,111 @@
+import { FC } from 'react'
+import { includes } from 'lodash'
+import { observer } from 'mobx-react-lite'
+import * as Tooltip from '@radix-ui/react-tooltip'
+import { PermissionAction } from '@supabase/shared-types/out/constants'
+import { Badge, Button, Dropdown, IconMoreVertical, IconTrash, IconEdit3 } from '@supabase/ui'
+
+import { checkPermissions, useStore } from 'hooks'
+import Table from 'components/to-be-cleaned/Table'
+
+interface Props {
+ schema: string
+ filterString: string
+ editHook: (hook: any) => void
+ deleteHook: (hook: any) => void
+}
+
+const HookList: FC<Props> = ({
+ schema,
+ filterString,
+ editHook = () => {},
+ deleteHook = () => {},
+}) => {
+ const { meta } = useStore()
+ const hooks = meta.hooks.list()
+ const filteredHooks = hooks.filter((x: any) =>
+ includes(x.name.toLowerCase(), filterString.toLowerCase())
+ )
+ const _hooks = filteredHooks.filter((x: any) => x.schema == schema)
+ const canUpdateWebhook = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, 'triggers')
+
+ function onEdit(trigger: any) {
+ editHook(trigger)
+ }
+
+ function onDelete(trigger: any) {
+ deleteHook(trigger)
+ }
+
+ return (
+ <>
+ {_hooks.map((x: any) => (
+ <Table.tr key={x.id}>
+ <Table.td className="space-x-2">
+ <p>{x.name}</p>
+ </Table.td>
+ <Table.td className="hidden space-x-2 lg:table-cell">
+ <p>{x.table}</p>
+ </Table.td>
+ <Table.td className="hidden xl:table-cell">
+ <p>
+ {x.events.map((event: string) => (
+ <Badge key={event}>{event}</Badge>
+ ))}
+ </p>
+ </Table.td>
+ <Table.td className="hidden xl:table-cell">
+ <p>
+ <Badge>{x.function_args[0]}</Badge>
+ </p>
+ </Table.td>
+ <Table.td className="text-right">
+ <div className="flex justify-end gap-4">
+ {canUpdateWebhook ? (
+ <Dropdown
+ side="left"
+ overlay={
+ <>
+ <Dropdown.Item icon={<IconEdit3 size="tiny" />} onClick={() => onEdit(x)}>
+ Edit hook
+ </Dropdown.Item>
+ <Dropdown.Item
+ icon={<IconTrash stroke="red" size="tiny" />}
+ onClick={() => onDelete(x)}
+ >
+ Delete hook
+ </Dropdown.Item>
+ </>
+ }
+ >
+ <Button as="span" type="default" icon={<IconMoreVertical />} />
+ </Dropdown>
+ ) : (
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger>
+ <Button as="span" disabled type="default" icon={<IconMoreVertical />} />
+ </Tooltip.Trigger>
+ <Tooltip.Content side="left">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'bg-scale-100 rounded py-1 px-2 leading-none shadow',
+ 'border-scale-200 border',
+ ].join(' ')}
+ >
+ <span className="text-scale-1200 text-xs">
+ You need additional permissions to update webhooks
+ </span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Root>
+ )}
+ </div>
+ </Table.td>
+ </Table.tr>
+ ))}
+ </>
+ )
+}
+
+export default observer(HookList)
diff --git a/studio/components/interfaces/Database/Hooks/HooksList/HooksList.tsx b/studio/components/interfaces/Database/Hooks/HooksList/HooksList.tsx
new file mode 100644
index 00000000000..3f3ef740909
--- /dev/null
+++ b/studio/components/interfaces/Database/Hooks/HooksList/HooksList.tsx
@@ -0,0 +1,150 @@
+import { FC } from 'react'
+import { includes, uniqBy, map as lodashMap } from 'lodash'
+import { observer } from 'mobx-react-lite'
+import * as Tooltip from '@radix-ui/react-tooltip'
+import { Button, Input, IconSearch, IconLoader } from '@supabase/ui'
+import { PermissionAction } from '@supabase/shared-types/out/constants'
+
+import { useStore, checkPermissions } from 'hooks'
+import SchemaTable from './SchemaTable'
+import AlphaPreview from 'components/to-be-cleaned/AlphaPreview'
+import ProductEmptyState from 'components/to-be-cleaned/ProductEmptyState'
+
+function isHooksEnabled(schemas: any): boolean {
+ return schemas.some((schema: any) => schema.name === 'supabase_functions')
+}
+
+const HooksList: FC<any> = ({
+ hooksEnabled,
+ filterString,
+ setFilterString = () => {},
+ createHook = () => {},
+ editHook = () => {},
+ deleteHook = () => {},
+ enableHooks = () => {},
+}) => {
+ const { meta } = useStore()
+ const hooks = meta.hooks.list()
+ const schemas = meta.schemas.list()
+ const filteredHooks = hooks.filter((x: any) =>
+ includes(x.name.toLowerCase(), filterString.toLowerCase())
+ )
+ const filteredHookSchemas = lodashMap(uniqBy(filteredHooks, 'schema'), 'schema')
+ const canCreateWebhooks = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, 'triggers')
+
+ const isEnabled = hooksEnabled || isHooksEnabled(schemas)
+
+ if (!isEnabled) {
+ return (
+ <>
+ {schemas.length === 0 ? (
+ <span className="text-scale-1100 px-6 py-4">Checking if hooks are enabled...</span>
+ ) : (
+ <ProductEmptyState
+ title="Hooks"
+ ctaButtonLabel="Enable Hooks"
+ onClickCta={() => enableHooks()}
+ disabled={!canCreateWebhooks}
+ disabledMessage="You need additional permissions to enable hooks"
+ >
+ <AlphaPreview />
+ <p className="text-scale-1100 text-sm">Enable hooks on your project.</p>
+ </ProductEmptyState>
+ )}
+ </>
+ )
+ }
+
+ if (meta.hooks.isLoading) {
+ return (
+ <div className="flex h-full w-full items-center justify-center space-x-2">
+ <IconLoader className="animate-spin" size={14} />
+ <p>Loading hooks...</p>
+ </div>
+ )
+ }
+
+ if (meta.hooks.hasError) {
+ return (
+ <p className="px-6 py-4">
+ <p>Error connecting to API</p>
+ <p>{`${meta.hooks.error?.message ?? 'Unknown error'}`}</p>
+ </p>
+ )
+ }
+
+ return (
+ <>
+ {hooks.length == 0 ? (
+ <div className="flex h-full w-full items-center justify-center">
+ <ProductEmptyState
+ title="Database Webhooks"
+ ctaButtonLabel="Create Webhook"
+ onClickCta={() => createHook()}
+ disabled={!canCreateWebhooks}
+ disabledMessage="You need additional permissions to create webhooks"
+ >
+ <AlphaPreview />
+ <p className="text-scale-1100 text-sm">
+ Database Webhooks can be used to trigger serverless functions or send requests to an
+ HTTP endpoint.
+ </p>
+ </ProductEmptyState>
+ </div>
+ ) : (
+ <div className="w-full space-y-4 py-4">
+ <div className="flex items-center justify-between px-6">
+ <Input
+ placeholder="Filter by name"
+ size="small"
+ icon={<IconSearch size="tiny" />}
+ value={filterString}
+ onChange={(e) => setFilterString(e.target.value)}
+ />
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger>
+ <Button disabled={!canCreateWebhooks} onClick={() => createHook()}>
+ Create a new hook
+ </Button>
+ </Tooltip.Trigger>
+ {!canCreateWebhooks && (
+ <Tooltip.Content side="bottom">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'bg-scale-100 rounded py-1 px-2 leading-none shadow',
+ 'border-scale-200 border',
+ ].join(' ')}
+ >
+ <span className="text-scale-1200 text-xs">
+ You need additional permissions to create webhooks
+ </span>
+ </div>
+ </Tooltip.Content>
+ )}
+ </Tooltip.Root>
+ </div>
+ {filteredHooks.length <= 0 && (
+ <div className="dark:border-dark mx-auto flex max-w-lg items-center justify-center space-x-3 rounded border p-6 shadow-md">
+ <p>No results match your filter query</p>
+ <Button type="outline" onClick={() => setFilterString('')}>
+ Reset filter
+ </Button>
+ </div>
+ )}
+ {filteredHookSchemas.map((schema: any) => (
+ <SchemaTable
+ key={schema}
+ filterString={filterString}
+ schema={schema}
+ editHook={editHook}
+ deleteHook={deleteHook}
+ />
+ ))}
+ </div>
+ )}
+ </>
+ )
+}
+
+export default observer(HooksList)
diff --git a/studio/components/interfaces/Database/Hooks/HooksList/SchemaTable.tsx b/studio/components/interfaces/Database/Hooks/HooksList/SchemaTable.tsx
new file mode 100644
index 00000000000..fd8f1927aac
--- /dev/null
+++ b/studio/components/interfaces/Database/Hooks/HooksList/SchemaTable.tsx
@@ -0,0 +1,60 @@
+import { FC } from 'react'
+import { observer } from 'mobx-react-lite'
+
+import Table from 'components/to-be-cleaned/Table'
+import HookList from './HookList'
+
+interface Props {
+ schema: string
+ filterString: string
+ editHook: (hook: any) => void
+ deleteHook: (hook: any) => void
+}
+
+const SchemaTable: FC<Props> = ({
+ schema,
+ filterString,
+ editHook = () => {},
+ deleteHook = () => {},
+}) => {
+ return (
+ <div key={schema} className="">
+ <div className="sticky top-0 backdrop-blur backdrop-filter">
+ <div className="flex items-baseline space-x-1 py-2 px-6">
+ <h5 className="text-scale-1000">schema</h5>
+ <h4>{schema}</h4>
+ </div>
+ </div>
+ <Table
+ className="table-fixed px-6"
+ head={
+ <>
+ <Table.th key="name" className="space-x-4">
+ Name
+ </Table.th>
+ <Table.th key="table" className="hidden lg:table-cell">
+ Table
+ </Table.th>
+ <Table.th key="events" className="hidden xl:table-cell">
+ Events
+ </Table.th>
+ <Table.th key="webhook" className="hidden xl:table-cell">
+ Webhook
+ </Table.th>
+ <Table.th key="buttons" className="w-1/6"></Table.th>
+ </>
+ }
+ body={
+ <HookList
+ filterString={filterString}
+ schema={schema}
+ editHook={editHook}
+ deleteHook={deleteHook}
+ />
+ }
+ />
+ </div>
+ )
+}
+
+export default observer(SchemaTable)
diff --git a/studio/components/to-be-cleaned/ProductEmptyState.tsx b/studio/components/to-be-cleaned/ProductEmptyState.tsx
index 7e20b80906b..486c5c3eb0e 100644
--- a/studio/components/to-be-cleaned/ProductEmptyState.tsx
+++ b/studio/components/to-be-cleaned/ProductEmptyState.tsx
@@ -37,7 +37,7 @@ const ProductEmptyState: FC<Props> = ({
{ctaButtonLabel}
</Button>
</Tooltip.Trigger>
- {disabled && disabledMessage && (
+ {disabled && disabledMessage.length > 0 && (
<Tooltip.Content side="bottom">
<Tooltip.Arrow className="radix-tooltip-arrow" />
<div
diff --git a/studio/pages/project/[ref]/database/hooks/index.tsx b/studio/pages/project/[ref]/database/hooks/index.tsx
index 0d845254a20..ee4904dfd5b 100644
--- a/studio/pages/project/[ref]/database/hooks/index.tsx
+++ b/studio/pages/project/[ref]/database/hooks/index.tsx
@@ -1,34 +1,17 @@
-import { FC, useEffect, useState } from 'react'
+import { useEffect, useState } from 'react'
import { useRouter } from 'next/router'
-import { includes, uniqBy, map as lodashMap } from 'lodash'
import { observer } from 'mobx-react-lite'
-import {
- Typography,
- Badge,
- Button,
- Dropdown,
- IconMoreVertical,
- IconTrash,
- Input,
- IconSearch,
- IconEdit3,
- IconLoader,
-} from '@supabase/ui'
+import { PermissionAction } from '@supabase/shared-types/out/constants'
-import { useStore } from 'hooks'
+import { NextPageWithLayout } from 'types'
+import { checkPermissions, useStore } from 'hooks'
import { API_URL } from 'lib/constants'
import { post } from 'lib/common/fetch'
import { DatabaseLayout } from 'components/layouts'
-import Table from 'components/to-be-cleaned/Table'
-import AlphaPreview from 'components/to-be-cleaned/AlphaPreview'
-import ProductEmptyState from 'components/to-be-cleaned/ProductEmptyState'
+import NoPermission from 'components/ui/NoPermission'
import CreateHook from 'components/interfaces/Database/Hooks/CreateHook'
import DeleteHook from 'components/interfaces/Database/Hooks/DeleteHook'
-import { NextPageWithLayout } from 'types'
-
-function isHooksEnabled(schemas: any): boolean {
- return schemas.some((schema: any) => schema.name === 'supabase_functions')
-}
+import HooksList from 'components/interfaces/Database/Hooks/HooksList/HooksList'
const HooksPage: NextPageWithLayout = () => {
const { meta, ui } = useStore()
@@ -42,6 +25,8 @@ const HooksPage: NextPageWithLayout = () => {
const [showCreateHookForm, setShowCreateHookForm] = useState<boolean>(false)
const [showDeleteHookForm, setShowDeleteHookForm] = useState<boolean>(false)
+ const canReadWebhooks = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_READ, 'triggers')
+
useEffect(() => {
if (ui.selectedProject) {
fetchHooks()
@@ -79,9 +64,13 @@ const HooksPage: NextPageWithLayout = () => {
setShowDeleteHookForm(true)
}
+ if (!canReadWebhooks) {
+ return <NoPermission isFullPage resourceText="view database webhooks" />
+ }
+
return (
<>
- <TriggersList
+ <HooksList
hooksEnabled={hooksEnabled}
filterString={filterString}
setFilterString={setFilterString}
@@ -107,242 +96,3 @@ const HooksPage: NextPageWithLayout = () => {
HooksPage.getLayout = (page) => <DatabaseLayout title="Hooks">{page}</DatabaseLayout>
export default observer(HooksPage)
-
-const TriggersList: FC<any> = observer(
- ({
- hooksEnabled,
- filterString,
- setFilterString = () => {},
- createHook = () => {},
- editHook = () => {},
- deleteHook = () => {},
- enableHooks = () => {},
- }) => {
- const { meta } = useStore()
- const hooks = meta.hooks.list()
- const schemas = meta.schemas.list()
- const filteredHooks = hooks.filter((x: any) =>
- includes(x.name.toLowerCase(), filterString.toLowerCase())
- )
- const filteredHookSchemas = lodashMap(uniqBy(filteredHooks, 'schema'), 'schema')
-
- const isEnabled = hooksEnabled || isHooksEnabled(schemas)
-
- if (!isEnabled) {
- return (
- <>
- {schemas.length === 0 ? (
- <span className="text-scale-1100 px-6 py-4">Checking if hooks are enabled...</span>
- ) : (
- <ProductEmptyState
- title="Hooks"
- ctaButtonLabel="Enable Hooks"
- onClickCta={() => enableHooks()}
- >
- <AlphaPreview />
- <p className="text-scale-1100 text-sm">Enable hooks on your project.</p>
- </ProductEmptyState>
- )}
- </>
- )
- }
-
- if (meta.hooks.isLoading) {
- return (
- <div className="flex h-full w-full items-center justify-center space-x-2">
- <IconLoader className="animate-spin" size={14} />
- <Typography.Text>Loading hooks...</Typography.Text>
- </div>
- )
- }
-
- if (meta.hooks.hasError) {
- return (
- <Typography.Text className="px-6 py-4" type="danger">
- <p>Error connecting to API</p>
- <p>{`${meta.hooks.error?.message ?? 'Unknown error'}`}</p>
- </Typography.Text>
- )
- }
-
- return (
- <>
- {hooks.length == 0 ? (
- <div className="flex h-full w-full items-center justify-center">
- <ProductEmptyState
- title="Database Webhooks"
- ctaButtonLabel="Create Webhook"
- onClickCta={() => createHook()}
- >
- <AlphaPreview />
- <p className="text-scale-1100 text-sm">
- Database Webhooks can be used to trigger serverless functions or send requests to an
- HTTP endpoint.
- </p>
- </ProductEmptyState>
- </div>
- ) : (
- <div className="w-full space-y-4 py-4">
- <div className="flex items-center justify-between px-6">
- <Input
- placeholder="Filter by name"
- size="small"
- icon={<IconSearch size="tiny" />}
- value={filterString}
- onChange={(e) => setFilterString(e.target.value)}
- />
- <Button onClick={() => createHook()}>Create a new hook</Button>
- </div>
- {filteredHooks.length <= 0 && (
- <div className="dark:border-dark mx-auto flex max-w-lg items-center justify-center space-x-3 rounded border p-6 shadow-md">
- <Typography.Text>No results match your filter query</Typography.Text>
- <Button type="outline" onClick={() => setFilterString('')}>
- Reset filter
- </Button>
- </div>
- )}
- {filteredHookSchemas.map((schema: any) => (
- <SchemaTable
- key={schema}
- filterString={filterString}
- schema={schema}
- editHook={editHook}
- deleteHook={deleteHook}
- />
- ))}
- </div>
- )}
- </>
- )
- }
-)
-
-type SchemaTableProps = {
- schema: string
- filterString: string
- editHook: (hook: any) => void
- deleteHook: (hook: any) => void
-}
-
-const SchemaTable: FC<SchemaTableProps> = observer(
- ({ schema, filterString, editHook = () => {}, deleteHook = () => {} }) => {
- return (
- <div key={schema} className="">
- <div className="sticky top-0 backdrop-blur backdrop-filter">
- <div className="flex items-baseline space-x-1 py-2 px-6">
- <Typography.Title level={5} className="opacity-50">
- schema
- </Typography.Title>
- <Typography.Title level={4}>{schema}</Typography.Title>
- </div>
- </div>
- <Table
- className="table-fixed px-6"
- head={
- <>
- <Table.th key="name" className="space-x-4">
- Name
- </Table.th>
- <Table.th key="table" className="hidden lg:table-cell">
- Table
- </Table.th>
- <Table.th key="events" className="hidden xl:table-cell">
- Events
- </Table.th>
- <Table.th key="webhook" className="hidden xl:table-cell">
- Webhook
- </Table.th>
- <Table.th key="buttons" className="w-1/6"></Table.th>
- </>
- }
- body={
- <HookList
- filterString={filterString}
- schema={schema}
- editHook={editHook}
- deleteHook={deleteHook}
- />
- }
- />
- </div>
- )
- }
-)
-
-type HookListProps = {
- schema: string
- filterString: string
- editHook: (hook: any) => void
- deleteHook: (hook: any) => void
-}
-
-const HookList: FC<HookListProps> = observer(
- ({ schema, filterString, editHook = () => {}, deleteHook = () => {} }) => {
- const { meta } = useStore()
- const hooks = meta.hooks.list()
- const filteredHooks = hooks.filter((x: any) =>
- includes(x.name.toLowerCase(), filterString.toLowerCase())
- )
- const _hooks = filteredHooks.filter((x: any) => x.schema == schema)
-
- function onEdit(trigger: any) {
- editHook(trigger)
- }
-
- function onDelete(trigger: any) {
- deleteHook(trigger)
- }
-
- return (
- <>
- {_hooks.map((x: any) => (
- <Table.tr key={x.id}>
- <Table.td className="space-x-2">
- <Typography.Text>{x.name}</Typography.Text>
- </Table.td>
- <Table.td className="hidden space-x-2 lg:table-cell">
- <Typography.Text>{x.table}</Typography.Text>
- </Table.td>
- <Table.td className="hidden xl:table-cell">
- <Typography.Text>
- {x.events.map((event: string) => (
- <Badge key={event}>{event}</Badge>
- ))}
- </Typography.Text>
- </Table.td>
- <Table.td className="hidden xl:table-cell">
- <Typography.Text>
- <Badge>{x.function_args[0]}</Badge>
- </Typography.Text>
- </Table.td>
- <Table.td className="text-right">
- <div className="flex justify-end gap-4">
- {/* <Link href={`/project/${_pageState.project.ref}/database/hooks/${x.id}/logs`}>
- <Button type="default">Logs</Button>
- </Link> */}
- <Dropdown
- side="left"
- overlay={
- <>
- <Dropdown.Item icon={<IconEdit3 size="tiny" />} onClick={() => onEdit(x)}>
- Edit hook
- </Dropdown.Item>
- <Dropdown.Item
- icon={<IconTrash stroke="red" size="tiny" />}
- onClick={() => onDelete(x)}
- >
- Delete hook
- </Dropdown.Item>
- </>
- }
- >
- <Button as="span" type="default" icon={<IconMoreVertical />} />
- </Dropdown>
- </div>
- </Table.td>
- </Table.tr>
- ))}
- </>
- )
- }
-)
diff --git a/studio/stores/pgmeta/MetaStore.ts b/studio/stores/pgmeta/MetaStore.ts
index 446efdd4c64..39bcdc23dac 100644
--- a/studio/stores/pgmeta/MetaStore.ts
+++ b/studio/stores/pgmeta/MetaStore.ts
@@ -143,6 +143,7 @@ export default class MetaStore implements IMetaStore {
'information_schema',
'net',
'pg_catalog',
+ 'pgsodium',
'pgbouncer',
'realtime',
'storage',