florian-lefebvre
08/15/2021, 8:20 AMSETY
08/15/2021, 12:52 PMflorian-lefebvre
08/15/2021, 1:42 PMSETY
08/15/2021, 1:44 PMdrop table if exists user_type;
drop type if exists userType;
-- Create an enum
create type userType as ENUM ('user', 'admin');
-- Create User Roles Table;
create table user_type (
id uuid references auth.users not null,
type userType not null default 'user',
primary key (id)
);
-- enable RLS
alter table user_type enable row level security;
-- create function for getting userType
create or replace function get_user_type(id uuid, out result varchar)
RETURNS varchar as
$$
BEGIN
select userType INTO result from user_type where user_type.id = id LIMIT 1;
IF NOT FOUND THEN
result := 'null';
END IF;
END
$$ language plpgsql;
-- create function for adding user to types at signup
CREATE OR REPLACE FUNCTION give_user_default_role() RETURNS TRIGGER as
$$
BEGIN
insert into public.user_type(id) VALUES (new.id);
return new;
END
$$ language plpgsql security definer;
--Use the function when auth.users gets an insert
drop trigger if exists default_user_type on auth.users;
create trigger default_user_type after insert on auth.users execute procedure give_user_default_role();
I am getting errors when i try to test this with new users, how do I see the new users?SETY
08/15/2021, 1:44 PMSETY
08/15/2021, 2:00 PMSETY
08/15/2021, 2:15 PMSETY
08/15/2021, 2:24 PMburggraf
08/15/2021, 10:07 PMPeanut
08/16/2021, 10:05 AMALTER TABLE users
ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Everyone can read all users."
ON users FOR SELECT USING (
true
);
CREATE POLICY "Users can update their own profile."
ON users FOR UPDATE USING (
auth.firebase_uid()::TEXT = id
);
CREATE FUNCTION auth.firebase_uid() RETURNS TEXT LANGUAGE sql AS $$
BEGIN
SELECT nullif(current_setting('request.jwt.claim.sub', true), '')::TEXT;
END $$;
I can read all documents but get I get a generic postgrest permission error in my frontend app on UPDATE.Scott P
08/16/2021, 1:22 PMauth.uid()::TEXT = id
or auth.uid() = id::UUID
work? Do you have a reason for not having your id
column set as uuid
type?Di
08/16/2021, 10:48 PMcreate policy write on table for insert with check (true);
be equivalent to grant insert on table
?
I noticed the examples used the former so I was wondering why not the latter.Peanut
08/17/2021, 12:19 AMauth.uid() = id::UUID
and it didnt workPeanut
08/17/2021, 12:25 AMjon.m
08/17/2021, 1:40 AMCREATE TABLE profiles (
id bigint generated by default as identity primary key,
user_id uuid references auth.users on delete cascade,
jon.m
08/17/2021, 1:40 AMDeleting user failed: update or delete on table "users" violates foreign key constraint "profiles_user_id_fkey" on table "profiles"
jon.m
08/17/2021, 1:41 AMuser
08/17/2021, 10:35 AMpublic.collaborators
table that models the many-to-many relationship between users in auth.users
and the equivalent of a public.resources
table for the resource in question. How do I write a policy that lets collaborators select and update rows from that public.resources
table?user
08/17/2021, 10:36 AMsql
CREATE TABLE public.resources (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
owner_id uuid REFERENCES auth.users(id) NOT NULL,
name TEXT NOT NULL,
invite_code CHARACTER(32) NOT NULL UNIQUE
);
CREATE TABLE public.collaborators (
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
user_id uuid REFERENCES auth.users(id) NOT NULL,
resource_id uuid REFERENCES public.resources(id) NOT NULL,
UNIQUE (user_id, resource_id)
);
user
08/17/2021, 10:42 AMsql
CREATE POLICY "Collaborators can view their resources."
ON resources FOR SELECT
USING ( EXISTS (SELECT * FROM collaborators WHERE resource_id = id AND user_id = auth.uid()) );
burggraf
08/17/2021, 12:34 PMPeanut
08/18/2021, 4:01 AMnet
functions like net.http_post
? I want to fix a function hook now while waiting for a release: https://github.com/supabase/supabase/issues/2871 but I am getting error ERROR: must be owner of function http_post
(I am user postgres
)DyingAlbatross
08/18/2021, 5:39 PMacpatrice
08/18/2021, 8:04 PMdailylurker
08/19/2021, 2:02 PMjianjie
08/19/2021, 2:23 PMbefore insert
dailylurker
08/19/2021, 2:30 PMScott P
08/19/2021, 3:10 PMmy_table
with the name of your table):
BEGIN
INSERT INTO public.my_table (admin_id)
VALUES (auth.uid())
return new;
END;
codart
08/19/2021, 4:09 PMburggraf
08/19/2021, 8:00 PM