192 lines
8.4 KiB
PL/PgSQL
192 lines
8.4 KiB
PL/PgSQL
-- 管理端门户授权码:menu:admin-portal
|
||
-- 背景:frontend1(系统管理前端)已整合进 frontend,挂在 /admin/* 下,系统只剩一个前端。
|
||
-- 原来管理端是独立端口(:9003 / :8090),只要登录就能进;合并后入口收进 CRM 侧边栏,
|
||
-- 需要能配置“谁能看到管理端”,所以新增这个权限码。
|
||
--
|
||
-- 判定顺序(frontend/src/lib/adminAccess.ts):
|
||
-- 1) profile.isAdmin 或 profile.isPlatformAdmin(内置超管/平台管理员)直接放行,不依赖本行;
|
||
-- 2) 其余账号必须在 sys_permission 里有 code='menu:admin-portal' 且被授权给所属角色,才显示入口、才允许进 /admin。
|
||
-- 故意不复用 canUsePermission():那个函数在权限码列表为空时一律放行,当门户门禁会误放开。
|
||
--
|
||
-- is_visible = 1:/sys/api/permissions/me 由 unisbase starter 提供(不在本仓库里),无法证实
|
||
-- 它会不会把 is_visible=0 的行过滤掉。门禁码一旦下发不到前端,非超管就永远进不去 /admin,
|
||
-- 所以这里保持可见,改由前端在管理端侧边栏按 code 过滤掉它
|
||
-- (frontend/src/admin/layouts/AppLayout.tsx 的 isAdminPortalEntryMenu),顺带让它在「权限管理」里可勾选。
|
||
--
|
||
-- 与 20261119_idp_menu_perm.sql 同样的幂等口径:只在真的有差异时 UPDATE,授权只在缺行时 INSERT,
|
||
-- 不复活运维手工收回的授权。默认只发给内置管理员角色码,不做 role_name 模糊匹配
|
||
-- (“商机管理员”“财务管理员”这类业务角色不该默认看到系统管理端)。
|
||
--
|
||
-- 执行:psql -f sql/20261119_admin_portal_perm.sql
|
||
-- 回滚:见文件末尾。
|
||
|
||
begin;
|
||
|
||
set search_path to public;
|
||
|
||
create temp table if not exists tmp_admin_portal_created_perm (
|
||
table_name text not null,
|
||
row_id bigint not null
|
||
);
|
||
truncate table tmp_admin_portal_created_perm;
|
||
|
||
do $$
|
||
declare
|
||
v_system_parent_id bigint;
|
||
v_menu_perm_id bigint;
|
||
v_has_role_permission_tenant boolean;
|
||
begin
|
||
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;
|
||
|
||
-- 挂在已有的“系统管理”目录下;没有就顺手建一个(与 idp 菜单脚本保持一致的做法)
|
||
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_admin_portal_created_perm values ('sys_permission', v_system_parent_id);
|
||
end if;
|
||
|
||
select perm_id
|
||
into v_menu_perm_id
|
||
from sys_permission
|
||
where code = 'menu:admin-portal'
|
||
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:admin-portal', 'menu', 2,
|
||
'/admin', null, 'SettingOutlined', 5, 1, 1,
|
||
'CRM 侧边栏“系统管理”入口开关:控制非超管账号能否进入 /admin 系统管理端',
|
||
jsonb_build_object('tenantScoped', true), 0, now(), now()
|
||
)
|
||
returning perm_id into v_menu_perm_id;
|
||
|
||
insert into tmp_admin_portal_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 = '/admin',
|
||
icon = 'SettingOutlined',
|
||
sort_order = 5,
|
||
-- 之前若按 is_visible=0 落过库,这里统一改回可见(下发不到前端就没法当门禁)
|
||
is_visible = 1,
|
||
status = 1,
|
||
description = 'CRM 侧边栏“系统管理”入口开关:控制非超管账号能否进入 /admin 系统管理端',
|
||
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 '/admin'
|
||
or icon is distinct from 'SettingOutlined'
|
||
or sort_order is distinct from 5
|
||
or is_visible is distinct from 1
|
||
or status is distinct from 1
|
||
or coalesce(is_deleted, 0) is distinct from 0
|
||
);
|
||
end if;
|
||
|
||
-- 默认只发给内置管理员角色码;已有授权(含被手工收回的)一律不动
|
||
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')
|
||
) 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_admin_portal_created_perm 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, 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')
|
||
) 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_admin_portal_created_perm select 'sys_role_permission', id from inserted;
|
||
end if;
|
||
|
||
raise notice 'menu:admin-portal perm_id=%', coalesce(v_menu_perm_id, 0);
|
||
raise notice '本次新建行数=%(回滚只会删这些)', (select count(*) from tmp_admin_portal_created_perm);
|
||
end $$;
|
||
|
||
select table_name, row_id from tmp_admin_portal_created_perm order by table_name, row_id;
|
||
|
||
commit;
|
||
|
||
-- 回滚:只删本脚本新建的行(同会话可用临时表;跨会话按上面输出的清单删)。
|
||
-- delete from sys_role_permission where perm_id = (select perm_id from sys_permission where code = 'menu:admin-portal');
|
||
-- delete from sys_permission where code = 'menu:admin-portal';
|
||
--
|
||
-- 日常运维(不改代码就能调整“谁能看”):
|
||
-- 给某个业务角色开入口:
|
||
-- insert into sys_role_permission (role_id, perm_id, tenant_id, is_deleted, created_at, updated_at)
|
||
-- select r.role_id, p.perm_id, r.tenant_id, 0, now(), now()
|
||
-- from sys_role r, sys_permission p
|
||
-- where r.role_code = 'YOUR_ROLE_CODE' and p.code = 'menu:admin-portal'
|
||
-- and not exists (select 1 from sys_role_permission rp where rp.role_id = r.role_id and rp.perm_id = p.perm_id);
|
||
-- 收回:update sys_role_permission set is_deleted = 1, updated_at = now()
|
||
-- where perm_id = (select perm_id from sys_permission where code = 'menu:admin-portal')
|
||
-- and role_id = (select role_id from sys_role where role_code = 'YOUR_ROLE_CODE');
|
||
|