forked from unimatrix/web
Separate personal bank eligibility from administrator inspection
This commit is contained in:
parent
716e14414b
commit
c58f3f2ffe
2 changed files with 18 additions and 4 deletions
|
|
@ -6,12 +6,13 @@ export function entityId(kind,key){const h=createHash('sha256').update('postbank
|
|||
const can= (rights,key)=>rights.nodes.includes(key);
|
||||
// Central identity/organisation records are read-only sources. No gameplay bank,
|
||||
// bearer gateway, old balance or historical bank transaction is read here.
|
||||
export function createPostbankAuthority(central,{clock=Date.now}={}){
|
||||
export function createPostbankAuthority(central,{clock=Date.now,rightsFor=accessFor}={}){
|
||||
const rows=async(sql,args=[])=>(await central.prepare(sql).bind(...args).all()).results||[];
|
||||
return async user=>{
|
||||
const rights=await accessFor(central,user),readAdmin=can(rights,'web.bank.staff.view'),manageAdmin=can(rights,'web.bank.staff.manage'),now=clock();
|
||||
const rights=await rightsFor(central,user),readAdmin=can(rights,'web.bank.staff.view'),manageAdmin=can(rights,'web.bank.staff.manage'),now=clock();
|
||||
const sources=[],operatorRoles=new Set(['owner','officer','admin','leader']);
|
||||
const add=(kind,key,name,operators=[],viewers=[],expiresAt=null)=>sources.push({id:entityId(kind,key),kind,key,name,canView:readAdmin||operators.includes(user.id)||viewers.includes(user.id),canActivate:manageAdmin||operators.includes(user.id),canPay:operators.includes(user.id),expiresAt});
|
||||
// Administrative inspection belongs to separate admin routes, never personal activation cards.
|
||||
const add=(kind,key,name,operators=[],viewers=[],expiresAt=null)=>sources.push({id:entityId(kind,key),kind,key,name,canView:operators.includes(user.id)||viewers.includes(user.id),canActivate:operators.includes(user.id),canPay:operators.includes(user.id),expiresAt});
|
||||
const policy=await central.prepare('SELECT game_link_exempt FROM site_account_policies WHERE user_id=?').bind(user.id).first();
|
||||
if(!policy?.game_link_exempt&&!['memorial','official_account'].includes(user.role))add('person',user.id,user.username,[user.id]);
|
||||
const clans=await rows('SELECT id,name,owner_id FROM site_clans WHERE active=1');
|
||||
|
|
@ -38,7 +39,7 @@ export function createPostbankAuthority(central,{clock=Date.now}={}){
|
|||
// Staff eligibility follows current central access roles, not cosmetic honours.
|
||||
const staff=await rows("SELECT a.id,a.username,a.role FROM site_accounts a LEFT JOIN site_account_policies p ON p.user_id=a.id WHERE a.password_hash NOT LIKE '!%' AND COALESCE(p.game_link_exempt,0)=0");
|
||||
const eligibleStaff=staff.filter(a=>STAFF_ROLES.has(a.role));
|
||||
for(const actor of eligibleStaff)add('staff',actor.id,actor.username+' · Staff',[actor.id]);
|
||||
for(const actor of eligibleStaff)add('staff',actor.id,actor.username+' · Staff',[actor.id]);
|
||||
return {entities:sources,readAdmin,manageAdmin,eligibleStaff:eligibleStaff.map(a=>({id:a.id,username:a.username})),server:{state:'not_linked',name:'Game server account'}};
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,3 +18,16 @@ test('account insertion failure and revoked authority roll back all activation r
|
|||
test('corporate entities remain pending; forbidden package or foreign entity cannot be accepted',async()=>{const db=new BankFixture(),clan={...entity,kind:'clan',name:'Existing clan'};try{const service=new PostbankService(db,async()=>({...await authority(),entities:[clan]}));const snap=await service.overview(user);assert.equal(snap.entities[0].clientNumber,null);assert.equal((await db.first('SELECT COUNT(*) n FROM pb_entities')).n,1);await assert.rejects(service.activate(user,input('pb_flow')));const done=await service.activate(user,input('pb_connect'));assert.match(done.clientNumber,/^CL\d{10}$/);const a=await db.first('SELECT * FROM pb_accounts');assert.equal(a.type_code,'105');const denied=new PostbankService(db,async()=>({...await authority(),entities:[]}));await assert.rejects(denied.activate(user,input()),e=>e.status===403);}finally{db.close();}});
|
||||
test('central bank API rejects unauthenticated, cross-origin and account-override requests',async()=>{const db=new BankFixture();try{const api=createPostbankApi(null,db,{}, {authenticate:async()=>user,authority});const url='https://cp.dunerealms.com/api/bank/activate';const r=await api(new Request(url,{method:'POST',headers:{Origin:'https://other.invalid','Content-Type':'application/json'},body:JSON.stringify(input())}));assert.equal(r.status,403);assert.equal((await api(new Request('https://cp.dunerealms.com/api/bank/overview?client=other'))).status,400);const noAuth=createPostbankApi(null,db,{}, {authenticate:async()=>null,authority});assert.equal((await noAuth(new Request(url))).status,401);assert.equal((await db.first('SELECT COUNT(*) n FROM pb_accounts')).n,0);}finally{db.close();}});
|
||||
test('all agreed product codes have valid account check digits and professional identifiers are distinct',()=>{for(const code of ['102','103','104','105','106','107','108','979'])assert.equal(validBankNumber(bankNumber(code,1)),true);for(const [kind,prefix]of [['clan','CL'],['company','CO'],['house','HO'],['office','PO'],['staff','ST']])assert.match(clientCandidate(kind,'stable-source'),new RegExp('^'+prefix+'\\d{10}$'));assert.equal(PACKAGES.find(p=>p.key==='pb_start').instant,0);});
|
||||
|
||||
test('bank admin status never exposes other staff or unrelated entities in personal banking',async()=>{
|
||||
const {createPostbankAuthority}=await import('../node/postbank-authority.mjs');
|
||||
const other={id:'42345678-1234-4234-8234-123456789abc',username:'other_staff',role:'network_admin'};
|
||||
const own={...user,role:'owners'},owned={id:'clan-owned',name:'Owned clan',owner_id:user.id},unrelated={id:'clan-other',name:'Other clan',owner_id:other.id};
|
||||
const central={prepare(sql){return {bind(){return this;},async first(){return null;},async all(){return {results:sql.includes('FROM site_clans')?[owned,unrelated]:sql.includes('FROM site_clan_members')?[]:sql.includes('FROM site_accounts a')?[own,other]:[]};}};}};
|
||||
const authority=createPostbankAuthority(central,{rightsFor:async()=>({nodes:['web.bank.staff.view','web.bank.staff.manage']})});
|
||||
const context=await authority(own);assert.equal(context.readAdmin,true);assert.equal(context.manageAdmin,true);
|
||||
const mine=context.entities.filter(e=>e.canView);assert.deepEqual(mine.map(e=>e.kind).sort(),['clan','person','staff']);assert.ok(mine.every(e=>e.canActivate&&e.canPay));
|
||||
const otherStaff=context.entities.find(e=>e.kind==='staff'&&e.key===other.id);assert.equal(otherStaff.canView,false);assert.equal(otherStaff.canActivate,false);assert.equal(otherStaff.canPay,false);
|
||||
assert.equal(context.entities.find(e=>e.key===unrelated.id).canView,false);assert.ok(context.entities.filter(e=>['house','office'].includes(e.kind)).every(e=>!e.canView&&!e.canActivate));
|
||||
const db=new BankFixture();try{const service=new PostbankService(db,authority),snapshot=await service.overview(own);assert.deepEqual(snapshot.entities.map(e=>e.name).sort(),mine.map(e=>e.name).sort());await assert.rejects(service.activate(own,{...input('pb_public'),entityId:otherStaff.id}),e=>e.status===403);assert.equal((await db.first('SELECT COUNT(*) n FROM pb_clients')).n,0);}finally{db.close();}
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue

Staff