vkashti / supabase / migrations / 20250301121235_remote_schema.sql
20250301121235_remote_schema.sql
Raw
drop policy "can delete answers" on "public"."quiz_answers";

drop policy "manage answers" on "public"."quiz_answers";

drop policy "can manage quizes" on "public"."quizes";

drop function if exists "public"."is_admin"();

alter table "public"."quizes" add column "url" text;

set check_function_bodies = off;

CREATE OR REPLACE FUNCTION public.create_profile()
 RETURNS trigger
 LANGUAGE plpgsql
 SECURITY DEFINER
AS $function$BEGIN
    INSERT INTO public.profiles (email, first_name, last_name, user_id)
    VALUES (NEW.email, '-', '-', NEW.id);
    
    RETURN NEW;
END;$function$
;

create policy "Allow team members to select profiles"
on "public"."profiles"
as permissive
for select
to authenticated
using ((EXISTS ( SELECT 1
   FROM user_roles
  WHERE ((user_roles.user_id = ( SELECT auth.uid() AS uid)) AND (user_roles.role = 'team'::text)))));


create policy "Allow admins and team members to select quiz answers"
on "public"."quiz_answers"
as permissive
for select
to authenticated
using ((EXISTS ( SELECT 1
   FROM user_roles
  WHERE ((user_roles.user_id = ( SELECT auth.uid() AS uid)) AND (user_roles.role = ANY (ARRAY['admin'::text, 'team'::text]))))));


create policy "Allow admins and team members to update quiz answers"
on "public"."quiz_answers"
as permissive
for update
to authenticated
using ((EXISTS ( SELECT 1
   FROM user_roles
  WHERE ((user_roles.user_id = ( SELECT auth.uid() AS uid)) AND (user_roles.role = ANY (ARRAY['admin'::text, 'team'::text]))))))
with check ((EXISTS ( SELECT 1
   FROM user_roles
  WHERE ((user_roles.user_id = ( SELECT auth.uid() AS uid)) AND (user_roles.role = ANY (ARRAY['admin'::text, 'team'::text]))))));


create policy "Allow team and admin to delete quiz answers"
on "public"."quiz_answers"
as permissive
for delete
to authenticated
using ((EXISTS ( SELECT 1
   FROM user_roles
  WHERE ((user_roles.user_id = ( SELECT auth.uid() AS uid)) AND (user_roles.role = ANY (ARRAY['team'::text, 'admin'::text]))))));


create policy "Allow admins and team to create quizes"
on "public"."quizes"
as permissive
for insert
to authenticated
with check ((EXISTS ( SELECT 1
   FROM user_roles
  WHERE ((user_roles.user_id = ( SELECT auth.uid() AS uid)) AND (user_roles.role = ANY (ARRAY['admin'::text, 'team'::text]))))));


create policy "Allow admins and team to delete quizes"
on "public"."quizes"
as permissive
for delete
to authenticated
using ((EXISTS ( SELECT 1
   FROM user_roles
  WHERE ((user_roles.user_id = ( SELECT auth.uid() AS uid)) AND (user_roles.role = ANY (ARRAY['admin'::text, 'team'::text]))))));


create policy "Allow admins and team to update quizes"
on "public"."quizes"
as permissive
for update
to authenticated
using ((EXISTS ( SELECT 1
   FROM user_roles
  WHERE ((user_roles.user_id = ( SELECT auth.uid() AS uid)) AND (user_roles.role = ANY (ARRAY['admin'::text, 'team'::text]))))))
with check ((EXISTS ( SELECT 1
   FROM user_roles
  WHERE ((user_roles.user_id = ( SELECT auth.uid() AS uid)) AND (user_roles.role = ANY (ARRAY['admin'::text, 'team'::text]))))));