|
| 1 | +-- Fix permissions for document_generations table |
| 2 | + |
| 3 | +-- First, ensure the table has RLS enabled |
| 4 | +ALTER TABLE document_generations ENABLE ROW LEVEL SECURITY; |
| 5 | + |
| 6 | +-- Drop all existing policies to start fresh |
| 7 | +DROP POLICY IF EXISTS document_generations_select_policy ON document_generations; |
| 8 | +DROP POLICY IF EXISTS document_generations_insert_policy ON document_generations; |
| 9 | +DROP POLICY IF EXISTS document_generations_all_policy ON document_generations; |
| 10 | +DROP POLICY IF EXISTS document_generations_service_role_policy ON document_generations; |
| 11 | + |
| 12 | +-- Create a policy for authenticated users to select their own document generations |
| 13 | +CREATE POLICY document_generations_select_policy ON document_generations |
| 14 | + FOR SELECT |
| 15 | + TO authenticated |
| 16 | + USING ( |
| 17 | + user_id = (SELECT id FROM users WHERE email = auth.uid()::text) |
| 18 | + ); |
| 19 | + |
| 20 | +-- Create a policy for authenticated users to insert their own document generations |
| 21 | +CREATE POLICY document_generations_insert_policy ON document_generations |
| 22 | + FOR INSERT |
| 23 | + TO authenticated |
| 24 | + WITH CHECK ( |
| 25 | + user_id = (SELECT id FROM users WHERE email = auth.uid()::text) |
| 26 | + ); |
| 27 | + |
| 28 | +-- Create a policy for the service role to manage all document generations |
| 29 | +CREATE POLICY document_generations_service_role_policy ON document_generations |
| 30 | + FOR ALL |
| 31 | + TO service_role |
| 32 | + USING (true); |
| 33 | + |
| 34 | +-- Grant specific permissions to authenticated users |
| 35 | +GRANT SELECT, INSERT ON document_generations TO authenticated; |
| 36 | + |
| 37 | +-- Grant all permissions to service_role |
| 38 | +GRANT ALL ON document_generations TO service_role; |
| 39 | + |
| 40 | +-- Log the changes |
| 41 | +DO $$ |
| 42 | +BEGIN |
| 43 | + RAISE NOTICE 'Fixed permissions for document_generations table'; |
| 44 | + RAISE NOTICE 'Created policies for authenticated users'; |
| 45 | + RAISE NOTICE 'Granted explicit permissions to roles'; |
| 46 | +END $$; |
0 commit comments