Skip to content

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

Only show RLS warning badge if banner has been dismissed

Alec Merdler2ec1d0bd2c6c
diff --git a/studio/components/grid/components/header/RLSBannerWarning.tsx b/studio/components/grid/components/header/RLSBannerWarning.tsx
index 2c40d373712..0695e64a4b2 100644
--- a/studio/components/grid/components/header/RLSBannerWarning.tsx
+++ b/studio/components/grid/components/header/RLSBannerWarning.tsx
@@ -1,19 +1,19 @@
-import { useState } from 'react'
+import { useCallback, useState } from 'react'
import { Button, IconAlertCircle, Modal } from 'ui'
import Link from 'next/link'
import { useStore } from 'hooks'
import { useParams } from 'common/hooks'
import ConfirmationModal from 'components/ui/ConfirmationModal'
import type { PostgresTable } from '@supabase/postgres-meta'
-import { RLS_ACKNOWLEDGED_KEY } from 'components/grid/constants'
+import { rlsAcknowledgedKey } from 'components/grid/constants'
import RLSDisableModalContent from 'components/interfaces/TableGridEditor/SidePanelEditor/TableEditor/RLSDisableModal'
export default function RLSBannerWarning() {
const { meta } = useStore()
const { ref: projectRef, id: tableID } = useParams()
- const isAcknowledged =
- localStorage?.getItem(`${RLS_ACKNOWLEDGED_KEY}-${tableID}`) === 'true' ?? false
+ const rlsKey = rlsAcknowledgedKey(tableID)
+ const isAcknowledged = localStorage?.getItem(rlsKey) === 'true' ?? false
const [isOpen, setIsOpen] = useState(false)
@@ -22,10 +22,11 @@ export default function RLSBannerWarning() {
const rlsEnabled = currentTable?.rls_enabled
const isPublicTable = currentTable?.schema === 'public'
- function handleDismissWarning() {
- localStorage.setItem(`${RLS_ACKNOWLEDGED_KEY}-${tableID}`, 'true')
+ const handleDismissWarning = useCallback(() => {
+ new BroadcastChannel(rlsKey).postMessage({ type: 'dismiss' })
+ localStorage.setItem(rlsKey, 'true')
setIsOpen(false)
- }
+ }, [rlsKey])
return (
<>
diff --git a/studio/components/grid/constants.ts b/studio/components/grid/constants.ts
index 78a2b9ef20e..5fed7c3ef88 100644
--- a/studio/components/grid/constants.ts
+++ b/studio/components/grid/constants.ts
@@ -17,3 +17,5 @@ export const ERROR_PRIMARY_KEY_NOTFOUND =
'Please add a primary key column to your table to update or delete rows'
export const RLS_ACKNOWLEDGED_KEY = 'supabase-acknowledge-rls-warning'
+
+export const rlsAcknowledgedKey = (tableID?: string) => `${RLS_ACKNOWLEDGED_KEY}-${tableID}`
diff --git a/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx b/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx
index 1bfbcfd4ae3..dda8035a15d 100644
--- a/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx
+++ b/studio/components/interfaces/TableGridEditor/GridHeaderActions.tsx
@@ -6,6 +6,8 @@ import Link from 'next/link'
import { useProjectContext } from 'components/layouts/ProjectLayout/ProjectContext'
import { useCheckPermissions, useStore } from 'hooks'
import { Button, IconAlertCircle, IconCode, IconLock } from 'ui'
+import { useEffect, useRef, useState } from 'react'
+import { RLS_ACKNOWLEDGED_KEY, rlsAcknowledgedKey } from 'components/grid/constants'
export interface GridHeaderActionsProps {
table: PostgresTable
@@ -29,6 +31,30 @@ const GridHeaderActions = ({
const canSqlWriteColumns = useCheckPermissions(PermissionAction.TENANT_SQL_ADMIN_WRITE, 'columns')
const isReadOnly = !canSqlWriteTables && !canSqlWriteColumns
+ const rlsKey = rlsAcknowledgedKey(table.id.toString())
+
+ const rlsAcknowledgedChannel = useRef(new BroadcastChannel(rlsKey))
+ const [showRLSWarning, setShowRLSWarning] = useState(
+ !table.rls_enabled && localStorage.getItem(rlsKey) === 'true'
+ )
+
+ const rlsDisabledMessage = showRLSWarning ? 'RLS is not enabled' : ''
+
+ useEffect(() => {
+ if (rlsAcknowledgedChannel.current) {
+ rlsAcknowledgedChannel.current.close()
+ }
+
+ rlsAcknowledgedChannel.current = new BroadcastChannel(rlsKey)
+ rlsAcknowledgedChannel.current.onmessage = (event) => {
+ if (event.data.type === 'dismiss') {
+ setShowRLSWarning(true)
+ }
+ }
+
+ return () => rlsAcknowledgedChannel.current.close()
+ }, [rlsKey])
+
function handlePreviewToggle() {
setApiPreviewPanelOpen(!apiPreviewPanelOpen)
refreshDocs()
@@ -74,26 +100,30 @@ const GridHeaderActions = ({
</Tooltip.Root>
)}
- <Link href={`/project/${projectRef}/auth/policies?search=${table.id}`}>
- <a>
- <Button
- type={table.rls_enabled ? 'link' : 'warning'}
- icon={
- table.rls_enabled ? (
- <IconLock strokeWidth={2} size={14} />
- ) : (
- <IconAlertCircle strokeWidth={2} size={14} />
- )
- }
- >
- {!table.rls_enabled
- ? 'RLS is not enabled'
- : `${policies.length == 0 ? 'No' : policies.length} active RLS polic${
- policies.length > 1 || policies.length == 0 ? 'ies' : 'y'
- }`}
- </Button>
- </a>
- </Link>
+ {table.rls_enabled ||
+ (showRLSWarning && (
+ <Link href={`/project/${projectRef}/auth/policies?search=${table.id}`}>
+ <a>
+ <Button
+ type={table.rls_enabled ? 'link' : 'warning'}
+ icon={
+ table.rls_enabled ? (
+ <IconLock strokeWidth={2} size={14} />
+ ) : (
+ <IconAlertCircle strokeWidth={2} size={14} />
+ )
+ }
+ >
+ {!table.rls_enabled
+ ? rlsDisabledMessage
+ : `${policies.length == 0 ? 'No' : policies.length} active RLS polic${
+ policies.length > 1 || policies.length == 0 ? 'ies' : 'y'
+ }`}
+ </Button>
+ </a>
+ </Link>
+ ))}
+
<div className="mt-[1px]">
<RenderAPIPreviewToggle />
</div>