271 lines
14 KiB
PL/PgSQL
271 lines
14 KiB
PL/PgSQL
-- 统一认证配置页的按钮级权限码:idp:config:read / idp:config:write
|
||
--
|
||
-- 与 18:5x 已执行版的差异仅为注释与 NOTICE 文案;Lead 2026-11-19 20:0x 重跑本脚本新建行数=0,
|
||
-- 库内状态无需变更(Leader 定的乙方案,原话照抄在此,供审计销账)。
|
||
-- 他的证据(tmp/idp/dbexec.py,剥自带 COMMIT + 回滚后真跑):本次新建行数=0,NOTICE 回
|
||
-- idp:config:read perm_id=93 / idp:config:write perm_id=94;库里 92 menu:idp-config、93 read、
|
||
-- 94 write 全部 is_deleted=0 且 status=1,各 1 条未删授权给 TENANT_ADMIN(6)。
|
||
-- 我方对"差异只有注释/NOTICE"的独立证据:v5 清单记的开发库执行版 mtime=18:04:01,盘上这版
|
||
-- 只由 apply_window.py 的 taskB 两处补丁改过,两处都是 -- 注释与 RAISE NOTICE 文本,
|
||
-- 写语句(INSERT/UPDATE)一条没动。要重跑也无害:全脚本幂等,UPDATE 臂不复活运维收回的授权。
|
||
--
|
||
-- 为什么要单独两个码(拍板 A1/A2):
|
||
-- GET /api/idp/admin/config 与 PUT /api/idp/admin/config 现在只要求"有 CRM 会话",
|
||
-- 也就是任何登录用户都能改 Hub base-url 与集成密钥 —— 那是免密链路的两根地基
|
||
-- (目录往哪儿发、验票听谁的),身份伪造量级,不该由"能登录"买单。
|
||
-- 菜单码 menu:idp-config(sql/20261119_idp_menu_perm.sql)当不了这个边界:
|
||
-- 那份授权臂带角色名模糊匹配兜底,"商机管理员"这类业务型角色也会拿到菜单码,
|
||
-- 于是"看得见页面"被静默当成"改得动配置"。所以这里发的是两个**按钮码**,
|
||
-- 代码侧只认 hasPermi("idp:config:read") / hasPermi("idp:config:write") 这两个形状。
|
||
--
|
||
-- 授权对象只按 role_code 命中内置超管角色,刻意不按角色名匹配(同上一段理由)。
|
||
-- 菜单联动(Leader 2026-11-19 裁定):凡已拿到 menu:idp-config 的角色,本脚本会补授 idp:config:read;
|
||
-- idp:config:write 不联动 —— "看得见"与"改得动"必须分开授权。只加在本文件里,
|
||
-- sql/20261119_idp_menu_perm.sql 不动(同一件事不在两份文件里各说一半)。
|
||
-- 业务型角色要放开得由超管在管理台显式勾选,脚本不猜。
|
||
--
|
||
-- 幂等:权限行只在字段真有差异时才 UPDATE(逐列 is distinct from),重复执行零写入;
|
||
-- 授权行只在缺失时 INSERT,且不复活已被运维收回的授权(UPDATE 臂不碰 is_deleted)。
|
||
-- 残留副作用:序列自检会前移(跑一次不回退,良性);首次执行若 menu:idp-config 还没有,
|
||
-- 两个按钮会先挂在根上,等菜单脚本跑完再跑本脚本即可自动归位(见下面的 UPDATE 臂)。
|
||
-- 执行:psql -f sql/20261119_idp_config_perms.sql(需要能写 sys_permission / sys_role_permission)
|
||
-- 回滚:见文件末尾,只删本次新建的行。
|
||
|
||
begin;
|
||
|
||
set search_path to public;
|
||
|
||
-- 记下本次真的新建了哪些行,回滚按这份清单删,不按权限码整片删
|
||
-- (同一个权限码可能早就有别的授权行,按码删会把执行前的授权一起删掉)。
|
||
create temp table if not exists tmp_idp_perm_created (
|
||
table_name text not null,
|
||
row_id bigint not null
|
||
);
|
||
truncate table tmp_idp_perm_created;
|
||
|
||
do $$
|
||
declare
|
||
v_menu_perm_id bigint;
|
||
v_read_perm_id bigint;
|
||
v_write_perm_id bigint;
|
||
v_has_role_permission_tenant boolean;
|
||
begin
|
||
-- 序列自检带存在性守卫:序列改名或不在 public schema 时判空跳过,
|
||
-- 不让 42P01 关系不存在把整个事务打断(本轮审计 §20(D))。
|
||
if to_regclass('sys_permission_perm_id_seq') is not null then
|
||
perform setval('sys_permission_perm_id_seq',
|
||
coalesce((select max(perm_id) from sys_permission), 0) + 1, false);
|
||
end if;
|
||
if to_regclass('sys_role_permission_id_seq') is not null then
|
||
perform setval('sys_role_permission_id_seq',
|
||
coalesce((select max(id) from sys_role_permission), 0) + 1, false);
|
||
end if;
|
||
|
||
select exists (
|
||
select 1
|
||
from information_schema.columns
|
||
where table_schema = current_schema()
|
||
and table_name = 'sys_role_permission'
|
||
and column_name = 'tenant_id'
|
||
) into v_has_role_permission_tenant;
|
||
|
||
select perm_id into v_menu_perm_id
|
||
from sys_permission
|
||
where code = 'menu:idp-config'
|
||
order by perm_id
|
||
limit 1;
|
||
|
||
-- ---- 读码 ----
|
||
select perm_id into v_read_perm_id
|
||
from sys_permission
|
||
where code = 'idp:config:read'
|
||
order by perm_id
|
||
limit 1;
|
||
|
||
if v_read_perm_id is null then
|
||
insert into sys_permission (
|
||
parent_id, name, code, perm_type, level, path, component, icon,
|
||
sort_order, is_visible, status, description, meta, is_deleted, created_at, updated_at
|
||
) values (
|
||
v_menu_perm_id, '查看统一认证配置', 'idp:config:read', 'button', 3,
|
||
null, null, null, 1, 1, 1,
|
||
'查看统一认证配置页与同步批次;不含任何修改能力', '{}'::jsonb, 0, now(), now()
|
||
)
|
||
returning perm_id into v_read_perm_id;
|
||
|
||
insert into tmp_idp_perm_created values ('sys_permission', v_read_perm_id);
|
||
else
|
||
-- 只在真有差异时写;菜单行后补的场景由这条臂自动归位。
|
||
update sys_permission
|
||
set parent_id = v_menu_perm_id,
|
||
name = '查看统一认证配置',
|
||
perm_type = 'button',
|
||
level = 3,
|
||
is_visible = 1,
|
||
status = 1,
|
||
description = '查看统一认证配置页与同步批次;不含任何修改能力',
|
||
is_deleted = 0,
|
||
updated_at = now()
|
||
where perm_id = v_read_perm_id
|
||
and (
|
||
parent_id is distinct from v_menu_perm_id
|
||
or name is distinct from '查看统一认证配置'
|
||
or perm_type is distinct from 'button'
|
||
or level is distinct from 3
|
||
or is_visible is distinct from 1
|
||
or status is distinct from 1
|
||
or coalesce(is_deleted, 0) is distinct from 0
|
||
);
|
||
end if;
|
||
|
||
-- ---- 写码 ----
|
||
select perm_id into v_write_perm_id
|
||
from sys_permission
|
||
where code = 'idp:config:write'
|
||
order by perm_id
|
||
limit 1;
|
||
|
||
if v_write_perm_id is null then
|
||
insert into sys_permission (
|
||
parent_id, name, code, perm_type, level, path, component, icon,
|
||
sort_order, is_visible, status, description, meta, is_deleted, created_at, updated_at
|
||
) values (
|
||
v_menu_perm_id, '修改统一认证配置', 'idp:config:write', 'button', 3,
|
||
null, null, null, 2, 1, 1,
|
||
'保存统一认证运行参数(Hub 地址、开关、建号模式、护栏阈值、内置管理员开关)',
|
||
'{}'::jsonb, 0, now(), now()
|
||
)
|
||
returning perm_id into v_write_perm_id;
|
||
|
||
insert into tmp_idp_perm_created values ('sys_permission', v_write_perm_id);
|
||
else
|
||
update sys_permission
|
||
set parent_id = v_menu_perm_id,
|
||
name = '修改统一认证配置',
|
||
perm_type = 'button',
|
||
level = 3,
|
||
is_visible = 1,
|
||
status = 1,
|
||
description = '保存统一认证运行参数(Hub 地址、开关、建号模式、护栏阈值、内置管理员开关)',
|
||
is_deleted = 0,
|
||
updated_at = now()
|
||
where perm_id = v_write_perm_id
|
||
and (
|
||
parent_id is distinct from v_menu_perm_id
|
||
or name is distinct from '修改统一认证配置'
|
||
or perm_type is distinct from 'button'
|
||
or level is distinct from 3
|
||
or is_visible is distinct from 1
|
||
or status is distinct from 1
|
||
or coalesce(is_deleted, 0) is distinct from 0
|
||
);
|
||
end if;
|
||
|
||
-- ---- 授权臂:只按 role_code 命中内置超管角色 ----
|
||
if v_has_role_permission_tenant then
|
||
with inserted as (
|
||
insert into sys_role_permission (role_id, perm_id, tenant_id, is_deleted, created_at, updated_at)
|
||
select role_source.role_id, target.perm_id, role_source.tenant_id, 0, now(), now()
|
||
from (
|
||
select distinct r.role_id, r.tenant_id
|
||
from sys_role r
|
||
where coalesce(r.is_deleted, 0) = 0
|
||
and r.role_code in ('TENANT_ADMIN', 'ADMIN', 'SYS_ADMIN', 'PLATFORM_ADMIN', 'SUPER_ADMIN')
|
||
) role_source
|
||
cross join (values (v_read_perm_id), (v_write_perm_id)) as target(perm_id)
|
||
where target.perm_id is not null
|
||
and not exists (
|
||
select 1
|
||
from sys_role_permission rp
|
||
where rp.role_id = role_source.role_id
|
||
and rp.perm_id = target.perm_id
|
||
)
|
||
returning id
|
||
)
|
||
insert into tmp_idp_perm_created select 'sys_role_permission', id from inserted;
|
||
else
|
||
with inserted as (
|
||
insert into sys_role_permission (role_id, perm_id, is_deleted, created_at, updated_at)
|
||
select role_source.role_id, target.perm_id, 0, now(), now()
|
||
from (
|
||
select distinct r.role_id
|
||
from sys_role r
|
||
where coalesce(r.is_deleted, 0) = 0
|
||
and r.role_code in ('TENANT_ADMIN', 'ADMIN', 'SYS_ADMIN', 'PLATFORM_ADMIN', 'SUPER_ADMIN')
|
||
) role_source
|
||
cross join (values (v_read_perm_id), (v_write_perm_id)) as target(perm_id)
|
||
where target.perm_id is not null
|
||
and not exists (
|
||
select 1
|
||
from sys_role_permission rp
|
||
where rp.role_id = role_source.role_id
|
||
and rp.perm_id = target.perm_id
|
||
)
|
||
returning id
|
||
)
|
||
insert into tmp_idp_perm_created select 'sys_role_permission', id from inserted;
|
||
end if;
|
||
|
||
-- ---- 菜单联动(Leader 2026-11-19 裁定):凡拿到 menu:idp-config 的角色,同时给 idp:config:read ----
|
||
-- 为什么:菜单码与按钮码分开是为了"看得见 != 改得动",但只给菜单码不给 read 会得到
|
||
-- "入口在、点进去一句加载失败"的组合,运维只会以为后端坏了。write 不联动 —— 改得动必须是单独授权。
|
||
-- 只在缺行时 INSERT,不复活运维手工收回的授权(与上面的授权臂同一个口径)。
|
||
if v_read_perm_id is not null and v_has_role_permission_tenant then
|
||
with inserted as (
|
||
insert into sys_role_permission (role_id, perm_id, tenant_id, is_deleted, created_at, updated_at)
|
||
select rp.role_id, v_read_perm_id, rp.tenant_id, 0, now(), now()
|
||
from sys_role_permission rp
|
||
where rp.perm_id = v_menu_perm_id
|
||
and v_menu_perm_id is not null
|
||
and not exists (
|
||
select 1
|
||
from sys_role_permission existing
|
||
where existing.role_id = rp.role_id
|
||
and existing.perm_id = v_read_perm_id
|
||
)
|
||
returning id
|
||
)
|
||
insert into tmp_idp_perm_created select 'sys_role_permission', id from inserted;
|
||
end if;
|
||
|
||
|
||
raise notice 'idp:config:read perm_id=% / idp:config:write perm_id=%',
|
||
coalesce(v_read_perm_id, 0), coalesce(v_write_perm_id, 0);
|
||
raise notice '本次新建行数=%(回滚只会删这些)', (select count(*) from tmp_idp_perm_created);
|
||
end $$;
|
||
|
||
-- 把"本次新建的行"打在结果里,跨会话回滚时照这个清单删。
|
||
select table_name, row_id from tmp_idp_perm_created order by table_name, row_id;
|
||
|
||
commit;
|
||
|
||
-- 执行完必须知道的一件事(写成 RAISE 而不是只留注释:这一条本轮真踩过)。
|
||
-- 改的是授权表,而框架的用户权限缓存**不会**因此失效,**重新登录也不刷新**:缓存里存的还是授权前的快照
|
||
-- (TTL 约 45 分钟),reviewer 的 F5 是删掉键才跑通的。框架有**两族**键,各管一半(审计 §P 现采):
|
||
-- sys:auth:perm:{userId}:{tenantId}:{menuFlag} —— 按钮级 hasPermi(漏了它=菜单出来了、按钮点不动);
|
||
-- sys:auth:perm:list:{userId}:{tenantId} —— 权限清单/侧边栏菜单(漏了它=入口都不出来)。
|
||
-- menu:idp-config 走后一族,idp:config:read / idp:config:write 这两个按钮码走前一族,两边都得清。
|
||
do $$
|
||
begin
|
||
raise notice '运维必做:授权已改,但权限缓存不会自动失效(**重新登录也不刷新**,TTL 约 45 分钟)。'
|
||
'两族键都要清,只清一族会得到半套权限:';
|
||
raise notice ' 1) sys:auth:perm:{userId}:{tenantId}:{menuFlag} —— 管按钮级 hasPermi;'
|
||
'漏了它的症状是「菜单出来了、按钮点不动」';
|
||
raise notice ' 2) sys:auth:perm:list:{userId}:{tenantId} —— 管权限清单与侧边栏菜单入口;'
|
||
'漏了它的症状是「授权给了、入口都不出来」';
|
||
raise notice ' 拿不准就整片清 sys:auth:perm*(本项目的 Redis 库),别把「让用户重新登录」当处置。';
|
||
end $$;
|
||
|
||
-- 回滚(只删本脚本新建的行;序列自检前移的位置不回退,也不该回退)。
|
||
-- 同会话回滚(临时表还在):
|
||
-- delete from sys_role_permission
|
||
-- where id in (select row_id from tmp_idp_perm_created where table_name = 'sys_role_permission');
|
||
-- delete from sys_permission
|
||
-- where perm_id in (select row_id from tmp_idp_perm_created where table_name = 'sys_permission');
|
||
-- 新会话回滚:先按上面 select 出来的清单手写 id 再删。
|
||
-- 只有确认这两个权限码的授权全部来自本脚本时,才可以按码删:
|
||
-- delete from sys_role_permission where perm_id in
|
||
-- (select perm_id from sys_permission where code in ('idp:config:read', 'idp:config:write'));
|
||
-- delete from sys_permission where code in ('idp:config:read', 'idp:config:write');
|
||
-- 注意:本脚本从不删角色、不碰别的权限行,也不动 menu:idp-config 那一行(归菜单脚本管)。
|