forked from unimatrix/web
23 lines
3.4 KiB
JavaScript
23 lines
3.4 KiB
JavaScript
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';import {OFFICIAL_ACCOUNT,organisationSubject} from '../node/official-account.mjs';
|
|
import {nativeSyncMutation} from '../node/native-sync-outbox.mjs';
|
|
const SERVICE_ID='f7b88778-f0bc-41cc-9227-d8ef2f52264d';
|
|
export async function bootstrapOfficialAlias(db,{actorId,apply=false}={}){
|
|
if(![OFFICIAL_ACCOUNT.ownerId,SERVICE_ID].includes(actorId))throw Error('Owner or pinned Dev actor required.');
|
|
return db.transaction(async tx=>{
|
|
const actor=await tx.first("SELECT p.principal_kind FROM org_control_principals p JOIN site_accounts a ON a.id=p.user_id WHERE p.user_id=? AND a.password_hash NOT LIKE '!%' FOR SHARE",[actorId]);
|
|
if(actor?.principal_kind!==(actorId===SERVICE_ID?'service':'owner'))throw Error('Current control actor required.');
|
|
const account=await tx.first("SELECT a.id FROM site_accounts a JOIN site_game_links l ON l.user_id=a.id WHERE a.id=? AND a.username=? AND a.role='official_account' AND a.password_hash NOT LIKE '!%' AND l.minecraft_uuid=? FOR SHARE",[OFFICIAL_ACCOUNT.id,OFFICIAL_ACCOUNT.username,OFFICIAL_ACCOUNT.minecraftUuid]);
|
|
const owner=await tx.first("SELECT p.user_id FROM org_control_principals p JOIN site_accounts a ON a.id=p.user_id WHERE p.user_id=? AND p.principal_kind='owner' AND a.username='unimatrix999' AND a.password_hash NOT LIKE '!%' FOR SHARE",[OFFICIAL_ACCOUNT.ownerId]);
|
|
if(!account||!owner)throw Error('Pinned official and owner identities must match.');
|
|
const existing=await tx.first('SELECT principal_kind,created_by FROM org_control_principals WHERE user_id=? FOR UPDATE',[OFFICIAL_ACCOUNT.id]);
|
|
if(existing&&(existing.principal_kind!=='owner_alias'||existing.created_by!==OFFICIAL_ACCOUNT.ownerId))throw Error('Existing control identity differs; refusing overwrite.');
|
|
if(!apply)return {applied:false,aliasUserId:OFFICIAL_ACCOUNT.id,permissionPrincipalId:OFFICIAL_ACCOUNT.ownerId,alreadyLinked:!!existing};
|
|
if(!existing){const now=Date.now();await tx.query('INSERT INTO org_control_principals(user_id,principal_kind,created_by,created_at) VALUES(?,?,?,?)',[OFFICIAL_ACCOUNT.id,'owner_alias',OFFICIAL_ACCOUNT.ownerId,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(),OFFICIAL_ACCOUNT.id,actorId,'owner-alias-baseline','organisation','dunerealms','Owner explicitly authorised dunerealms Bedrock alternate account to follow the same function rights',JSON.stringify({permissionPrincipalId:OFFICIAL_ACCOUNT.ownerId,extraStaffAppointment:false}),now]);
|
|
const sync=nativeSyncMutation(now);await tx.query(sync.sql,sync.values);}
|
|
const subject=await organisationSubject(tx,OFFICIAL_ACCOUNT.id,{lock:true});if(!subject.alias)throw Error('Official identity verification failed.');
|
|
return {applied:true,created:!existing,aliasUserId:OFFICIAL_ACCOUNT.id,permissionPrincipalId:subject.userId};
|
|
});
|
|
}
|
|
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 bootstrapOfficialAlias(db,{actorId:process.argv.find(a=>a.startsWith('--actor='))?.slice(8),apply:process.argv.includes('--apply')})));}finally{await db.close();}}
|

Staff