Skip to content

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

Refactor functions and add abac

Joshen Lim96105aa9c9d9
diff --git a/studio/components/interfaces/Database/Functions/FunctionsList/FunctionList.tsx b/studio/components/interfaces/Database/Functions/FunctionsList/FunctionList.tsx
new file mode 100644
index 00000000000..9bc300c68bc
--- /dev/null
+++ b/studio/components/interfaces/Database/Functions/FunctionsList/FunctionList.tsx
@@ -0,0 +1,122 @@
+import { FC } from 'react'
+import { useRouter } from 'next/router'
+import { includes } from 'lodash'
+import {
+ Button,
+ Dropdown,
+ IconEdit3,
+ IconFileText,
+ IconMoreVertical,
+ IconTrash,
+} from '@supabase/ui'
+import { observer } from 'mobx-react-lite'
+import * as Tooltip from '@radix-ui/react-tooltip'
+import { PermissionAction } from '@supabase/shared-types/out/constants'
+
+import { checkPermissions, useStore } from 'hooks'
+import Table from 'components/to-be-cleaned/Table'
+
+interface Props {
+ schema: string
+ filterString: string
+ editFunction: (fn: any) => void
+ deleteFunction: (fn: any) => void
+}
+
+const FunctionList: FC<Props> = ({
+ schema,
+ filterString,
+ editFunction = () => {},
+ deleteFunction = () => {},
+}) => {
+ const router = useRouter()
+ const { ui, meta } = useStore()
+ const functions = meta.functions.list((fn: any) => !meta.excludedSchemas.includes(fn.schema))
+ const filteredFunctions = functions.filter((x: any) =>
+ includes(x.name.toLowerCase(), filterString.toLowerCase())
+ )
+ const _functions = filteredFunctions.filter((x) => x.schema == schema)
+ const isApiDocumentAvailable = schema == 'public'
+ const projectRef = ui.selectedProject?.ref
+
+ const canUpdateFunctions = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, 'functions')
+
+ function onEdit(func: any) {
+ editFunction(func)
+ }
+
+ function onDelete(func: any) {
+ deleteFunction(func)
+ }
+
+ return (
+ <>
+ {_functions.map((x) => (
+ <Table.tr key={x.id}>
+ <Table.td>
+ <p>{x.name}</p>
+ </Table.td>
+ <Table.td className="hidden md:table-cell md:overflow-auto">
+ <p>{x.argument_types}</p>
+ </Table.td>
+ <Table.td className="hidden lg:table-cell">
+ <p>{x.return_type}</p>
+ </Table.td>
+ <Table.td className="text-right">
+ <div className="flex items-center justify-end">
+ {canUpdateFunctions ? (
+ <Dropdown
+ side="left"
+ overlay={
+ <>
+ {isApiDocumentAvailable && (
+ <Dropdown.Item
+ icon={<IconFileText size="tiny" />}
+ onClick={() => router.push(`/project/${projectRef}/api?rpc=${x.name}`)}
+ >
+ Client API docs
+ </Dropdown.Item>
+ )}
+ <Dropdown.Item icon={<IconEdit3 size="tiny" />} onClick={() => onEdit(x)}>
+ Edit function
+ </Dropdown.Item>
+ <Dropdown.Item
+ icon={<IconTrash stroke="red" size="tiny" />}
+ onClick={() => onDelete(x)}
+ >
+ Delete function
+ </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 functions
+ </span>
+ </div>
+ </Tooltip.Content>
+ </Tooltip.Root>
+ )}
+ </div>
+ </Table.td>
+ </Table.tr>
+ ))}
+ </>
+ )
+}
+
+export default observer(FunctionList)
diff --git a/studio/components/interfaces/Database/Functions/FunctionsList/FunctionsList.tsx b/studio/components/interfaces/Database/Functions/FunctionsList/FunctionsList.tsx
new file mode 100644
index 00000000000..4cad4a76591
--- /dev/null
+++ b/studio/components/interfaces/Database/Functions/FunctionsList/FunctionsList.tsx
@@ -0,0 +1,121 @@
+import { FC } from 'react'
+import { uniqBy, map as lodashMap, includes } from 'lodash'
+import { Button, IconSearch, IconLoader, Input } from '@supabase/ui'
+import { observer } from 'mobx-react-lite'
+import * as Tooltip from '@radix-ui/react-tooltip'
+import { PermissionAction } from '@supabase/shared-types/out/constants'
+
+import { checkPermissions, useStore } from 'hooks'
+import AlphaPreview from 'components/to-be-cleaned/AlphaPreview'
+import ProductEmptyState from 'components/to-be-cleaned/ProductEmptyState'
+import SchemaTable from './SchemaTable'
+
+const FunctionsList: FC<any> = ({
+ filterString,
+ setFilterString = () => {},
+ createFunction = () => {},
+ editFunction = () => {},
+ deleteFunction = () => {},
+}) => {
+ const { meta } = useStore()
+ const functions = meta.functions.list((fn: any) => !meta.excludedSchemas.includes(fn.schema))
+ const filteredFunctions = functions.filter((x: any) =>
+ includes(x.name.toLowerCase(), filterString.toLowerCase())
+ )
+ const filteredFunctionSchemas = lodashMap(uniqBy(filteredFunctions, 'schema'), 'schema')
+ const canCreateFunctions = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, 'functions')
+
+ if (meta.functions.isLoading) {
+ return (
+ <div className="flex h-full w-full items-center justify-center space-x-2">
+ <IconLoader className="animate-spin" size={14} />
+ <p>Loading functions...</p>
+ </div>
+ )
+ }
+
+ if (meta.functions.hasError) {
+ return (
+ <p className="px-6 py-4">
+ <p>Error connecting to API</p>
+ <p>{`${meta.functions.error?.message ?? 'Unknown error'}`}</p>
+ </p>
+ )
+ }
+
+ return (
+ <>
+ {functions.length == 0 ? (
+ <div className="flex h-full w-full items-center justify-center">
+ <ProductEmptyState
+ title="Functions"
+ ctaButtonLabel="Create a new function"
+ onClickCta={() => createFunction()}
+ >
+ <AlphaPreview />
+ <p className="text-scale-1100 text-sm">
+ PostgreSQL functions, also known as stored procedures, is a set of SQL and procedural
+ commands such as declarations, assignments, loops, flow-of-control, etc.
+ </p>
+ <p className="text-scale-1100 text-sm">
+ It's stored on the database server and can be invoked using the SQL interface.
+ </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={!canCreateFunctions} onClick={() => createFunction()}>
+ Create a new function
+ </Button>
+ </Tooltip.Trigger>
+ {!canCreateFunctions && (
+ <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 functions
+ </span>
+ </div>
+ </Tooltip.Content>
+ )}
+ </Tooltip.Root>
+ </div>
+ {filteredFunctions.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>
+ )}
+ {filteredFunctionSchemas.map((schema: any) => (
+ <SchemaTable
+ key={schema}
+ filterString={filterString}
+ schema={schema}
+ editFunction={editFunction}
+ deleteFunction={deleteFunction}
+ />
+ ))}
+ </div>
+ )}
+ </>
+ )
+}
+
+export default observer(FunctionsList)
diff --git a/studio/components/interfaces/Database/Functions/FunctionsList/SchemaTable.tsx b/studio/components/interfaces/Database/Functions/FunctionsList/SchemaTable.tsx
new file mode 100644
index 00000000000..aac4af5bda6
--- /dev/null
+++ b/studio/components/interfaces/Database/Functions/FunctionsList/SchemaTable.tsx
@@ -0,0 +1,57 @@
+import { FC } from 'react'
+import { observer } from 'mobx-react-lite'
+
+import Table from 'components/to-be-cleaned/Table'
+import FunctionList from './FunctionList'
+
+interface Props {
+ schema: string
+ filterString: string
+ editFunction: (fn: any) => void
+ deleteFunction: (fn: any) => void
+}
+
+const SchemaTable: FC<Props> = ({
+ schema,
+ filterString,
+ editFunction = () => {},
+ deleteFunction = () => {},
+}) => {
+ 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="w-1/3 space-x-4">
+ Name
+ </Table.th>
+ <Table.th key="arguments" className="hidden md:table-cell">
+ Arguments
+ </Table.th>
+ <Table.th key="return_type" className="hidden lg:table-cell">
+ Return type
+ </Table.th>
+ <Table.th key="buttons" className="w-1/6"></Table.th>
+ </>
+ }
+ body={
+ <FunctionList
+ schema={schema}
+ filterString={filterString}
+ editFunction={editFunction}
+ deleteFunction={deleteFunction}
+ />
+ }
+ />
+ </div>
+ )
+}
+
+export default observer(SchemaTable)
diff --git a/studio/pages/project/[ref]/database/functions.tsx b/studio/pages/project/[ref]/database/functions.tsx
index 313d2d85c37..86a33229ebb 100644
--- a/studio/pages/project/[ref]/database/functions.tsx
+++ b/studio/pages/project/[ref]/database/functions.tsx
@@ -1,27 +1,13 @@
-import { FC, useState, useEffect } from 'react'
-import { useRouter } from 'next/router'
-import { uniqBy, map as lodashMap, includes } from 'lodash'
-import {
- Button,
- Dropdown,
- IconEdit3,
- IconFileText,
- IconMoreVertical,
- IconSearch,
- IconTrash,
- IconLoader,
- Input,
- Typography,
-} from '@supabase/ui'
+import { useState, useEffect } from 'react'
import { observer } from 'mobx-react-lite'
+import { PermissionAction } from '@supabase/shared-types/out/constants'
-import { useStore } from 'hooks'
+import { useStore, checkPermissions } from 'hooks'
import { DatabaseLayout } from 'components/layouts'
import { CreateFunction, DeleteFunction } from 'components/interfaces/Database'
-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 { NextPageWithLayout } from 'types'
+import NoPermission from 'components/ui/NoPermission'
+import FunctionsList from 'components/interfaces/Database/Functions/FunctionsList/FunctionsList'
const FunctionsPage: NextPageWithLayout = () => {
const { meta, ui } = useStore()
@@ -30,6 +16,8 @@ const FunctionsPage: NextPageWithLayout = () => {
const [showCreateFunctionForm, setShowCreateFunctionForm] = useState<boolean>(false)
const [showDeleteFunctionForm, setShowDeleteFunctionForm] = useState<boolean>(false)
+ const canReadFunctions = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_READ, 'functions')
+
useEffect(() => {
if (ui.selectedProject) {
fetchFunctions()
@@ -55,9 +43,13 @@ const FunctionsPage: NextPageWithLayout = () => {
setShowDeleteFunctionForm(true)
}
+ if (!canReadFunctions) {
+ return <NoPermission isFullPage resourceText="view database functions" />
+ }
+
return (
<>
- <PageLayout
+ <FunctionsList
filterString={filterString}
setFilterString={setFilterString}
createFunction={createFunction}
@@ -81,215 +73,3 @@ const FunctionsPage: NextPageWithLayout = () => {
FunctionsPage.getLayout = (page) => <DatabaseLayout title="Database">{page}</DatabaseLayout>
export default observer(FunctionsPage)
-
-const PageLayout: FC<any> = observer(
- ({
- filterString,
- setFilterString = () => {},
- createFunction = () => {},
- editFunction = () => {},
- deleteFunction = () => {},
- }) => {
- const { meta } = useStore()
- const functions = meta.functions.list((fn: any) => !meta.excludedSchemas.includes(fn.schema))
- const filteredFunctions = functions.filter((x: any) =>
- includes(x.name.toLowerCase(), filterString.toLowerCase())
- )
- const filteredFunctionSchemas = lodashMap(uniqBy(filteredFunctions, 'schema'), 'schema')
-
- if (meta.functions.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 functions...</Typography.Text>
- </div>
- )
- }
-
- if (meta.functions.hasError) {
- return (
- <Typography.Text className="px-6 py-4" type="danger">
- <p>Error connecting to API</p>
- <p>{`${meta.functions.error?.message ?? 'Unknown error'}`}</p>
- </Typography.Text>
- )
- }
-
- return (
- <>
- {functions.length == 0 ? (
- <div className="flex h-full w-full items-center justify-center">
- <ProductEmptyState
- title="Functions"
- ctaButtonLabel="Create a new function"
- onClickCta={() => createFunction()}
- >
- <AlphaPreview />
- <p className="text-scale-1100 text-sm">
- PostgreSQL functions, also known as stored procedures, is a set of SQL and
- procedural commands such as declarations, assignments, loops, flow-of-control, etc.
- </p>
- <p className="text-scale-1100 text-sm">
- It's stored on the database server and can be invoked using the SQL interface.
- </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={() => createFunction()}>Create a new function</Button>
- </div>
- {filteredFunctions.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>
- )}
- {filteredFunctionSchemas.map((schema: any) => (
- <SchemaTable
- key={schema}
- filterString={filterString}
- schema={schema}
- editFunction={editFunction}
- deleteFunction={deleteFunction}
- />
- ))}
- </div>
- )}
- </>
- )
- }
-)
-
-type SchemaTableProps = {
- schema: string
- filterString: string
- editFunction: (fn: any) => void
- deleteFunction: (fn: any) => void
-}
-
-const SchemaTable: FC<SchemaTableProps> = observer(
- ({ schema, filterString, editFunction = () => {}, deleteFunction = () => {} }) => {
- 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="w-1/3 space-x-4">
- Name
- </Table.th>
- <Table.th key="arguments" className="hidden md:table-cell">
- Arguments
- </Table.th>
- <Table.th key="return_type" className="hidden lg:table-cell">
- Return type
- </Table.th>
- <Table.th key="buttons" className="w-1/6"></Table.th>
- </>
- }
- body={
- <FunctionList
- schema={schema}
- filterString={filterString}
- editFunction={editFunction}
- deleteFunction={deleteFunction}
- />
- }
- />
- </div>
- )
- }
-)
-
-type FunctionListProps = {
- schema: string
- filterString: string
- editFunction: (fn: any) => void
- deleteFunction: (fn: any) => void
-}
-
-const FunctionList: FC<FunctionListProps> = observer(
- ({ schema, filterString, editFunction = () => {}, deleteFunction = () => {} }) => {
- const router = useRouter()
- const { ui, meta } = useStore()
- const functions = meta.functions.list((fn: any) => !meta.excludedSchemas.includes(fn.schema))
- const filteredFunctions = functions.filter((x: any) =>
- includes(x.name.toLowerCase(), filterString.toLowerCase())
- )
- const _functions = filteredFunctions.filter((x) => x.schema == schema)
- const isApiDocumentAvailable = schema == 'public'
- const projectRef = ui.selectedProject?.ref
-
- function onEdit(func: any) {
- editFunction(func)
- }
-
- function onDelete(func: any) {
- deleteFunction(func)
- }
-
- return (
- <>
- {_functions.map((x) => (
- <Table.tr key={x.id}>
- <Table.td>
- <Typography.Text>{x.name}</Typography.Text>
- </Table.td>
- <Table.td className="hidden md:table-cell md:overflow-auto">
- <Typography.Text>{x.argument_types}</Typography.Text>
- </Table.td>
- <Table.td className="hidden lg:table-cell">
- <Typography.Text>{x.return_type}</Typography.Text>
- </Table.td>
- <Table.td className="text-right">
- <Dropdown
- side="left"
- overlay={
- <>
- {isApiDocumentAvailable && (
- <Dropdown.Item
- icon={<IconFileText size="tiny" />}
- onClick={() => router.push(`/project/${projectRef}/api?rpc=${x.name}`)}
- >
- Client API docs
- </Dropdown.Item>
- )}
- <Dropdown.Item icon={<IconEdit3 size="tiny" />} onClick={() => onEdit(x)}>
- Edit function
- </Dropdown.Item>
- <Dropdown.Item
- icon={<IconTrash stroke="red" size="tiny" />}
- onClick={() => onDelete(x)}
- >
- Delete function
- </Dropdown.Item>
- </>
- }
- >
- <Button as="span" type="default" icon={<IconMoreVertical />} />
- </Dropdown>
- </Table.td>
- </Table.tr>
- ))}
- </>
- )
- }
-)