import { Check, ChevronDown, Edit, X } from 'lucide-react' import { useMemo } from 'react' import { cn, Collapsible, CollapsibleContent, CollapsibleTrigger, WarningIcon } from 'ui' import type { Policy } from '@/components/interfaces/Database/Policies/PolicyTableRow/PolicyTableRow.utils' import { ButtonTooltip } from '@/components/ui/ButtonTooltip' import { type ParseSQLQueryOperations } from '@/data/misc/parse-query-mutation' interface RLSTableCardProps { table: { schema: string; name: string; isRLSEnabled: boolean } operation: ParseSQLQueryOperations role?: string policies: Policy[] hasError: boolean handleSelectEditPolicy: (policy: Policy) => void } export const RLSTableCard = ({ table, operation, role, policies, hasError, handleSelectEditPolicy, }: RLSTableCardProps) => { const { schema, name, isRLSEnabled } = table const trueOnlyPolicy = policies.find((x) => x.definition === 'true') const falseOnlyPolicy = policies.find((x) => x.definition === 'false') const noPolicies = isRLSEnabled && policies.length === 0 const tableAccessDescription = useMemo(() => { if (!isRLSEnabled) { return (

RLS is disabled and all data is publicly accessible. We highly recommend enabling RLS and adding policies to restrict access.

) } if (noPolicies) { return (

RLS is enabled but no policies exist for the{' '} {role} role on this table -{' '} {operation === 'SELECT' ? 'no data will be returned' : `no data will be ${operation?.toLowerCase()}${operation?.toLowerCase().endsWith('e') ? 'd' : 'ed'}`} .

) } if (trueOnlyPolicy) { return ( <>

The policy "{trueOnlyPolicy.name}" for the{' '} {role} role on this table evaluates to{' '} true, so all data from this query is accessible to this user.

) } if (falseOnlyPolicy) { return ( <>

The policy "{falseOnlyPolicy.name}" for the{' '} {role} role on this table evaluates to{' '} false, so no data from this query is accessible to this user.

) } return ( <>

{policies.length} {policies.length > 1 ? 'policies apply' : 'policy applies'} for the{' '} {role} role on this table.{' '} {operation === 'SELECT' ? `Only rows that match ${policies.length > 1 ? 'these conditions' : 'this condition'} are returned.` : `The ${operation} operation will only be successful if the conditions are matched.`}

) }, [ isRLSEnabled, noPolicies, trueOnlyPolicy, falseOnlyPolicy, policies, role, operation, handleSelectEditPolicy, ]) return (
{!isRLSEnabled ? ( ) : (hasError && operation === 'INSERT') || noPolicies || falseOnlyPolicy ? ( ) : ( )}

{schema}.{name}

{operation === 'SELECT' && (

{noPolicies || falseOnlyPolicy ? 'Returns no rows' : !isRLSEnabled || !!trueOnlyPolicy ? 'Returns all rows' : null}

)}
{tableAccessDescription}
) } const TableAccessPolicySummary = ({ policies, operation, handleSelectEditPolicy, }: { policies: Policy[] operation: ParseSQLQueryOperations handleSelectEditPolicy: (policy: Policy) => void }) => { return (

{policies.length} {policies.length > 1 ? 'policies' : 'policy'} applied

    {policies.map((policy) => (
  • {policy.name}

    {operation === 'SELECT' ? 'Show rows' : `Allow ${operation?.toLocaleLowerCase()}s`}{' '} where:{' '} {policy.definition ?? policy.check}

    } className="w-7" tooltip={{ content: { side: 'bottom', text: 'Edit policy' } }} onClick={() => { handleSelectEditPolicy(policy) }} />
  • ))}
) }