การควบคุมการเข้าถึงตามบทบาทด้วย RLS และข้ออ้างสิทธิ์แบบกำหนดเอง
สร้างนโยบายความปลอดภัยระดับแถวขั้นสูงที่มอบสิทธิ์แตกต่างกันตามบทบาทผู้ใช้ โดยใช้ข้ออ้างสิทธิ์จาก JWT และฟังก์ชันช่วยเหลือ
การควบคุมการเข้าถึงตามบทบาทด้วย RLS และข้ออ้างสิทธิ์แบบกำหนดเอง เป็นบทเรียน Supabase Backend as a Service ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 3 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
เรียนรู้ Supabase Backend as a Service ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 11
- บทเรียน
- 40
คำถามที่พบบ่อย
บทเรียน “การควบคุมการเข้าถึงตามบทบาทด้วย RLS และข้ออ้างสิทธิ์แบบกำหนดเอง” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การควบคุมการเข้าถึงตามบทบาทด้วย RLS และข้ออ้างสิทธิ์แบบกำหนดเอง” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Supabase Backend as a Service ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Supabase Backend as a Service มีบทเรียนทั้งหมด 3 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การควบคุมการเข้าถึงตามบทบาทด้วย RLS และข้ออ้างสิทธิ์แบบกำหนดเอง”
สร้างนโยบายความปลอดภัยระดับแถวขั้นสูงที่มอบสิทธิ์แตกต่างกันตามบทบาทผู้ใช้ โดยใช้ข้ออ้างสิทธิ์จาก JWT และฟังก์ชันช่วยเหลือ คุณปฏิบัติ Supabase Backend as a Service ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Supabase Backend as a Service หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Supabase Backend as a Service บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 3 บทเรียน
บทเรียน “การควบคุมการเข้าถึงตามบทบาทด้วย RLS และข้ออ้างสิทธิ์แบบกำหนดเอง” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Supabase Backend as a Service นี้ได้ไหม
ได้ บทเรียน Supabase Backend as a Service ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่นโยบาย RLS
- การทดสอบและการแก้จุดบกพร่องของ RLS
- การควบคุมการเข้าถึงตามบทบาทด้วย RLS และข้ออ้างสิทธิ์แบบกำหนดเอง