使用 RLS 和自定义声明进行基于角色的访问控制
构建高级行级安全策略,通过 JWT 声明和辅助函数,根据用户角色授予不同的访问权限。
使用 RLS 和自定义声明进行基于角色的访问控制 是 CoddyKit 上的免费 Supabase Backend as a Service 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 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 和自定义声明进行基于角色的访问控制」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Supabase Backend as a Service 课程的其余内容,请升级到 CoddyKit PRO。 Supabase Backend as a Service 课程共包含 3 节课。
「使用 RLS 和自定义声明进行基于角色的访问控制」这节课中我会学到什么?
构建高级行级安全策略,通过 JWT 声明和辅助函数,根据用户角色授予不同的访问权限。 你通过在浏览器中直接运行的动手代码来练习 Supabase Backend as a Service,全天候 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 反馈 — 无需本地设置。