-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhasura-step3-policies.sql
More file actions
38 lines (31 loc) · 1.63 KB
/
hasura-step3-policies.sql
File metadata and controls
38 lines (31 loc) · 1.63 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
-- Step 3: Create RLS Policies
-- Run this after RLS is enabled
-- Updated to use current_setting for Nhost/Hasura compatibility
-- 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);