Enforce customer boundaries on token administration and finalize fleet navigation

This commit is contained in:
2026-11-17 22:05:44 +01:00
parent 0afaca1d64
commit 312da6f39e
25 changed files with 1207 additions and 208 deletions
+305 -73
View File
@@ -258,87 +258,319 @@ function UnknownClosure({ tenantId, id, job, auth, done }: { tenantId: string; i
await request(tenantPath(tenantId, `/jobs/${encodeURIComponent(id)}/unknown-closure`), { csrf: auth.csrf_token, body: { attempt_id: stringField(job, 'attempt_id'), revision: job.revision, reason: reason.trim() } }); setConfirmed(false); done();
} catch (e) { setError(errorText(e)); } finally { setBusy(false); } }}>{auth.principal.simulated ? 'SIMULIERT: Unknown-Job ausdrücklich abschließen' : 'Unknown-Job ausdrücklich abschließen'}</button></section>;
}
const pages: Record<NavKey,string> = { geraete: 'Geräte', auftraege: 'Aufträge', skripte: 'Skripte', kunden: 'Kunden', verwaltung: 'Verwaltung' };
const pages: Record<NavKey, string> = {
geraete: 'Geräte',
auftraege: 'Aufträge',
skripte: 'Skripte',
kunden: 'Kunden',
verwaltung: 'Verwaltung'
};
function currentRoute() {
const [key = 'geraete',tab] = window.location.hash.slice(1).split('/');
return { page: (Object.hasOwn(pages,key) ? key : 'geraete') as NavKey, tab: tab ?? '' };
const [key = 'geraete', ...parts] = window.location.hash.slice(1).split('/');
return { page: (Object.hasOwn(pages, key) ? key : 'geraete') as NavKey, tab: parts.join('/') };
}
function go(page: NavKey, tab = '') {
window.location.hash = page + (tab ? '/' + tab : '');
}
function go(page: NavKey, tab = '') { window.location.hash = page + (tab ? '/' + tab : ''); }
function Workspace({ auth, logout }: { auth: Session; logout: () => void }) {
const [route,setRoute] = useState(currentRoute);
useEffect(()=>{ const change=()=>{setRoute(currentRoute());window.scrollTo(0,0);}; window.addEventListener('hashchange',change);return ()=>window.removeEventListener('hashchange',change); },[]);
useEffect(()=>{document.title=pages[route.page]+' · Rust Discovery';},[route.page]);
const tenants=useResource(async signal=>{
const data=record(await request('/api/v1/tenants',{signal}));
return {items:page(data).items.map(tenant),canManage:data.can_manage_customers===true};
},[auth.principal.id]);
const customers=tenants.data?.items ?? auth.tenants;
const [customer,setCustomer]=useState(auth.tenants[0]?.tenant_id ?? '');
const [fleetFilter,setFleetFilter]=useState('');
const [selection,setSelection]=useState<{customer:string;ids:string[]}>({customer:'',ids:[]});
const [error,setError]=useState<string|null>(null);
const [planId,setPlan]=useState<string|null>(null), [operationId,setOperation]=useState<string|null>(null);
function switchCustomer(id:string) {
if(id===customer)return;
setSelection({customer:'',ids:[]});setPlan(null);setOperation(null);setCustomer(id);
const [route, setRoute] = useState(currentRoute);
useEffect(() => {
const change = () => {
setRoute(currentRoute());
window.scrollTo(0, 0);
};
window.addEventListener('hashchange', change);
return () => window.removeEventListener('hashchange', change);
}, []);
useEffect(() => {
document.title = pages[route.page] + ' · Rust Discovery';
}, [route.page]);
const tenants = useResource(
async (signal) => {
const data = record(await request('/api/v1/tenants', { signal }));
return { items: page(data).items.map(tenant), canManage: data.can_manage_customers === true };
},
[auth.principal.id]
);
const customers = tenants.data?.items ?? auth.tenants;
const [customer, setCustomer] = useState(auth.tenants[0]?.tenant_id ?? '');
const [fleetFilter, setFleetFilter] = useState('');
const [selection, setSelection] = useState<{ customer: string; ids: string[] }>({
customer: '',
ids: []
});
const [error, setError] = useState<string | null>(null);
const [planId, setPlan] = useState<string | null>(null),
[operationId, setOperation] = useState<string | null>(null);
function switchCustomer(id: string) {
if (id === customer) return;
setSelection({ customer: '', ids: [] });
setPlan(null);
setOperation(null);
setCustomer(id);
}
function selectFleetCustomer(id:string) {
setFleetFilter(id);setSelection({customer:'',ids:[]});
if(id) switchCustomer(id);
function selectFleetCustomer(id: string) {
setFleetFilter(id);
setSelection({ customer: '', ids: [] });
if (id) switchCustomer(id);
}
const primaryTenant=auth.tenants[0]?.tenant_id;
const current = useResource(async signal=>{
if(!customer || customer===primaryTenant)return {id:customer,auth};
const scoped=session(await request('/auth/session',{signal,tenantId:customer}));
return {id:customer,auth:scoped};
},[customer,auth]);
const primaryTenant = auth.tenants[0]?.tenant_id;
const current = useResource(
async (signal) => {
if (!customer || customer === primaryTenant) return { id: customer, auth };
const scoped = session(await request('/auth/session', { signal, tenantId: customer }));
return { id: customer, auth: scoped };
},
[customer, auth]
);
// Gate rendering during a tenant change; a previously loaded session is scoped to its own customer.
const scoped=current.data?.id===customer ? current.data.auth : null;
const customerName=customers.find(c=>c.tenant_id===customer)?.name ?? '';
const openOperation=(id:string)=>{setPlan(null);setOperation(id);go('auftraege');};
const showPlan=(id:string)=>{setOperation(null);setPlan(id);go('auftraege');};
const selected=selection.customer===customer?selection.ids:[];
const tabs: [string,string][]= [['zugang','MCP-Tokens'],['aufnahme','Agent-Releases & Aufnahme'],['kataloge','Paketkataloge'],['rechte','Rechte & Ausführungsstopp']];
const adminTab=tabs.some(([id])=>id===route.tab)?route.tab:'zugang';
return <div className="workbench">
<aside className="sidebar">
<a className="brand" href="#geraete" aria-label="Rust Discovery – Geräte"><span className="brand-mark" aria-hidden="true"><i/><i/><i/></span>Rust Discovery</a>
<nav aria-label="Hauptnavigation">{(Object.entries(pages) as [NavKey,string][]).map(([key,title])=><a key={key} href={'#'+key} aria-current={route.page===key?'page':undefined}><NavIcon name={key}/><span>{title}</span></a>)}</nav>
<div className="account"><div><span className="account-dot" aria-hidden="true"/><span title={auth.principal.id}>{auth.principal.display_name}</span></div><button onClick={async()=>{
try {await request('/auth/logout',{csrf:auth.csrf_token,body:{}});window.history.replaceState(null,'',window.location.pathname+window.location.search);logout();}
catch(e){setError(errorText(e));}
}}><NavIcon name="abmelden"/> Abmelden</button></div>
</aside>
<div className="work-area"><header className="topline"><div className="crumb"><span>Arbeitsbereich</span><i aria-hidden="true">/</i><b>{pages[route.page]}</b></div>
{route.page!=='geraete' && route.page!=='kunden' && <label className="context"><span>Kunde</span><select value={customer} onChange={e=>switchCustomer(e.target.value)}>{customers.map(c=><option value={c.tenant_id} key={c.tenant_id}>{c.name}</option>)}</select></label>}
</header>
{auth.principal.simulated && <aside className="simulation" role="status">SIMULIERT · dev/test-Testprincipal · Genehmigung ist eine Entwicklungs-Browsersimulation, kein menschlicher Produktions-Freigabenachweis.</aside>}
<main className="content"><ErrorBox error={error}/><ErrorBox error={tenants.error}/><ErrorBox error={current.error}/>
{route.page==='geraete' && <Devices auth={scoped ?? auth} customers={customers} filter={fleetFilter} onFilter={selectFleetCustomer}
selection={selection} onSelection={value=>{if(value.customer)switchCustomer(value.customer);setSelection(value);}}
onEnroll={()=>go('verwaltung','aufnahme')} onScripts={()=>go('skripte')}
onOperation={(tenantId,id)=>{switchCustomer(tenantId);openOperation(id);}}/>}
{route.page==='kunden' && <><div className="page-title"><h1>Kunden</h1></div><CustomerManagement auth={auth} customers={customers} canManage={tenants.data?.canManage===true} onChange={tenants.reload} onSelect={id=>{selectFleetCustomer(id);go('geraete');}}/></>}
{!['geraete','kunden'].includes(route.page) && (!customer || !scoped) && !current.error && <p>Kundenbereich wird geladen …</p>}
{scoped && <React.Fragment key={customer}>
{route.page==='skripte' && <><div className="page-title"><h1>Skripte</h1><a className="soft-button" href="#geraete">Geräte auswählen</a></div>
{selected.length>0 && <p className="target-note">{selected.length} {selected.length===1?'Zielgerät':'Zielgeräte'} für {customerName} ausgewählt</p>}
<ScriptLibrary tenantId={customer} auth={scoped} selected={selected} showPlan={showPlan}/></>}
{route.page==='auftraege' && <><div className="page-title"><h1>Aufträge</h1>{route.tab!=='neu' && <a className="soft-button" href="#auftraege/neu"><NavIcon name="plus"/> Auftrag planen</a>}</div>
{route.tab==='neu' ? <><a className="quiet" href="#geraete">Geräte auswählen</a><p className="target-note">{selected.length} {selected.length===1?'Zielgerät':'Zielgeräte'} für {customerName} ausgewählt</p><Planner tenantId={customer} auth={scoped} selected={selected} showPlan={showPlan}/></> : <>
{operationId && <OperationDetail key={operationId} tenantId={customer} id={operationId} auth={scoped} openPlan={id=>setPlan(id)}/>}
{planId && <PlanDetail key={planId} tenantId={customer} id={planId} auth={scoped} onOperation={setOperation}/>}
<Operations tenantId={customer} open={openOperation}/></>}</>}
{route.page==='verwaltung' && <><div className="page-title"><h1>Verwaltung</h1></div><nav className="subnav" aria-label="Verwaltungsbereiche">{tabs.map(([id,title])=><a href={'#verwaltung/'+id} key={id} aria-current={adminTab===id?'page':undefined}>{title}</a>)}</nav>
{adminTab==='zugang' && <McpTokenManagement tenantId={customer} auth={scoped}/>}
{adminTab==='aufnahme' && <AgentReleaseCatalog tenantId={customer} auth={scoped} customerName={customerName} enrollmentOpen/>}
{adminTab==='kataloge' && <><Catalog tenantId={customer} auth={scoped}/><WindowsMsiCatalog tenantId={customer} auth={scoped}/></>}
{adminTab==='rechte' && <TenantControls tenantId={customer} auth={scoped}/>}</>}
</React.Fragment>}
</main></div>
</div>;
const scoped = current.data?.id === customer ? current.data.auth : null;
const customerName = customers.find((c) => c.tenant_id === customer)?.name ?? '';
const openOperation = (id: string) => {
setPlan(null);
setOperation(id);
go('auftraege');
};
const showPlan = (id: string) => {
setOperation(null);
setPlan(id);
go('auftraege');
};
const selected = selection.customer === customer ? selection.ids : [];
const tabs: [string, string][] = [
['zugang', 'MCP-Tokens'],
['aufnahme', 'Agent-Releases & Aufnahme'],
['kataloge', 'Paketkataloge'],
['rechte', 'Rechte & Ausführungsstopp']
];
const adminTab = tabs.some(([id]) => id === route.tab) ? route.tab : 'zugang';
return (
<div className="workbench">
<aside className="sidebar">
<a className="brand" href="#geraete" aria-label="Rust Discovery – Geräte">
<span className="brand-mark" aria-hidden="true">
<i />
<i />
<i />
</span>
Rust Discovery
</a>
<nav aria-label="Hauptnavigation">
{(Object.entries(pages) as [NavKey, string][]).map(([key, title]) => (
<a key={key} href={'#' + key} aria-current={route.page === key ? 'page' : undefined}>
<NavIcon name={key} />
<span>{title}</span>
</a>
))}
</nav>
<div className="account">
<div>
<span className="account-dot" aria-hidden="true" />
<span title={auth.principal.id}>{auth.principal.display_name}</span>
</div>
<button
onClick={async () => {
try {
await request('/auth/logout', { csrf: auth.csrf_token, body: {} });
window.history.replaceState(null, '', window.location.pathname + window.location.search);
logout();
} catch (e) {
setError(errorText(e));
}
}}
>
<NavIcon name="abmelden" /> Abmelden
</button>
</div>
</aside>
<div className="work-area">
<header className="topline">
<div className="crumb">
<span>Arbeitsbereich</span>
<i aria-hidden="true">/</i>
<b>{pages[route.page]}</b>
</div>
{route.page !== 'geraete' && route.page !== 'kunden' && (
<label className="context">
<span>Kunde</span>
<select value={customer} onChange={(e) => switchCustomer(e.target.value)}>
{customers.map((c) => (
<option value={c.tenant_id} key={c.tenant_id}>
{c.name}
</option>
))}
</select>
</label>
)}
</header>
{auth.principal.simulated && (
<aside className="simulation" role="status">
SIMULIERT · dev/test-Testprincipal · Genehmigung ist eine Entwicklungs-Browsersimulation,
kein menschlicher Produktions-Freigabenachweis.
</aside>
)}
<main className="content">
<ErrorBox error={error} />
<ErrorBox error={tenants.error} />
<ErrorBox error={current.error} />
{route.page === 'geraete' && (
<Devices
detailPath={route.tab}
auth={scoped ?? auth}
customers={customers}
filter={fleetFilter}
onFilter={selectFleetCustomer}
selection={selection}
onSelection={(value) => {
if (value.customer) switchCustomer(value.customer);
setSelection(value);
}}
onEnroll={() => go('verwaltung', 'aufnahme')}
onScripts={() => go('skripte')}
onOperation={(tenantId, id) => {
switchCustomer(tenantId);
openOperation(id);
}}
/>
)}
{route.page === 'kunden' && (
<>
<div className="page-title">
<h1>Kunden</h1>
</div>
<CustomerManagement
auth={auth}
customers={customers}
canManage={tenants.data?.canManage === true}
onChange={tenants.reload}
onSelect={(id) => {
selectFleetCustomer(id);
go('geraete');
}}
/>
</>
)}
{!['geraete', 'kunden'].includes(route.page) && (!customer || !scoped) && !current.error && (
<p>Kundenbereich wird geladen …</p>
)}
{scoped && (
<React.Fragment key={customer}>
{route.page === 'skripte' && (
<>
<div className="page-title">
<h1>Skripte</h1>
<a className="soft-button" href="#geraete">
Geräte auswählen
</a>
</div>
{selected.length > 0 && (
<p className="target-note">
{selected.length} {selected.length === 1 ? 'Zielgerät' : 'Zielgeräte'} für{' '}
{customerName} ausgewählt
</p>
)}
<ScriptLibrary
tenantId={customer}
auth={scoped}
selected={selected}
showPlan={showPlan}
/>
</>
)}
{route.page === 'auftraege' && (
<>
<div className="page-title">
<h1>Aufträge</h1>
{route.tab !== 'neu' && (
<a className="soft-button" href="#auftraege/neu">
<NavIcon name="plus" /> Auftrag planen
</a>
)}
</div>
{route.tab === 'neu' ? (
<>
<a className="quiet" href="#geraete">
Geräte auswählen
</a>
<p className="target-note">
{selected.length} {selected.length === 1 ? 'Zielgerät' : 'Zielgeräte'} für{' '}
{customerName} ausgewählt
</p>
<Planner
tenantId={customer}
auth={scoped}
selected={selected}
showPlan={showPlan}
/>
</>
) : (
<>
{operationId && (
<OperationDetail
key={operationId}
tenantId={customer}
id={operationId}
auth={scoped}
openPlan={(id) => setPlan(id)}
/>
)}
{planId && (
<PlanDetail
key={planId}
tenantId={customer}
id={planId}
auth={scoped}
onOperation={setOperation}
/>
)}
<Operations tenantId={customer} open={openOperation} />
</>
)}
</>
)}
{route.page === 'verwaltung' && (
<>
<div className="page-title">
<h1>Verwaltung</h1>
</div>
<nav className="subnav" aria-label="Verwaltungsbereiche">
{tabs.map(([id, title]) => (
<a
href={'#verwaltung/' + id}
key={id}
aria-current={adminTab === id ? 'page' : undefined}
>
{title}
</a>
))}
</nav>
{adminTab === 'zugang' && <McpTokenManagement tenantId={customer} auth={scoped} />}
{adminTab === 'aufnahme' && (
<AgentReleaseCatalog
tenantId={customer}
auth={scoped}
customerName={customerName}
enrollmentOpen
/>
)}
{adminTab === 'kataloge' && (
<>
<Catalog tenantId={customer} auth={scoped} />
<WindowsMsiCatalog tenantId={customer} auth={scoped} />
</>
)}
{adminTab === 'rechte' && <TenantControls tenantId={customer} auth={scoped} />}
</>
)}
</React.Fragment>
)}
</main>
</div>
</div>
);
}
function App() {
const [auth, setAuth] = useState<Session | null>(null), [loading, setLoading] = useState(true), [error, setError] = useState<string | null>(null);
const config = useResource(async signal => authConfig(await request('/auth/config', { signal })), []);