ai-chatbot/supabase/migrations/20240216000002_update_rls_for_public_properties.sql
Aymeric Rabot aa27e0dd7a refactor: move supabase config to repo root, fix port and env setup
- Move supabase/ from packages/db/ to repo root for simpler CLI usage
- Use custom ports (553xx) to avoid conflicts with other local Supabase instances
- Add --env-mode=loose to turbo dev so root .env vars reach Next.js
- Remove hardcoded --port 3000 from next dev (reads PORT env var instead)
- Add .env.example with Supabase env var placeholders
- Add supabase local state dirs to .gitignore

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-02-18 01:48:53 -05:00

79 lines
3.0 KiB
SQL

-- Drop existing RLS policies for properties
DROP POLICY IF EXISTS "Users can view their own properties" ON properties;
DROP POLICY IF EXISTS "Users can insert their own properties" ON properties;
DROP POLICY IF EXISTS "Users can update their own properties" ON properties;
DROP POLICY IF EXISTS "Users can delete their own properties" ON properties;
-- New RLS policy: Users can view their own properties OR public properties
CREATE POLICY "Users can view own or public properties"
ON properties FOR SELECT
USING (
owner_id = current_setting('app.user_id', true)::TEXT
OR is_private = false
OR owner_id IS NULL
);
-- Keep other policies the same (insert/update/delete still require ownership)
CREATE POLICY "Users can insert their own properties"
ON properties FOR INSERT
WITH CHECK (owner_id = current_setting('app.user_id', true)::TEXT OR owner_id IS NULL);
CREATE POLICY "Users can update their own properties"
ON properties FOR UPDATE
USING (owner_id = current_setting('app.user_id', true)::TEXT OR owner_id IS NULL);
CREATE POLICY "Users can delete their own properties"
ON properties FOR DELETE
USING (owner_id = current_setting('app.user_id', true)::TEXT OR owner_id IS NULL);
-- Drop existing RLS policies for models
DROP POLICY IF EXISTS "Users can view models of their own properties" ON properties_models;
DROP POLICY IF EXISTS "Users can insert models for their own properties" ON properties_models;
DROP POLICY IF EXISTS "Users can update models of their own properties" ON properties_models;
DROP POLICY IF EXISTS "Users can delete models of their own properties" ON properties_models;
-- Update models policy: Users can view models of their own properties OR public properties
CREATE POLICY "Users can view models of own or public properties"
ON properties_models FOR SELECT
USING (
EXISTS (
SELECT 1 FROM properties
WHERE properties.id = properties_models.property_id
AND (
properties.owner_id = current_setting('app.user_id', true)::TEXT
OR properties.is_private = false
)
)
);
-- Keep other model policies the same
CREATE POLICY "Users can insert models for their own properties"
ON properties_models FOR INSERT
WITH CHECK (
EXISTS (
SELECT 1 FROM properties
WHERE properties.id = properties_models.property_id
AND properties.owner_id = current_setting('app.user_id', true)::TEXT
)
);
CREATE POLICY "Users can update models of their own properties"
ON properties_models FOR UPDATE
USING (
EXISTS (
SELECT 1 FROM properties
WHERE properties.id = properties_models.property_id
AND properties.owner_id = current_setting('app.user_id', true)::TEXT
)
);
CREATE POLICY "Users can delete models of their own properties"
ON properties_models FOR DELETE
USING (
EXISTS (
SELECT 1 FROM properties
WHERE properties.id = properties_models.property_id
AND properties.owner_id = current_setting('app.user_id', true)::TEXT
)
);