MC/website/node/organisation-runtime.mjs

40 lines
4.2 KiB
JavaScript

import {effectiveOrganisationAccess} from './organisation-effective.mjs';
import {assignmentLabel,validStaffRole} from './organisation-model.mjs';
import {organisationSubject} from './official-account.mjs';
const SERVICE_ID='f7b88778-f0bc-41cc-9227-d8ef2f52264d';
const legacyStaff=new Set(['owners','owner','corestaff','staff','network_admin','web_net_admin','web_net_owner','web_network','official_account','admin','administrator','moderator','guardian','streamer','twitch','yt']);
const privileged=value=>{
const node=value.startsWith('cp.')?'web.'+value.slice(3):value;
return /^web\.(accounts\.|ranks\.|servers\.|luckperms\.|tickets\.|bank\.staff\.|payroll\.|posts\.(edit|publish)|items\.manage|bans\.|forum\.(moderate|admin)|git\.admin|community\.manage|quests\.manage)/.test(node)
||/^dune\.(admin|staff\.|moderation\.|ban\.|unban\.|items\.give|tickets\.staff)/.test(node)
||node.startsWith('duneprotect.')||/^dunelobby\.(admin|build)$/.test(node)
||/^web\.menu\.(?:administration|staffmanual|permissions|servers|luckperms|bans|forum|items|posts|tickets)$/.test(node)
||/^web\.(?:shop\.support|bank\.projects\.approve|mail\.staff\.)/.test(node);
};
const nativeEditor=node=>['web.luckperms.edit','cp.luckperms.edit','luckperms.editor','luckperms.applyedits','luckperms.trusteditor'].includes(node);
export async function organisationRuntimeAccess(db,user,{registry,basicRank,legacyRanks,clock=Date.now}={}){
const now=clock();
const snapshot=await db.readSnapshot(async reader=>{
const subject=await organisationSubject(reader,user.id);
const principal=(await reader.query('SELECT principal_kind FROM org_control_principals WHERE user_id=?',[subject.userId]))[0];
if(principal?.principal_kind==='service'){
const service=(await reader.query('SELECT service_uuid,game_link_exempt FROM site_account_policies WHERE user_id=?',[user.id]))[0];
if(user.id!==SERVICE_ID||!service?.game_link_exempt||service.service_uuid!=='00000000-0000-4000-8000-00000000de01')throw Error('Invalid service control identity.');
return {service:true};
}
const assignments=await reader.query("SELECT id,scope_type,scope_key,role_key,status,starts_at,ends_at FROM org_assignments WHERE user_id=? AND scope_type IN ('organisation','department','branch') AND status='active' AND starts_at<=? AND (ends_at IS NULL OR ends_at>?)",[subject.userId,now,now]);
const policies=await reader.query('SELECT scope_type,scope_key,role_key,permissions_json,version FROM org_role_policies',[]);
return {subject,assignments,access:effectiveOrganisationAccess({assignments,policies,registry,principalKind:principal?.principal_kind,now})};
});
if(snapshot.service)return null; // Preserve the existing verified Dev service access, including native tools.
const citizenRanks=legacyRanks.filter(r=>!legacyStaff.has(r.key)&&!r.key.startsWith('web_')&&!r.key.startsWith('game_'));
const ranks=[basicRank,...citizenRanks.filter(r=>r.key!==basicRank.key)];
const nodes=new Set(ranks.flatMap(r=>r.nodes||[]).filter(n=>Object.hasOwn(registry,n)&&!privileged(n)&&!nativeEditor(n)&&!n.includes('*')));
for(const n of snapshot.access.nodes)nodes.add(n);
for(const n of [...nodes])if(n.startsWith('cp.')&&Object.hasOwn(registry,'web.'+n.slice(3)))nodes.add('web.'+n.slice(3));
const active=snapshot.assignments.filter(a=>validStaffRole(a.scope_type,a.scope_key,a.role_key)&&a.status==='active'&&Number(a.starts_at)<=now&&(a.ends_at===null||Number(a.ends_at)>now));
const precedence={owner_ceo:100,executive:90,department_head:80,branch_lead:70,officer:50};
active.sort((a,b)=>(precedence[b.role_key]||0)-(precedence[a.role_key]||0)||a.scope_key.localeCompare(b.scope_key));
const primary=active[0],citizen=ranks.reduce((a,b)=>a.level>=b.level?a:b,basicRank);
return {rank:primary?'org_'+primary.role_key:citizen.key,ranks:[...new Set([...ranks.map(r=>r.key),...active.map(a=>'org_'+a.role_key)])],label:primary?assignmentLabel(primary).roleLabel:citizen.label,level:primary?precedence[primary.role_key]||0:citizen.level,nodes:[...nodes].sort(),organisation:{mode:'active',permissionPrincipalId:snapshot.subject.userId,officialOwnerAlias:snapshot.subject.alias,assignments:active.map(assignmentLabel),missingPolicies:snapshot.access.missingPolicies,blocked:snapshot.access.blocked.length}};
}