代码之家  ›  专栏  ›  技术社区  ›  AndersD

Subasese realtime-RLS仅允许在url/xyz与DB列中的xyz匹配时删除

  •  0
  • AndersD  · 技术社区  · 2 年前

    我有一个表“boards”,其中有一列“edit_link”,这是一个随机字符串

    我希望访问页面/edit/xyz的用户只能更新和删除edit_link为xyz的行。实时。

    我使用RPC对此进行了研究: Postgresql: remove ability to query every row in a table 但不确定这是否能转化为实时。

    我还没有找到在调用RLS策略时发送参数的方法

    我该如何在Supadase限制它?没有登录。只是一个唯一的url。

    0 回复  |  直到 2 年前
        1
  •  0
  •   Zegarek    2 年前

    您可以使用 custom setting 要保持当前链接,请从RLS中引用它: demo

    create policy only_xyz on boards 
    using      (edit_link = current_setting('public.pass_the_current_link_here',true)) 
    with check (edit_link = current_setting('public.pass_the_current_link_here',true));
    

    只要该设置始终设置为当前访问的页面,它将只允许对属于它的行进行操作:

    set foo.pass_the_current_link_here='example.com/mypage';
    
    insert into boards (edit_link) values ('example.com/mypage') returning *;
    
    id 编辑链接
    2. example.com/mypage

    设置不匹配将在上引发错误 insert :

    insert into boards (edit_link) values ('example.com/another_page') returning *;
    
    ERROR:  new row violates row-level security policy for table "boards"
    

    全部的 select , update , merge delete 语句将一无所获,除非设置匹配。如果 使现代化 合并 新值和旧值都需要匹配(策略中的规则 using 部分强制新的匹配, with check 保护旧的):

    delete from boards where edit_link='example.com/another_page' returning *;
    
    DELETE 0
    
    update boards set edit_link='example.com/mypage' 
      where edit_link='example.com/another_page' 
      returning *;
    
    UPDATE 0