509 lines
27 KiB
PL/PgSQL
509 lines
27 KiB
PL/PgSQL
-- =============================================================================
|
||
-- 2026-11-23 统一认证(Hub / IDP)功能上线:一次性结构升级(必做)
|
||
-- =============================================================================
|
||
-- 整理日期:2026-11-23
|
||
-- 覆盖范围:上一次 SQL 发布点 sql/20261029_prod_sequence_sync_pg16.sql(2026-10-29)
|
||
-- 之后到 2026-11-23 之间全部落在「表 / 列 / 索引 / 函数 / 触发器」层的改动,
|
||
-- 也就是统一认证(Hub)用户同步与免密登录这一整批功能。
|
||
--
|
||
-- 本脚本做什么(5 件事):
|
||
-- 1) sys_user 加 9 列:hub_user_id / hub_account / hub_display_name / hub_mobile /
|
||
-- hub_org_id / hub_synced_at / account_source / password_locally_managed /
|
||
-- local_login_enabled,并补 account_source 与本地登录旗标的 2 条 CHECK 约束;
|
||
-- 2) 建 3 张同步表:crm_idp_sync_run(批次)、crm_sync_change_log(改动人货账流水)、
|
||
-- crm_idp_pending(待补全队列)及其索引;
|
||
-- 3) 建 idx_idp_pending_ignored(待补全「忽略」粘住闸门的探测索引);
|
||
-- 4) 装 sys_user 两道守卫:主键不可变 trg_guard_sys_user_pk、
|
||
-- 带数据不许物理删号 trg_guard_sys_user_delete(全库无外键指向 sys_user,不拦就攒孤儿);
|
||
-- 5) 回填 account_source(存量漂移纠偏:有 Hub 锚点却还停在 'local' 的行)。
|
||
--
|
||
-- 为什么需要这份合并脚本(问题所在):
|
||
-- 上面这些结构原先**没有任何一份 sql/ 脚本能独立建全**,唯一入口是后端启动器
|
||
-- common/IdpSchemaInitializer(crm.idp.schema.enabled=true 时每次启动对共享热表 sys_user 发 DDL)。
|
||
-- tmp/idp/ddl_draft.sql 只是开发期的解析验证草稿(末尾自带幂等重跑与「守卫必须真的拦住」断言),
|
||
-- 不是运维入口。只跑应用不跑 SQL 的库能起来,但 account_source 停在缺省值、
|
||
-- 也没有 CHECK 约束(启动器刻意不加约束),要等一次性回填才归位。
|
||
-- ⇒ 本脚本与启动器逐字同源(DDL 文本照抄 IdpSchemaInitializer 的 STEPS 与
|
||
-- sql/20261120_idp_account_source.sql、sql/20261120_idp_local_login_flags.sql),
|
||
-- 先跑本脚本的库,启动器会因为「存在性探测」命中而跳过这些步骤,两边不冲突。
|
||
--
|
||
-- 文件结构:
|
||
-- A 段(默认执行):第 1~8 步 + 跑完自查 + 回滚说明 —— 统一认证的结构前提。
|
||
-- B 段(文件末尾,整段注释):sys_log 口令脱敏触发器,可选护栏,要跑自己取消注释。
|
||
--
|
||
-- 幂等:所有语句可反复执行。add column if not exists / create table if not exists /
|
||
-- create index if not exists / create or replace function 都是幂等的;
|
||
-- 约束与触发器用存在性判断包住;回填 update 带 where,第二次跑变化 0 行。
|
||
-- 事务:整份一个事务,半途失败不会留下「有列没旗标」的中间态。
|
||
-- 锁:sys_user 是共享热表,本脚本带 lock_timeout='5s' 与 statement_timeout='10min';
|
||
-- 拿不到锁直接报错退出,不会把线上堵在事务里。
|
||
-- 前置:sys_user 必须已存在(框架基础表,见 sql/init_full_pg17.sql)——
|
||
-- 第 1 步的 alter table 就会因为它不存在而报错退出,这是有意的:
|
||
-- 连 sys_user 都没有的库应该先跑初始化脚本,而不是让这份增量补出半套结构。
|
||
--
|
||
-- 执行:
|
||
-- psql -d <db> -v ON_ERROR_STOP=1 -f sql/20261123_idp_schema_upgrade_pg17.sql
|
||
--
|
||
-- 执行完之后(这些不在本脚本里,见 sql/README.md「统一认证(Hub)上线」一节):
|
||
-- * 权限/授权类 SQL(菜单码、按钮码、内置管理员账号):sql/20261119_idp_menu_perm.sql、
|
||
-- sql/20261119_idp_config_perms.sql、sql/20261120_idp_mapping_write_perm.sql、
|
||
-- sql/20261119_admin_portal_perm.sql、sql/20261120_idp_local_admin_backfill.sql;
|
||
-- 跑完必须清 Redis 权限缓存 sys:auth:perm*(重新登录不刷新)。
|
||
-- * 口令掩码触发器:本文件末尾的 B 段,或单独的 sql/20261119_idp_sys_log_password_mask.sql。
|
||
-- * 序列自检:sql/20261119_idp_sys_param_setval.sql(sys_param 手工插过显式 param_id 时必跑)。
|
||
--
|
||
-- 回滚:见文件末尾注释。account_source 与两个旗标位都是派生/开关数据,删掉不丢原始事实;
|
||
-- 唯一顺序要求是**先把后端退回不读这些列的版本,再删列**。
|
||
-- =============================================================================
|
||
|
||
begin;
|
||
|
||
set search_path to public;
|
||
set local lock_timeout = '5s';
|
||
set local statement_timeout = '10min';
|
||
|
||
-- -----------------------------------------------------------------------------
|
||
-- 1) sys_user:Hub 身份锚点六列(与 IdpSchemaInitializer.STEPS 逐字一致)
|
||
-- hub_user_id 是唯一终身锚点,其余五列是 Hub 侧快照。
|
||
-- -----------------------------------------------------------------------------
|
||
alter table sys_user add column if not exists hub_user_id bigint;
|
||
alter table sys_user add column if not exists hub_account varchar(128);
|
||
alter table sys_user add column if not exists hub_display_name varchar(255);
|
||
alter table sys_user add column if not exists hub_mobile varchar(32);
|
||
alter table sys_user add column if not exists hub_org_id varchar(128);
|
||
alter table sys_user add column if not exists hub_synced_at timestamptz;
|
||
|
||
create unique index if not exists uk_sys_user_hub_user_id
|
||
on sys_user (hub_user_id) where hub_user_id is not null;
|
||
|
||
comment on column sys_user.hub_user_id is '统一认证(Hub)用户主键,认人阶梯的一级锚点;非空即代表这一行已由 Hub 托管';
|
||
comment on column sys_user.hub_account is 'Hub 账号(一次性线索,会随企微改名,不作为长期判据)';
|
||
comment on column sys_user.hub_display_name is 'Hub 侧姓名快照,同步按它覆盖 display_name(account_source=hub 时)';
|
||
comment on column sys_user.hub_mobile is 'Hub 侧手机号快照';
|
||
comment on column sys_user.hub_org_id is 'Hub 侧组织主键快照';
|
||
comment on column sys_user.hub_synced_at is '最后一次被 Hub 同步写过的时间,用于区分「同步改的」与「人工改的」';
|
||
|
||
-- -----------------------------------------------------------------------------
|
||
-- 2) sys_user.account_source:这一行归谁管(sql/20261120_idp_account_source.sql)
|
||
-- 'hub' = 改名/调部门/停用跟着 Hub 走;'local' = 本地自理,同步不碰。
|
||
-- 缺省 'local' 的含义是「没人接管 = 本地自理」,新库/漏跑回填都不会误判成交出去了。
|
||
-- -----------------------------------------------------------------------------
|
||
alter table sys_user add column if not exists account_source varchar(16) not null default 'local';
|
||
|
||
comment on column sys_user.account_source is '账号归属:hub=统一认证同步建的号或已交给统一认证接管(改名/调部门/停用跟 Hub 走);local=本地账号,同步不碰;is_platform_admin=true 的行恒为 local';
|
||
|
||
do $$
|
||
begin
|
||
if not exists (
|
||
select 1 from pg_constraint
|
||
where conrelid = 'sys_user'::regclass
|
||
and conname = 'ck_sys_user_account_source'
|
||
) then
|
||
alter table sys_user
|
||
add constraint ck_sys_user_account_source
|
||
check (account_source in ('local', 'hub'));
|
||
end if;
|
||
end
|
||
$$;
|
||
|
||
-- -----------------------------------------------------------------------------
|
||
-- 3) sys_user 本地登录旗标(sql/20261120_idp_local_login_flags.sql)
|
||
-- 两位分开:「口令归本地管」是账号属性,「允许本地登录」是运维手里的开关。
|
||
-- 缺省必须都是 false:库里几乎人人手一个历史 password_hash,
|
||
-- 「有 hash」绝不等于「能本地登录」,默认开就等于把所有人放开后门。
|
||
-- -----------------------------------------------------------------------------
|
||
alter table sys_user add column if not exists password_locally_managed boolean not null default false;
|
||
alter table sys_user add column if not exists local_login_enabled boolean not null default false;
|
||
|
||
comment on column sys_user.password_locally_managed is '口令归 CRM 本地管理(该账号在 Hub 无身份);/users/me 的 passwordLocallyManaged 由此位给';
|
||
comment on column sys_user.local_login_enabled is '允许从唯一登录入口 /api/idp/login 用本地口令进入;与总闸 crm.idp.local-admin.enabled 是 AND 关系';
|
||
|
||
do $$
|
||
begin
|
||
if not exists (
|
||
select 1 from pg_constraint
|
||
where conrelid = 'sys_user'::regclass
|
||
and conname = 'ck_sys_user_local_login_needs_local_password'
|
||
) then
|
||
alter table sys_user
|
||
add constraint ck_sys_user_local_login_needs_local_password
|
||
check (local_login_enabled = false or password_locally_managed = true);
|
||
end if;
|
||
end
|
||
$$;
|
||
|
||
-- 注意:本脚本**不放开任何账号的本地登录**,那是数据变更(UPDATE),
|
||
-- 在 sql/20261120_idp_local_login_flags.sql 里做(它会把应急钥匙账号与
|
||
-- admin / admin_crm 三位点名放开)。只跑本脚本 = 结构与跑之前完全一致,
|
||
-- 除了回填那一段(见第 8 步)。
|
||
|
||
-- -----------------------------------------------------------------------------
|
||
-- 4) crm_idp_sync_run:同步批次表(建表与索引语句与 IdpSchemaInitializer 逐字一致)
|
||
-- -----------------------------------------------------------------------------
|
||
create table if not exists crm_idp_sync_run (
|
||
id bigserial primary key,
|
||
tenant_id bigint not null default 4,
|
||
started_at timestamptz not null default now(),
|
||
finished_at timestamptz,
|
||
result varchar(24) not null default 'RUNNING',
|
||
abort_reason text,
|
||
dry_run boolean not null default false,
|
||
mode varchar(16) not null default 'FULL',
|
||
cursor_after bigint,
|
||
pulled_users int,
|
||
users_with_wecom_userid int,
|
||
directory_truncated boolean not null default false,
|
||
claimed int,
|
||
updated int,
|
||
created int,
|
||
disabled int,
|
||
reactivated int,
|
||
skipped int,
|
||
pending int,
|
||
duration_ms bigint,
|
||
detail jsonb not null default '{}'::jsonb
|
||
);
|
||
|
||
create unique index if not exists uk_idp_sync_run_running
|
||
on crm_idp_sync_run (coalesce(tenant_id, 0)) where result = 'RUNNING';
|
||
|
||
comment on table crm_idp_sync_run is '统一认证同步批次台账:一轮全量/增量一行,回执与 detail 都在这里,dry-run 也留痕';
|
||
|
||
-- -----------------------------------------------------------------------------
|
||
-- 5) crm_sync_change_log:改动人货账流水
|
||
-- -----------------------------------------------------------------------------
|
||
create table if not exists crm_sync_change_log (
|
||
id bigserial primary key,
|
||
tenant_id bigint not null default 4,
|
||
batch_id bigint not null,
|
||
entity_type varchar(16) not null default 'USER',
|
||
entity_key bigint not null,
|
||
action varchar(16) not null,
|
||
field_name varchar(64),
|
||
old_value text,
|
||
new_value text,
|
||
rolled_back_at timestamptz,
|
||
created_at timestamptz not null default now()
|
||
);
|
||
|
||
create index if not exists idx_sync_change_log_batch
|
||
on crm_sync_change_log (batch_id, entity_type);
|
||
|
||
comment on table crm_sync_change_log is '同步对每一行的每一次字段改动留痕(old/new 成对),回滚与「这个人是谁建的号」都查这里;batch_id=0 是免密登录 JIT 建号';
|
||
|
||
-- -----------------------------------------------------------------------------
|
||
-- 6) crm_idp_pending:待补全队列 + 「忽略」粘住索引
|
||
-- sql/20261123_idp_pending_sticky_ignore.sql 的合并点(idx_idp_pending_ignored),
|
||
-- 与启动器逐字一致,漂移由 IdpPendingIgnoreTest 钉住。
|
||
-- -----------------------------------------------------------------------------
|
||
create table if not exists crm_idp_pending (
|
||
id bigserial primary key,
|
||
tenant_id bigint not null default 4,
|
||
batch_id bigint,
|
||
kind varchar(32) not null,
|
||
hub_user_id bigint,
|
||
hub_account varchar(128),
|
||
local_user_id bigint,
|
||
detail jsonb not null default '{}'::jsonb,
|
||
status varchar(16) not null default 'OPEN',
|
||
resolved_by varchar(64),
|
||
resolved_at timestamptz,
|
||
resolve_note text,
|
||
created_at timestamptz not null default now(),
|
||
updated_at timestamptz not null default now()
|
||
);
|
||
|
||
create unique index if not exists uk_idp_pending_open
|
||
on crm_idp_pending (kind, coalesce(hub_user_id, 0), coalesce(local_user_id, 0)) where status = 'OPEN';
|
||
create index if not exists idx_idp_pending_local
|
||
on crm_idp_pending (local_user_id) where status = 'OPEN';
|
||
create index if not exists idx_idp_pending_ignored
|
||
on crm_idp_pending (tenant_id, kind, coalesce(hub_user_id, 0), coalesce(local_user_id, 0))
|
||
where status = 'IGNORED';
|
||
|
||
comment on table crm_idp_pending is '同步/免密登录认不出人时的待补全队列;IGNORED 行是人工判定「不再为这事排队」的粘住记录,撤销走 POST /api/idp/admin/pending/{id}/reopen';
|
||
|
||
-- -----------------------------------------------------------------------------
|
||
-- 7) sys_user 两道守卫(全库没有一条外键指向 sys_user,user_id 被 30+ 个业务列引用)
|
||
-- 没有它们:改主键静默成功、物理删号删完不报错也不清理,
|
||
-- 测试库一个下午就能攒出 88 条指向不存在 user_id 的角色行。
|
||
-- -----------------------------------------------------------------------------
|
||
create or replace function crm_guard_sys_user_pk() returns trigger as $fn$
|
||
begin
|
||
if new.user_id is distinct from old.user_id then
|
||
raise exception 'sys_user.user_id is immutable: old=% new=%', old.user_id, new.user_id;
|
||
end if;
|
||
return new;
|
||
end;
|
||
$fn$ language plpgsql;
|
||
|
||
drop trigger if exists trg_guard_sys_user_pk on sys_user;
|
||
create trigger trg_guard_sys_user_pk before update of user_id on sys_user
|
||
for each row execute function crm_guard_sys_user_pk();
|
||
|
||
create or replace function crm_guard_sys_user_delete() returns trigger as $fn$
|
||
declare
|
||
ref record;
|
||
hits text := '';
|
||
total bigint := 0;
|
||
begin
|
||
if coalesce(current_setting('crm.allow_user_delete_with_references', true), '') = 'on' then
|
||
return old;
|
||
end if;
|
||
-- 引用面写在这里而不是散在各个删除入口:框架的用户管理物理删 sys_user 时不走本仓代码,
|
||
-- 应用层拦一道只拦得住自己写的路径。表可能不存在(新库/裁剪部署),所以逐张 to_regclass。
|
||
for ref in
|
||
select * from (values
|
||
('sys_user_role', 'user_id', '角色'),
|
||
('sys_tenant_user', 'user_id', '租户成员关系'),
|
||
('wecom_user_mapping', 'user_id', '企微映射'),
|
||
('sys_user_data_scope_user', 'owner_user_id', '数据范围授权'),
|
||
('crm_opportunity', 'owner_user_id', '商机'),
|
||
('crm_customer', 'owner_user_id', '客户'),
|
||
('crm_sales_expansion', 'owner_user_id', '销售拓展'),
|
||
('crm_channel_expansion', 'owner_user_id', '渠道拓展'),
|
||
('crm_crm_expansion', 'owner_user_id', 'CRM拓展'),
|
||
('crm_opportunity_followup', 'followup_user_id', '商机跟进'),
|
||
('crm_expansion_followup', 'followup_user_id', '拓展跟进'),
|
||
('work_daily_report', 'user_id', '日报'),
|
||
('work_checkin', 'user_id', '打卡'),
|
||
('work_todo', 'user_id', '待办')
|
||
) as t(tbl, col, label)
|
||
loop
|
||
if to_regclass(ref.tbl) is null then
|
||
continue;
|
||
end if;
|
||
execute format(
|
||
'select count(*) from %I where %I = $1', ref.tbl, ref.col)
|
||
into total using old.user_id;
|
||
if total > 0 then
|
||
hits := hits || case when hits = '' then '' else '、' end
|
||
|| ref.label || '(' || ref.tbl || ') ' || total || ' 条';
|
||
end if;
|
||
end loop;
|
||
if hits <> '' then
|
||
raise exception 'user_id=% 仍挂着数据,先转移归属再删(确属清理可 set crm.allow_user_delete_with_references = on 放行):%',
|
||
old.user_id, hits;
|
||
end if;
|
||
return old;
|
||
end;
|
||
$fn$ language plpgsql;
|
||
|
||
drop trigger if exists trg_guard_sys_user_delete on sys_user;
|
||
create trigger trg_guard_sys_user_delete before delete on sys_user
|
||
for each row execute function crm_guard_sys_user_delete();
|
||
|
||
-- -----------------------------------------------------------------------------
|
||
-- 8) account_source 存量回填(一次性纠偏,判据与同步侧写入完全同口径)
|
||
-- 为什么必须回填:列是启动器补建的(default 'local' 不带回填),
|
||
-- 而属性对齐那条路(IdpUserResolver.applySnapshot)只刷 hub_* 与 email,
|
||
-- 于是「有 Hub 锚点却仍标 local」的行漂了就回不来 ——
|
||
-- dev 库实测漂移 35 行(docs/统一认证-用户同步与免密登录方案.md §17)。
|
||
-- 危害是反过来说谎:同步照旧按 hub_user_id 管这些行,
|
||
-- 而用户管理页写着「本地账号,同步不碰」,运维信了去手工改名或点解绑就是事故。
|
||
-- 判据:coalesce(is_deleted,0)=0 且 hub_user_id 非空 且 非平台管理员 → 'hub'。
|
||
-- 硬红线:is_platform_admin=true 永远是 'local'(应急钥匙不能被 Hub 停用)。
|
||
-- -----------------------------------------------------------------------------
|
||
do $$
|
||
declare
|
||
v_updated bigint := 0;
|
||
v_soft_deleted bigint := 0;
|
||
v_drift bigint := 0;
|
||
begin
|
||
if to_regclass('public.sys_user') is null then
|
||
raise warning 'sys_user 不存在,跳过 account_source 回填(新库请先执行 sql/init_full_pg17.sql)';
|
||
return;
|
||
end if;
|
||
|
||
update sys_user
|
||
set account_source = 'hub'
|
||
where coalesce(is_deleted, 0) = 0
|
||
and hub_user_id is not null
|
||
and coalesce(is_platform_admin, false) = false
|
||
and account_source <> 'hub';
|
||
get diagnostics v_updated = row_count;
|
||
|
||
-- 软删行只报数不动:它们占着 hub_user_id 锚点,归属位停在 local(同步与页面都不看它们)。
|
||
select count(*)
|
||
into v_soft_deleted
|
||
from sys_user
|
||
where coalesce(is_deleted, 0) <> 0
|
||
and hub_user_id is not null;
|
||
|
||
raise notice 'account_source 回填:本次纠偏 % 行 → hub;另有软删行 % 行不改动'
|
||
'(它们仍占着 hub_user_id 锚点,uk_sys_user_hub_user_id 的谓词不带 is_deleted = 0,'
|
||
'人工映射抢同一个 Hub 账号时会撞这个唯一索引)',
|
||
v_updated, v_soft_deleted;
|
||
|
||
-- 漂移自检:回填之后再出现这种行 = 某个写入口只写了 hub_user_id 没写归属位。
|
||
select count(*)
|
||
into v_drift
|
||
from sys_user
|
||
where account_source = 'local'
|
||
and coalesce(is_deleted, 0) = 0
|
||
and hub_user_id is not null
|
||
and coalesce(is_platform_admin, false) = false;
|
||
if v_drift > 0 then
|
||
raise warning '有 % 行带 hub_user_id 却仍是 account_source=local', v_drift;
|
||
end if;
|
||
end
|
||
$$;
|
||
|
||
commit;
|
||
|
||
-- =============================================================================
|
||
-- 跑完自查(只读):illegal_values / admin_is_hub / hub_without_anchor / flag_pair_broken
|
||
-- 必须都是 0;local_with_anchor 本脚本跑完应当是 0(变正 = 有新写入口漏写归属位)。
|
||
-- local_login_accounts 在本脚本之后应当是 0(放开账号在 sql/20261120_idp_local_login_flags.sql 里做)。
|
||
-- =============================================================================
|
||
select (select count(*) from sys_user where account_source = 'hub') as hub_accounts,
|
||
(select count(*) from sys_user where account_source = 'local') as local_accounts,
|
||
(select count(*) from sys_user where account_source not in ('local', 'hub')) as illegal_values,
|
||
(select count(*) from sys_user
|
||
where account_source = 'hub' and coalesce(is_platform_admin, false)) as admin_is_hub,
|
||
(select count(*) from sys_user where account_source = 'hub' and hub_user_id is null) as hub_without_anchor,
|
||
(select count(*) from sys_user
|
||
where account_source = 'local' and coalesce(is_deleted, 0) = 0
|
||
and hub_user_id is not null
|
||
and coalesce(is_platform_admin, false) = false) as local_with_anchor,
|
||
(select count(*) from sys_user
|
||
where coalesce(is_deleted, 0) <> 0 and hub_user_id is not null) as soft_anchored_deleted,
|
||
(select count(*) from sys_user where local_login_enabled) as local_login_accounts,
|
||
(select count(*) from sys_user
|
||
where local_login_enabled and not password_locally_managed) as flag_pair_broken,
|
||
(select count(*) from information_schema.tables
|
||
where table_name in ('crm_idp_sync_run', 'crm_sync_change_log', 'crm_idp_pending')) as idp_tables,
|
||
(select count(*) from pg_trigger t join pg_class c on c.oid = t.tgrelid
|
||
where c.relname = 'sys_user' and t.tgname in ('trg_guard_sys_user_pk', 'trg_guard_sys_user_delete')
|
||
and not t.tgisinternal) as guard_triggers;
|
||
|
||
-- =============================================================================
|
||
-- 回滚(按需执行;顺序不能反)
|
||
-- =============================================================================
|
||
-- ① 先把后端退回不读这些列/不依赖这些表的版本,再动结构。
|
||
-- 列没了还跑新后端 = 用户管理页整表 500(column u.account_source does not exist)。
|
||
-- ② 旗标位:先清 0 再删列,别把「谁曾经能本地进来」这条事实连着列一起丢掉。
|
||
-- update sys_user set password_locally_managed = false, local_login_enabled = false,
|
||
-- updated_at = now()
|
||
-- where password_locally_managed or local_login_enabled;
|
||
-- alter table sys_user drop constraint if exists ck_sys_user_local_login_needs_local_password;
|
||
-- alter table sys_user drop column if exists local_login_enabled;
|
||
-- alter table sys_user drop column if exists password_locally_managed;
|
||
-- ③ 归属位(派生自 hub_user_id,删掉不丢原始事实):
|
||
-- alter table sys_user drop constraint if exists ck_sys_user_account_source;
|
||
-- alter table sys_user drop column if exists account_source;
|
||
-- ④ 守卫(不需要回滚数据,直接摘):
|
||
-- drop trigger if exists trg_guard_sys_user_pk on sys_user;
|
||
-- drop trigger if exists trg_guard_sys_user_delete on sys_user;
|
||
-- drop function if exists crm_guard_sys_user_pk();
|
||
-- drop function if exists crm_guard_sys_user_delete();
|
||
-- ⑤ Hub 锚点六列 + 三张同步表:删之前确认同步已关(crm.idp.enabled=false),
|
||
-- 并且已经按 tmp/idp/mapping_worksheet.py 导出过对照表留档。
|
||
-- drop index if exists uk_sys_user_hub_user_id;
|
||
-- alter table sys_user drop column if exists hub_synced_at;
|
||
-- alter table sys_user drop column if exists hub_org_id;
|
||
-- alter table sys_user drop column if exists hub_mobile;
|
||
-- alter table sys_user drop column if exists hub_display_name;
|
||
-- alter table sys_user drop column if exists hub_account;
|
||
-- alter table sys_user drop column if exists hub_user_id;
|
||
-- drop table if exists crm_idp_pending;
|
||
-- drop table if exists crm_sync_change_log;
|
||
-- drop table if exists crm_idp_sync_run;
|
||
|
||
-- =============================================================================
|
||
-- 附:可选护栏(B 段)—— sys_log 操作日志里的口令明文脱敏
|
||
-- =============================================================================
|
||
-- 来源:sql/20261119_idp_sys_log_password_mask.sql(原文照搬,未改写)。
|
||
-- 干什么:给 sys_log.params 装一个写侧脱敏触发器,把 password / newPassword /
|
||
-- 旧密码这类键的值换成 ***,并顺手清洗一遍存量明文。
|
||
-- 为什么是「可选」:它动的是框架的 sys_log 表,不属于统一认证的功能前提,
|
||
-- 不装也照常跑;但它是一次性写侧改动 —— **存量掩码不可逆**(原值替换即丢),
|
||
-- 所以单列在事务之外,让运维自己决定跑不跑。
|
||
-- 独立性:本段不依赖上面 A 段的任何对象,可以单独执行。
|
||
-- 判据与验收谓词以源脚本为准,别在别处再抄一份(同一意图本轮出现过
|
||
-- 156 / 426 / 608 / 0 四个读数,每个都真、量的不是同一件事)。
|
||
-- 回滚(存量掩码不回滚,原值已丢):
|
||
-- drop trigger if exists crm_sys_log_mask_password on public.sys_log;
|
||
-- drop function if exists crm_mask_sys_log_password();
|
||
-- drop function if exists crm_mask_password_values(text);
|
||
-- 要跑就把下面整段取消注释:
|
||
/*
|
||
begin;
|
||
|
||
set search_path to public;
|
||
|
||
-- 掩码函数:写侧与存量清洗共用同一份判据,分成两份口径一定漂。
|
||
create or replace function crm_mask_password_values(p_params text)
|
||
returns text
|
||
language plpgsql
|
||
immutable
|
||
as $$
|
||
declare
|
||
v_masked text;
|
||
begin
|
||
if p_params is null or p_params !~* 'password' then
|
||
return p_params;
|
||
end if;
|
||
-- 三种写法都盖:JSON 带引号的值、JSON 不带引号的值、序列化失败退回的 toString 形状(key=value)。
|
||
-- 引号要留着:只把值换成星号、连引号一起吃掉会让 params 不再是合法 JSON,
|
||
-- 下一个想 jsonb 解析它的人会被绊住(本轮现采的第一版就是这么错的)。
|
||
v_masked := regexp_replace(p_params,
|
||
'("[A-Za-z0-9_]*password[A-Za-z0-9_]*")([[:space:]]*:[[:space:]]*)"[^"]*"',
|
||
'\1\2"***"',
|
||
'gi');
|
||
v_masked := regexp_replace(v_masked,
|
||
'("[A-Za-z0-9_]*password[A-Za-z0-9_]*")([[:space:]]*:[[:space:]]*)([^"{,}[:space:]][^,}]*)',
|
||
'\1\2***',
|
||
'gi');
|
||
return regexp_replace(v_masked,
|
||
'([A-Za-z0-9_]*password[A-Za-z0-9_]*)([[:space:]]*=[[:space:]]*)[^,}\]]+',
|
||
'\1\2***',
|
||
'gi');
|
||
end;
|
||
$$;
|
||
|
||
create or replace function crm_mask_sys_log_password()
|
||
returns trigger
|
||
language plpgsql
|
||
as $$
|
||
begin
|
||
new.params := crm_mask_password_values(new.params);
|
||
return new;
|
||
end;
|
||
$$;
|
||
|
||
do $$
|
||
declare
|
||
v_rows integer;
|
||
begin
|
||
if to_regclass('public.sys_log') is null then
|
||
raise notice 'public.sys_log 不存在,本段不适用(已整段跳过)';
|
||
return;
|
||
end if;
|
||
|
||
update sys_log
|
||
set params = crm_mask_password_values(params)
|
||
where params ~* '"[a-z0-9_]*password[a-z0-9_]*"[[:space:]]*:';
|
||
get diagnostics v_rows = row_count;
|
||
raise notice 'sys_log 存量口令脱敏:候选 % 行(值已是 *** 的不会被改回去)', v_rows;
|
||
|
||
drop trigger if exists crm_sys_log_mask_password on public.sys_log;
|
||
create trigger crm_sys_log_mask_password
|
||
before insert or update of params on public.sys_log
|
||
for each row execute function crm_mask_sys_log_password();
|
||
raise notice 'sys_log.params 写侧脱敏触发器已安装(crm_sys_log_mask_password)';
|
||
end $$;
|
||
|
||
-- 装完自查:触发器在不在 + 还剩多少明文。
|
||
select tgname as trigger_name, tgenabled as enabled
|
||
from pg_trigger t
|
||
join pg_class c on c.oid = t.tgrelid
|
||
where c.relname = 'sys_log' and not t.tgisinternal;
|
||
|
||
select count(*) as plaintext_password_rows
|
||
from sys_log
|
||
where params ~* '"[a-z0-9_]*password[a-z0-9_]*"[[:space:]]*:[[:space:]]*"[^*"]{3,}"';
|
||
|
||
commit;
|
||
*/
|