File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ -- Add INSERT policy for document_generations table
2+ -- This allows users to insert records only for their own user_id
3+ CREATE POLICY document_generations_insert_policy ON document_generations
4+ FOR INSERT
5+ WITH CHECK (
6+ user_id IN (
7+ SELECT id FROM users WHERE email = auth .uid ()::text
8+ )
9+ );
10+
11+ -- Verify the SELECT policy exists (it should already be there from previous migrations)
12+ -- This ensures users can only see their own document generations
13+ DO $$
14+ DECLARE
15+ policy_exists boolean ;
16+ BEGIN
17+ SELECT EXISTS (
18+ SELECT 1 FROM pg_policies
19+ WHERE schemaname = ' public'
20+ AND tablename = ' document_generations'
21+ AND policyname = ' document_generations_select_policy'
22+ ) INTO policy_exists;
23+
24+ IF NOT policy_exists THEN
25+ EXECUTE ' CREATE POLICY document_generations_select_policy ON document_generations
26+ FOR SELECT
27+ USING (
28+ user_id IN (
29+ SELECT id FROM users WHERE email = auth.uid()::text
30+ )
31+ )' ;
32+ RAISE NOTICE ' Created SELECT policy for document_generations table' ;
33+ ELSE
34+ RAISE NOTICE ' SELECT policy for document_generations table already exists' ;
35+ END IF;
36+ END $$;
37+
38+ -- Ensure the table has RLS enabled
39+ ALTER TABLE document_generations ENABLE ROW LEVEL SECURITY;
40+
41+ -- Log the change
42+ DO $$
43+ BEGIN
44+ RAISE NOTICE ' Added INSERT policy for document_generations table' ;
45+ RAISE NOTICE ' Document generations are now properly secured - users can only insert and view their own records' ;
46+ END $$;
You can’t perform that action at this time.
0 commit comments