Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
This commit is contained in:
+8
-10
@@ -5,8 +5,7 @@
|
||||
// HR 拿掉主管身分,這些權限就自動消失。
|
||||
// - 權限頁手動設定(COST_MODEL_DATA_DIR/permissions.json,新檔案):要手動拿掉。
|
||||
// 最高管理者(20150906)看得到全部,也是唯一能改核心參數、按定錨、刪資料、管理權限的人。
|
||||
import { mkdir, readFile, rename, writeFile } from 'node:fs/promises';
|
||||
import { dirname } from 'node:path';
|
||||
import { bindFile } from './storage.js';
|
||||
import { splitOrganization } from './hr-org.js';
|
||||
|
||||
export const TOP_ADMIN = '20150906';
|
||||
@@ -77,14 +76,16 @@ export function hasAnyScope(permission) {
|
||||
return Boolean(permission && !permission.denied && (permission.admin || permission.company || permission.depts.length || permission.groups.length));
|
||||
}
|
||||
|
||||
export function createPermissionStore(path) {
|
||||
export function createPermissionStore(source) {
|
||||
const file = bindFile(source);
|
||||
let state = null;
|
||||
let queue = Promise.resolve();
|
||||
async function load() {
|
||||
if (state) return state;
|
||||
let saved = null;
|
||||
if (path) {
|
||||
try { saved = JSON.parse(await readFile(path, 'utf8')); } catch (error) { if (error.code !== 'ENOENT') throw error; }
|
||||
if (file) {
|
||||
const text = file.read();
|
||||
if (text !== null) saved = JSON.parse(text);
|
||||
}
|
||||
state = saved && typeof saved === 'object'
|
||||
? { grants: saved.grants || {}, denied: Array.isArray(saved.denied) ? saved.denied : [], log: saved.log || [] }
|
||||
@@ -92,11 +93,8 @@ export function createPermissionStore(path) {
|
||||
return state;
|
||||
}
|
||||
async function save() {
|
||||
if (!path) return;
|
||||
await mkdir(dirname(path), { recursive: true });
|
||||
const temporary = `${path}.${process.pid}.tmp`;
|
||||
await writeFile(temporary, `${JSON.stringify(state)}\n`, { mode: 0o600 });
|
||||
await rename(temporary, path);
|
||||
if (!file) return;
|
||||
file.write(`${JSON.stringify(state)}\n`);
|
||||
}
|
||||
function serial(task) {
|
||||
const run = queue.then(task, task);
|
||||
|
||||
Reference in New Issue
Block a user