-- 20261123 统一认证建号未授权:给「同步建出来但一个角色都没授上」的账号回补默认角色 -- -- 干什么:找出还挂着「建号未授权」(kind = 'NO_MATCH' 且 reasonCode = DEFAULT_ROLE_*) -- 并且人还在(sys_user.is_deleted = 0)、身上一个未删角色都没有的账号, -- 按 crm.idp.provision.default-role-code 当时的配置(现采 'XS_Col,crm_xs')把角色补上。 -- 角色按 role_code 现场查,不写死 role_id:换库、重建、租户迁移之后 role_id 会变,编码不会。 -- -- 修的是哪个问题:2026-11-23 12:37 那批全量同步(batch_id = 324)从 Hub 新建了 57 个账号, -- 当时 default-role-code 配的是整串 'XS_Col,crm_xs',老实现拿整串去查 role_id, -- 一个都查不到,于是这 57 个人建号成功、角色零授 —— 登进 CRM 是一片空白, -- 队列里 57 条「建号未授权」。配置在 18:55 才改成逐个编码解析(16475 号操作留痕), -- 但那 57 条是改之前建的,改配置不会回头给他们补授权。 -- -- 为什么必须回补、不能只等新配置:默认角色只在 provision() 建号那一刻授一次, -- 已有的号不会再走那条路。而 C3 只允许两处写 sys_user_role(建号、内置管理员自愈), -- 同步运行期没有"给存量号补默认角色"的入口 —— 所以这一批只能显式迁一次。 -- -- 幂等:insert ... on conflict (tenant_id, user_id, role_id) do nothing, -- 目标人群用「现在还没有任何未删角色」现算,跑第二遍 0 行。 -- 跑完由下一轮同步(全量/增量/立即同步/预演都算)自动把那 57 条待办结案, -- 结案走 IdpUserSyncService.closeSatisfiedPending,不需要在这份脚本里动队列表。 -- -- 为什么不顺手把部门也一起补了:Hub 侧那 8 个部门名(软件开发事业部、交付运维科……) -- 在 CRM 这边一个都对不上现成的 sys_org,补哪个都是替业务猜归属。 -- 机器不猜部门是同步的既有判据(契约 C4 同样禁止同步写 org_id), -- 所以这里只补授权这一件有唯一正确答案的事。部门见文件末尾的清单,要人工定。 begin; -- A. 回补默认角色。目标人群的判据与读侧/自动结案逐字一致: -- OPEN + NO_MATCH + DEFAULT_ROLE_*,人在,且身上没有未删的活角色。 with targets as ( select distinct pp.tenant_id, pp.local_user_id as user_id from crm_idp_pending pp join sys_user u on u.user_id = pp.local_user_id and u.is_deleted = 0 where pp.status = 'OPEN' and pp.kind = 'NO_MATCH' and pp.local_user_id is not null and (pp.detail ->> 'reasonCode' in ('DEFAULT_ROLE_MISSING', 'DEFAULT_ROLE_PARTIAL') or (pp.detail ->> 'reasonCode' is null and pp.detail ->> 'reason' like '%默认角色编码%')) and not exists ( select 1 from sys_user_role ur join sys_role r on r.role_id = ur.role_id and coalesce(r.is_deleted, 0) = 0 where ur.user_id = pp.local_user_id and coalesce(ur.is_deleted, 0) = 0) ), roles as ( -- 只授「配置里那几个编码 + 同租户 + 未删」的角色行;查不到编码就整段 0 行,不会瞎授 select r.tenant_id, r.role_id from sys_role r where r.role_code in ('XS_Col', 'crm_xs') and coalesce(r.is_deleted, 0) = 0 ) insert into sys_user_role (tenant_id, user_id, role_id, is_deleted, created_at, updated_at) select t.tenant_id, t.user_id, ro.role_id, 0, now(), now() from targets t join roles ro on ro.tenant_id = t.tenant_id on conflict (tenant_id, user_id, role_id) do nothing; commit; -- B. 跑完自查 1:还剩几个「建号未授权且一个角色都没有」的人。跑成功应当是 0。 -- 注意这里数的是「人」不是「队列条数」:结案要等下一轮同步,条数不会当场变。 select count(distinct pp.local_user_id) as role_missing_accounts_left from crm_idp_pending pp join sys_user u on u.user_id = pp.local_user_id and u.is_deleted = 0 where pp.status = 'OPEN' and pp.kind = 'NO_MATCH' and pp.local_user_id is not null and (pp.detail ->> 'reasonCode' in ('DEFAULT_ROLE_MISSING', 'DEFAULT_ROLE_PARTIAL') or (pp.detail ->> 'reasonCode' is null and pp.detail ->> 'reason' like '%默认角色编码%')) and not exists ( select 1 from sys_user_role ur join sys_role r on r.role_id = ur.role_id and coalesce(r.is_deleted, 0) = 0 where ur.user_id = pp.local_user_id and coalesce(ur.is_deleted, 0) = 0); -- C. 跑完自查 2:这批人现在各自有几个角色,应当人人都 >= 1。 select count(*) as accounts, min(granted) as min_roles, max(granted) as max_roles from ( select pp.local_user_id, (select count(*) from sys_user_role ur join sys_role r on r.role_id = ur.role_id and coalesce(r.is_deleted, 0) = 0 where ur.user_id = pp.local_user_id and coalesce(ur.is_deleted, 0) = 0) as granted from (select distinct local_user_id from crm_idp_pending where status = 'OPEN' and kind = 'NO_MATCH' and local_user_id is not null and (detail ->> 'reasonCode' in ('DEFAULT_ROLE_MISSING', 'DEFAULT_ROLE_PARTIAL') or (detail ->> 'reasonCode' is null and detail ->> 'reason' like '%默认角色编码%'))) pp where exists (select 1 from sys_user u where u.user_id = pp.local_user_id and u.is_deleted = 0) ) g; -- D. 部门还没人定:这是「部门待确认」按 Hub 部门名的分布,人工认领后去 -- 「用户管理 → 编辑」逐个挂部门(或一次性 update sys_tenant_user.org_id)。 -- 挂完之后同样由下一轮同步自动结案,不用回来改这份脚本。 select pp.detail ->> 'hubOrgName' as hub_org_name, count(distinct pp.local_user_id) as accounts, count(distinct pp.local_user_id) filter ( where exists (select 1 from sys_tenant_user tu where tu.user_id = pp.local_user_id and coalesce(tu.is_deleted, 0) = 0 and tu.org_id is not null)) as already_set from crm_idp_pending pp join sys_user u on u.user_id = pp.local_user_id and u.is_deleted = 0 where pp.status = 'OPEN' and pp.kind = 'ORG_ASSIST' and pp.local_user_id is not null group by 1 order by 2 desc; -- 回滚:只删本轮按编码授的那两类角色,且按"这一批人"收窄 —— 直接按 role_id 删 -- 会把早就在用的销售职员/销售一起删掉(现采 crm_xs 全量 102 人,XS_Col 23 人, -- 本轮只该影响这 57 个人)。所以回滚条件必须带 user_id 子查询,别图省事去掉。 -- begin; -- delete from sys_user_role -- where role_id in (select role_id from sys_role -- where role_code in ('XS_Col', 'crm_xs') and coalesce(is_deleted, 0) = 0) -- and coalesce(is_deleted, 0) = 0 -- and user_id in (select distinct pp.local_user_id -- from crm_idp_pending pp -- where pp.status in ('OPEN', 'RESOLVED') -- and pp.kind = 'NO_MATCH' -- and pp.local_user_id is not null -- and (pp.detail ->> 'reasonCode' in ('DEFAULT_ROLE_MISSING', 'DEFAULT_ROLE_PARTIAL') -- or (pp.detail ->> 'reasonCode' is null -- and pp.detail ->> 'reason' like '%默认角色编码%'))); -- commit; -- -- 提醒一句:回滚之后**别在回滚前跑过同步**,否则那批待办已经被自动结案, -- 回滚角色不会把它们重新打开;下一轮同步会照旧重新排队,等一轮即可。