48 lines
3.6 KiB
JavaScript
48 lines
3.6 KiB
JavaScript
import {realpath} from 'node:fs/promises';
|
|
import {fileURLToPath,pathToFileURL} from 'node:url';
|
|
import {resolve} from 'node:path';
|
|
import {createOrganisationManagement} from '../node/organisation-management.mjs';
|
|
import {validStaffRole} from '../node/organisation-model.mjs';
|
|
import {OFFICIAL_ACCOUNT} from '../node/official-account.mjs';
|
|
import {openDatabase} from '../node/mysql.mjs';
|
|
|
|
export const STAFF_TOOL_NODES=Object.freeze(['dunestaff.fly','dunestaff.vanish','dunestaff.vanish.see']);
|
|
const same=(a,b)=>JSON.stringify([...a].sort())===JSON.stringify([...b].sort());
|
|
export function staffToolPolicyPlan(policies){
|
|
return policies.filter(p=>validStaffRole(p.scope_type,p.scope_key,p.role_key)).flatMap(p=>{
|
|
if(!Array.isArray(p.nodes)||!Number.isSafeInteger(p.version)||p.version<1)throw Error('Invalid current staff policy');
|
|
const nodes=[...new Set([...p.nodes,...STAFF_TOOL_NODES])].sort();
|
|
return same(nodes,p.nodes)?[]:[{scopeType:p.scope_type,scopeKey:p.scope_key,role:p.role_key,version:p.version,before:[...p.nodes],nodes}];
|
|
});
|
|
}
|
|
export async function enableStaffTools(db,actor,{apply=false}={}){
|
|
if(actor?.id!==OFFICIAL_ACCOUNT.ownerId||actor?.username!=='unimatrix999')throw Error('Existing verified Owner required');
|
|
const origin='https://cp.dunerealms.com',url=origin+'/api/admin/organisation/policies';
|
|
const route=createOrganisationManagement(db,{origin,authenticate:async()=>actor});
|
|
const read=async()=>{const r=await route(new Request(url));if(r.status!==200)throw Error('Staff policy authority unavailable');const j=await r.json();if(j.canEdit!==true||j.enforcement!=='active')throw Error('Active central policy authority required');return j;};
|
|
const plan=staffToolPolicyPlan((await read()).policies);
|
|
if(!apply)return {applied:0,planned:plan.length,nodes:STAFF_TOOL_NODES};
|
|
let applied=0;
|
|
for(const p of plan){
|
|
const current=await read(),row=current.policies.find(r=>r.scope_type===p.scopeType&&r.scope_key===p.scopeKey&&r.role_key===p.role);
|
|
if(!row||row.version!==p.version||!same(row.nodes,p.before))throw Error('Staff policy changed; review a fresh plan');
|
|
const scope=current.versions.find(r=>r.scope_type===p.scopeType&&r.scope_key===p.scopeKey);
|
|
const r=await route(new Request(url,{method:'POST',headers:{origin,'Content-Type':'application/json'},body:JSON.stringify({scopeType:p.scopeType,scopeKey:p.scopeKey,role:p.role,nodes:p.nodes,scopeVersion:Number(scope?.version||1),reason:'Owner requested staff /fly and /vanish for The Wild Update'})}));
|
|
if(r.status!==200)throw Error('Original staff policy publisher rejected update: '+r.status);
|
|
applied++;
|
|
}
|
|
return {applied,planned:plan.length,nodes:STAFF_TOOL_NODES};
|
|
}
|
|
async function main(){
|
|
const args=process.argv.slice(2),source=args.find(a=>a.startsWith('--source='))?.slice(9);
|
|
if(!/^[a-f0-9]{40}$/.test(source||'')||args.some(a=>a!=='--apply'&&!a.startsWith('--source=')))throw Error('Exact reviewed --source required');
|
|
const release=await realpath(fileURLToPath(new URL('..',import.meta.url)));
|
|
if(!release.endsWith('/releases/ci-'+source))throw Error('Run only the exact immutable reviewed release');
|
|
process.loadEnvFile('/home/dunerealms/.config/dunerealms.env');
|
|
const db=await openDatabase(process.env);
|
|
try{
|
|
const actor=await db.prepare("SELECT id,username FROM site_accounts WHERE id=? AND username='unimatrix999' AND password_hash NOT LIKE '!%'").bind(OFFICIAL_ACCOUNT.ownerId).first();
|
|
console.log(JSON.stringify({source,...await enableStaffTools(db,actor,{apply:args.includes('--apply')})}));
|
|
}finally{await db.close();}
|
|
}
|
|
if(process.argv[1]&&import.meta.url===pathToFileURL(await realpath(resolve(process.argv[1]))).href)await main();
|

Staff