unis_crm/sql/20261119_idp_menu_perm.sql

244 lines
12 KiB
PL/PgSQL
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

-- 统一认证配置页(管理台)菜单权限:menu:idp-config
-- 为什么要这一行(取证结论):
-- frontend1 的侧边菜单完全由 GET /sys/api/permissions/me 返回的 sys_permission 行渲染
-- (frontend1/src/layouts/AppLayout.tsx:153 只保留 permType in ('menu','directory') 且 is_visible=1 且 status=1),
-- 静态路由表里的 perm 不参与菜单渲染,所以库里没有这行时菜单里根本不会出现入口,超管也一样
-- (usePermission 的 isAdmin 只放行按钮级 can(),不注入菜单行)。
-- 而路由本身只有 RequireAuth 登录校验、不按权限码拦截(frontend1/src/routes/index.tsx),
-- 所以本 SQL 没执行前,直接敲 /idp-config 也能打开页面,只是侧边栏没有入口。
--
-- 幂等,且尽量做到"重复执行零变化"(审计 §20 (b)):
-- 菜单行只在字段真的有差异时才 UPDATE(逐列 is distinct from 比较),
-- 不再每次跑都把 updated_at 改一遍;授权只在缺行时 INSERT。
-- 残留副作用(做不到零变化的部分,如实登记):
-- 1) 序列自检会前移(见下),跑一次不回退,属良性;
-- 2) 首次执行若 'system' 目录行不存在,会真的新建一行;
-- 3) 授权补写时 updated_at/created_at 取 now(),与执行时刻有关。
--
-- 授权面刻意**不复活**已收回的授权(审计 §20 (b) 第二点):
-- 旧写法无条件 set is_deleted = 0,等于每跑一次就把运维手工收回的授权再发一遍。
-- 现在 UPDATE 只回填 tenant_id,不碰 is_deleted;已被收回的行保持收回状态。
--
-- 授权对象收紧(审计 §20 (c)):
-- 兜底臂只发内置超管角色码(role_code in (...),并同时约束角色未删除)。
-- 旧写法多一条"join sys_user 后按 username='admin' 把他持有的全部角色都发一遍",
-- admin 若还挂着"普通用户"这类角色,菜单会一并给出去,而且不看 u.status、跨租户同名 admin 也命中。
-- role_name ilike 模糊匹配只用于首次 insert 的兜底,不进 UPDATE 臂(否则"商机管理员""财务管理员"会被反复拖进来)。
-- 按 username='admin' 反查角色的那条兜底臂已**整条删除**而不是收紧:
-- 收紧(要求 ur.is_deleted=0 且 r.role_code in 内置超管码 且 u.status=1)之后,
-- 它能命中的角色集合已经是上面 role_code 那条的子集,留着只会多一次 join、不会多发一行授权。
-- 删掉的效果与"收紧后的它"完全等价,且不再受"跨租户同名 admin""admin 挂着普通用户"这两类误伤。
--
-- 执行:需要能写 sys_permission / sys_role_permission 的账号,例如 psql -f sql/20261119_idp_menu_perm.sql
-- 回滚:见文件末尾,只删"本次新建的行"(审计 §20 (d))。
begin;
-- 本脚本按 public schema 写死(表名不带限定、setval 用裸序列名)。
-- 如果目标库把 CRM 表装在别的 schema,下面 to_regclass 的守卫会判空并跳过序列自检,
-- 而 insert/update 会直接报"表不存在",不会静默写到 public 去。
set search_path to public;
-- 记下"本次真的新建了哪些行",回滚只删这些,不碰执行前就存在的行。
-- 用临时表而不是靠 perm_id 反查:sys_role_permission 里同一个 perm_id 可能早就有授权行,
-- 按 perm_id 直接 DELETE 会把本脚本之前就有的授权一起删掉。
create temp table if not exists tmp_idp_menu_created_perm (
table_name text not null,
row_id bigint not null
);
truncate table tmp_idp_menu_created_perm;
do $$
declare
v_system_parent_id bigint;
v_menu_perm_id bigint;
v_has_role_permission_tenant boolean;
begin
-- 序列对齐,避免 nextval 撞上历史残留行(本轮已踩过这个坑)。
-- 加 to_regclass 守卫:序列不叫这个名字(改名/非 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_system_parent_id
from sys_permission
where code = 'system'
and coalesce(is_deleted, 0) = 0
order by perm_id
limit 1;
if v_system_parent_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 (
null, '系统管理', 'system', 'directory', 1, null, null, 'SettingOutlined',
110, 1, 1, '系统管理目录', '{}'::jsonb, 0, now(), now()
)
returning perm_id into v_system_parent_id;
insert into tmp_idp_menu_created_perm values ('sys_permission', v_system_parent_id);
end if;
-- 菜单行:不存在才 insert;已存在就"先比较再 update",字段全一致时一行都不写。
select perm_id
into v_menu_perm_id
from sys_permission
where code = 'menu:idp-config'
order by perm_id
limit 1;
if v_menu_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_system_parent_id, '统一认证配置', 'menu:idp-config', 'menu', 2,
'/idp-config', null, 'SafetyCertificateOutlined', 12, 1, 1,
'Hub 对接参数在线配置:免登入口、同步与建号、护栏、登录与会话',
jsonb_build_object('tenantScoped', true), 0, now(), now()
)
returning perm_id into v_menu_perm_id;
insert into tmp_idp_menu_created_perm values ('sys_permission', v_menu_perm_id);
else
update sys_permission
set parent_id = v_system_parent_id,
name = '统一认证配置',
perm_type = 'menu',
level = 2,
path = '/idp-config',
component = null,
icon = 'SafetyCertificateOutlined',
sort_order = 12,
is_visible = 1,
status = 1,
description = 'Hub 对接参数在线配置:免登入口、同步与建号、护栏、登录与会话',
meta = jsonb_build_object('tenantScoped', true),
is_deleted = 0,
updated_at = now()
where perm_id = v_menu_perm_id
and (
parent_id is distinct from v_system_parent_id
or name is distinct from '统一认证配置'
or perm_type is distinct from 'menu'
or level is distinct from 2
or path is distinct from '/idp-config'
or component is distinct from null
or icon is distinct from 'SafetyCertificateOutlined'
or sort_order is distinct from 12
or is_visible is distinct from 1
or status is distinct from 1
or description is distinct from 'Hub 对接参数在线配置:免登入口、同步与建号、护栏、登录与会话'
or meta is distinct from jsonb_build_object('tenantScoped', true)
or coalesce(is_deleted, 0) is distinct from 0
);
end if;
-- 授权臂 A:内置超管角色码 + 模糊角色名兜底(只在 insert 里用,不复活已收回的授权)
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, v_menu_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')
or r.role_name ilike '%管理员%'
or r.role_name ilike '%admin%'
)
) role_source
where v_menu_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 = v_menu_perm_id
)
returning id
)
insert into tmp_idp_menu_created_perm select 'sys_role_permission', id from inserted;
-- 授权臂 B 的 UPDATE:只把空着的 tenant_id 填上(等价于原来的 coalesce),
-- 不碰 is_deleted、不用模糊角色名(见文件头 (c) 段);已有值一律保留不动。
update sys_role_permission rp
set tenant_id = r.tenant_id,
updated_at = now()
from sys_role r
where rp.role_id = r.role_id
and rp.perm_id = v_menu_perm_id
and rp.tenant_id is null
and r.tenant_id is not null
and coalesce(r.is_deleted, 0) = 0
and r.role_code in ('TENANT_ADMIN', 'ADMIN', 'SYS_ADMIN', 'PLATFORM_ADMIN', 'SUPER_ADMIN');
else
with inserted as (
insert into sys_role_permission (role_id, perm_id, is_deleted, created_at, updated_at)
select role_source.role_id, v_menu_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')
or r.role_name ilike '%管理员%'
or r.role_name ilike '%admin%'
)
) role_source
where v_menu_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 = v_menu_perm_id
)
returning id
)
insert into tmp_idp_menu_created_perm select 'sys_role_permission', id from inserted;
-- 无 tenant_id 列的库:这一臂没有需要回填的字段,刻意不做 UPDATE——
-- 已存在的行(含运维手工收回的 is_deleted=1)一律保持原样,不再被复活。
end if;
raise notice 'menu:idp-config perm_id=%', coalesce(v_menu_perm_id, 0);
raise notice '本次新建行数=%(回滚只会删这些)', (select count(*) from tmp_idp_menu_created_perm);
end $$;
-- 把"本次新建的行"打在结果里,跨会话回滚时照这个清单删。
select table_name, row_id from tmp_idp_menu_created_perm order by table_name, row_id;
commit;
-- 回滚:只删本脚本新建的行(审计 §20 (d))。
-- 同会话回滚(临时表还在):
-- delete from sys_role_permission
-- where id in (select row_id from tmp_idp_menu_created_perm where table_name = 'sys_role_permission');
-- delete from sys_permission
-- where perm_id in (select row_id from tmp_idp_menu_created_perm where table_name = 'sys_permission');
-- 新会话回滚:先按上面 select 出来的清单手写 id,再删;
-- 只有确认 menu:idp-config 这一行的授权全部来自本脚本时,才可以按 perm_id 删:
-- delete from sys_role_permission
-- where perm_id = (select perm_id from sys_permission where code = 'menu:idp-config');
-- delete from sys_permission where code = 'menu:idp-config';
-- 注意:
-- * 'system' 目录行只有"本次新建"时才需要删,库里本来就有就别动(清单里有它才删)。
-- * 序列自检前移的位置不回退,也不该回退。
-- * 本脚本从不删角色、不碰别的权限行。