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'}`}
.
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.
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.
{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.`}
{schema}.{name}
{noPolicies || falseOnlyPolicy ? 'Returns no rows' : !isRLSEnabled || !!trueOnlyPolicy ? 'Returns all rows' : null}
)}{policies.length} {policies.length > 1 ? 'policies' : 'policy'} applied
{policy.name}
{operation === 'SELECT' ? 'Show rows' : `Allow ${operation?.toLocaleLowerCase()}s`}{' '}
where:{' '}
{policy.definition ?? policy.check}