fix(realm): keep organisation registers signed-in and individual profiles public

This commit is contained in:
unimatrix 2026-11-17 20:11:58 +00:00
commit a01fd008e6
2 changed files with 19 additions and 5 deletions

View file

@ -1,3 +1,4 @@
import {identity} from './auth.mjs';
import {readGovernmentOffices} from './public-government.mjs';
const assets='https://www.dunerealms.com/assets/';
// Public projections contain no account identifiers, private keys or financial records.
@ -7,11 +8,13 @@ export async function readPublicOrganisations(db,kind,now=Date.now()){
const {results=[]}=await db.prepare("SELECT c.slug,c.name,c.description,c.created_at,a.username AS owner_username,a.display_name AS owner_name FROM corrino_companies c LEFT JOIN site_accounts a ON a.id=c.ceo_account_id AND a.password_hash NOT LIKE '!%' WHERE c.status='active' ORDER BY c.name").all();
return results.map(c=>({...c,id:c.slug,tag:c.slug,emblem:assets+'guild.webp',standing:'Registered company',realm:'Spacing Guild · Commerce',member_count:null,members:c.owner_username?[{username:c.owner_username,title:'Chief Executive'}]:[]}));
}
export function createPublicOrganisations(db,{clock=Date.now}={}){return async request=>{
export function createPublicOrganisations(db,{clock=Date.now,authenticate=r=>identity(r,db)}={}){return async request=>{
const url=new URL(request.url),match=/^\/api\/public\/(companies|political-houses)(?:\/([a-z0-9_-]{1,64}))?\/?$/i.exec(url.pathname);
const reply=(data,status=200)=>Response.json(data,{status,headers:{'Cache-Control':'no-store'}});
if(!['GET','HEAD'].includes(request.method))return reply({message:'Read-only organisation register.'},405);
if(!match||url.search)return reply({message:'Organisation not found.'},404);
// Registers are community-visible to signed-in users; individual profiles stay public.
if(!match[2]&&!await authenticate(request))return reply({message:'Sign in to browse the organisation register.'},401);
try{const kind=match[1]==='companies'?'company':'politics',rows=await readPublicOrganisations(db,kind,clock()),record=match[2]?rows.find(r=>r.tag.toLowerCase()===match[2].toLowerCase()):null;
if(match[2]&&!record)return reply({message:'Organisation not found.'},404);
return request.method==='HEAD'?new Response(null,{headers:{'Cache-Control':'no-store'}}):reply(record?{kind,organisation:record}:{kind,organisations:rows});

View file

@ -6,7 +6,7 @@ const [route,id]=location.pathname.split('/').filter(Boolean),company=route==='c
document.body.classList.add(company?'public-company':'public-politics');
const back=document.querySelector('.public-clan>nav a');back.href='/'+route;back.textContent='← '+noun+' register';
try{
const response=await fetch('/api/public/'+route+(id?'/'+encodeURIComponent(id):''),{cache:'no-store',credentials:'omit'}),data=await response.json();if(!response.ok)throw Error(data.message||'Register unavailable.');
const response=await fetch('/api/public/'+route+(id?'/'+encodeURIComponent(id):''),{cache:'no-store',credentials:id?'omit':'same-origin'}),data=await response.json();if(!response.ok)throw Object.assign(Error(data.message||'Register unavailable.'),{status:response.status});
if(!id){document.title=noun+' Registry | DuneRealms';const search=el('input'),results=el('div');search.type='search';search.placeholder='Search the register…';search.setAttribute('aria-label','Search organisations');host.append(el('h1',noun+' Registry'),search,results);const render=()=>{const q=search.value.toLocaleLowerCase();renderClanRegister(results,data.organisations.filter(r=>[r.name,r.tag,r.owner_name,r.owner_username,r.realm].some(v=>String(v||'').toLocaleLowerCase().includes(q))),{kind});};search.oninput=render;render();}
else{
const r=data.organisation;document.title=r.name+' · '+noun+' Register | DuneRealms';
@ -17,4 +17,4 @@ try{
const deed=el('section',undefined,'clan-deed-card'),art=el('div',undefined,'clan-deed-picture'),copy=el('div');art.setAttribute('role','img');art.setAttribute('aria-label',company?'Company register seal':'Protected office seal');art.style.backgroundImage='url(https://cp.dunerealms.com/assets/'+(company?'organisation-deeds-company-tiers-v1.png':'organisation-deeds-protected-v1.png')+')';art.style.backgroundRepeat='no-repeat';art.style.backgroundSize=company?'300% 100%':'200% 100%';art.style.backgroundPosition=!company&&r.tag==='spacing_guild'?'right center':'left center';
copy.append(el('small',company?'COMMERCIAL REGISTER':'IMPERIAL REGISTER · OFFICE DEED'),el('h2',r.name),el('p',r.standing));if(r.created_at)copy.append(el('p','Est. '+formatDuneDate(Number(r.created_at))));if(r.term)copy.append(el('p','Term: '+formatDuneDate(r.term.startsAt)+' – '+formatDuneDate(r.term.expiresAt)));deed.append(art,copy);host.append(hero,about,deed);
}status.hidden=true;
}catch(error){status.textContent=error.message;host.replaceChildren();}
}catch(error){status.textContent=error.message;host.replaceChildren();if(error.status===401){const login=el('a','Sign in →');login.href='/login.html?return_to='+encodeURIComponent(location.pathname);host.append(login);}}