0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

next.jsとsupabaseでRow Level Securityを使用して権限による抽出をおこなってみた

Posted at

あなたが提供した情報に基づいて、SupabaseでのRow-Level Security (RLS) ポリシーを設定するプロセスを検討しましょう。まず、public.posts テーブルの作成に関しては問題ありません。次に、提案されたRLSポリシーに関して検討します。

参考資料
https://qiita.com/kaho_eng/items/6f9ac01d77ab198881f4

詳しい!
https://www.docswell.com/s/hmatsu47/5RMEWZ-2022-05-30-232653#p31

ポリシーの設定

  1. 管理者アクセス:
    このポリシーは、「管理者」(profiles.introduceadmin の場合)であれば、全ての投稿を取得できるように設定します。

    create policy "管理者アクセス" on public.posts for select using (
      (SELECT profiles.introduce
       FROM profiles
       WHERE profiles.id = auth.uid()) = 'admin'
    );
    

    このポリシーは、profiles テーブルの introduce カラムをチェックして、その値が admin であるかどうかを確認します。その条件が真の場合、そのユーザーは posts テーブルの全レコードにアクセスできます。

  2. ユーザー別アクセス:
    こちらのポリシーは、ユーザーが自分の投稿のみにアクセスできるように設定します。

    create policy "ユーザー別アクセス" on public.posts for select using (
      auth.uid() = user_id
    );
    

    このポリシーでは、auth.uid()(ログインしたユーザーのID)が posts テーブルの user_id と一致する場合にのみ、その行へのアクセスを許可します。

注意点

  • ポリシーの適用順序: 複数のポリシーが適用される場合、いずれかのポリシーが true を返せば、その行へのアクセスが許可されます。そのため、セキュリティポリシーを慎重に設計する必要があります。

  • テストと検証: ポリシーを実装した後は、異なるユーザーのロールでのアクセスが期待通りに機能するかをテストすることが重要です。

  • プロファイルテーブルとの関連付け: この設定は profiles テーブルが存在し、適切に設定されていることを前提としています。profiles テーブルにはユーザーIDと introduce カラムが必要です。

以上のポリシー設定は、あなたが記述した要件に基づいていますが、実際のアプリケーションのセキュリティニーズに応じて調整する必要があるかもしれません。常にセキュリティのベストプラクティスに従い、慎重にポリシーを設計してください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?