Skip to content

Row Level Security (RLS)

All PIP AI tables have RLS enabled. Policies enforce data isolation and access control.

Helper Functions

pip_ai_check_is_admin()

sql
CREATE FUNCTION pip_ai_check_is_admin()
RETURNS boolean
LANGUAGE plpgsql
SECURITY DEFINER  -- Bypasses RLS to avoid recursion
SET search_path = 'public'
AS $$
BEGIN
  RETURN EXISTS (
    SELECT 1 FROM pip_ai_user_profiles
    WHERE id = auth.uid() AND role = 'admin'
  );
END;
$$;

Why SECURITY DEFINER?

RLS policies on pip_ai_user_profiles that query the same table cause infinite recursion. Using SECURITY DEFINER functions that bypass RLS avoids this problem.

pip_ai_is_project_member(project_id)

Returns true if the current user is a member of the specified project.

pip_ai_can_edit_project(project_id)

Returns true if the current user has edit access (owner, editor, or admin).

Policy Summary

TableSELECTINSERTUPDATEDELETE
projectsMember or AdminAuthenticatedEditor/Owner/AdminOwner/Admin
project_membersMember or AdminEditor/Owner/AdminOwner or SelfOwner or Self
documentsMember or AdminEditor/Owner/AdminEditor/Owner/AdminEditor/Owner/Admin
spec_uploadsAdmin onlyAdmin onlyAdmin onlyAdmin only
spec_sectionsAdmin or brand matchAdmin onlyAdmin onlyAdmin only
pip_itemsMember or AdminEditor/Owner/AdminEditor/Owner/AdminEditor/Owner/Admin
matchesVia PIP membershipVia PIP editVia PIP editVia PIP edit
floor_plansMember or AdminEditor/Owner/AdminEditor/Owner/AdminEditor/Owner/Admin
floor_plan_elementsVia floor plan membershipVia floor plan editVia floor plan editVia floor plan edit
user_profilesOwn or AdminSystem triggerOwn or Admin
upload_jobsAdmin onlyAdmin onlyAdmin onlyAdmin only

Brand Isolation Policy

The critical security policy that prevents cross-brand data leakage:

sql
CREATE POLICY "spec_sections_select_scoped"
ON pip_ai_spec_sections FOR SELECT
USING (
  pip_ai_check_is_admin()
  OR brand_name IN (
    SELECT DISTINCT brand_name
    FROM pip_ai_projects
    WHERE created_by = auth.uid()
  )
);

Users can only see specs for brands they have projects in.

Built with VitePress