38980-vm/app-9w9pd00g5j41/supabase/migrations/00054_fix_anonymous_trip_rls_policies.sql
2026-03-04 18:25:09 +00:00

206 lines
5.5 KiB
PL/PgSQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- RLS politikalarını düzelt - daha pratik yaklaşım
-- Token kontrolü application layer'da yapılacak
-- 1. Önceki politikaları kaldır
DROP POLICY IF EXISTS "Güvenli seyahat görüntüleme" ON trips;
DROP POLICY IF EXISTS "Güvenli seyahat güncelleme" ON trips;
DROP POLICY IF EXISTS "Güvenli seyahat silme" ON trips;
DROP POLICY IF EXISTS "Güvenli seyahat günleri yönetimi" ON trip_days;
DROP POLICY IF EXISTS "Güvenli seyahat yerleri yönetimi" ON trip_places;
-- 2. Trips için yeni politikalar
-- SELECT: Public geziler veya kendi gezileri (anonim geziler için client-side token kontrolü)
CREATE POLICY "Seyahatleri görüntüleme"
ON trips FOR SELECT
USING (
is_public = true
OR user_id = auth.uid()
);
-- INSERT: Herkes oluşturabilir (anonim veya kayıtlı)
CREATE POLICY "Seyahat oluşturma"
ON trips FOR INSERT
WITH CHECK (
user_id = auth.uid() OR user_id IS NULL
);
-- UPDATE: Sadece kendi gezileri (RPC fonksiyonu token kontrolü yapacak)
CREATE POLICY "Seyahat güncelleme"
ON trips FOR UPDATE
USING (user_id = auth.uid())
WITH CHECK (user_id = auth.uid());
-- DELETE: Sadece kendi gezileri (RPC fonksiyonu token kontrolü yapacak)
CREATE POLICY "Seyahat silme"
ON trips FOR DELETE
USING (user_id = auth.uid());
-- 3. trip_days için politikalar
CREATE POLICY "Seyahat günleri yönetimi"
ON trip_days FOR ALL
USING (
EXISTS (
SELECT 1 FROM trips
WHERE trips.id = trip_days.trip_id
AND (trips.is_public = true OR trips.user_id = auth.uid())
)
)
WITH CHECK (
EXISTS (
SELECT 1 FROM trips
WHERE trips.id = trip_days.trip_id
AND trips.user_id = auth.uid()
)
);
-- 4. trip_places için politikalar
CREATE POLICY "Seyahat yerleri yönetimi"
ON trip_places FOR ALL
USING (
EXISTS (
SELECT 1 FROM trip_days
JOIN trips ON trips.id = trip_days.trip_id
WHERE trip_days.id = trip_places.trip_day_id
AND (trips.is_public = true OR trips.user_id = auth.uid())
)
)
WITH CHECK (
EXISTS (
SELECT 1 FROM trip_days
JOIN trips ON trips.id = trip_days.trip_id
WHERE trip_days.id = trip_places.trip_day_id
AND trips.user_id = auth.uid()
)
);
-- 5. Anonim gezi için güncelleme RPC fonksiyonu
CREATE OR REPLACE FUNCTION update_anonymous_trip(
trip_id_param UUID,
token_param TEXT,
updates JSONB
)
RETURNS trips
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
DECLARE
result trips;
BEGIN
-- Token kontrolü yap
IF NOT EXISTS (
SELECT 1 FROM trips
WHERE id = trip_id_param
AND user_id IS NULL
AND anonymous_token = token_param
) THEN
RAISE EXCEPTION 'Geçersiz token veya gezi bulunamadı';
END IF;
-- Güncelleme yap
UPDATE trips
SET
title = COALESCE((updates->>'title')::TEXT, title),
description = COALESCE((updates->>'description')::TEXT, description),
destination = COALESCE((updates->>'destination')::TEXT, destination),
start_date = COALESCE((updates->>'start_date')::DATE, start_date),
end_date = COALESCE((updates->>'end_date')::DATE, end_date),
cover_image = COALESCE((updates->>'cover_image')::TEXT, cover_image),
is_public = COALESCE((updates->>'is_public')::BOOLEAN, is_public),
interests = COALESCE((updates->>'interests')::TEXT[], interests),
start_location_type = COALESCE((updates->>'start_location_type')::TEXT, start_location_type),
start_location_name = COALESCE((updates->>'start_location_name')::TEXT, start_location_name),
start_lat = COALESCE((updates->>'start_lat')::DOUBLE PRECISION, start_lat),
start_lng = COALESCE((updates->>'start_lng')::DOUBLE PRECISION, start_lng),
has_balloon = COALESCE((updates->>'has_balloon')::BOOLEAN, has_balloon),
updated_at = NOW()
WHERE id = trip_id_param
RETURNING * INTO result;
RETURN result;
END;
$$;
-- 6. Anonim gezi için silme RPC fonksiyonu
CREATE OR REPLACE FUNCTION delete_anonymous_trip(
trip_id_param UUID,
token_param TEXT
)
RETURNS BOOLEAN
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
BEGIN
-- Token kontrolü yap ve sil
DELETE FROM trips
WHERE
id = trip_id_param
AND user_id IS NULL
AND anonymous_token = token_param;
RETURN FOUND;
END;
$$;
-- 7. Anonim gezi için okuma RPC fonksiyonu (token ile)
CREATE OR REPLACE FUNCTION get_anonymous_trip(
trip_id_param UUID,
token_param TEXT
)
RETURNS TABLE (
id UUID,
user_id UUID,
title TEXT,
description TEXT,
destination TEXT,
start_date DATE,
end_date DATE,
cover_image TEXT,
is_public BOOLEAN,
interests TEXT[],
created_at TIMESTAMPTZ,
updated_at TIMESTAMPTZ,
public_slug TEXT,
start_location_type TEXT,
start_location_name TEXT,
start_lat DOUBLE PRECISION,
start_lng DOUBLE PRECISION,
has_balloon BOOLEAN
)
LANGUAGE plpgsql
SECURITY DEFINER
AS $$
BEGIN
RETURN QUERY
SELECT
t.id,
t.user_id,
t.title,
t.description,
t.destination,
t.start_date,
t.end_date,
t.cover_image,
t.is_public,
t.interests,
t.created_at,
t.updated_at,
t.public_slug,
t.start_location_type,
t.start_location_name,
t.start_lat,
t.start_lng,
t.has_balloon
FROM trips t
WHERE
t.id = trip_id_param
AND t.user_id IS NULL
AND t.anonymous_token = token_param;
END;
$$;
-- 8. Fonksiyonlara yorum ekle
COMMENT ON FUNCTION update_anonymous_trip IS 'Token ile anonim bir geziyi günceller';
COMMENT ON FUNCTION delete_anonymous_trip IS 'Token ile anonim bir geziyi siler';
COMMENT ON FUNCTION get_anonymous_trip IS 'Token ile anonim bir geziyi getirir';