RLSとカスタムクレームによるロールベースアクセス
JWTクレームとヘルパー関数を使い、ユーザーロールごとに異なるアクセスを許可する高度な行レベルセキュリティ(RLS)ポリシーを構築します。
「RLSとカスタムクレームによるロールベースアクセス」はCoddyKit上の無料Supabase Backend as a Serviceレッスンです。 これはレッスン3/3です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSupabase Backend as a Service学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Supabase Backend as a Serviceコースには全3レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
Beyond Owner-Only Policies
Basic RLS often checks auth.uid() for ownership. Real apps also need role-based rules, like admins seeing everything and editors seeing more than viewers.
Where Roles Live
You can store roles in a column on a profiles table, or embed them as custom claims in the user's JWT for fast, joinless checks.
Reading the JWT in SQL
Supabase exposes the JWT via auth.jwt(). You can read custom claims from it.
-- returns the 'user_role' claim as text
select auth.jwt() ->> 'user_role';A Role Helper Function
Wrap the claim read in a helper so policies stay readable.
create or replace function current_role_name()
returns text
language sql stable
as 'select coalesce(auth.jwt() ->> ''user_role'', ''viewer'')';Admin-Sees-All Policy
Grant admins unrestricted read access while others are limited.
create policy admin_read on documents
for select
using (current_role_name() = 'admin');Combining Ownership and Role
Policies can OR conditions: a user sees a row if they own it OR they are an admin.
create policy read_own_or_admin on documents
for select
using (
owner_id = auth.uid()
or current_role_name() = 'admin'
);Separate Policies per Action
Define distinct policies for SELECT, INSERT, UPDATE, DELETE so each action has the right rule, for example editors can update but viewers cannot.
Modeling a Role Hierarchy
Map roles to permission levels so you can compare numerically.
function level(role) {
return { viewer: 1, editor: 2, admin: 3 }[role] || 0;
}
console.log(level('editor') >= level('viewer'));Setting Custom Claims
Custom claims are added to the JWT via an auth hook or by a trusted server using the admin API. Never let clients set their own role.
Security Reminders
Always keep RLS enabled on the table. Helper functions should be stable, and role logic must never trust client-supplied values directly.
Putting It Together
Combine ownership checks with role claims read from the JWT to express rich, secure access rules entirely in the database.
Quick Check
Test your understanding of role-based RLS.
Recap
You built role-based RLS using auth.jwt() custom claims, helper functions, combined ownership-or-role policies, per-action rules, and learned to set roles only on a trusted server.
よくある質問
「RLSとカスタムクレームによるロールベースアクセス」レッスンは無料ですか?
はい。「RLSとカスタムクレームによるロールベースアクセス」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Supabase Backend as a Serviceコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Supabase Backend as a Serviceコースには全3レッスンが含まれています。
「RLSとカスタムクレームによるロールベースアクセス」で何を学びますか?
JWTクレームとヘルパー関数を使い、ユーザーロールごとに異なるアクセスを許可する高度な行レベルセキュリティ(RLS)ポリシーを構築します。 ブラウザで直接実行するハンズオンコードでSupabase Backend as a Serviceを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Supabase Backend as a Serviceを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのSupabase Backend as a Serviceは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/3です。
「RLSとカスタムクレームによるロールベースアクセス」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このSupabase Backend as a Serviceレッスンでコードを書いて実行できますか?
はい。すべてのSupabase Backend as a Serviceレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- RLSポリシー入門
- RLSのテストとデバッグ
- RLSとカスタムクレームによるロールベースアクセス