-- 20261120 内置本地管理员(localadmin)存量回补迁移(契约 §30,Lead 2026-11-19 裁定:回补放在当天迁移里做) -- -- 干什么:把启动自愈(IdpLocalAdminAccountService.ensureLocalAdmin)做的三件事用 SQL 再做一遍 —— -- 账号行 sys_user + 成员行 sys_tenant_user + 角色行 sys_user_role,三件齐全才算"能登录且调得通租户接口" -- (只建账号行的形状 = 拿到 token 但任何要租户的接口 403,判据 D7)。 -- 为什么再来一份:sql/20261119_idp_local_admin.sql 已经在开发库执行过(现采 sys_user 里 -- user_id=900125 就是那一行),改那份等于改一段运维已经跑过的历史;本轮存量回补以**本文件**为准, -- 新环境只跑本文件即可。两份内容同源、都幂等,先后跑谁都只有一行。 -- 什么时候需要手工跑:应用账号对这三张表没有写权限、上线前想先手工验证入口, -- 或多实例滚动启动时想让库里先有一行(自愈本身幂等,两边都不会撞)。 -- -- 口令:本脚本**不写也不改已存在的 password_hash**——只在"库里还没有这一行"时插入下面那个 -- admin123 的 bcrypt(cost 10,与配置键 crm.idp.local-admin.password 默认值同一份)。 -- 运维已改过口令的库上跑本脚本 = 一句都不改口令(升级把运维锁在门外是最难查的那种事故)。 -- 要重置成默认口令请走 crm.idp.local-admin.reset-password=true 重启一次,别改本脚本。 -- 幂等:三条 INSERT 全部 on conflict do nothing(sys_user 撞 username 全表唯一键、成员行撞 -- (user_id,tenant_id) 的 is_deleted=0 部分唯一键、角色行撞 (tenant_id,user_id,role_id)); -- 已软删的同名僵尸行只复活(is_deleted/status),不重建、不改 hash。 -- 跑第二遍:零新增行、零报错,结果里的 notice 会说"已存在/hash 未改"。 -- 授权:只按候选角色码取**已存在**的内置超管角色(顺序 TENANT_ADMIN,ADMIN,SYS_ADMIN,SUPER_ADMIN, -- PLATFORM_ADMIN,crm_xs,与自愈同一顺序),一个都不存在时不猜、直接 notice 报出来。不新建角色。 -- 序列:sys_user_user_id_seq 用 greatest(max(user_id)+1, last_value) 前移,**只上调不下调** -- (已经跑到 max+1 之上就不往回拉);sys_param 那次主键撞车就是这段没写造成的。 -- 执行:psql -f sql/20261120_idp_local_admin_backfill.sql(脚本自带 begin/commit,见文件末尾) -- 回滚:见文件末尾注释,只删本脚本可能新建的行,且按 user_id 收窄。 begin; set search_path to public; -- 租户、账号名与候选角色码在这里改,不用动下面的 PL/pgSQL。 -- idp.local_admin_tenant_id 必须和 application.yml 的 crm.idp.tenant-id 一致, -- 否则成员行落在别的租户,登录时 selectTenantsByUsername 解析不到租户(判据 D7)。 set local idp.local_admin_tenant_id = '4'; set local idp.local_admin_username = 'localadmin'; set local idp.local_admin_role_codes = 'TENANT_ADMIN,ADMIN,SYS_ADMIN,SUPER_ADMIN,PLATFORM_ADMIN,crm_xs'; do $$ declare v_username text := coalesce(nullif(current_setting('idp.local_admin_username', true), ''), 'localadmin'); v_tenant_id bigint := coalesce(nullif(current_setting('idp.local_admin_tenant_id', true), '')::bigint, 4); v_role_codes text := coalesce(nullif(current_setting('idp.local_admin_role_codes', true), ''), 'TENANT_ADMIN,ADMIN,SYS_ADMIN,SUPER_ADMIN,PLATFORM_ADMIN,crm_xs'); v_hash text := '$2a$10$.yoxb.yQmnrcR53KNHogx.RLLHubATlJ9ORYGsiIj/NUNv9jOf9Qq'; v_user_id bigint; v_role_id bigint; v_role_code text; v_role_tenant bigint; v_is_deleted integer; begin if to_regclass('public.sys_user') is null then raise notice 'public.sys_user 不存在,本脚本不适用(已整段跳过)'; return; end if; -- 序列自检带存在性守卫:撞主键的成因与 sys_param 那次一样(本轮真库实测)。 if to_regclass('sys_user_user_id_seq') is not null then perform setval('sys_user_user_id_seq', greatest( coalesce((select max(user_id) from sys_user), 0) + 1, (select last_value from sys_user_user_id_seq)), false); end if; select user_id, coalesce(is_deleted, 0) into v_user_id, v_is_deleted from sys_user where username = v_username order by user_id limit 1; if v_user_id is null then insert into sys_user ( username, display_name, password_hash, pwd_reset_required, status, is_deleted, is_platform_admin, created_at, updated_at ) values ( v_username, '本地管理员(内置应急账号)', v_hash, 0, 1, 0, true, now(), now() ) on conflict (username) do nothing returning user_id into v_user_id; if v_user_id is null then -- on conflict 命中:另一实例/另一支脚本刚刚建了同名行,本脚本不抢、不改它的 hash。 select user_id, coalesce(is_deleted, 0) into v_user_id, v_is_deleted from sys_user where username = v_username order by user_id limit 1; raise notice '内置本地管理员 % 已存在(user_id=%),本脚本不动它的 password_hash', v_username, v_user_id; else raise notice '已创建内置本地管理员 % (user_id=%),口令是配置默认值 admin123', v_username, v_user_id; end if; else raise notice '内置本地管理员 % 已存在(user_id=%),本次只确保成员行与角色行', v_username, v_user_id; end if; if v_user_id is null then raise exception '拿不到 % 的 user_id,成员行与角色行无法继续', v_username; end if; if coalesce(v_is_deleted, 0) <> 0 then update sys_user set is_deleted = 0, status = 1, updated_at = now() where user_id = v_user_id and coalesce(is_deleted, 0) <> 0; raise notice '内置本地管理员 % 原为软删行,已复活(user_id=%,hash 未改)', v_username, v_user_id; end if; -- 成员行:没有它登录会报「该账号未关联任何租户」(判据 D7)。 if to_regclass('public.sys_tenant_user') is not null then insert into sys_tenant_user (user_id, tenant_id, org_id, status, is_deleted, created_at, updated_at) select v_user_id, v_tenant_id, null, 1, 0, now(), now() where not exists ( select 1 from sys_tenant_user tu where tu.user_id = v_user_id and tu.tenant_id = v_tenant_id and coalesce(tu.is_deleted, 0) = 0 ) on conflict (user_id, tenant_id) where (is_deleted = 0) do nothing; else raise notice 'sys_tenant_user 不存在,跳过成员行'; end if; -- 角色行:按候选角色码取第一个存在的(与自愈同一顺序),并按归属收窄到这一个 user_id。 if to_regclass('public.sys_role') is not null then select r.role_id, r.role_code, r.tenant_id into v_role_id, v_role_code, v_role_tenant from sys_role r where coalesce(r.is_deleted, 0) = 0 and r.role_code = any (string_to_array(v_role_codes, ',')) order by array_position(string_to_array(v_role_codes, ','), r.role_code) limit 1; -- 只检测不改选择(与 IdpLocalAdminAccountService.ensureRole 同口径、同一枚稳定码): -- 现采 role_code=TENANT_ADMIN 有两行(role_id=2 属 tenant 2 已软删、role_id=6 属 tenant 4 未删), -- 今天挡住我们的是"另一行恰好软删"这个数据巧合,不是判据;真带租户过滤会换掉首批选出的角色, -- 首切当天承担不起 ⇒ 先喊出来(症状 = 判据 D7:token 有、角色行有,但权限属于别的租户 → 403)。 if v_role_id is not null and v_role_tenant is not null and v_role_tenant <> v_tenant_id then raise notice '选中的角色 % (role_id=%) 属 tenant_id=%,与配置租户 % 不一致' '(reason=local_admin_role_tenant_mismatch)—— 内置账号可能拿到 token 但需要角色的接口一律 403', v_role_code, v_role_id, v_role_tenant, v_tenant_id; end if; if v_role_id is null then raise notice '候选角色码一个都不存在,内置管理员没有角色行(先建角色或改候选清单): %', v_role_codes; else insert into sys_user_role (tenant_id, user_id, role_id, is_deleted, created_at, updated_at) select v_tenant_id, v_user_id, v_role_id, 0, now(), now() where not exists ( select 1 from sys_user_role ur where ur.user_id = v_user_id and ur.role_id = v_role_id and ur.tenant_id = v_tenant_id and coalesce(ur.is_deleted, 0) = 0 ) on conflict (tenant_id, user_id, role_id) do nothing; raise notice '已确保 % 的角色行:role_code=% role_id=% tenant_id=%', v_username, v_role_code, v_role_id, v_tenant_id; end if; else raise notice 'sys_role 不存在,跳过角色行'; end if; raise notice '内置本地管理员自检完成:username=% user_id=% tenant_id=%', v_username, v_user_id, v_tenant_id; end $$; -- 结果直读:账号行 + 成员租户 + 角色码三者都要有,缺任何一个 = 能登录但调不通租户接口(判据 D7)。 select u.user_id, u.username, u.status, u.is_deleted, u.pwd_reset_required, (u.password_hash like '$2a$%' or u.password_hash like '$2b$%' or u.password_hash like '$2y$%') as has_bcrypt_hash, tu.tenant_id as membership_tenant, r.role_code as granted_role from sys_user u left join sys_tenant_user tu on tu.user_id = u.user_id and coalesce(tu.is_deleted, 0) = 0 left join sys_user_role ur on ur.user_id = u.user_id and coalesce(ur.is_deleted, 0) = 0 left join sys_role r on r.role_id = ur.role_id where u.username = coalesce(nullif(current_setting('idp.local_admin_username', true), ''), 'localadmin') order by u.user_id, r.role_code; commit; -- 回滚(只撤本脚本可能新建的行;库里本来就有 localadmin 时别执行,会把既有授权删掉)。 -- 三条都按 user_id 收窄。理由要真(20:07 现采 unis_crm_dev,口径=sys_user_role 全表含软删): -- sys_user_role 共 181 行 / 177 行未删;按 role_id 分组最宽的是 role_id=7(crm_xs)102 行, -- 其次 role_id=23 14 行;**role_id=1 在 sys_role 里根本不存在(sys_user_role 里也 0 行)**。 -- ⇒ 早先写的"role_id=1 有 129 行"数字来自另一件事(审计 19:31 P1 探针的 hub_bound_rows=129), -- 数是真的、口径不是这张表,所以那句理由当时站不住。102 行已经足够致命:只按 role_id 删 -- 等于把 crm_xs 里 102 个用户的授权一起删掉。 -- delete from sys_user_role where user_id = (select user_id from sys_user where username = 'localadmin'); -- delete from sys_tenant_user where user_id = (select user_id from sys_user where username = 'localadmin'); -- delete from sys_user where username = 'localadmin'; -- 只下线不删行(推荐;重启后自愈不会重建角色以外的东西): -- update sys_user set status = 0 where username = 'localadmin'; -- 注意:把配置键 crm.idp.local-admin.username 改名后再跑本脚本,会按新名字再建一行, -- 老那一行不会自动删,按上面的 delete 清单(换成旧用户名)手工收掉。