MC/website/ops/grant-user-voice.mjs

32 lines
3.1 KiB
JavaScript

import {readFile} from 'node:fs/promises';
import {fileURLToPath} from 'node:url';
import {realpath} from 'node:fs/promises';
import mysql from 'mysql2/promise';
import {randomUUID} from 'node:crypto';
import {OFFICIAL_ACCOUNT} from '../node/official-account.mjs';
import {accessFor} from '../node/permissions.mjs';
import {syncGamePermissions} from '../node/game-permission-sync.mjs';
import {voicePermissions} from '../node/user-voice-permissions.mjs';
async function main(){
const [source,apply]=process.argv.slice(2);
if(!/^[a-f0-9]{40}$/.test(source||'')||apply&&apply!=='--apply')throw Error('Exact reviewed release required');
if(!(await realpath(fileURLToPath(new URL('..',import.meta.url)))).endsWith('/releases/ci-'+source))throw Error('Run the immutable built release');
process.loadEnvFile('/home/dunerealms/.config/dunerealms.env');const e=process.env;
const c=await mysql.createConnection({host:e.DB_HOST||'127.0.0.1',port:Number(e.DB_PORT||3306),user:e.DB_USER,password:e.DB_PASSWORD,database:e.DB_NAME,multipleStatements:false});
const db={query:async(sql,v=[])=> (await c.execute(sql,v))[0],organisationAccessEnabled:e.ORGANISATION_ACCESS_ENABLED==='true',prepare(sql){let v=[];return {bind(...values){v=values;return this;},async first(){return (await c.execute(sql,v))[0][0]||null;},async all(){return {results:(await c.execute(sql,v))[0]};}};},async readSnapshot(fn){await c.query('START TRANSACTION READ ONLY');try{const r=await fn(db);await c.commit();return r;}catch(err){await c.rollback();throw err;}}};
try{
const actor=(await c.execute("SELECT id,username,role FROM site_accounts WHERE id=? AND username='unimatrix999' AND password_hash NOT LIKE '!%'",[OFFICIAL_ACCOUNT.ownerId]))[0][0];
if(!actor||!(await accessFor(db,actor)).nodes.includes('web.ranks.manage'))throw Error('Existing Owner rank authority required');
await c.beginTransaction();
try{
const row=(await c.execute("SELECT permissions_json,version FROM cp_rank_game_permissions WHERE rank_key='user' FOR UPDATE"))[0][0];if(!row)throw Error('Existing User rank required');
const before=JSON.parse(row.permissions_json),after=voicePermissions(before),changed=JSON.stringify(before)!==JSON.stringify(after);
if(!apply){await c.rollback();console.log(JSON.stringify({applied:false,rank:'user',added:after.filter(n=>!before.includes(n)),source}));return;}
if(changed){await c.execute("UPDATE cp_rank_game_permissions SET permissions_json=?,version=version+1 WHERE rank_key='user' AND version=?",[JSON.stringify(after),row.version]);await c.execute('INSERT INTO account_audit(id,actor_id,action,details,created_at) VALUES(?,?,?,?,?)',[randomUUID(),actor.id,'game-rank-permissions',JSON.stringify({rank:'user',version:row.version,added:after.filter(n=>!before.includes(n)),source}),Date.now()]);}
await c.commit();console.log(JSON.stringify({applied:true,changed,rank:'user',source}));
}catch(err){await c.rollback();throw err;}
await syncGamePermissions(db);console.log(JSON.stringify({nativeSyncConfirmed:true,source}));
}finally{await c.end();}
}
main().catch(()=>{console.error('User voice permission update or synchronization could not be confirmed.');process.exitCode=1;});