mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 08:29:01 +00:00
## Context
Final clean up of deprecated Scaffold components, just touches
- Account audit logs
- Project home page
- Database -> Column privileges
- Storage -> Analytics bucket details
- Storage -> Vector bucket details
- Settings -> Infrastructure -> Disk management
- Settings -> Infrastructure -> Read replica details
- Integrations -> Wrappers overview (Only for non supported integration
UI)
- Edge functions -> Empty state
- Auth -> Performance
Also replaces usage of `ScaffoldDivider` with just the `Separator`
component
And also adjusts the page container sizes of database pages from large
to default - these only have visual differences on viewport widths
larger than a laptop
Testing just needs a quick sweep of all these surfaces to make sure
nothing looks weird - will do so on my end too once the builds go
through!
For @supabase/security's review - review requirement was triggered as
`Documents.tsx` was updated in the organization settings page. But it
was just to change the Divider component 🙏
386 lines
14 KiB
TypeScript
386 lines
14 KiB
TypeScript
import { createFileRoute, Link } from '@tanstack/react-router'
|
|
import { LOCAL_STORAGE_KEYS, useParams } from 'common'
|
|
import { AlertCircleIcon, XIcon } from 'lucide-react'
|
|
import { useCallback, useEffect, useMemo, useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import { Alert, AlertDescription, AlertTitle, Button } from 'ui'
|
|
import { PageContainer } from 'ui-patterns/PageContainer'
|
|
import {
|
|
PageHeader,
|
|
PageHeaderAside,
|
|
PageHeaderDescription,
|
|
PageHeaderMeta,
|
|
PageHeaderSummary,
|
|
PageHeaderTitle,
|
|
} from 'ui-patterns/PageHeader'
|
|
import { PageSection } from 'ui-patterns/PageSection'
|
|
import { GenericSkeletonLoader } from 'ui-patterns/ShimmeringLoader'
|
|
|
|
import {
|
|
useFeaturePreviewModal,
|
|
useIsColumnLevelPrivilegesEnabled,
|
|
} from '@/components/interfaces/App/FeaturePreview/FeaturePreviewContext'
|
|
import {
|
|
getDefaultColumnCheckedStates,
|
|
getDefaultTableCheckedStates,
|
|
useApplyPrivilegeOperations,
|
|
usePrivilegesState,
|
|
} from '@/components/interfaces/Database/Privileges/Privileges.utils'
|
|
import PrivilegesHead from '@/components/interfaces/Database/Privileges/PrivilegesHead'
|
|
import { PrivilegesTable } from '@/components/interfaces/Database/Privileges/PrivilegesTable'
|
|
import { ProtectedSchemaWarning } from '@/components/interfaces/Database/ProtectedSchemaWarning'
|
|
import { AlertError } from '@/components/ui/AlertError'
|
|
import { DocsButton } from '@/components/ui/DocsButton'
|
|
import { FeaturePreviewBadge } from '@/components/ui/FeaturePreviewBadge'
|
|
import { PgRole, useDatabaseRolesQuery } from '@/data/database-roles/database-roles-query'
|
|
import { useColumnPrivilegesQuery } from '@/data/privileges/column-privileges-query'
|
|
import { useTablePrivilegesQuery } from '@/data/privileges/table-privileges-query'
|
|
import { useTablesQuery } from '@/data/tables/tables-query'
|
|
import { useLocalStorage } from '@/hooks/misc/useLocalStorage'
|
|
import { useQuerySchemaState } from '@/hooks/misc/useSchemaQueryState'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
import { useIsProtectedSchema } from '@/hooks/useProtectedSchemas'
|
|
import { DOCS_URL } from '@/lib/constants'
|
|
|
|
export const Route = createFileRoute('/project/$ref/database/column-privileges')({
|
|
component: PrivilegesPage,
|
|
staticData: {
|
|
databaseLayoutTitle: 'Column Privileges',
|
|
},
|
|
})
|
|
|
|
const EDITABLE_ROLES = ['authenticated', 'anon', 'service_role']
|
|
|
|
function PrivilegesPage() {
|
|
const { ref } = Route.useParams()
|
|
const { table: paramTable } = useParams()
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const { toggleFeaturePreviewModal } = useFeaturePreviewModal()
|
|
const isEnabled = useIsColumnLevelPrivilegesEnabled()
|
|
|
|
const { selectedSchema, setSelectedSchema } = useQuerySchemaState()
|
|
const [selectedTable, setSelectedTable] = useState<string | undefined>(paramTable)
|
|
const [selectedRole, setSelectedRole] = useState<string>('authenticated')
|
|
|
|
const {
|
|
data: tableList,
|
|
isPending: isLoadingTables,
|
|
isSuccess: isSuccessTables,
|
|
} = useTablesQuery({
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
schema: selectedSchema,
|
|
})
|
|
|
|
// Default to (or fall back to) the first table of the selected schema. The
|
|
// fallback matters because the schema can also change via the URL, which
|
|
// leaves `selectedTable` pointing at a table from the previous schema.
|
|
useEffect(() => {
|
|
if (!isSuccessTables) return
|
|
const tableNames = tableList.map((table) => table.name)
|
|
if (selectedTable === undefined || !tableNames.includes(selectedTable)) {
|
|
setSelectedTable(tableNames[0])
|
|
}
|
|
}, [isSuccessTables, tableList, selectedTable])
|
|
|
|
const { data: allRoles, isPending: isLoadingRoles } = useDatabaseRolesQuery({
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
})
|
|
|
|
const tables = tableList?.map((table) => table.name)
|
|
|
|
const {
|
|
data: allTablePrivileges,
|
|
isPending: isLoadingTablePrivileges,
|
|
isError: isErrorTablePrivileges,
|
|
error: errorTablePrivileges,
|
|
} = useTablePrivilegesQuery({
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
includedSchemas: [selectedSchema],
|
|
})
|
|
|
|
const tablePrivilege = useMemo(() => {
|
|
const tablePrivilege = allTablePrivileges?.find(
|
|
(tablePrivilege) => tablePrivilege.name === selectedTable
|
|
)
|
|
|
|
if (tablePrivilege) {
|
|
return {
|
|
...tablePrivilege,
|
|
privileges: tablePrivilege.privileges.filter(
|
|
(privilege) => privilege.grantee === selectedRole
|
|
),
|
|
}
|
|
}
|
|
}, [allTablePrivileges, selectedRole, selectedTable])
|
|
|
|
const {
|
|
data: allColumnPrivileges,
|
|
isPending: isPendingColumnPrivileges,
|
|
isError: isErrorColumnPrivileges,
|
|
error: errorColumnPrivileges,
|
|
} = useColumnPrivilegesQuery({
|
|
projectRef: project?.ref,
|
|
connectionString: project?.connectionString,
|
|
schema: selectedSchema,
|
|
table: selectedTable,
|
|
})
|
|
|
|
// The query is disabled until a table is selected, and a disabled query stays
|
|
// `isPending` forever -- so don't report it as loading in that state, or a
|
|
// schema with no tables never gets past the skeleton.
|
|
const isLoadingColumnPrivileges = selectedTable !== undefined && isPendingColumnPrivileges
|
|
|
|
const columnPrivileges = useMemo(
|
|
() =>
|
|
allColumnPrivileges?.map((privilege) => ({
|
|
...privilege,
|
|
privileges: privilege.privileges.filter((privilege) => privilege.grantee === selectedRole),
|
|
})) ?? [],
|
|
[allColumnPrivileges, selectedRole]
|
|
)
|
|
|
|
const rolesList = allRoles?.filter((role: PgRole) => EDITABLE_ROLES.includes(role.name)) ?? []
|
|
const roles = rolesList.map((role: PgRole) => role.name)
|
|
|
|
const table = tableList?.find((table) => table.name === selectedTable)
|
|
const { isSchemaLocked } = useIsProtectedSchema({ schema: selectedSchema })
|
|
|
|
const {
|
|
tableCheckedStates,
|
|
columnCheckedStates,
|
|
toggleTablePrivilege,
|
|
toggleColumnPrivilege,
|
|
operations,
|
|
resetOperations,
|
|
} = usePrivilegesState(
|
|
useMemo(
|
|
() => ({
|
|
tableId: table?.id ?? -1,
|
|
role: selectedRole,
|
|
defaultTableCheckedStates: tablePrivilege
|
|
? getDefaultTableCheckedStates(tablePrivilege)
|
|
: {},
|
|
defaultColumnCheckedStates: getDefaultColumnCheckedStates(columnPrivileges),
|
|
}),
|
|
[columnPrivileges, selectedRole, table?.id, tablePrivilege]
|
|
)
|
|
)
|
|
|
|
const hasChanges = operations.length > 0
|
|
|
|
const handleChangeSchema = (schema: string) => {
|
|
if (hasChanges) {
|
|
if (window.confirm('You will lose your changes. Are you sure?')) {
|
|
resetOperations()
|
|
} else {
|
|
return
|
|
}
|
|
}
|
|
|
|
setSelectedSchema(schema)
|
|
// The table list is scoped to the schema, so the incoming schema's tables
|
|
// aren't loaded yet -- the effect above picks the first one once they are.
|
|
setSelectedTable(undefined)
|
|
}
|
|
|
|
const handleChangeTable = (table: string) => {
|
|
if (hasChanges) {
|
|
if (window.confirm('You will lose your changes. Are you sure?')) {
|
|
resetOperations()
|
|
} else {
|
|
return
|
|
}
|
|
}
|
|
|
|
setSelectedTable(table)
|
|
}
|
|
|
|
const handleChangeRole = (role: string) => {
|
|
setSelectedRole(role)
|
|
}
|
|
|
|
const { apply: applyColumnPrivileges, isLoading: isApplyingChanges } =
|
|
useApplyPrivilegeOperations({
|
|
schema: selectedSchema,
|
|
table: selectedTable,
|
|
onSuccess: useCallback(() => {
|
|
toast.success(
|
|
`Successfully updated privileges on ${selectedSchema}.${selectedTable} for ${selectedRole}`,
|
|
{ duration: 6000 }
|
|
)
|
|
resetOperations()
|
|
}, [resetOperations, selectedRole, selectedSchema, selectedTable]),
|
|
})
|
|
|
|
function applyChanges() {
|
|
applyColumnPrivileges(operations)
|
|
}
|
|
|
|
const [diffWarningDismissed, setDiffWarningDismissed] = useLocalStorage(
|
|
LOCAL_STORAGE_KEYS.CLS_DIFF_WARNING,
|
|
false
|
|
)
|
|
const [selectStarWarningDismissed, setSelectStarWarningDismissed] = useLocalStorage(
|
|
LOCAL_STORAGE_KEYS.CLS_SELECT_STAR_WARNING,
|
|
false
|
|
)
|
|
|
|
const isLoading =
|
|
isLoadingTablePrivileges || isLoadingColumnPrivileges || isLoadingTables || isLoadingRoles
|
|
|
|
const isError = isErrorTablePrivileges || isErrorColumnPrivileges
|
|
|
|
return (
|
|
<>
|
|
<PageHeader size="default">
|
|
<PageHeaderMeta>
|
|
<PageHeaderSummary>
|
|
<div className="flex items-center gap-x-4">
|
|
<PageHeaderTitle>Column-level privileges</PageHeaderTitle>
|
|
{isEnabled && <FeaturePreviewBadge featureKey={LOCAL_STORAGE_KEYS.UI_PREVIEW_CLS} />}
|
|
</div>
|
|
<PageHeaderDescription>
|
|
Grant or revoke privileges on a column based on user role.
|
|
</PageHeaderDescription>
|
|
</PageHeaderSummary>
|
|
<PageHeaderAside>
|
|
<DocsButton href={`${DOCS_URL}/guides/auth/column-level-security`} />
|
|
</PageHeaderAside>
|
|
</PageHeaderMeta>
|
|
</PageHeader>
|
|
<PageContainer size="default" className="flex-1 flex flex-col">
|
|
<PageSection className="flex-1 last:pb-6">
|
|
<div className="flex-1 flex flex-col pb-4 gap-y-4">
|
|
{isEnabled ? (
|
|
<>
|
|
{!diffWarningDismissed && (
|
|
<Alert variant="warning">
|
|
<AlertCircleIcon strokeWidth={2} />
|
|
<AlertTitle>
|
|
Changes to column privileges will not be reflected in migrations when running{' '}
|
|
<code>supabase db diff</code>.
|
|
</AlertTitle>
|
|
<AlertDescription>
|
|
Column privileges are not supported in the current version of the Supabase
|
|
CLI.
|
|
<br />
|
|
You will need to manually apply these changes to your database.
|
|
</AlertDescription>
|
|
<Button
|
|
variant="outline"
|
|
aria-label="Dismiss"
|
|
className="absolute top-2 right-2 p-1 pl-1!"
|
|
onClick={() => {
|
|
setDiffWarningDismissed(true)
|
|
}}
|
|
>
|
|
<XIcon width={14} height={14} />
|
|
</Button>
|
|
</Alert>
|
|
)}
|
|
|
|
{!selectStarWarningDismissed && (
|
|
<Alert variant="warning">
|
|
<AlertCircleIcon strokeWidth={2} />
|
|
<AlertTitle>Changing column privileges can break existing queries.</AlertTitle>
|
|
<AlertDescription>
|
|
If you remove a column privilege for a role, that role will lose all access to
|
|
that column.
|
|
<br />
|
|
All operations selecting <code>*</code> (including <code>returning *</code>{' '}
|
|
for <code>insert</code>, <code>update</code>, and <code>delete</code>) will
|
|
fail.
|
|
</AlertDescription>
|
|
<Button
|
|
variant="outline"
|
|
aria-label="Dismiss"
|
|
className="absolute top-2 right-2 p-1 pl-1!"
|
|
onClick={() => {
|
|
setSelectStarWarningDismissed(true)
|
|
}}
|
|
>
|
|
<XIcon width={14} height={14} />
|
|
</Button>
|
|
</Alert>
|
|
)}
|
|
|
|
<PrivilegesHead
|
|
disabled={isSchemaLocked}
|
|
selectedSchema={selectedSchema}
|
|
selectedRole={selectedRole}
|
|
selectedTable={table}
|
|
tables={tables ?? []}
|
|
roles={roles}
|
|
onChangeSchema={handleChangeSchema}
|
|
onChangeRole={handleChangeRole}
|
|
onChangeTable={handleChangeTable}
|
|
applyChanges={applyChanges}
|
|
resetChanges={resetOperations}
|
|
hasChanges={hasChanges}
|
|
isApplyingChanges={isApplyingChanges}
|
|
/>
|
|
{isSchemaLocked && (
|
|
<ProtectedSchemaWarning schema={selectedSchema} entity="column privileges" />
|
|
)}
|
|
{isLoading ? (
|
|
<GenericSkeletonLoader />
|
|
) : isError ? (
|
|
<AlertError error={errorTablePrivileges || errorColumnPrivileges} />
|
|
) : table && tablePrivilege ? (
|
|
<div>
|
|
<PrivilegesTable
|
|
disabled={isSchemaLocked}
|
|
columnPrivileges={columnPrivileges}
|
|
tableCheckedStates={tableCheckedStates}
|
|
columnCheckedStates={columnCheckedStates}
|
|
toggleTablePrivilege={toggleTablePrivilege}
|
|
toggleColumnPrivilege={toggleColumnPrivilege}
|
|
isApplyingChanges={isApplyingChanges}
|
|
/>
|
|
</div>
|
|
) : (tables ?? []).length === 0 ? (
|
|
<div className="grow flex flex-col items-center justify-center w-[600px] mx-auto">
|
|
<p className="text-center">
|
|
There are no tables in the {selectedSchema} schema
|
|
</p>
|
|
<p className="text-sm text-foreground-light text-center">
|
|
Once a table is available in the schema, you may manage it's column-level
|
|
privileges here
|
|
</p>
|
|
{selectedSchema === 'public' && (
|
|
<Button variant="primary" asChild className="mt-4">
|
|
<Link to="/project/$ref/editor" params={{ ref }}>
|
|
Create a new table
|
|
</Link>
|
|
</Button>
|
|
)}
|
|
</div>
|
|
) : (
|
|
<div className="flex flex-col items-center justify-center h-64 ">
|
|
<p className="text-foreground-light">Select a table to edit privileges</p>
|
|
</div>
|
|
)}
|
|
</>
|
|
) : (
|
|
<Alert>
|
|
<AlertTitle>Column-level privileges is a dashboard feature preview</AlertTitle>
|
|
<AlertDescription>
|
|
You may access this feature by enabling it under dashboard feature previews.
|
|
</AlertDescription>
|
|
<div className="mt-4">
|
|
<Button onClick={() => toggleFeaturePreviewModal(true)}>
|
|
View feature previews
|
|
</Button>
|
|
</div>
|
|
</Alert>
|
|
)}
|
|
</div>
|
|
</PageSection>
|
|
</PageContainer>
|
|
</>
|
|
)
|
|
}
|