Skip to content

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

Allow selection of schema in auth policies page

Joshen Lim64fd4034b33f
diff --git a/studio/components/interfaces/Auth/Policies/Policies.tsx b/studio/components/interfaces/Auth/Policies/Policies.tsx
index d734e40500d..81669a1a6f5 100644
--- a/studio/components/interfaces/Auth/Policies/Policies.tsx
+++ b/studio/components/interfaces/Auth/Policies/Policies.tsx
@@ -5,20 +5,20 @@ import { useRouter } from 'next/router'
import { observer } from 'mobx-react-lite'
import { useParams, useStore } from 'hooks'
import { PolicyEditorModal, PolicyTableRow } from 'components/interfaces/Auth/Policies'
-import { PostgresRole } from '@supabase/postgres-meta'
+import { PostgresRole, PostgresTable } from '@supabase/postgres-meta'
import ConfirmModal from 'components/ui/Dialogs/ConfirmDialog'
-import NoTableState from 'components/ui/States/NoTableState'
import NoSearchResults from 'components/to-be-cleaned/NoSearchResults'
import ProductEmptyState from 'components/to-be-cleaned/ProductEmptyState'
import InformationBox from 'components/ui/InformationBox'
interface Props {
- tables: any[]
- hasPublicTables: boolean
+ tables: PostgresTable[]
+ hasTables: boolean
+ isLocked: boolean
}
-const Policies: FC<Props> = ({ tables, hasPublicTables }) => {
+const Policies: FC<Props> = ({ tables, hasTables, isLocked }) => {
const router = useRouter()
const { ref } = useParams()
@@ -116,9 +116,6 @@ const Policies: FC<Props> = ({ tables, hasPublicTables }) => {
closeConfirmModal()
}
- if (!hasPublicTables)
- return <NoTableState message="Create a table first before writing a policy" />
-
return (
<>
{tables.length > 0 ? (
@@ -126,6 +123,7 @@ const Policies: FC<Props> = ({ tables, hasPublicTables }) => {
<section key={table.id}>
<PolicyTableRow
table={table}
+ isLocked={isLocked}
onSelectToggleRLS={onSelectToggleRLS}
onSelectCreatePolicy={onSelectCreatePolicy}
onSelectEditPolicy={onSelectEditPolicy}
@@ -133,7 +131,7 @@ const Policies: FC<Props> = ({ tables, hasPublicTables }) => {
/>
</section>
))
- ) : hasPublicTables ? (
+ ) : hasTables ? (
<NoSearchResults />
) : (
<div className="flex-grow">
@@ -163,8 +161,8 @@ const Policies: FC<Props> = ({ tables, hasPublicTables }) => {
}
/>
<p className="text-sm text-scale-1100">
- A table within the public schema is required before you can create a Row-Level
- Security policy.
+ A table within this schema is required before you can create a Row-Level Security
+ policy.
</p>
</div>
</ProductEmptyState>
diff --git a/studio/components/interfaces/Auth/Policies/PolicyTableRow/PolicyTableRowHeader.tsx b/studio/components/interfaces/Auth/Policies/PolicyTableRow/PolicyTableRowHeader.tsx
index e985b6af5db..1e2d975e7a4 100644
--- a/studio/components/interfaces/Auth/Policies/PolicyTableRow/PolicyTableRowHeader.tsx
+++ b/studio/components/interfaces/Auth/Policies/PolicyTableRow/PolicyTableRowHeader.tsx
@@ -9,12 +9,14 @@ import { checkPermissions } from 'hooks'
interface Props {
table: PostgresTable
+ isLocked: boolean
onSelectToggleRLS: (table: PostgresTable) => void
onSelectCreatePolicy: (table: PostgresTable) => void
}
const PolicyTableRowHeader: FC<Props> = ({
table,
+ isLocked,
onSelectToggleRLS = () => {},
onSelectCreatePolicy = () => {},
}) => {
@@ -35,63 +37,65 @@ const PolicyTableRowHeader: FC<Props> = ({
{table.rls_enabled ? 'RLS enabled' : 'RLS disabled'}
</Badge>
</div>
- <div className="flex-1">
- <div className="flex flex-row-reverse">
- <Tooltip.Root delayDuration={0}>
- <Tooltip.Trigger>
- <Button
- type="outline"
- disabled={!canCreatePolicies}
- className="ml-2"
- onClick={() => onSelectCreatePolicy(table)}
- >
- New Policy
- </Button>
- </Tooltip.Trigger>
- {!canCreatePolicies && (
- <Tooltip.Content side="bottom">
- <Tooltip.Arrow className="radix-tooltip-arrow" />
- <div
- className={[
- 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
- 'border border-scale-200',
- ].join(' ')}
+ {!isLocked && (
+ <div className="flex-1">
+ <div className="flex flex-row-reverse">
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger>
+ <Button
+ type="outline"
+ disabled={!canCreatePolicies}
+ className="ml-2"
+ onClick={() => onSelectCreatePolicy(table)}
>
- <span className="text-xs text-scale-1200">
- You need additional permissions to create RLS policies
- </span>
- </div>
- </Tooltip.Content>
- )}
- </Tooltip.Root>
- <Tooltip.Root delayDuration={0}>
- <Tooltip.Trigger>
- <Button
- type="default"
- disabled={!canToggleRLS}
- onClick={() => onSelectToggleRLS(table)}
- >
- {table.rls_enabled ? 'Disable RLS' : 'Enable RLS'}
- </Button>
- </Tooltip.Trigger>
- {!canToggleRLS && (
- <Tooltip.Content side="bottom">
- <Tooltip.Arrow className="radix-tooltip-arrow" />
- <div
- className={[
- 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
- 'border border-scale-200',
- ].join(' ')}
+ New Policy
+ </Button>
+ </Tooltip.Trigger>
+ {!canCreatePolicies && (
+ <Tooltip.Content side="bottom">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
+ 'border border-scale-200',
+ ].join(' ')}
+ >
+ <span className="text-xs text-scale-1200">
+ You need additional permissions to create RLS policies
+ </span>
+ </div>
+ </Tooltip.Content>
+ )}
+ </Tooltip.Root>
+ <Tooltip.Root delayDuration={0}>
+ <Tooltip.Trigger>
+ <Button
+ type="default"
+ disabled={!canToggleRLS}
+ onClick={() => onSelectToggleRLS(table)}
>
- <span className="text-xs text-scale-1200">
- You need additional permissions to toggle RLS
- </span>
- </div>
- </Tooltip.Content>
- )}
- </Tooltip.Root>
+ {table.rls_enabled ? 'Disable RLS' : 'Enable RLS'}
+ </Button>
+ </Tooltip.Trigger>
+ {!canToggleRLS && (
+ <Tooltip.Content side="bottom">
+ <Tooltip.Arrow className="radix-tooltip-arrow" />
+ <div
+ className={[
+ 'rounded bg-scale-100 py-1 px-2 leading-none shadow',
+ 'border border-scale-200',
+ ].join(' ')}
+ >
+ <span className="text-xs text-scale-1200">
+ You need additional permissions to toggle RLS
+ </span>
+ </div>
+ </Tooltip.Content>
+ )}
+ </Tooltip.Root>
+ </div>
</div>
- </div>
+ )}
</div>
)
}
diff --git a/studio/components/interfaces/Auth/Policies/PolicyTableRow/index.tsx b/studio/components/interfaces/Auth/Policies/PolicyTableRow/index.tsx
index d181c0d6463..b3f53cd0df6 100644
--- a/studio/components/interfaces/Auth/Policies/PolicyTableRow/index.tsx
+++ b/studio/components/interfaces/Auth/Policies/PolicyTableRow/index.tsx
@@ -9,50 +9,52 @@ import Panel from 'components/ui/Panel'
interface Props {
table: PostgresTable
+ isLocked: boolean
onSelectToggleRLS: (table: PostgresTable) => void
onSelectCreatePolicy: (table: PostgresTable) => void
onSelectEditPolicy: (policy: PostgresPolicy) => void
onSelectDeletePolicy: (policy: PostgresPolicy) => void
}
-const PolicyTableRow: FC<Props> = observer(
- ({
- table,
- onSelectToggleRLS = () => {},
- onSelectCreatePolicy = () => {},
- onSelectEditPolicy = () => {},
- onSelectDeletePolicy = () => {},
- }) => {
- const { meta } = useStore()
- const policies = meta.policies.list((x: PostgresPolicy) => x.table === table.name)
- return (
- <Panel
- title={
- <PolicyTableRowHeader
- table={table}
- onSelectToggleRLS={onSelectToggleRLS}
- onSelectCreatePolicy={onSelectCreatePolicy}
- />
- }
- >
- {policies.length === 0 && (
- <div className="p-4 px-6">
- <p className="text-scale-900 text-sm">No policies created yet</p>
- </div>
- )}
+const PolicyTableRow: FC<Props> = ({
+ table,
+ isLocked,
+ onSelectToggleRLS = () => {},
+ onSelectCreatePolicy = () => {},
+ onSelectEditPolicy = () => {},
+ onSelectDeletePolicy = () => {},
+}) => {
+ const { meta } = useStore()
+ const policies = meta.policies.list((x: PostgresPolicy) => x.table === table.name)
- {/* @ts-ignore */}
- {policies.map((policy) => (
- <PolicyRow
- key={policy.id}
- policy={policy}
- onSelectEditPolicy={onSelectEditPolicy}
- onSelectDeletePolicy={onSelectDeletePolicy}
- />
- ))}
- </Panel>
- )
- }
-)
+ return (
+ <Panel
+ title={
+ <PolicyTableRowHeader
+ table={table}
+ isLocked={isLocked}
+ onSelectToggleRLS={onSelectToggleRLS}
+ onSelectCreatePolicy={onSelectCreatePolicy}
+ />
+ }
+ >
+ {policies.length === 0 && (
+ <div className="p-4 px-6">
+ <p className="text-scale-900 text-sm">No policies created yet</p>
+ </div>
+ )}
-export default PolicyTableRow
+ {/* @ts-ignore */}
+ {policies.map((policy) => (
+ <PolicyRow
+ key={policy.id}
+ policy={policy}
+ onSelectEditPolicy={onSelectEditPolicy}
+ onSelectDeletePolicy={onSelectDeletePolicy}
+ />
+ ))}
+ </Panel>
+ )
+}
+
+export default observer(PolicyTableRow)
diff --git a/studio/components/interfaces/Database/Tables/TableList.tsx b/studio/components/interfaces/Database/Tables/TableList.tsx
index 2c4b60c1acb..002c788aa95 100644
--- a/studio/components/interfaces/Database/Tables/TableList.tsx
+++ b/studio/components/interfaces/Database/Tables/TableList.tsx
@@ -67,7 +67,7 @@ const TableList: FC<Props> = ({
size="small"
value={selectedSchema}
onChange={onSelectSchema}
- icon={isLocked && <IconLock size={16} strokeWidth={2} />}
+ icon={isLocked && <IconLock size={14} strokeWidth={2} />}
>
<Listbox.Option disabled key="normal-schemas" value="normal-schemas" label="Schemas">
<p className="text-sm">Schemas</p>
diff --git a/studio/components/layouts/AuthLayout/AuthLayout.tsx b/studio/components/layouts/AuthLayout/AuthLayout.tsx
index 8fa69472d88..f747c9adf22 100644
--- a/studio/components/layouts/AuthLayout/AuthLayout.tsx
+++ b/studio/components/layouts/AuthLayout/AuthLayout.tsx
@@ -28,6 +28,7 @@ const AuthLayout: FC<Props> = ({ title, children }) => {
meta.policies.load()
meta.tables.load()
meta.roles.load()
+ meta.schemas.load()
}
}, [ui.selectedProject?.ref])
diff --git a/studio/pages/project/[ref]/auth/policies.tsx b/studio/pages/project/[ref]/auth/policies.tsx
index 9d52be5eee7..42562e4bc55 100644
--- a/studio/pages/project/[ref]/auth/policies.tsx
+++ b/studio/pages/project/[ref]/auth/policies.tsx
@@ -1,5 +1,6 @@
import { useState, useEffect } from 'react'
-import { Button, IconSearch, Input, IconExternalLink } from 'ui'
+import { partition } from 'lodash'
+import { Button, Listbox, IconSearch, Input, IconExternalLink, IconLock } from 'ui'
import { observer } from 'mobx-react-lite'
import { PostgresTable, PostgresPolicy } from '@supabase/postgres-meta'
import { PermissionAction } from '@supabase/shared-types/out/constants'
@@ -48,15 +49,25 @@ const onFilterTables = (
const AuthPoliciesPage: NextPageWithLayout = () => {
const { meta } = useStore()
const { search } = useParams()
- const [policiesFilter, setPoliciesFilter] = useState<string>('')
+ const [selectedSchema, setSelectedSchema] = useState<string>('public')
+ const [searchString, setSearchString] = useState<string>('')
useEffect(() => {
- if (search) setPoliciesFilter(search)
+ if (search) setSearchString(search)
}, [search])
- const publicTables = meta.tables.list((table: { schema: string }) => table.schema === 'public')
+ const schemas = meta.schemas.list()
+ const [protectedSchemas, openSchemas] = partition(schemas, (schema) =>
+ meta.excludedSchemas.includes(schema?.name ?? '')
+ )
+ // @ts-ignore
+ const schema = schemas.find((schema) => schema.name === selectedSchema)
+ const isLocked = protectedSchemas.some((s) => s.id === schema?.id)
+
const policies = meta.policies.list()
- const filteredTables = onFilterTables(publicTables, policies, policiesFilter)
+
+ const tables = meta.tables.list((table: { schema: string }) => table.schema === selectedSchema)
+ const filteredTables = onFilterTables(tables, policies, searchString)
const canReadPolicies = checkPermissions(PermissionAction.TENANT_SQL_ADMIN_READ, 'policies')
@@ -66,29 +77,78 @@ const AuthPoliciesPage: NextPageWithLayout = () => {
return (
<div className="flex flex-col h-full">
- {(publicTables || []).length > 0 && (
- <div className="mb-4">
- <div className="flex items-center justify-between">
+ <div className="mb-4">
+ <div className="flex items-center justify-between">
+ <div className="flex items-center space-x-4">
+ <div className="w-[230px]">
+ <Listbox
+ size="small"
+ value={selectedSchema}
+ onChange={(schema: string) => {
+ setSelectedSchema(schema)
+ setSearchString('')
+ }}
+ icon={isLocked && <IconLock size={14} strokeWidth={2} />}
+ >
+ <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.Option
+ key={schema.id}
+ value={schema.name}
+ label={schema.name}
+ addOnBefore={() => <span className="text-scale-900">schema</span>}
+ >
+ <span className="text-scale-1200 text-sm">{schema.name}</span>
+ </Listbox.Option>
+ ))}
+ <Listbox.Option
+ disabled
+ key="protected-schemas"
+ value="protected-schemas"
+ label="Protected schemas"
+ >
+ <p className="text-sm">Protected schemas</p>
+ </Listbox.Option>
+ {protectedSchemas.map((schema) => (
+ <Listbox.Option
+ key={schema.id}
+ value={schema.name}
+ label={schema.name}
+ addOnBefore={() => <span className="text-scale-900">schema</span>}
+ >
+ <span className="text-scale-1200 text-sm">{schema.name}</span>
+ </Listbox.Option>
+ ))}
+ </Listbox>
+ </div>
<Input
size="small"
placeholder="Filter tables and policies"
className="block w-64 text-sm placeholder-gray-400"
- value={policiesFilter}
- onChange={(e) => setPoliciesFilter(e.target.value)}
+ value={searchString}
+ onChange={(e) => setSearchString(e.target.value)}
icon={<IconSearch size="tiny" />}
/>
- <Button type="link" icon={<IconExternalLink size={14} strokeWidth={1.5} />}>
- <a
- target="_blank"
- href="https://supabase.com/docs/learn/auth-deep-dive/auth-row-level-security"
- >
- What is RLS?
- </a>
- </Button>
</div>
+ <Button type="link" icon={<IconExternalLink size={14} strokeWidth={1.5} />}>
+ <a
+ target="_blank"
+ href="https://supabase.com/docs/learn/auth-deep-dive/auth-row-level-security"
+ >
+ What is RLS?
+ </a>
+ </Button>
</div>
- )}
- <Policies hasPublicTables={publicTables.length > 0} tables={filteredTables} />
+ </div>
+ <Policies tables={filteredTables} hasTables={tables.length > 0} isLocked={isLocked} />
</div>
)
}