import m, {MithrilComponent} from '@garage44/mithril' import {Badge, Button, Card, FieldCheckbox, FieldText} from '@garage44/mithril/components' import {check, required, validation} from '@garage44/mithril/validation' import type {PermissionInfo, Role} from '@/types' import {$s} from '@/app' import {bind, local} from '@/lib/reactive' import {createRole, deleteRole, loadRoles, roleId, saveRole} from '@/lib/roles' const GROUPS: {group: PermissionInfo['group']; label: string}[] = [ {group: 'prims', label: 'Prims'}, {group: 'scripts', label: 'Scripts'}, {group: 'buildings', label: 'Buildings'}, {group: 'server', label: 'Running the server'}, ] /** * Two permissions are trust, not features: whoever may manage roles can grant * themselves anything, and whoever may manage accounts can lock other people * out. The grid says so next to the box rather than in a manual. */ const WARNINGS: Record = { accounts_manage: 'Can disable and delete accounts (but not admins).', roles_manage: 'Can give any role any permission — including to themselves. Treat as an admin.', } interface RoleCardAttrs { role: Role } /* * Each role stays an open card rather than a row whose "Edit" opens the context * panel: the grid *is* the page, and comparing what two roles grant means seeing * both at once. Nothing here was an inline expansion to move. */ class RoleCard extends MithrilComponent { s: {busy: boolean; confirming: boolean; held: string[]} /** The `role.permissions` the last `sync` followed. */ private seen: string constructor(vnode: m.Vnode) { super() this.s = local({busy: false, confirming: false, held: vnode.attrs.role.permissions}) this.seen = vnode.attrs.role.permissions.join(',') } // A save elsewhere (another tab, the server's answer) replaces the role; // follow it rather than keep showing the edit that was saved. private sync(role: Role) { const key = role.permissions.join(',') if (key === this.seen) { return } this.seen = key this.s.held = role.permissions } onupdate(vnode: m.Vnode) { this.sync(vnode.attrs.role) } act = async (fn: () => Promise) => { this.s.busy = true await fn() this.s.busy = false } view(vnode: m.Vnode) { const {role} = vnode.attrs const s = this.s const held = s.held const changed = held.slice().sort().join(',') !== role.permissions.slice().sort().join(',') // One box's model: whether the edit holds the permission. const box = (slug: string) => ({ get value() { return held.includes(slug) }, set value(on: boolean) { s.held = on ? [...held, slug] : held.filter((p) => p !== slug) }, }) return ( every account ) : ( {role.members} {role.members === 1 ? 'account' : 'accounts'} ) } class="role" footer={ <> {!role.built_in && (s.confirming ? ( <> ) : ( ))} } title={role.name} > {role.built_in && (

Every signed-in account holds this role. Taking a permission away here takes it away from everybody who is not given it by another role.

)}
{GROUPS.map(({group, label}) => (
{label} {$s.permissionCatalogue .filter((p) => p.group === group) .map((p) => ( {p.label} {WARNINGS[p.slug] && held.includes(p.slug) && ( — {WARNINGS[p.slug]} )} } model={box(p.slug)} /> ))}
))}
) } } class NewRole extends MithrilComponent { s = local({busy: false, name: '', open: false}) // The rule the old disabled-button check was: the id the name turns into // must be at least two characters, which is what the server stores it as. name = validation(bind(this.s, 'name'), [ required, check((v: string) => roleId(v).length >= 2, 'Use at least two letters or digits'), ]) submit = async (e: Event) => { e.preventDefault() const s = this.s if (!this.name.touch() || s.busy) { return } s.busy = true const ok = await createRole(roleId(s.name), s.name.trim(), []) s.busy = false if (ok) { s.name = '' s.open = false this.name.reset() } } view() { const s = this.s const id = roleId(s.name) if (!s.open) { return ( ) } return (
} title="New role" >
) } } /** * The roles page (`docs/notes/permissions.md`). Admins are not a role — they * hold everything, and are made on the Accounts page. */ export class Roles extends MithrilComponent { oninit() { void loadRoles() } view() { return (
{$s.roles.length} {$s.roles.length === 1 ? 'role' : 'roles'}

A role is a set of permissions. Every account holds Player; give an account more roles on the Accounts page. Admins hold every permission and are not listed here. A change applies to an account's next sign-in in the game, and to its next request everywhere else.

{!$s.permissionCatalogue.length ? (
Loading…
) : (
{$s.roles.map((role) => ( ))}
)}
) } }