Skip to content

Commit ebcd290

Browse files
committed
add doc policy to db
1 parent c67f354 commit ebcd290

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 $$;

0 commit comments

Comments
 (0)