-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhasura-policies-only.sql
More file actions
54 lines (44 loc) · 2.61 KB
/
hasura-policies-only.sql
File metadata and controls
54 lines (44 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
-- Run this if tables already exist but policies/indexes are missing
-- Create indexes for better performance (will skip if they exist)
CREATE INDEX IF NOT EXISTS idx_chats_user_id ON public.chats(user_id);
CREATE INDEX IF NOT EXISTS idx_messages_chat_id ON public.messages(chat_id);
CREATE INDEX IF NOT EXISTS idx_messages_user_id ON public.messages(user_id);
-- Enable RLS (Row Level Security) - safe to run multiple times
ALTER TABLE public.chats ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.messages ENABLE ROW LEVEL SECURITY;
-- Drop existing policies if they exist (to avoid conflicts)
DROP POLICY IF EXISTS "Users can view their own chats" ON public.chats;
DROP POLICY IF EXISTS "Users can insert their own chats" ON public.chats;
DROP POLICY IF EXISTS "Users can update their own chats" ON public.chats;
DROP POLICY IF EXISTS "Users can delete their own chats" ON public.chats;
DROP POLICY IF EXISTS "Users can view messages in their chats" ON public.messages;
DROP POLICY IF EXISTS "Users can insert messages in their chats" ON public.messages;
DROP POLICY IF EXISTS "Bots can insert messages" ON public.messages;
-- RLS Policies for chats table
CREATE POLICY "Users can view their own chats" ON public.chats
FOR SELECT USING (current_setting('request.jwt.claims', true)::json->>'x-hasura-user-id' = user_id::text);
CREATE POLICY "Users can insert their own chats" ON public.chats
FOR INSERT WITH CHECK (current_setting('request.jwt.claims', true)::json->>'x-hasura-user-id' = user_id::text);
CREATE POLICY "Users can update their own chats" ON public.chats
FOR UPDATE USING (current_setting('request.jwt.claims', true)::json->>'x-hasura-user-id' = user_id::text);
CREATE POLICY "Users can delete their own chats" ON public.chats
FOR DELETE USING (current_setting('request.jwt.claims', true)::json->>'x-hasura-user-id' = user_id::text);
-- RLS Policies for messages table
CREATE POLICY "Users can view messages in their chats" ON public.messages
FOR SELECT USING (
EXISTS (
SELECT 1 FROM public.chats
WHERE chats.id = messages.chat_id
AND current_setting('request.jwt.claims', true)::json->>'x-hasura-user-id' = chats.user_id::text
)
);
CREATE POLICY "Users can insert messages in their chats" ON public.messages
FOR INSERT WITH CHECK (
EXISTS (
SELECT 1 FROM public.chats
WHERE chats.id = messages.chat_id
AND current_setting('request.jwt.claims', true)::json->>'x-hasura-user-id' = chats.user_id::text
)
);
CREATE POLICY "Bots can insert messages" ON public.messages
FOR INSERT WITH CHECK (is_bot = true);