29 lines
3.8 KiB
JavaScript
29 lines
3.8 KiB
JavaScript
import {companyDeedOperators} from './company-deeds.mjs';
|
|
import {clanDeedOperators} from './clan-deeds.mjs';
|
|
// Native forum projections are derived from current organisation records on
|
|
// every sign-in/identity refresh. Never accept memberships from browser input.
|
|
export function projectSystemBoardAccess({clans=[],companies=[],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});
|
|
}
|
|
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()
|
|
]);
|
|
const projected=projectSystemBoardAccess({clans,companies,userId:user.id,inspectAll,moderateAll}),operators=await clanDeedOperators(db);for(const [id,users]of operators)if(users.includes(user.id)){const existing=projected.organisations.find(o=>o.kind==='clan'&&o.id===id);if(existing)existing.manage=true;else projected.organisations.push({kind:'clan',id,read:true,post:true,manage:true});}for(const org of projected.organisations)if(org.kind==='clan'&&!org.name){const record=await db.prepare('SELECT name FROM site_clans WHERE id=? AND active=1').bind(org.id).first();if(record)org.name=record.name;}const companyOperators=await companyDeedOperators(db);for(const [id,users]of companyOperators)if(users.includes(user.id)){const existing=projected.organisations.find(o=>o.kind==='company'&&o.id===id);if(existing)existing.manage=true;else{const company=await db.prepare("SELECT name FROM corrino_companies WHERE id=? AND status='active'").bind(id).first();if(company)projected.organisations.push({kind:'company',id,name:company.name,read:true,post:true,manage:true});}}return projected;
|
|
}
|

Staff