-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
44 additions
and
0 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
mirror-2/supabase/migrations/20240929062844_collaborators.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
|
||
-- Create the space_user_collaborators table | ||
create table space_user_collaborators ( | ||
id uuid not null primary key, | ||
space_id uuid references spaces(id) on delete cascade not null, | ||
user_id uuid references auth.users(id) on delete cascade not null, | ||
created_at timestamp with time zone not null default now(), | ||
constraint unique_space_user unique(space_id, user_id) | ||
); | ||
|
||
-- Enable RLS for space_user_collaborators | ||
alter table space_user_collaborators | ||
enable row level security; | ||
|
||
-- Only space owners can add collaborators | ||
create policy "Only space owners can add collaborators" | ||
on space_user_collaborators | ||
for insert | ||
with check ( | ||
exists ( | ||
select 1 from spaces | ||
where spaces.id = space_id | ||
and spaces.creator_user_id = auth.uid() | ||
) | ||
); | ||
|
||
-- Policy for selecting space_user_collaborators | ||
create policy "Users can view where they are a collaborator" | ||
on space_user_collaborators | ||
for select | ||
using (user_id = auth.uid()); | ||
|
||
|
||
-- Policy for collaborators to view spaces | ||
create policy "Collaborators can view spaces" | ||
on spaces | ||
for select | ||
using ( | ||
exists ( | ||
select 1 from space_user_collaborators | ||
where space_user_collaborators.space_id = spaces.id | ||
and space_user_collaborators.user_id = auth.uid() | ||
) | ||
); |