-- 内置本地管理员 localadmin(契约 §30 本地登录收敛) -- -- 干什么:把启动自愈(IdpLocalAdminAccountService.ensureLocalAdmin)做的三件事用 SQL 再做一遍 —— -- 账号行 sys_user + 成员行 sys_tenant_user + 角色行 sys_user_role。 -- 什么时候需要手工跑:应用账号对这三张表没有写权限、上线前想先手工验证入口、 -- 或多实例滚动启动时想让库里先有一行(自愈是幂等的,两边都不会撞)。 -- -- 口令:下面那个 hash 是 admin123 的 bcrypt(cost 10),与配置键 crm.idp.local-admin.password -- 的默认值同一份。生产应先用环境变量 CRM_IDP_LOCAL_ADMIN_PASSWORD 换掉默认口令再开入口; -- 换了 env 就别跑本脚本(脚本只在缺行时写 hash,不会把已改过的口令打回 admin123)。 -- 要重置成默认口令:把 crm.idp.local-admin.reset-password 置 true 重启一次,别改本脚本。 -- -- 幂等:账号行 on conflict (username) do nothing;成员行与角色行同理;已软删的同名僵尸行 -- 只复活不改口令(sys_user_username_key 是全表唯一、不区分 is_deleted,不复活就一直建不出来)。 -- 授权:只按 role_code 取内置超管角色码(与 sql/20261119_idp_config_perms.sql 同一批), -- 一个都不存在时退回 crm_xs。不建新角色,也不额外发别的授权。 -- 执行:psql -f sql/20261119_idp_local_admin.sql -- 回滚:见文件末尾,只删本脚本可能新建的行。 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_is_deleted integer; begin if to_regclass('public.sys_user') is null then raise notice 'public.sys_user 不存在,本脚本不适用(已整段跳过)'; return; end if; -- 序列自检带存在性守卫:撞主键的成因与 sys_param 那次一样(本轮真库实测)。 -- 只上调不下调(greatest 取两者里更高的那个,与 20261119_idp_sys_param_setval.sql 同一口径): -- 已经跑到 max+1 之上就不往回拉,免得把序列位置退回去让后来人以为没修过。 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 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; else raise notice '已创建内置本地管理员 % (user_id=%),口令是配置默认值 admin123', v_username, v_user_id; end if; 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) values (v_user_id, v_tenant_id, null, 1, 0, now(), now()) on conflict (user_id, tenant_id) where (is_deleted = 0) do nothing; else raise notice 'sys_tenant_user 不存在,跳过成员行'; end if; -- 角色行:按候选角色码取第一个存在的(与自愈同一顺序)。 if to_regclass('public.sys_role') is not null then select r.role_id, r.role_code into v_role_id, v_role_code 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; 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) values (v_tenant_id, v_user_id, v_role_id, 0, now(), now()) 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 时别执行,会把既有授权删掉)。 -- 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 清单手工收掉。