This commit is contained in:
Saxon Fletcher committed 2025-02-07 16:44:38 +10:00
1 parent 35b3eea0d0
commit 391c34565a
2 files changed
+69 -4

No files matched your search

@@ -37,7 +37,7 @@ const PolicyRow = ({
onSelectEditPolicy = noop,
onSelectDeletePolicy = noop,
}: PolicyRowProps) => {
const { setAiAssistantPanel } = useAppStateSnapshot()
const { setAiAssistantPanel, setEditorPanel } = useAppStateSnapshot()
const canUpdatePolicies = useCheckPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, 'policies')
const { project } = useProjectContext()
@@ -105,7 +105,45 @@ const PolicyRow = ({
<Button type="default" className="px-1.5" icon={<MoreVertical />} />
</DropdownMenuTrigger>
<DropdownMenuContent side="bottom" align="end" className="w-52">
<DropdownMenuItem className="gap-x-2" onClick={() => onSelectEditPolicy(policy)}>
<DropdownMenuItem
className="gap-x-2"
onClick={() => {
const sql = generatePolicyCreateSQL(policy)
setEditorPanel({
open: true,
initialValue: sql,
label: `Edit policy "${policy.name}"`,
saveLabel: 'Update policy',
templates: [
{
name: 'Enable Row Level Security',
description: 'Enable RLS and create a policy that allows all operations',
content: `ALTER TABLE ${policy.schema}.${policy.table} ENABLE ROW LEVEL SECURITY;\n\nCREATE POLICY "Allow all" ON ${policy.schema}.${policy.table}\nAS PERMISSIVE FOR ALL\nTO authenticated\nUSING (true)\nWITH CHECK (true);`,
},
{
name: 'User Owns Row',
description: 'Only allow users to access their own data',
content: `CREATE POLICY "Users can only access their own rows" ON ${policy.schema}.${policy.table}\nAS PERMISSIVE FOR ALL\nTO authenticated\nUSING (auth.uid() = user_id)\nWITH CHECK (auth.uid() = user_id);`,
},
{
name: 'Tenant Access',
description: 'Multi-tenant policy based on org_id',
content: `CREATE POLICY "Tenant isolation" ON ${policy.schema}.${policy.table}\nAS PERMISSIVE FOR ALL\nTO authenticated\nUSING (org_id IN (SELECT org_id FROM memberships WHERE user_id = auth.uid()))\nWITH CHECK (org_id IN (SELECT org_id FROM memberships WHERE user_id = auth.uid()));`,
},
{
name: 'Public Read Only',
description: 'Allow public read access but restrict modifications',
content: `CREATE POLICY "Public read access" ON ${policy.schema}.${policy.table}\nAS PERMISSIVE FOR SELECT\nTO public\nUSING (true);`,
},
{
name: 'Role-Based Access',
description: 'Control access based on user role',
content: `CREATE POLICY "Role-based access control" ON ${policy.schema}.${policy.table}\nAS PERMISSIVE FOR ALL\nTO authenticated\nUSING (\n auth.jwt() ? auth.jwt()->>'role' = 'admin'\n)\nWITH CHECK (\n auth.jwt() ? auth.jwt()->>'role' = 'admin'\n);`,
},
],
})
}}
>
<Edit size={14} />
<p>Edit policy</p>
</DropdownMenuItem>
@@ -50,6 +50,22 @@ const SQLEditor = ({ onChange }: SQLEditorProps) => {
const editorRef = useRef<IStandaloneCodeEditor | null>(null)
const monacoRef = useRef<any | null>(null)
useEffect(() => {
if (editorRef.current && editorPanel.initialValue !== currentValue) {
const editor = editorRef.current
const model = editor.getModel()
if (model) {
editor.executeEdits('update-value', [
{
text: editorPanel.initialValue || '',
range: model.getFullModelRange(),
},
])
setCurrentValue(editorPanel.initialValue || '')
}
}
}, [editorPanel.initialValue])
const {
complete,
completion,
@@ -119,6 +135,18 @@ const SQLEditor = ({ onChange }: SQLEditorProps) => {
editorRef.current = editor
monacoRef.current = monaco
// Set initial value on mount
const model = editor.getModel()
if (model && editorPanel.initialValue) {
editor.executeEdits('initial-value', [
{
text: editorPanel.initialValue,
range: model.getFullModelRange(),
},
])
setCurrentValue(editorPanel.initialValue)
}
editor.addAction({
id: 'run-query',
label: 'Run Query',
@@ -239,7 +267,7 @@ const SQLEditor = ({ onChange }: SQLEditorProps) => {
)
})
console.log('templates:', editorPanel.templates)
console.log('templates:', editorPanel.initialValue)
return (
<div className="flex-1 overflow-hidden flex flex-col h-full">
@@ -249,7 +277,6 @@ const SQLEditor = ({ onChange }: SQLEditorProps) => {
className="monaco-editor"
theme="supabase"
language="pgsql"
defaultValue={editorPanel.initialValue}
onChange={handleChange}
onMount={handleEditorOnMount}
options={{