feat(auth): 前端 ua.auth 全局权限判定并保留 data-auth 回退

This commit is contained in:
augushong
2026-10-04 00:43:04 +00:00
parent ba22150bde
commit 1a27454c38

View File

@@ -112,6 +112,31 @@
return { 'X-CSRF-TOKEN': window.CONFIG.CSRF_TOKEN };
},
// 全局权限判定:读服务端注入的权限集合 window.UA_AUTH({all, controller, nodes})。
// 无有效载荷(undefined/false,旧服务端渲染或异常)返回 undefined,由 checkAuth 落回 data-auth 属性判定;
// 前端不做 snake/驼峰规范化转换,全节点字符串必须与注册节点逐字一致
auth: function (node) {
var payload = window.UA_AUTH;
if (typeof payload !== 'object' || payload === null) {
return undefined;
}
if (payload.all) {
return true;
}
var controller, action;
if (node.indexOf('/') === -1) {
controller = payload.controller;
action = node;
} else {
// 按第一个 '/' 拆解;禁止 split('/', 2):JS 的 limit 是丢弃余段语义,多段节点会静默截断
var i = node.indexOf('/');
controller = node.slice(0, i);
action = node.slice(i + 1);
}
var actions = payload.nodes ? payload.nodes[controller] : undefined;
return actions ? actions.indexOf(action) !== -1 : false;
},
checkAuth: function (node, elem) {
if (node === '1') {
return true;
@@ -119,11 +144,10 @@
if (CONFIG.IS_SUPER_ADMIN) {
return true;
}
if ($(elem).attr('data-auth-' + node) === '1') {
if (ua.auth(node) === true) {
return true;
} else {
return false;
}
return !!(elem && $(elem).attr('data-auth-' + node) === '1');
},
parame: function (param, defaultParam) {
return param !== undefined ? param : defaultParam;