HorseShoe
08/25/2021, 5:24 PMHorseShoe
08/25/2021, 5:24 PMHorseShoe
08/25/2021, 5:25 PMselect msg.name, usr.name as user from messages msg, users usr
user
08/25/2021, 6:30 PMPeanut
08/26/2021, 4:04 AMCREATE FUNCTION auth.firebase_uid()
RETURNS TEXT
AS $$
SELECT nullif(current_setting('request.jwt.claim.sub', true), '')::TEXT
$$ LANGUAGE sql;
CREATE FUNCTION getIsUserEditorOrAdmin(id TEXT)
RETURNS BOOLEAN
AS $$
SELECT role = 'admin' OR role = 'editor' FROM userAdminMeta WHERE id = $1
$$ LANGUAGE sql;
CREATE POLICY "Users can update their own profile OR staff can update any profile."
ON users
FOR UPDATE USING (
auth.firebase_uid()::TEXT = users.id OR
getIsUserEditorOrAdmin(auth.firebase_uid()) = TRUE
);
This doesn't seem to work:
postgREST returns 404
getIsUserEditorOrAdmin returns "true" for my user ID (which I provide using a JWT token)
If I update my OWN user it works perfect
It looks like an issue with my getIsUserEditorOrAdmin() function. Have I defined it wrong? Do I need to change how it returns?jason-lynx
08/26/2021, 8:13 AMselect
before it?
CREATE POLICY "Users can update their own profile OR staff can update any profile."
ON users
FOR UPDATE USING (
auth.firebase_uid()::TEXT = users.id OR
(SELECT getIsUserEditorOrAdmin(auth.firebase_uid())) = TRUE
);
jason-lynx
08/26/2021, 8:22 AMjason-lynx
08/26/2021, 8:22 AMPeanut
08/26/2021, 8:42 AMCREATE POLICY "Users can update their own profile OR staff can update any profile."
ON users
FOR UPDATE USING (
auth.firebase_uid()::TEXT = users.id OR
(SELECT(getIsUserEditorOrAdmin(auth.firebase_uid())) = TRUE)
);
Peanut
08/26/2021, 8:43 AMgetIsUderEditorAdmin()
call I get a 204 (success). So definitely the policyPeanut
08/26/2021, 8:44 AMPATCH {{serverurl}}/rest/v1/users?id=eq.abcdef
where the doc id is a real idPeanut
08/26/2021, 8:44 AMjason-lynx
08/26/2021, 9:07 AMSET LOCAL request.jwt.claim.sub = <YOUR USER ID>;
SET LOCAL ROLE authenticated;
SELECT getIsUserEditorOrAdmin(auth.firebase_uid()) = TRUE
this should return truejason-lynx
08/26/2021, 9:08 AMIS TRUE
instead of = TRUE
but it shouldnt make a diff in this case i think...)Peanut
08/26/2021, 9:09 AMPeanut
08/26/2021, 9:09 AMpostgres
using my own pg client)jason-lynx
08/26/2021, 9:10 AMpostgres
doesnt have permission to do thatjason-lynx
08/26/2021, 9:10 AMPeanut
08/26/2021, 9:13 AMjason-lynx
08/26/2021, 9:13 AMPeanut
08/26/2021, 9:14 AMmissing FROM-clause entry for table "users"
so progress!Peanut
08/26/2021, 9:14 AMPeanut
08/26/2021, 9:15 AMusers
comes from the policyjason-lynx
08/26/2021, 9:19 AMjason-lynx
08/26/2021, 9:19 AMPeanut
08/26/2021, 9:20 AMSET LOCAL request.jwt.claim.sub = 'MY USER ID';
SET LOCAL ROLE authenticated;
SELECT getIsUserEditorOrAdmin(auth.firebase_uid()) = TRUE
> Success. No rows returned
Peanut
08/26/2021, 9:24 AMPeanut
08/26/2021, 9:25 AMCREATE POLICY "Everyone can read all users."
ON users
FOR SELECT USING (
true
);
jason-lynx
08/26/2021, 9:27 AMSET request.jwt.claim.sub = 'MY USER ID';
SET ROLE authenticated;
then remove that code, then run the remaining SELECT getIsUserEditorOrAdmin(auth.firebase_uid()) = TRUE
btw later once you're done debugging all this, remember to set it back to what it was:
SET request.jwt.claim.sub = '';
SET ROLE supabase_admin
;
Peanut
08/26/2021, 9:28 AM