28 lines
2.9 KiB
JavaScript
28 lines
2.9 KiB
JavaScript
// One-time, explicit Owner-authorised control baseline. Does not activate role grants.
|
|
import {randomUUID} from 'node:crypto';
|
|
import {fileURLToPath} from 'node:url';
|
|
import {resolve} from 'node:path';
|
|
import {openDatabase} from '../node/mysql.mjs';
|
|
import {initializeOrganisation} from '../node/organisation-model.mjs';
|
|
const OWNER_ID='f3ffda37-01e4-41ea-b89d-fbeda82e453e';
|
|
const SERVICE_ID='f7b88778-f0bc-41cc-9227-d8ef2f52264d';
|
|
const desired={unimatrix999:'owner',grmacken:'executive',richwjr:'executive',mr_blueray:'executive',dev:'service'};
|
|
export async function bootstrapControl(db,{apply=false}={}){
|
|
const rows=(await db.prepare(`SELECT id,username,role FROM site_accounts WHERE username IN (${Object.keys(desired).map(()=>'?')}) AND password_hash NOT LIKE '!%'`).bind(...Object.keys(desired)).all()).results;
|
|
if(rows.length!==5||new Set(rows.map(r=>r.username)).size!==5||rows.find(r=>r.username==='unimatrix999')?.id!==OWNER_ID||rows.find(r=>r.username==='dev')?.id!==SERVICE_ID)throw Error('Expected existing control identities do not match.');
|
|
const policy=await db.prepare('SELECT service_uuid,game_link_exempt FROM site_account_policies WHERE user_id=?').bind(SERVICE_ID).first();
|
|
if(!policy?.game_link_exempt||policy.service_uuid!=='00000000-0000-4000-8000-00000000de01')throw Error('Dev service exemption mismatch.');
|
|
const plan=rows.map(r=>({id:r.id,username:r.username,kind:desired[r.username]}));if(!apply)return {applied:false,principals:plan};
|
|
await db.transaction(async tx=>{
|
|
for(const p of plan){
|
|
const existing=await tx.first('SELECT principal_kind FROM org_control_principals WHERE user_id=? FOR UPDATE',[p.id]);
|
|
if(existing){if(existing.principal_kind!==p.kind)throw Error('Existing control assignment differs; never overwrite automatically.');continue;}
|
|
const account=await tx.first("SELECT id,username FROM site_accounts WHERE id=? AND password_hash NOT LIKE '!%' FOR SHARE",[p.id]);if(account?.username!==p.username)throw Error('Identity changed during bootstrap.');
|
|
const now=Date.now();await tx.query('INSERT INTO org_control_principals(user_id,principal_kind,created_by,created_at) VALUES(?,?,?,?)',[p.id,p.kind,OWNER_ID,now]);
|
|
await tx.query('INSERT INTO org_assignment_events(id,user_id,actor_id,action,scope_type,scope_key,reason,after_json,created_at) VALUES(?,?,?,?,?,?,?,?,?)',[randomUUID(),p.id,OWNER_ID,'control-baseline','organisation','dunerealms','Owner-approved existing Big Four and Dev service control baseline',JSON.stringify({principal:p.kind,livePermissionsChanged:false}),now]);
|
|
}
|
|
});return {applied:true,principals:plan,livePermissionsChanged:false};
|
|
}
|
|
if(process.argv[1]&&fileURLToPath(import.meta.url)===resolve(process.argv[1])){
|
|
const db=await openDatabase(process.env);try{await initializeOrganisation(db);console.log(JSON.stringify(await bootstrapControl(db,{apply:process.argv.includes('--apply')})));}finally{await db.close();}
|
|
}
|

Staff