0Pricing
Supabase Backend as a Service · درس

التحكم بالوصول القائم على الأدوار باستخدام RLS والادعاءات المخصصة

أنشئ سياسات متقدمة لأمان مستوى الصفوف تمنح مستويات وصول مختلفة حسب دور المستخدم، باستخدام ادعاءات JWT والوظائف المساعدة.

التحكم بالوصول القائم على الأدوار باستخدام RLS والادعاءات المخصصة درس مجاني في Supabase Backend as a Service على CoddyKit. هذا هو الدرس 3 من أصل 3. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 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.

الأسئلة الشائعة

هل درس «التحكم بالوصول القائم على الأدوار باستخدام RLS والادعاءات المخصصة» مجاني؟

نعم — نص درس «التحكم بالوصول القائم على الأدوار باستخدام RLS والادعاءات المخصصة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Supabase Backend as a Service، انتقل إلى CoddyKit PRO. تتضمن دورة Supabase Backend as a Service 3 دروس في المجموع.

ماذا ستتعلم في «التحكم بالوصول القائم على الأدوار باستخدام RLS والادعاءات المخصصة»؟

أنشئ سياسات متقدمة لأمان مستوى الصفوف تمنح مستويات وصول مختلفة حسب دور المستخدم، باستخدام ادعاءات JWT والوظائف المساعدة. تتمرن على Supabase Backend as a Service مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 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 يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. مقدمة إلى سياسات RLS
  2. اختبار RLS وتصحيح أخطائه
  3. التحكم بالوصول القائم على الأدوار باستخدام RLS والادعاءات المخصصة
← العودة إلى Supabase Backend as a Service