mirror of
https://github.com/supabase/supabase.git
synced 2026-12-06 00:19:08 +00:00
## Problem Migrating Studio's permission checks from the old RLS json logic to openfga backed permissions. ### New hooks and helpers - `usePermissionsQueryV2` fetches the new profile openfga permisisons - `useAsyncCheckPermissionsV2` the replacement for the old check hook; takes an FGA permission string (or a list) - `FGA_PERMISSIONS` permission constants derived from `@supabase/shared-types`. At the moment, the hook also accepts plain strings for permissions not yet published in the package - `useAsyncCheckUserContentPermissions` new hook for permission checks on user content (SQL snippets, reports, log snippets). This wraps ``useAsyncCheckPermissionsV2` and adds existing content level rules on top of the fga permission check ### Notes - ~~Need api to support new permissions: project_operations_write, billing_read/write, audit_logs_read~~ [DONE] - ~~Still need to support: organization_config_read/write, auth hooks and users permissions~~ [DONE] --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Charis Lam <26616127+charislam@users.noreply.github.com>
87 lines
2.9 KiB
TypeScript
87 lines
2.9 KiB
TypeScript
import { useParams } from 'common'
|
|
import { partition } from 'lodash'
|
|
import { useRouter } from 'next/router'
|
|
import { toast } from 'sonner'
|
|
import { cn, SQL_ICON } from 'ui'
|
|
|
|
import { createSqlSnippetSkeletonV2 } from '../SQLEditor.utils'
|
|
import { SQL_TEMPLATES } from '@/components/interfaces/SQLEditor/SQLEditor.queries'
|
|
import { ActionCard } from '@/components/layouts/Tabs/ActionCard'
|
|
import {
|
|
FGA_PERMISSIONS,
|
|
useAsyncCheckUserContentPermissions,
|
|
} from '@/hooks/misc/useCheckPermissionsV2'
|
|
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
|
|
import { useProfile } from '@/lib/profile'
|
|
import { useTrack } from '@/lib/telemetry/track'
|
|
import { useSqlEditorV2StateSnapshot } from '@/state/sql-editor/sql-editor-state'
|
|
|
|
const SQLTemplates = () => {
|
|
const router = useRouter()
|
|
const { ref } = useParams()
|
|
const { profile } = useProfile()
|
|
const { data: project } = useSelectedProjectQuery()
|
|
const [sql] = partition(SQL_TEMPLATES, { type: 'template' })
|
|
|
|
const snapV2 = useSqlEditorV2StateSnapshot()
|
|
const track = useTrack()
|
|
|
|
const { can: canCreateSQLSnippet } = useAsyncCheckUserContentPermissions(
|
|
FGA_PERMISSIONS.PROJECT.SNIPPETS_WRITE,
|
|
{ mode: 'create', type: 'sql' }
|
|
)
|
|
|
|
const handleNewQuery = async (sql: string, name: string) => {
|
|
if (!ref) return console.error('Project ref is required')
|
|
if (!project) return console.error('Project is required')
|
|
if (!profile) return console.error('Profile is required')
|
|
|
|
if (!canCreateSQLSnippet) {
|
|
return toast('Your queries will not be saved as you do not have sufficient permissions')
|
|
}
|
|
|
|
try {
|
|
const snippet = createSqlSnippetSkeletonV2({
|
|
name,
|
|
sql,
|
|
owner_id: profile?.id,
|
|
project_id: project?.id,
|
|
})
|
|
snapV2.addSnippet({ projectRef: ref, snippet })
|
|
snapV2.addNeedsSaving(snippet.id)
|
|
|
|
router.push(`/project/${ref}/sql/${snippet.id}`)
|
|
} catch (error: any) {
|
|
toast.error(`Failed to create new query: ${error.message}`)
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="block h-full space-y-8 overflow-y-auto p-6 px-10 bg-dash-sidebar dark:bg-surface-100">
|
|
<div>
|
|
<div className="mb-6">
|
|
<h2 className="mb-1">Templates</h2>
|
|
<p className="text-foreground-light text-sm">Reusable SQL snippets for common tasks</p>
|
|
</div>
|
|
<div className="grid gap-4 sm:grid-cols-1 lg:grid-cols-2 xl:grid-cols-3 ">
|
|
{sql.map((x, i) => (
|
|
<ActionCard
|
|
key={`action-card-${i}`}
|
|
title={x.title}
|
|
description={x.description}
|
|
bgColor="bg-alternative border"
|
|
icon={<SQL_ICON className={cn('fill-foreground', 'w-4 h-4')} strokeWidth={1.5} />}
|
|
onClick={() => {
|
|
handleNewQuery(x.sql, x.title)
|
|
track('sql_editor_template_clicked', { templateName: x.title })
|
|
}}
|
|
/>
|
|
))}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default SQLTemplates
|