-- =====================================================================================
-- RLS: 用户等级表
-- 位置：docs/sql/20_rls/user/
-- 对象类型：RLS 策略
-- 版本：v1
-- 说明：消费者端可读（仅可见/启用/未删除）；管理端全量访问通过 RPC 完成
-- =====================================================================================

ALTER TABLE public.ak_user_levels ENABLE ROW LEVEL SECURITY;

-- 消费者端：允许读取可见且启用的等级（未删除）
DROP POLICY IF EXISTS ak_user_levels_public_select_visible_active ON public.ak_user_levels;
CREATE POLICY ak_user_levels_public_select_visible_active
  ON public.ak_user_levels
  FOR SELECT
  TO anon, authenticated
  USING (
    deleted_at IS NULL
    AND status = 1
    AND is_visible = TRUE
  );

-- 默认不开放写权限（INSERT/UPDATE/DELETE）给 anon/authenticated
