170 lines
5.6 KiB
JavaScript
170 lines
5.6 KiB
JavaScript
(function (root, factory) {
|
|
if (typeof module === 'object' && module.exports) {
|
|
module.exports = factory(root);
|
|
return;
|
|
}
|
|
|
|
root.AdminPermissionPages = factory(root);
|
|
if (root.document) {
|
|
root.document.addEventListener('DOMContentLoaded', function () {
|
|
root.AdminPermissionPages.init();
|
|
});
|
|
}
|
|
})(typeof globalThis !== 'undefined' ? globalThis : window, function (root) {
|
|
'use strict';
|
|
|
|
var documentRef = root.document;
|
|
var roleLabels = {
|
|
owner: '谱主',
|
|
admin: '管理员',
|
|
editor: '编辑',
|
|
member: '成员',
|
|
visitor: '访客'
|
|
};
|
|
|
|
function getApi() {
|
|
return root.GenealogyApi && root.GenealogyApi.defaultClient;
|
|
}
|
|
|
|
function query(selector) {
|
|
return documentRef ? documentRef.querySelector(selector) : null;
|
|
}
|
|
|
|
function normalizeId(value) {
|
|
var text;
|
|
|
|
if (typeof value === 'number' && !Number.isSafeInteger(value)) return '';
|
|
if (value === undefined || value === null) return '';
|
|
text = String(value).trim();
|
|
return /^[1-9][0-9]*$/.test(text) ? text : '';
|
|
}
|
|
|
|
function getCurrentGenealogyId(search) {
|
|
var source = search === undefined && root.location ? root.location.search : search;
|
|
var params;
|
|
var contextId;
|
|
|
|
if (search === undefined && root.ProfileUI && root.ProfileUI.getGenealogyId) {
|
|
contextId = normalizeId(root.ProfileUI.getGenealogyId());
|
|
if (contextId) return contextId;
|
|
}
|
|
params = new URLSearchParams(String(source || '').replace(/^\?/, ''));
|
|
return normalizeId(params.get('genealogyId'));
|
|
}
|
|
|
|
function normalizePermissionOverview(data, expectedGenealogyId) {
|
|
var source = data || {};
|
|
var genealogyId = normalizeId(source.genealogyId);
|
|
var roleType = String(source.roleType === undefined || source.roleType === null ? '' : source.roleType).trim();
|
|
var expectedId = normalizeId(expectedGenealogyId);
|
|
|
|
if (!genealogyId || !expectedId || genealogyId !== expectedId || !Object.prototype.hasOwnProperty.call(roleLabels, roleType)) {
|
|
return null;
|
|
}
|
|
return {
|
|
genealogyId: genealogyId,
|
|
roleType: roleType,
|
|
canManage: source.canManage === true,
|
|
canEditContent: source.canEditContent === true
|
|
};
|
|
}
|
|
|
|
function canMaintainContent(access) {
|
|
return Boolean(access && (access.canEditContent === true || access.canManage === true));
|
|
}
|
|
|
|
function escapeHtml(value) {
|
|
return String(value === undefined || value === null ? '' : value)
|
|
.replace(/&/g, '&')
|
|
.replace(/</g, '<')
|
|
.replace(/>/g, '>')
|
|
.replace(/"/g, '"')
|
|
.replace(/'/g, ''');
|
|
}
|
|
|
|
function renderCurrentPermissions(access) {
|
|
if (!access) return '';
|
|
return '<div class="form-like admin-permission-current">' +
|
|
'<p><span>当前角色</span><b>' + escapeHtml(roleLabels[access.roleType]) + '</b></p>' +
|
|
'<p><span>成员角色维护</span><b>' + (access.canManage ? '可管理' : '不可管理') + '</b></p>' +
|
|
'<p><span>入谱审核</span><b>' + (access.canManage ? '可审核' : '不可审核') + '</b></p>' +
|
|
'<p><span>内容维护</span><b>' + (canMaintainContent(access) ? '可维护' : '不可维护') + '</b></p>' +
|
|
'</div><p class="module-meta">实际写入操作仍由服务端按当前角色与家谱权限校验。</p>';
|
|
}
|
|
|
|
function setState(type, message) {
|
|
var container = query('[data-admin-permission-current]');
|
|
|
|
if (!container) return;
|
|
if (root.ProfileUI && root.ProfileUI.setApiState) {
|
|
root.ProfileUI.setApiState(container, type, message);
|
|
return;
|
|
}
|
|
container.textContent = message;
|
|
}
|
|
|
|
function shouldRedirectToLogin(api, error) {
|
|
var status = error && (error.status || error.code);
|
|
|
|
return !api || !api.getToken || !api.getToken() || Number(status) === 401;
|
|
}
|
|
|
|
function redirectToLogin(api) {
|
|
if (api && api.clearToken) api.clearToken();
|
|
root.NavigationUtil.open('login.html');
|
|
}
|
|
|
|
function isForbidden(error) {
|
|
return Number(error && (error.status || error.code)) === 403;
|
|
}
|
|
|
|
async function loadPage() {
|
|
var api = getApi();
|
|
var genealogyId;
|
|
var overview;
|
|
var container;
|
|
|
|
if (shouldRedirectToLogin(api)) {
|
|
redirectToLogin(api);
|
|
return;
|
|
}
|
|
genealogyId = getCurrentGenealogyId();
|
|
if (!genealogyId) {
|
|
setState('empty', '请先选择家谱,再查看当前角色权限。');
|
|
return;
|
|
}
|
|
if (root.ProfileUI && root.ProfileUI.syncGenealogyContextLinks) root.ProfileUI.syncGenealogyContextLinks();
|
|
setState('loading', '正在核对当前家谱角色与权限…');
|
|
try {
|
|
overview = normalizePermissionOverview(await api.genealogyDetail(genealogyId), genealogyId);
|
|
if (!overview) throw new Error('家谱权限响应缺少稳定角色或能力字段');
|
|
container = query('[data-admin-permission-current]');
|
|
if (container) container.innerHTML = renderCurrentPermissions(overview);
|
|
} catch (error) {
|
|
if (shouldRedirectToLogin(api, error)) {
|
|
redirectToLogin(api);
|
|
return;
|
|
}
|
|
setState(
|
|
isForbidden(error) ? 'forbidden' : 'error',
|
|
isForbidden(error) ? '当前账号无权查看该家谱权限。' : '当前角色权限读取失败,请稍后重试。'
|
|
);
|
|
}
|
|
}
|
|
|
|
function init() {
|
|
if (!documentRef || !query('[data-admin-permission-page]')) return;
|
|
loadPage();
|
|
}
|
|
|
|
return {
|
|
getCurrentGenealogyId: getCurrentGenealogyId,
|
|
normalizePermissionOverview: normalizePermissionOverview,
|
|
canMaintainContent: canMaintainContent,
|
|
renderCurrentPermissions: renderCurrentPermissions,
|
|
shouldRedirectToLogin: shouldRedirectToLogin,
|
|
isForbidden: isForbidden,
|
|
init: init
|
|
};
|
|
});
|