The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Add abac for table editor
Joshen Lim7a00fc19eaf2
diff --git a/studio/components/interfaces/TableGridEditor/EmptyState.tsx b/studio/components/interfaces/TableGridEditor/EmptyState.tsxindex 92d91dcb02a..8c9a1486b09 100644--- a/studio/components/interfaces/TableGridEditor/EmptyState.tsx+++ b/studio/components/interfaces/TableGridEditor/EmptyState.tsx@@ -1,8 +1,9 @@import { FC } from 'react'-import { Button } from '@supabase/ui'+import { observer } from 'mobx-react-lite'import { PostgresTable } from '@supabase/postgres-meta'-import { checkPermissions, useStore } from 'hooks'import { PermissionAction } from '@supabase/shared-types/out/constants'+import { checkPermissions, useStore } from 'hooks'+import ProductEmptyState from 'components/to-be-cleaned/ProductEmptyState'interface Props {selectedSchema: string@@ -12,28 +13,36 @@ interface Props {const EmptyState: FC<Props> = ({ selectedSchema, onAddTable }) => {const { meta } = useStore()const tables = meta.tables.list((table: PostgresTable) => table.schema === selectedSchema)-- const renderNoTablesCTA = () => {- return (- <div className="flex flex-col items-center justify-center space-y-4">- <p className="text-sm">There are no tables available in this schema</p>- {selectedSchema === 'public' && <Button onClick={onAddTable}>Create a new table</Button>}- </div>- )- }+ const isProtectedSchema = meta.excludedSchemas.includes(selectedSchema)+ const canCreateTables =+ !isProtectedSchema && checkPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, 'tables')return (<div className="w-full h-full flex items-center justify-center">{tables.length === 0 ? (- renderNoTablesCTA()+ <ProductEmptyState+ title="Table Editor"+ ctaButtonLabel={canCreateTables ? 'Create a new table' : undefined}+ onClickCta={canCreateTables ? onAddTable : undefined}+ >+ <p className="text-sm text-scale-1100">There are no tables available in this schema.</p>+ </ProductEmptyState>) : (<div className="flex flex-col items-center space-y-4">- <p className="text-sm">Select a table or create a new one</p>- <Button onClick={onAddTable}>Create a new table</Button>+ <ProductEmptyState+ title="Table Editor"+ ctaButtonLabel={canCreateTables ? 'Create a new table' : undefined}+ onClickCta={canCreateTables ? onAddTable : undefined}+ >+ <p className="text-sm text-scale-1100">+ Select a table from the navigation panel on the left to view its data+ {canCreateTables && ', or create a new one.'}+ </p>+ </ProductEmptyState></div>)}</div>)}-export default EmptyState+export default observer(EmptyState)diff --git a/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx b/studio/components/interfaces/TableGridEditor/TableGridEditor.tsxindex 3ebd91f940c..65d738dc56b 100644--- a/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx+++ b/studio/components/interfaces/TableGridEditor/TableGridEditor.tsx@@ -2,8 +2,8 @@ import { FC, useRef } from 'react'import { observer } from 'mobx-react-lite'import { useRouter } from 'next/router'import { find, isUndefined } from 'lodash'-import { PermissionAction } from '@supabase/shared-types/out/constants'import { PostgresColumn, PostgresTable } from '@supabase/postgres-meta'+import { PermissionAction } from '@supabase/shared-types/out/constants'import { checkPermissions, useStore } from 'hooks'import GridHeaderActions from './GridHeaderActions'@@ -11,7 +11,6 @@ import NotFoundState from './NotFoundState'import SidePanelEditor from './SidePanelEditor'import { SchemaView } from 'components/layouts/TableEditorLayout/TableEditorLayout.types'import { Dictionary, parseSupaTable, SupabaseGrid, SupabaseGridRef } from 'components/grid'-import NoPermission from 'components/ui/NoPermission'interface Props {/** Theme for the editor */@@ -60,32 +59,17 @@ const TableGridEditor: FC<Props> = ({const gridRef = useRef<SupabaseGridRef>(null)const projectRef = ui.selectedProject?.ref- // Permissions scaffolding- const canRead = false- const canInsert = false- const canUpdate = false- const canDelete = false-- // [Joshen] When we get to this, double check again what are the actions- // const canUpdate = checkPermissions(PermissionAction.TENANT_SQL_UPDATE, String(tableId))- // const canAdminWrite = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, String(tableId))- // const canInsert = checkPermissions(PermissionAction.CREATE, String(tableId))- // const canEdit = canAdminWrite && canInsert && canUpdate-if (isUndefined(selectedTable)) {return <NotFoundState id={Number(router.query.id)} />}- // if (!canRead) {- // return <NoPermission isFullPage resourceText={`access the table "${selectedTable.name}"`} />- // }-const tableId = selectedTable?.id// @ts-ignoreconst schema = meta.schemas.list().find((schema) => schema.name === selectedSchema)const isViewSelected = Object.keys(selectedTable).length === 2const isLocked = meta.excludedSchemas.includes(schema?.name ?? '')+ const canUpdateTables = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, 'tables')const gridTable = !isViewSelected? parseSupaTable({@@ -157,7 +141,7 @@ const TableGridEditor: FC<Props> = ({theme={theme}gridProps={{ height: '100%' }}storageRef={projectRef}- editable={!isViewSelected && !isLocked}+ editable={canUpdateTables && !isViewSelected && !isLocked}schema={selectedTable.schema}table={gridTable}headerActions={diff --git a/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx b/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsxindex aa6863c6300..59bf9ae1777 100644--- a/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx+++ b/studio/components/layouts/TableEditorLayout/TableEditorLayout.tsx@@ -1,12 +1,14 @@import { FC, ReactNode, useState, useEffect } from 'react'+import { isUndefined } from 'lodash'import { observer } from 'mobx-react-lite'import { PostgresTable } from '@supabase/postgres-meta'+import { PermissionAction } from '@supabase/shared-types/out/constants'-import { useStore } from 'hooks'+import { checkPermissions, useStore } from 'hooks'import Error from 'components/ui/Error'import ProjectLayout from '../ProjectLayout/ProjectLayout'import TableEditorMenu from './TableEditorMenu'-import { isUndefined } from 'lodash'+import NoPermission from 'components/ui/NoPermission'interface Props {selectedSchema?: string@@ -31,6 +33,7 @@ const TableEditorLayout: FC<Props> = ({const { isInitialized, isLoading, error } = meta.tablesconst [loaded, setLoaded] = useState<boolean>(isInitialized)+ const canReadTables = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_READ, 'tables')useEffect(() => {if (ui.selectedProject) {@@ -52,6 +55,14 @@ const TableEditorLayout: FC<Props> = ({}}, [isLoading])+ if (!canReadTables) {+ return (+ <ProjectLayout>+ <NoPermission isFullPage resourceText="view tables from this project" />+ </ProjectLayout>+ )+ }+if (error) {return (<ProjectLayout>diff --git a/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx b/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsxindex f57a4038000..3de017cf5c3 100644--- a/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx+++ b/studio/components/layouts/TableEditorLayout/TableEditorMenu.tsx@@ -19,13 +19,14 @@ import {Menu,Typography,} from '@supabase/ui'+import * as Tooltip from '@radix-ui/react-tooltip'+import { PermissionAction } from '@supabase/shared-types/out/constants'import { PostgresSchema, PostgresTable } from '@supabase/postgres-meta'import Base64 from 'lib/base64'import { checkPermissions, useStore } from 'hooks'import { SchemaView } from './TableEditorLayout.types'import ProductMenuItem from 'components/ui/ProductMenu/ProductMenuItem'-import { PermissionAction } from '@supabase/shared-types/out/constants'interface Props {selectedSchema?: string@@ -56,6 +57,7 @@ const TableEditorMenu: FC<Props> = ({// @ts-ignoreconst schema = schemas.find((schema) => schema.name === selectedSchema)+ const canCreateTables = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, 'tables')const [searchText, setSearchText] = useState<string>('')const [schemaViews, setSchemaViews] = useState<SchemaView[]>([])@@ -122,11 +124,11 @@ const TableEditorMenu: FC<Props> = ({<Listbox.Option disabled key="normal-schemas" value="normal-schemas" label="Schemas"><p className="text-sm">Schemas</p></Listbox.Option>+ {/* @ts-ignore */}{openSchemas.map((schema) => (<Listbox.Optionkey={schema.id}value={schema.name}- // @ts-ignorelabel={schema.name}addOnBefore={() => <span className="text-scale-900">schema</span>}>@@ -145,7 +147,6 @@ const TableEditorMenu: FC<Props> = ({<Listbox.Optionkey={schema.id}value={schema.name}- // @ts-ignorelabel={schema.name}addOnBefore={() => <span className="text-scale-900">schema</span>}>@@ -160,20 +161,41 @@ const TableEditorMenu: FC<Props> = ({{!isLocked && (<div className="px-3">{/* Add new table button */}- <Button- block- size="tiny"- icon={- <div className="text-scale-900">- <IconEdit size={14} strokeWidth={1.5} />- </div>- }- type="default"- style={{ justifyContent: 'start' }}- onClick={onAddTable}- >- New table- </Button>+ <Tooltip.Root delayDuration={0}>+ <Tooltip.Trigger className="w-full">+ <Button+ block+ as="span"+ disabled={!canCreateTables}+ size="tiny"+ icon={+ <div className="text-scale-900">+ <IconEdit size={14} strokeWidth={1.5} />+ </div>+ }+ type="default"+ style={{ justifyContent: 'start' }}+ onClick={onAddTable}+ >+ New table+ </Button>+ </Tooltip.Trigger>+ {!canCreateTables && (+ <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 tables+ </span>+ </div>+ </Tooltip.Content>+ )}+ </Tooltip.Root></div>)}{/* Table search input */}diff --git a/studio/components/to-be-cleaned/ProductEmptyState.tsx b/studio/components/to-be-cleaned/ProductEmptyState.tsxindex 7e20b80906b..bbc91439743 100644--- a/studio/components/to-be-cleaned/ProductEmptyState.tsx+++ b/studio/components/to-be-cleaned/ProductEmptyState.tsx@@ -22,47 +22,55 @@ const ProductEmptyState: FC<Props> = ({onClickCta = () => {},disabled = false,disabledMessage = '',-}) => (- <div className="flex items-center justify-center w-full h-full">- <div className="flex space-x-4 p-6 rounded border border-panel-border-light dark:border-panel-border-dark bg-panel-body-light dark:bg-panel-body-dark shadow-md">- {/* A graphic can probably be placed here as a sibling to the div below*/}- <div className="flex flex-col">- <div className="w-80 space-y-4">- <Typography.Title level={5}>{title}</Typography.Title>- <div className="flex flex-col space-y-2">{children}</div>- <div className="flex items-center space-x-2">- <Tooltip.Root delayDuration={0}>- <Tooltip.Trigger>- <Button type="primary" onClick={onClickCta} disabled={disabled}>- {ctaButtonLabel}- </Button>- </Tooltip.Trigger>- {disabled && disabledMessage && (- <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">{disabledMessage}</span>- </div>- </Tooltip.Content>- )}- </Tooltip.Root>- {infoButtonUrl && infoButtonLabel ? (- <Button type="default">- <a target="_blank" href={infoButtonUrl}>- {infoButtonLabel}- </a>- </Button>- ) : null}+}) => {+ const hasAction = (ctaButtonLabel && onClickCta) || (infoButtonUrl && infoButtonLabel)++ return (+ <div className="flex items-center justify-center w-full h-full">+ <div className="flex space-x-4 p-6 rounded border border-panel-border-light dark:border-panel-border-dark bg-panel-body-light dark:bg-panel-body-dark shadow-md">+ {/* A graphic can probably be placed here as a sibling to the div below*/}+ <div className="flex flex-col">+ <div className="w-80 space-y-4">+ <Typography.Title level={5}>{title}</Typography.Title>+ <div className="flex flex-col space-y-2">{children}</div>+ {hasAction && (+ <div className="flex items-center space-x-2">+ {ctaButtonLabel && onClickCta && (+ <Tooltip.Root delayDuration={0}>+ <Tooltip.Trigger>+ <Button type="primary" onClick={onClickCta} disabled={disabled}>+ {ctaButtonLabel}+ </Button>+ </Tooltip.Trigger>+ {disabled && disabledMessage && (+ <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">{disabledMessage}</span>+ </div>+ </Tooltip.Content>+ )}+ </Tooltip.Root>+ )}+ {infoButtonUrl && infoButtonLabel ? (+ <Button type="default">+ <a target="_blank" href={infoButtonUrl}>+ {infoButtonLabel}+ </a>+ </Button>+ ) : null}+ </div>+ )}</div></div></div></div>- </div>-)+ )+}export default ProductEmptyStatediff --git a/studio/pages/project/[ref]/editor/[id].tsx b/studio/pages/project/[ref]/editor/[id].tsxindex 77d1b2591bb..799a6c06870 100644--- a/studio/pages/project/[ref]/editor/[id].tsx+++ b/studio/pages/project/[ref]/editor/[id].tsx@@ -3,16 +3,16 @@ import { NextPage } from 'next'import { useRouter } from 'next/router'import { observer } from 'mobx-react-lite'import { isUndefined, isNaN } from 'lodash'-import { Dictionary } from 'components/grid'+import { Modal } from '@supabase/ui'import { PostgresTable, PostgresColumn } from '@supabase/postgres-meta'import Base64 from 'lib/base64'import { tryParseJson } from 'lib/helpers'import { useStore, withAuth } from 'hooks'+import { Dictionary } from 'components/grid'import { TableEditorLayout } from 'components/layouts'import { TableGridEditor } from 'components/interfaces'import ConfirmationModal from 'components/ui/ConfirmationModal'-import { Modal } from '@supabase/ui'const TableEditorPage: NextPage = () => {const router = useRouter()diff --git a/studio/pages/project/[ref]/editor/index.tsx b/studio/pages/project/[ref]/editor/index.tsxindex 406a8316992..481c332e059 100644--- a/studio/pages/project/[ref]/editor/index.tsx+++ b/studio/pages/project/[ref]/editor/index.tsx@@ -1,5 +1,6 @@import { useState } from 'react'import { NextPage } from 'next'+import router from 'next/router'import { observer } from 'mobx-react-lite'import { isUndefined } from 'lodash'import { PostgresTable } from '@supabase/postgres-meta'@@ -8,8 +9,6 @@ import { useStore, withAuth } from 'hooks'import { TableEditorLayout } from 'components/layouts'import { EmptyState, SidePanelEditor } from 'components/interfaces/TableGridEditor'import ConfirmationModal from 'components/ui/ConfirmationModal'-import router from 'next/router'-import { SidePanel } from '@supabase/ui'const Editor: NextPage = () => {const { meta, ui } = useStore()