Skip to content

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

Hook up roles update functionality and refactor update policy editor description

Joshen Lim0ec719aca649
diff --git a/studio/components/interfaces/Authentication/Policies/Policies.utils.ts b/studio/components/interfaces/Authentication/Policies/Policies.utils.ts
index c91cd30f9e1..cebb9443387 100644
--- a/studio/components/interfaces/Authentication/Policies/Policies.utils.ts
+++ b/studio/components/interfaces/Authentication/Policies/Policies.utils.ts
@@ -1,3 +1,4 @@
+import { PostgresRole } from '@supabase/postgres-meta'
import { has, isEmpty, isEqual } from 'lodash'
/**
@@ -33,20 +34,26 @@ export const createSQLPolicy = (policyFormFields: any, originalPolicyFormFields:
if (!isEqual(formattedPolicyFormFields.check, originalPolicyFormFields.check)) {
fieldsToUpdate.check = formattedPolicyFormFields.check
}
+ if (!isEqual(formattedPolicyFormFields.roles, originalPolicyFormFields.roles)) {
+ fieldsToUpdate.roles = formattedPolicyFormFields.roles
+ }
if (!isEmpty(fieldsToUpdate)) {
return createSQLStatementForUpdatePolicy(formattedPolicyFormFields, fieldsToUpdate)
}
- return { description: 'hello', statement: 'hello' }
+ return {}
}
export const createSQLStatementForCreatePolicy = (policyFormFields: any) => {
const { name, definition, check, command, schema, table } = policyFormFields
+ const roles = policyFormFields.roles.length === 0 ? ['public'] : policyFormFields.roles
const description = `Add policy for the ${command} operation under the policy "${name}"`
const statement =
`
- CREATE POLICY "${name}" ON ${schema}.${table} FOR ${command}
+ CREATE POLICY "${name}" ON "${schema}"."${table}"
+ AS PERMISSIVE FOR ${command}
+ TO ${roles.join(', ')}
${definition ? `USING (${definition})` : ''}
${check ? `WITH CHECK (${check})` : ''}
`
@@ -64,46 +71,40 @@ export const createSQLStatementForUpdatePolicy = (
const definitionChanged = has(fieldsToUpdate, ['definition'])
const checkChanged = has(fieldsToUpdate, ['check'])
const nameChanged = has(fieldsToUpdate, ['name'])
-
- let description = `
- ${
- definitionChanged || checkChanged
- ? `Update policy's ${definitionChanged ? 'USING expression' : ''} ${
- definitionChanged && checkChanged ? ' and' : ''
- } ${checkChanged ? ' WITH CHECK expression.' : ''}`
- : ''
- }
- ${nameChanged ? `Rename policy to ${fieldsToUpdate.name}.` : ''}
+ const rolesChanged = has(fieldsToUpdate, ['roles'])
+
+ const parameters = Object.keys(fieldsToUpdate)
+ const description = `Update policy's ${
+ parameters.length === 1
+ ? parameters[0]
+ : `${parameters.slice(0, parameters.length - 1).join(', ')} and ${
+ parameters[parameters.length - 1]
+ }`
+ } `
+
+ const alterStatement = `ALTER POLICY "${name}" ON "${schema}"."${table}"`
+ const statement = `
+ BEGIN;
+ ${definitionChanged ? `${alterStatement} USING (${fieldsToUpdate.definition});` : ''}
+ ${checkChanged ? `${alterStatement} WITH CHECK (${fieldsToUpdate.check});` : ''}
+ ${rolesChanged ? `${alterStatement} TO ${fieldsToUpdate.roles.join(', ')};` : ''}
+ ${nameChanged ? `${alterStatement} RENAME TO "${fieldsToUpdate.name}";` : ''}
+ COMMIT;
`
- // Need to figure out a way to derive description: name, definition and check can change
-
- const definitionOrCheckUpdateStatement =
- definitionChanged || checkChanged
- ? `
- ALTER POLICY "${name}" ON ${schema}.${table} ${
- definitionChanged ? `USING (${fieldsToUpdate.definition})` : ''
- } ${checkChanged ? `WITH CHECK (${fieldsToUpdate.check})` : ''};
- `
- .replace(/\s+/g, ' ')
- .trim()
- : ''
-
- const renameStatement = nameChanged
- ? `ALTER POLICY "${name}" ON ${schema}.${table} RENAME TO "${fieldsToUpdate.name}";`
- : ''
-
- const statement = definitionOrCheckUpdateStatement + renameStatement
+ .replace(/\s+/g, ' ')
+ .trim()
return { description, statement }
}
export const createPayloadForCreatePolicy = (policyFormFields: any = {}) => {
- const { definition, check } = policyFormFields
+ const { definition, check, roles } = policyFormFields
return {
...policyFormFields,
action: 'PERMISSIVE',
definition: definition || undefined,
check: check || undefined,
+ roles: roles.length > 0 ? roles : undefined,
}
}
@@ -129,6 +130,9 @@ export const createPayloadForUpdatePolicy = (
if (!isEqual(formattedPolicyFormFields.check, originalPolicyFormFields.check)) {
payload.check = formattedPolicyFormFields.check || undefined
}
+ if (!isEqual(formattedPolicyFormFields.roles, originalPolicyFormFields.roles)) {
+ payload.roles = formattedPolicyFormFields.roles || undefined
+ }
return payload
}
diff --git a/studio/components/interfaces/Authentication/Policies/PolicyEditor/PolicyRoles.tsx b/studio/components/interfaces/Authentication/Policies/PolicyEditor/PolicyRoles.tsx
index 61bd3423fff..3ace51e6b1d 100644
--- a/studio/components/interfaces/Authentication/Policies/PolicyEditor/PolicyRoles.tsx
+++ b/studio/components/interfaces/Authentication/Policies/PolicyEditor/PolicyRoles.tsx
@@ -1,12 +1,12 @@
import { FC } from 'react'
import { PostgresRole } from '@supabase/postgres-meta'
-import MultiSelectUI from 'components/ui/MultiSelect'
+import MultiSelect from 'components/ui/MultiSelect'
interface Props {
roles: PostgresRole[]
- selectedRoles: PostgresRole[]
- onUpdateSelectedRoles: (roles: PostgresRole[]) => void
+ selectedRoles: string[]
+ onUpdateSelectedRoles: (roles: string[]) => void
}
const PolicyRoles: FC<Props> = ({ roles, selectedRoles, onUpdateSelectedRoles }) => {
@@ -29,13 +29,14 @@ const PolicyRoles: FC<Props> = ({ roles, selectedRoles, onUpdateSelectedRoles })
<p className="text-scale-900 text-sm">Apply policy to the specified roles</p>
</div>
<div className="relative w-2/3">
- <MultiSelectUI
+ <MultiSelect
options={formattedRoles}
- value={selectedRoles.map((role) => role.name)}
+ value={selectedRoles}
+ placeholder='Defaults to "public" role if none selected'
searchPlaceholder="Search for a role"
onChange={(updatedRoles) => {
- const selectedRoles = roles.filter((role) => updatedRoles.includes(role.name))
- onUpdateSelectedRoles(selectedRoles)
+ // const selectedRoles = roles.filter((role) => updatedRoles.includes(role.name))
+ onUpdateSelectedRoles(updatedRoles)
}}
/>
</div>
diff --git a/studio/components/interfaces/Authentication/Policies/PolicyEditor/index.tsx b/studio/components/interfaces/Authentication/Policies/PolicyEditor/index.tsx
index 39e4fe249ed..64cf6015c4d 100644
--- a/studio/components/interfaces/Authentication/Policies/PolicyEditor/index.tsx
+++ b/studio/components/interfaces/Authentication/Policies/PolicyEditor/index.tsx
@@ -25,7 +25,7 @@ const PolicyEditor: FC<Props> = ({
onViewTemplates = () => {},
onReviewPolicy = () => {},
}) => {
- const operation = policyFormFields?.operation ?? ''
+ const operation = policyFormFields?.command ?? ''
const definition = policyFormFields?.definition ?? ''
const check = policyFormFields?.check ?? ''
const selectedRoles = policyFormFields?.roles ?? []
diff --git a/studio/components/interfaces/Authentication/Policies/PolicyEditorModal/index.tsx b/studio/components/interfaces/Authentication/Policies/PolicyEditorModal/index.tsx
index d82ce6e2c83..48d4c4686f5 100644
--- a/studio/components/interfaces/Authentication/Policies/PolicyEditorModal/index.tsx
+++ b/studio/components/interfaces/Authentication/Policies/PolicyEditorModal/index.tsx
@@ -165,7 +165,6 @@ const PolicyEditorModal: FC<Props> = ({
onSelectBackFromTemplates={onSelectBackFromTemplates}
/>,
]}
- // style={{ maxWidth: 'none', width: '60rem' }}
onCancel={onSelectCancel}
>
<div className="">
diff --git a/studio/components/interfaces/Authentication/Policies/PolicyTableRow/PolicyRow.tsx b/studio/components/interfaces/Authentication/Policies/PolicyTableRow/PolicyRow.tsx
index 96773f1a4e2..414e50f3806 100644
--- a/studio/components/interfaces/Authentication/Policies/PolicyTableRow/PolicyRow.tsx
+++ b/studio/components/interfaces/Authentication/Policies/PolicyTableRow/PolicyRow.tsx
@@ -15,15 +15,10 @@ const PolicyRow: FC<Props> = ({
}) => {
return (
<Panel.Content
- className="
- border-panel-border-light
- dark:border-panel-border-dark flex
- w-full
- gap-2
- border-b
- py-4
- lg:items-center
- "
+ className={[
+ 'border-panel-border-light dark:border-panel-border-dark flex',
+ 'w-full gap-4 border-b py-4 lg:items-center',
+ ].join(' ')}
>
<div className="text-scale-900 font-mono text-xs">{policy.command}</div>
<div className="flex grow flex-col gap-2 truncate lg:flex-row">
diff --git a/studio/components/ui/MultiSelect/index.tsx b/studio/components/ui/MultiSelect/index.tsx
index b8beff0cba2..3761f5421df 100644
--- a/studio/components/ui/MultiSelect/index.tsx
+++ b/studio/components/ui/MultiSelect/index.tsx
@@ -1,4 +1,4 @@
-import { useRef, useEffect, useState, FormEvent, KeyboardEvent } from 'react'
+import { useRef, useEffect, useState, FormEvent, KeyboardEvent, ReactNode } from 'react'
import { orderBy, filter, without } from 'lodash'
import { Popover, IconCheck, IconAlertCircle, IconSearch } from '@supabase/ui'
@@ -15,9 +15,10 @@ interface Props {
options: MultiSelectOption[]
value: string[]
label?: string
+ placeholder?: string | ReactNode
searchPlaceholder?: string
- descriptionText?: string | React.ReactNode
- emptyMessage?: string | React.ReactNode
+ descriptionText?: string | ReactNode
+ emptyMessage?: string | ReactNode
onChange?(x: string[]): void
}
@@ -26,6 +27,7 @@ export default function MultiSelect({
value,
label,
descriptionText,
+ placeholder,
searchPlaceholder = 'Search for option',
emptyMessage,
onChange = () => {},
@@ -37,7 +39,7 @@ export default function MultiSelect({
const [inputWidth, setInputWidth] = useState(128)
// Selected is `value` if defined, otherwise use local useState
- const selectedOptions = value || selected
+ const selectedOptions = (value || selected).filter((option) => option !== 'public')
// Calculate width of the Popover
useEffect(() => {
@@ -191,6 +193,9 @@ export default function MultiSelect({
`${selectedOptions.length === 0 ? 'h-9' : ''}`,
].join(' ')}
>
+ {selectedOptions.length === 0 && placeholder && (
+ <div className="text-scale-1000 px-2 text-sm">{placeholder}</div>
+ )}
{formattedOptions.map((option) => {
const active =
selectedOptions &&