32 lines
3.3 KiB
JavaScript
32 lines
3.3 KiB
JavaScript
// Native forum projections are derived from current organisation records on
|
|
// every sign-in/identity refresh. Never accept memberships from browser input.
|
|
import {passiveOrderRecords} from './political-passive-orders.mjs';
|
|
export function projectSystemBoardAccess({clans=[],companies=[],orders=[],userId,inspectAll=false,moderateAll=false}={}){
|
|
const grants=new Map();
|
|
if(typeof userId!=='string'||!userId)return {version:1,inspectAll:false,moderateAll:false,organisations:[]};
|
|
for(const clan of clans){
|
|
if(Number(clan.active)!==1||clan.user_id!==userId||!['owner','officer','member'].includes(clan.clan_role))continue;
|
|
grants.set('clan:'+clan.id,{kind:'clan',id:clan.id,...(typeof clan.name==='string'?{name:clan.name}:{}),read:true,post:true,manage:clan.owner_id===userId&&clan.clan_role==='owner'});
|
|
}
|
|
for(const company of companies){
|
|
const deed=typeof company.owner_account_id==='string',owner=deed?company.owner_account_id===userId:company.founded_by===userId,member=deed&&company.member_user_id===userId;
|
|
if(company.status!=='active'||!owner&&!member&&company.ceo_account_id!==userId)continue;
|
|
// Deed owners and current CEOs manage their subtree; registered members
|
|
// can read/post. Legacy registries retain their existing founder projection.
|
|
grants.set('company:'+company.id,{kind:'company',id:company.id,...(typeof company.name==='string'?{name:company.name}:{}),read:true,post:true,manage:company.ceo_account_id===userId||deed&&owner});
|
|
}
|
|
// Existing private board storage is retained; its legacy company ID is only a storage alias.
|
|
for(const order of orders)if(order.capabilities?.readBoards)grants.set('company:'+order.legacyBoardId,{kind:'company',id:order.legacyBoardId,name:order.name,read:true,post:order.capabilities.postBoards===true,manage:order.capabilities.manageBoards===true});
|
|
return {version:1,inspectAll:inspectAll===true,moderateAll:moderateAll===true,organisations:[...grants.values()].sort((a,b)=>(a.kind+':'+a.id).localeCompare(b.kind+':'+b.id))};
|
|
}
|
|
|
|
export async function loadSystemBoardAccess(db,user,{inspectAll=false,moderateAll=false}={}){
|
|
if(!user?.id)return projectSystemBoardAccess();
|
|
const [{results:clans=[]},{results:companies=[]}]=await Promise.all([
|
|
db.prepare('SELECT c.id,c.name,c.active,c.owner_id,m.user_id,m.clan_role FROM site_clans c JOIN site_clan_members m ON m.clan_id=c.id WHERE m.user_id=? AND c.active=1').bind(user.id).all(),
|
|
db.companyDeedsEnabled?db.prepare("SELECT c.id,c.name,c.status,c.founded_by,c.ceo_account_id,d.owner_account_id,m.user_id member_user_id FROM corrino_companies c JOIN site_company_deeds d ON d.company_id=c.id LEFT JOIN site_company_members m ON m.company_id=c.id AND m.user_id=? WHERE c.status='active' AND (d.owner_account_id=? OR c.ceo_account_id=? OR m.user_id=?) AND EXISTS(SELECT 1 FROM site_company_members deed_owner_member WHERE deed_owner_member.company_id=c.id AND deed_owner_member.user_id=d.owner_account_id)").bind(user.id,user.id,user.id,user.id).all():db.prepare('SELECT id,name,status,founded_by,ceo_account_id FROM corrino_companies WHERE founded_by=? OR ceo_account_id=?').bind(user.id,user.id).all()
|
|
]);
|
|
// Parent-deed administration never grants membership in a child's private boards.
|
|
const orders=await passiveOrderRecords(db,{actor:user});
|
|
return projectSystemBoardAccess({clans,companies,orders,userId:user.id,inspectAll,moderateAll});
|
|
}
|

Staff