来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
215 lines
6.5 KiB
PL/PgSQL
215 lines
6.5 KiB
PL/PgSQL
-- 数据库层分公司隔离:扩展 tenant_isolation_* 覆盖主数据与市场/成本表
|
|
-- 配合应用 /api/data 在有效分公司下 SET LOCAL ROLE canteen_app_rls + app.branch_code
|
|
-- 表 owner 连接仍绕过 RLS(迁移/后台任务不受影响);不 FORCE ROW LEVEL SECURITY。
|
|
|
|
CREATE OR REPLACE FUNCTION pg_temp._drop_anon_policies(p_table text) RETURNS void
|
|
LANGUAGE plpgsql AS $$
|
|
DECLARE
|
|
r record;
|
|
BEGIN
|
|
IF to_regclass('public.' || p_table) IS NULL THEN
|
|
RETURN;
|
|
END IF;
|
|
FOR r IN
|
|
SELECT policyname
|
|
FROM pg_policies
|
|
WHERE schemaname = 'public'
|
|
AND tablename = p_table
|
|
AND (
|
|
policyname LIKE 'anon_%'
|
|
OR policyname LIKE 'anon_all_%'
|
|
OR policyname LIKE 'anon_select_%'
|
|
OR policyname LIKE 'anon_insert_%'
|
|
OR policyname LIKE 'anon_update_%'
|
|
OR policyname LIKE 'anon_delete_%'
|
|
)
|
|
LOOP
|
|
EXECUTE format('DROP POLICY IF EXISTS %I ON %I', r.policyname, p_table);
|
|
END LOOP;
|
|
END;
|
|
$$;
|
|
|
|
CREATE OR REPLACE FUNCTION pg_temp._ensure_branch_rls(p_table text) RETURNS void
|
|
LANGUAGE plpgsql AS $$
|
|
DECLARE
|
|
has_branch boolean;
|
|
policy_name text;
|
|
BEGIN
|
|
IF to_regclass('public.' || p_table) IS NULL THEN
|
|
RETURN;
|
|
END IF;
|
|
|
|
SELECT EXISTS (
|
|
SELECT 1
|
|
FROM information_schema.columns
|
|
WHERE table_schema = 'public'
|
|
AND table_name = p_table
|
|
AND column_name = 'branch_code'
|
|
) INTO has_branch;
|
|
|
|
IF NOT has_branch THEN
|
|
RETURN;
|
|
END IF;
|
|
|
|
EXECUTE format('ALTER TABLE %I ENABLE ROW LEVEL SECURITY', p_table);
|
|
PERFORM pg_temp._drop_anon_policies(p_table);
|
|
|
|
policy_name := 'tenant_isolation_' || p_table;
|
|
EXECUTE format('DROP POLICY IF EXISTS %I ON %I', policy_name, p_table);
|
|
EXECUTE format(
|
|
'CREATE POLICY %I ON %I FOR ALL
|
|
USING (branch_code::text = current_setting(''app.branch_code'', true))
|
|
WITH CHECK (branch_code::text = current_setting(''app.branch_code'', true))',
|
|
policy_name,
|
|
p_table
|
|
);
|
|
END;
|
|
$$;
|
|
|
|
-- 1) 直属 branch_code 表
|
|
SELECT pg_temp._ensure_branch_rls(t)
|
|
FROM unnest(ARRAY[
|
|
'orders',
|
|
'order_items',
|
|
'customers',
|
|
'customer_skus',
|
|
'inventory',
|
|
'supplier_preparations',
|
|
'cost_details',
|
|
'cost_adjustments',
|
|
'product_catalog',
|
|
'categories',
|
|
'skus',
|
|
'sku_aliases',
|
|
'suppliers',
|
|
'purchase_orders',
|
|
'delivery_tasks',
|
|
'delivery_persons',
|
|
'inspections',
|
|
'inspection_returns',
|
|
'inspection_loss_confirmations',
|
|
'processing_tasks',
|
|
'picking_tasks',
|
|
'picking_items',
|
|
'picking_containers',
|
|
'picking_scan_logs',
|
|
'customer_quote_versions',
|
|
'customer_quote_sheets',
|
|
'customer_quote_sheet_items',
|
|
'customer_quote_sheet_customers',
|
|
'customer_statements',
|
|
'supplier_statements',
|
|
'statement_lines',
|
|
'statement_disputes',
|
|
'invoices',
|
|
'after_sales',
|
|
'exception_tickets',
|
|
'customer_delivery_assignments',
|
|
'smart_classification_rules',
|
|
'supplier_rating_snapshots',
|
|
'market_prices',
|
|
'market_price_indices',
|
|
'market_compare_exclusions',
|
|
'fixed_costs',
|
|
'seasonal_recommendations',
|
|
'order_allocation_batches'
|
|
]) AS t;
|
|
|
|
-- 2) 无 branch_code:经关联表隔离
|
|
DO $$
|
|
BEGIN
|
|
IF to_regclass('public.supplier_categories') IS NOT NULL THEN
|
|
ALTER TABLE supplier_categories ENABLE ROW LEVEL SECURITY;
|
|
PERFORM pg_temp._drop_anon_policies('supplier_categories');
|
|
DROP POLICY IF EXISTS tenant_isolation_supplier_categories ON supplier_categories;
|
|
CREATE POLICY tenant_isolation_supplier_categories ON supplier_categories
|
|
FOR ALL
|
|
USING (
|
|
supplier_code IN (
|
|
SELECT s.supplier_code FROM suppliers s
|
|
WHERE s.branch_code::text = current_setting('app.branch_code', true)
|
|
)
|
|
)
|
|
WITH CHECK (
|
|
supplier_code IN (
|
|
SELECT s.supplier_code FROM suppliers s
|
|
WHERE s.branch_code::text = current_setting('app.branch_code', true)
|
|
)
|
|
);
|
|
END IF;
|
|
|
|
IF to_regclass('public.sku_suppliers') IS NOT NULL THEN
|
|
ALTER TABLE sku_suppliers ENABLE ROW LEVEL SECURITY;
|
|
PERFORM pg_temp._drop_anon_policies('sku_suppliers');
|
|
DROP POLICY IF EXISTS tenant_isolation_sku_suppliers ON sku_suppliers;
|
|
CREATE POLICY tenant_isolation_sku_suppliers ON sku_suppliers
|
|
FOR ALL
|
|
USING (
|
|
sku_code IN (
|
|
SELECT s.sku_code FROM skus s
|
|
WHERE s.branch_code::text = current_setting('app.branch_code', true)
|
|
)
|
|
AND supplier_code IN (
|
|
SELECT sp.supplier_code FROM suppliers sp
|
|
WHERE sp.branch_code::text = current_setting('app.branch_code', true)
|
|
)
|
|
)
|
|
WITH CHECK (
|
|
sku_code IN (
|
|
SELECT s.sku_code FROM skus s
|
|
WHERE s.branch_code::text = current_setting('app.branch_code', true)
|
|
)
|
|
AND supplier_code IN (
|
|
SELECT sp.supplier_code FROM suppliers sp
|
|
WHERE sp.branch_code::text = current_setting('app.branch_code', true)
|
|
)
|
|
);
|
|
END IF;
|
|
|
|
IF to_regclass('public.customer_sku_aliases') IS NOT NULL THEN
|
|
ALTER TABLE customer_sku_aliases ENABLE ROW LEVEL SECURITY;
|
|
PERFORM pg_temp._drop_anon_policies('customer_sku_aliases');
|
|
DROP POLICY IF EXISTS tenant_isolation_customer_sku_aliases ON customer_sku_aliases;
|
|
CREATE POLICY tenant_isolation_customer_sku_aliases ON customer_sku_aliases
|
|
FOR ALL
|
|
USING (
|
|
customer_code IN (
|
|
SELECT c.customer_code FROM customers c
|
|
WHERE c.branch_code::text = current_setting('app.branch_code', true)
|
|
)
|
|
)
|
|
WITH CHECK (
|
|
customer_code IN (
|
|
SELECT c.customer_code FROM customers c
|
|
WHERE c.branch_code::text = current_setting('app.branch_code', true)
|
|
)
|
|
);
|
|
END IF;
|
|
END $$;
|
|
|
|
-- 3) 确保 RLS 角色对新表有权限(若角色存在)
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'canteen_app_rls') THEN
|
|
GRANT SELECT, INSERT, UPDATE, DELETE ON ALL TABLES IN SCHEMA public TO canteen_app_rls;
|
|
GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO canteen_app_rls;
|
|
ALTER DEFAULT PRIVILEGES IN SCHEMA public
|
|
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO canteen_app_rls;
|
|
END IF;
|
|
EXCEPTION
|
|
WHEN insufficient_privilege THEN
|
|
RAISE NOTICE 'skip grants for canteen_app_rls: %', SQLERRM;
|
|
END $$;
|
|
|
|
DO $$
|
|
BEGIN
|
|
IF EXISTS (
|
|
SELECT 1 FROM pg_policies
|
|
WHERE schemaname = 'public' AND tablename = 'product_catalog'
|
|
AND policyname = 'tenant_isolation_product_catalog'
|
|
) THEN
|
|
COMMENT ON POLICY tenant_isolation_product_catalog ON product_catalog IS
|
|
'DB 层分公司隔离:须 SET app.branch_code;应用经 canteen_app_rls 访问时生效';
|
|
END IF;
|
|
END $$;
|