Skip to content

An open-source, self-hosted system for apps built with web technologies.

save

zohayrslileh522cce1dfa04
diff --git a/scripts/contracts/client-system-access.ts b/scripts/contracts/client-system-access.ts
index 0f7a8c8..28d0188 100644
--- a/scripts/contracts/client-system-access.ts
+++ b/scripts/contracts/client-system-access.ts
@@ -1,11 +1,13 @@
import assert from "node:assert/strict"
import type AuthManager from "@client/core/link-manager/auth-manager/auth-manager"
import SystemAccess from "@client/view/components/desktop-host/system-access"
+import { permissionCatalog } from "@server/core/permissions"
+import type { PermissionName, Permissions } from "@phreshos/core"
const owner = { identity: "process:owner", program: "owner" }
const sibling = { identity: "process:sibling", program: "owner" }
const outside = { identity: "process:outside", program: "outside" }
-let unrestricted = false
+let permissions: Permissions = {}
const authManager = {
processManager: { processes: new Map([
@@ -13,24 +15,23 @@ const authManager = {
[sibling.identity, sibling],
[outside.identity, outside]
]) },
- async grantsPermission(process: string, name: "all", values: readonly never[]) {
+ async grantsPermission(process: string, name: PermissionName, values: readonly string[]) {
assert.equal(process, owner.identity)
- assert.equal(name, "all")
- assert.deepEqual(values, [])
- return unrestricted
+ return permissionCatalog.allows(name, permissions.all ?? null, permissions[name] ?? null, values as never)
}
} as unknown as AuthManager
const access = new SystemAccess(authManager, owner.identity)
const ownProgram = { identity: "owner" }
+const outsideProgram = { identity: "outside" }
const ownService = { program: "owner", process: "main", endpoint: "server" } as const
const ownExactService = { process: sibling.identity, endpoint: "client" } as const
const outsideService = { program: "outside", process: "main", endpoint: "server" } as const
-assert(access.ownsProgram({ identity: "owner" }))
-assert(!access.ownsProgram({ identity: "outside" }))
+assert(access.ownsProgram(ownProgram))
+assert(!access.ownsProgram(outsideProgram))
assert(access.ownsProcess(sibling as never))
assert(!access.ownsProcess(outside as never))
assert(access.ownsService(ownService))
@@ -39,14 +40,51 @@ assert(!access.ownsService(outsideService))
assert.equal(await access.program(ownProgram as never), ownProgram)
assert.equal(await access.service(ownService), ownService)
assert.equal(await access.service(ownExactService), ownExactService)
-await assert.rejects(access.program({ identity: "outside" } as never), /Execution is not permitted/)
+await assert.rejects(access.program(outsideProgram as never), /Execution is not permitted/)
await assert.rejects(access.process(outside as never), /Execution is not permitted/)
await assert.rejects(access.service(outsideService), /Execution is not permitted/)
-await assert.rejects(access.requireAll(), /Execution is not permitted/)
+await assert.rejects(access.requirePrograms(), /Execution is not permitted/)
+await assert.rejects(access.require("appearance", []), /Execution is not permitted/)
+await assert.rejects(access.require("desktopPreferences", []), /Execution is not permitted/)
-unrestricted = true
+permissions = { services: ["outside"] }
-assert.equal((await access.program({ identity: "outside" } as never)).identity, "outside")
+assert.equal(await access.service(outsideService), outsideService)
+await assert.rejects(access.program(outsideProgram as never), /Execution is not permitted/)
+await assert.rejects(access.process(outside as never), /Execution is not permitted/)
+await assert.rejects(access.requirePrograms(), /Execution is not permitted/)
+
+permissions = { services: [] }
+
+assert.equal(await access.service(outsideService), outsideService)
+await assert.rejects(access.program(outsideProgram as never), /Execution is not permitted/)
+await assert.rejects(access.requirePrograms(), /Execution is not permitted/)
+
+permissions = { programs: ["outside"] }
+
+assert.equal(await access.program(outsideProgram as never), outsideProgram)
+assert.equal(await access.process(outside as never), outside)
+assert.equal(await access.service(outsideService), outsideService)
+await assert.rejects(access.requirePrograms(), /Execution is not permitted/)
+
+permissions = { programs: [] }
+
+await access.requirePrograms()
+assert.equal(await access.program(outsideProgram as never), outsideProgram)
+assert.equal(await access.service(outsideService), outsideService)
+
+permissions = { appearance: [], desktopPreferences: [] }
+
+await access.require("appearance", [])
+await access.require("desktopPreferences", [])
+await assert.rejects(access.program(outsideProgram as never), /Execution is not permitted/)
+
+permissions = { all: [] }
+
+assert.equal((await access.program(outsideProgram as never)).identity, "outside")
assert.equal((await access.process(outside as never)).identity, outside.identity)
assert.equal(await access.service(outsideService), outsideService)
+await access.requirePrograms()
+await access.require("appearance", [])
+await access.require("desktopPreferences", [])
await access.requireAll()
diff --git a/scripts/contracts/permissions.ts b/scripts/contracts/permissions.ts
index a77df30..b2e7f05 100644
--- a/scripts/contracts/permissions.ts
+++ b/scripts/contracts/permissions.ts
@@ -8,42 +8,90 @@ const catalog = new PermissionCatalog({
title: "All permissions",
description: "Grant every available Client permission.",
requiresReload: (before, permission) => Array.isArray(before) !== Array.isArray(permission)
+ },
+ services: {
+ default: [],
+ title: "Services",
+ description: "Access Services belonging to every Program or selected Programs."
+ },
+ programs: {
+ default: [],
+ title: "Programs",
+ description: "Access every Program or selected Programs and their Services."
+ },
+ appearance: {
+ default: [],
+ title: "Appearance",
+ description: "Change the System Appearance."
+ },
+ desktopPreferences: {
+ default: [],
+ title: "Desktop preferences",
+ description: "Change this Desktop's preferences."
}
})
assert.deepEqual(catalog.definition("all"), {
- values: [],
+ valueDomain: "none",
default: [],
title: "All permissions",
description: "Grant every available Client permission."
})
+assert.deepEqual(catalog.definition("programs"), {
+ valueDomain: "program",
+ default: [],
+ title: "Programs",
+ description: "Access every Program or selected Programs and their Services."
+})
assert.deepEqual(catalog.resolve("all", true), [])
assert.deepEqual(catalog.resolve("all", []), [])
+assert.deepEqual(catalog.resolve("services", true), [])
+assert.deepEqual(catalog.resolve("services", ["flambo", "terminal", "flambo"]), ["flambo", "terminal"])
assert.equal(catalog.resolve("all", false), false)
assert.equal(catalog.resolve("all", null), null)
-assert.deepEqual(catalog.declarations({ all: true }), { all: [] })
+assert.deepEqual(catalog.declarations({ all: true, services: ["flambo"] }), { all: [], services: ["flambo"] })
assert(Object.isFrozen(catalog.declarations({})))
assert(Object.isFrozen(catalog.declarations({ all: true }).all))
-assert(catalog.grants([], []))
-assert(!catalog.grants(false, []))
-assert(!catalog.grants(null, []))
+assert(catalog.grants("all", [], []))
+assert(!catalog.grants("all", false, []))
+assert(!catalog.grants("all", null, []))
+assert(catalog.grants("services", [], []))
+assert(catalog.grants("services", [], ["flambo"]))
+assert(catalog.grants("services", ["flambo"], ["flambo"]))
+assert(!catalog.grants("services", ["flambo"], ["terminal"]))
+assert(!catalog.grants("services", ["flambo"], []))
assert(catalog.allows("all", [], null, []))
assert(!catalog.allows("all", null, null, []))
+assert(catalog.allows("services", [], null, ["flambo"]))
+assert(catalog.allows("services", null, ["flambo"], ["flambo"]))
assert.equal(catalog.combine("all", null, false), null)
assert.deepEqual(catalog.combine("all", []), [])
+assert.deepEqual(catalog.combine("services", ["flambo"], ["terminal"]), ["flambo", "terminal"])
+assert.deepEqual(catalog.combine("services", ["flambo"], []), [])
assert.equal(catalog.effective("all", null, false), false)
assert.deepEqual(catalog.effective("all", [], false), [])
assert.deepEqual(catalog.merge("all", null, []), [])
+assert.deepEqual(catalog.merge("services", ["flambo"], ["terminal"]), ["flambo", "terminal"])
+assert.deepEqual(catalog.merge("services", ["flambo"], []), [])
+assert.deepEqual(catalog.merge("services", [], ["flambo"]), [])
assert(!catalog.changed([], []))
+assert(!catalog.changed(["flambo", "terminal"], ["terminal", "flambo"]))
+assert(catalog.changed(["flambo"], []))
assert(catalog.needReload("all", null, []))
assert(catalog.needReload("all", [], null))
assert(!catalog.needReload("all", [], []))
+assert(!catalog.needReload("services", null, []))
+assert(!catalog.needReload("programs", ["flambo"], []))
+assert(!catalog.needReload("appearance", null, []))
+assert(!catalog.needReload("desktopPreferences", null, []))
assert.throws(() => parsePermissionName("files"), /does not know/)
assert.throws(() => catalog.resolve("all", ["unknown"]), /unknown value/)
+assert.throws(() => catalog.resolve("appearance", ["flambo"]), /unknown value/)
+assert.throws(() => catalog.resolve("services", ["Not a Program"]), /unknown value/)
assert.throws(() => catalog.declarations({ all: ["unknown"] }), /unknown value/)
assert.throws(() => catalog.stored({ all: true }), /unresolved shorthand/)
assert.throws(() => new PermissionCatalog({} as never), /needs a definition/)
@@ -52,11 +100,16 @@ assert.throws(() => new PermissionCatalog({
default: ["unknown"],
title: "All permissions",
description: "Invalid default."
- }
+ },
+ services: catalog.definition("services"),
+ programs: catalog.definition("programs"),
+ appearance: catalog.definition("appearance"),
+ desktopPreferences: catalog.definition("desktopPreferences")
} as never), /invalid default/)
assert.deepEqual(permissionCatalog.resolve("all", true), [])
assert.deepEqual(permissionCatalog.declarations({ all: true }), { all: [] })
+assert.deepEqual(permissionCatalog.declarations({ programs: true }), { programs: [] })
assert(permissionCatalog.granted([]))
assert(!permissionCatalog.granted(null))
assert.throws(() => permissionCatalog.definition("files" as never), /does not know/)
diff --git a/source/client/view/components/desktop-host/announcements.ts b/source/client/view/components/desktop-host/announcements.ts
index 56b2085..bf7a2f1 100644
--- a/source/client/view/components/desktop-host/announcements.ts
+++ b/source/client/view/components/desktop-host/announcements.ts
@@ -19,8 +19,7 @@ export default function useAnnouncements(authManager: AuthManager, panes: Map<st
const access = new SystemAccess(authManager, pane)
- if (access.ownsProgram({ identity: program })) traffic.emit(pane, route, ...message).catch(() => undefined)
- else access.all().then(granted => {
+ access.canProgram({ identity: program }).then(granted => {
if (granted) return traffic.emit(pane, route, ...message)
}).catch(() => undefined)
diff --git a/source/client/view/components/desktop-host/client-process-boundary.ts b/source/client/view/components/desktop-host/client-process-boundary.ts
index 77fce09..ae22002 100644
--- a/source/client/view/components/desktop-host/client-process-boundary.ts
+++ b/source/client/view/components/desktop-host/client-process-boundary.ts
@@ -4,7 +4,7 @@ import host, { TransferredAnswer } from "./host"
import ClientTraffic from "./client-traffic"
import { failed, succeeded } from "@server/core/outcome"
import { type TrafficKind } from "@server/core/link-manager/auth-manager/process-manager/process-traffic"
-import { isServiceKey, type DesktopSurfaceSnapshot, type ShellOptions } from "@phreshos/core"
+import { isServiceKey, type DesktopSurfaceSnapshot, type ServiceKey, type ShellOptions } from "@phreshos/core"
import { type LocalWindowHost } from "./local-window"
import messagepack from "@libs/messagepack"
import { sdkProcess, type SdkProcessSource } from "./sdk-records"
@@ -37,8 +37,8 @@ export default class ClientProcessBoundary extends TheLink {
private readonly trafficSubscriptions = new Map<string, TrafficSubscription>()
- /** Program ownership of each System-routed subscription. */
- private readonly systemSubscriptions = new Map<string, string | null>()
+ /** The exact System authority exercised by each routed subscription. */
+ private readonly systemSubscriptions = new Map<string, SystemSubscriptionTarget>()
private readonly desktopPreferencesSubscriptions = new Set<string>()
@@ -350,10 +350,7 @@ export default class ClientProcessBoundary extends TheLink {
if (operation === "service-follow") {
- this.systemSubscriptions.set(
- subscription,
- isServiceKey(target) ? this.systemAccess.serviceProgram(target) : null
- )
+ if (isServiceKey(target)) this.systemSubscriptions.set(subscription, { domain: "service", service: target })
return
}
@@ -366,7 +363,7 @@ export default class ClientProcessBoundary extends TheLink {
? this.authManager.processManager.processes.get(target.identity)
: undefined
- this.systemSubscriptions.set(subscription, process?.program ?? null)
+ if (process) this.systemSubscriptions.set(subscription, { domain: "program", program: process.program })
return
}
@@ -382,26 +379,20 @@ export default class ClientProcessBoundary extends TheLink {
if (route !== "observed" && route !== "emitted" && route !== "service-event") return true
if (typeof subscription !== "string" || !this.systemSubscriptions.has(subscription)) return false
- const program = this.systemSubscriptions.get(subscription)
+ const target = this.systemSubscriptions.get(subscription)
- return program !== null && program !== undefined && this.systemAccess.ownsProgram({ identity: program })
- ? true
- : await this.systemAccess.all()
+ return target?.domain === "service"
+ ? await this.systemAccess.canService(target.service)
+ : target?.domain === "program"
+ ? await this.systemAccess.canProgram({ identity: target.program })
+ : false
}
private async visibleReferences(values: unknown[]) {
- let unrestricted: Promise<boolean> | null = null
-
const visible = async (reference: EndpointReference) => {
- const process = reference.process
-
- if (this.systemAccess.ownsProgram(process.program)) return reference
-
- unrestricted ??= this.systemAccess.all()
-
- return await unrestricted ? reference : null
+ return await this.systemAccess.canProgram(reference.process.program) ? reference : null
}
return await Promise.all(values.map(async value => {
@@ -1036,6 +1027,10 @@ interface EndpointSubscription {
subject: string | null
}
+type SystemSubscriptionTarget =
+ | Readonly<{ domain: "program", program: string }>
+ | Readonly<{ domain: "service", service: ServiceKey }>
+
function desktopPreferencesSubscription(subscription: EndpointSubscription) {
return subscription.kind === "publish" && subscription.route === "host-desktop-preferences" && (subscription.event === null || subscription.event === "change")
diff --git a/source/client/view/components/desktop-host/host.ts b/source/client/view/components/desktop-host/host.ts
index 8148bbf..5bd7598 100644
--- a/source/client/view/components/desktop-host/host.ts
+++ b/source/client/view/components/desktop-host/host.ts
@@ -164,7 +164,9 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "host-program-list") {
const programs = [...programManager.programs.values()].filter(program => args[0] !== true || program.installed)
- const visible = await access.all() ? programs : programs.filter(program => access.ownsProgram(program))
+ const visible = []
+
+ for (const program of programs) if (await access.canProgram(program)) visible.push(program)
return [visible.map(sdkProgram)]
}
@@ -180,7 +182,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "host-program-create" || word === "host-program-force-create") {
- await access.requireAll()
+ await access.requirePrograms()
const identity = word === "host-program-create"
? await programManager.create(args[0])
@@ -192,7 +194,9 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "host-process-list") {
const processes = [...processManager.processes.values()]
- const visible = await access.all() ? processes : processes.filter(process => access.ownsProcess(process))
+ const visible = []
+
+ for (const process of processes) if (await access.canProcess(process)) visible.push(process)
return [visible.map(record)]
}
@@ -210,7 +214,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "updateAppearance") {
- await access.requireAll()
+ await access.require("appearance", [])
await authManager.updateAppearance(args[0])
@@ -221,7 +225,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "updateDesktopPreferences") {
- await access.requireAll()
+ await access.require("desktopPreferences", [])
const preferences = desktopPreferencesUpdate(args[0])
@@ -441,7 +445,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
return []
}
- if (!access.ownsProcess(target) && !await access.all()) {
+ if (!await access.canProcess(target)) {
if (reportImpossible) throw new Error("Execution is not permitted")
@@ -492,7 +496,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
return []
}
- if (!access.ownsProcess(target) && !await access.all()) {
+ if (!await access.canProcess(target)) {
if (reportImpossible) throw new Error("Execution is not permitted")
@@ -615,7 +619,7 @@ export default function host(authManager: AuthManager, pane: string, desktop: ()
if (word === "fork") {
- await access.requireAll()
+ await access.requirePrograms()
const identity = await programManager.fork(args[0], String(args[1]))
const program = programManager.programs.get(identity)
diff --git a/source/client/view/components/desktop-host/system-access.ts b/source/client/view/components/desktop-host/system-access.ts
index 09ac6a7..174709a 100644
--- a/source/client/view/components/desktop-host/system-access.ts
+++ b/source/client/view/components/desktop-host/system-access.ts
@@ -1,7 +1,7 @@
import type AuthManager from "@client/core/link-manager/auth-manager/auth-manager"
import type Program from "@client/core/link-manager/auth-manager/program-manager/program"
import type Process from "@client/core/link-manager/auth-manager/process-manager/process"
-import type { ServiceKey } from "@phreshos/core"
+import type { PermissionName, PermissionValue, ServiceKey } from "@phreshos/core"
const denied = "Execution is not permitted"
@@ -35,23 +35,46 @@ export default class SystemAccess {
return await this.authManager.grantsPermission(this.pane, "all", [])
}
+ public async canProgram(program: Pick<Program, "identity">) {
+
+ return this.ownsProgram(program)
+ || await this.authManager.grantsPermission(this.pane, "programs", [program.identity])
+ }
+
+ public async canProcess(process: Pick<Process, "program">) {
+
+ return this.ownsProcess(process)
+ || await this.authManager.grantsPermission(this.pane, "programs", [process.program])
+ }
+
+ public async canService(service: ServiceKey) {
+
+ const program = this.serviceProgram(service)
+
+ if (program === null) return await this.all()
+ if (program === this.owner().program) return true
+
+ return await this.authManager.grantsPermission(this.pane, "programs", [program])
+ || await this.authManager.grantsPermission(this.pane, "services", [program])
+ }
+
public async program(program: Program) {
- if (!this.ownsProgram(program) && !await this.all()) throw new Error(denied)
+ if (!await this.canProgram(program)) throw new Error(denied)
return program
}
public async process(process: Process) {
- if (!this.ownsProcess(process) && !await this.all()) throw new Error(denied)
+ if (!await this.canProcess(process)) throw new Error(denied)
return process
}
public async service(service: ServiceKey) {
- if (!this.ownsService(service) && !await this.all()) throw new Error(denied)
+ if (!await this.canService(service)) throw new Error(denied)
return service
}
@@ -61,6 +84,16 @@ export default class SystemAccess {
if (!await this.all()) throw new Error(denied)
}
+ public async requirePrograms() {
+
+ await this.require("programs", [])
+ }
+
+ public async require<Name extends PermissionName>(name: Name, values: readonly PermissionValue<Name>[]) {
+
+ if (!await this.authManager.grantsPermission(this.pane, name, values)) throw new Error(denied)
+ }
+
private owner() {
const process = this.authManager.processManager.processes.get(this.pane)
diff --git a/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts b/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts
index f2d998c..6522a89 100644
--- a/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts
+++ b/source/server/core/link-manager/auth-manager/process-manager/process-manager.ts
@@ -1446,10 +1446,10 @@ export default class ProcessManager extends TheLink {
if (name !== "all" && permissionCatalog.granted(this.effectivePermission(process, "all"))) {
- return permissionChange([...permissionCatalog.definition(name).values], false)
+ return permissionChange([...permissionCatalog.definition(name).default], false)
}
- if (permissionCatalog.grants(effective, requested)) return permissionChange(effective, false)
+ if (permissionCatalog.grants(name, effective, requested)) return permissionChange(effective, false)
if (effective === false) return Object.freeze({ permission: false, needReload: false })
const choice = await this.authManager.dialogManager.requestPermission(process, request, name, requested)
diff --git a/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts b/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
index 430c664..35c2e78 100644
--- a/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
+++ b/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts
@@ -185,8 +185,7 @@ export default class ProgramManager extends TheLink {
const permission = permissionCatalog.resolve(name, value)
if (permissionCatalog.changed(before, permission)) {
- permissions[name] = permission
- writePermissions(program, permissions)
+ writePermissions(program, { ...permissions, [name]: permission })
}
return await this.permissionChange(program, name, before, permission)
diff --git a/source/server/core/permissions.ts b/source/server/core/permissions.ts
index c2e0540..6a0a3aa 100644
--- a/source/server/core/permissions.ts
+++ b/source/server/core/permissions.ts
@@ -1,5 +1,6 @@
import {
clientPermissionCatalog,
+ parsePermission,
parsePermissionName,
type ClientPermissions,
type Permission,
@@ -7,6 +8,7 @@ import {
type PermissionDefinitions,
type PermissionName,
type PermissionValue,
+ type PermissionValueDomain,
type Permissions
} from "@phreshos/core"
@@ -32,13 +34,24 @@ export class PermissionCatalog {
if (typeof rule.title !== "string" || !rule.title.trim()) throw new Error(`Permission "${name}" needs a title`)
if (typeof rule.description !== "string" || !rule.description.trim()) throw new Error(`Permission "${name}" needs a description`)
- const values = clientPermissionCatalog[name] as readonly string[]
- const defaults = unique(strings(rule.default, `Permission "${name}" default`))
+ let defaults: PermissionValue<typeof name>[]
- if (defaults.some(value => !values.includes(value))) throw new Error(`Permission "${name}" has an invalid default`)
+ try {
+
+ const parsed = parsePermission(name, rule.default)
+
+ if (!Array.isArray(parsed)) throw new Error()
+
+ defaults = parsed
+ }
+
+ catch {
+
+ throw new Error(`Permission "${name}" has an invalid default`)
+ }
validated[name] = Object.freeze({
- values,
+ valueDomain: clientPermissionCatalog[name],
default: Object.freeze(defaults),
title: rule.title,
description: rule.description
@@ -73,13 +86,17 @@ export class PermissionCatalog {
if (value === false || value === null) return value
const requested = unique(strings(value, `Permission "${name}"`))
- const values = definition.values as readonly string[]
- if (requested.some(entry => !values.includes(entry))) {
- throw new Error(`Permission "${name}" contains an unknown value`)
+ try {
+
+ const parsed = parsePermission(name, requested)
+
+ if (Array.isArray(parsed)) return parsed
}
- return values.filter(entry => requested.includes(entry)) as PermissionValue<Name>[]
+ catch { }
+
+ throw new Error(`Permission "${name}" contains an unknown value`)
}
public declarations(value: unknown): ClientPermissions {
@@ -120,9 +137,16 @@ export class PermissionCatalog {
return permissions as Permissions
}
- public grants<Name extends PermissionName>(grant: PermissionGrant<Name>, requested: readonly PermissionValue<Name>[]) {
+ public grants<Name extends PermissionName>(name: Name, grant: PermissionGrant, requested: readonly PermissionValue<Name>[]) {
+
+ if (!Array.isArray(grant)) return false
+
+ if (programScoped(this.definition(name).valueDomain)) {
+
+ return grant.length === 0 || (requested.length > 0 && requested.every(value => grant.includes(value)))
+ }
- return Array.isArray(grant) && requested.every(value => grant.includes(value))
+ return requested.length === 0
}
/** Whether one value-less or valued permission is present at all. */
@@ -134,39 +158,42 @@ export class PermissionCatalog {
/** Applies the value-less all grant before one permission's own values. */
public allows<Name extends PermissionName>(
name: Name,
- all: PermissionGrant<"all">,
- permission: PermissionGrant<Name>,
+ all: PermissionGrant,
+ permission: PermissionGrant,
requested: readonly PermissionValue<Name>[]
) {
this.definition(name)
- return this.granted(all) || this.grants(permission, requested)
+ return this.granted(all) || this.grants(name, permission, requested)
}
- public combine<Name extends PermissionName>(name: Name, ...grants: PermissionGrant<Name>[]): Permission<Name> {
+ public combine<Name extends PermissionName>(name: Name, ...grants: PermissionGrant[]): Permission<Name> {
+
+ const present = grants.filter((grant): grant is readonly string[] => Array.isArray(grant))
- const present = grants.filter((grant): grant is readonly PermissionValue<Name>[] => Array.isArray(grant))
+ if (present.length === 0) return null
+ if (!programScoped(this.definition(name).valueDomain)) return []
+ if (present.some(grant => grant.length === 0)) return []
- return present.length
- ? present.reduce<PermissionValue<Name>[]>((combined, grant) => this.merge(name, combined, grant), [])
- : null
+ return this.resolve(name, present.flat()) as PermissionValue<Name>[]
}
- public effective<Name extends PermissionName>(name: Name, ...grants: PermissionGrant<Name>[]): Permission<Name> {
+ public effective<Name extends PermissionName>(name: Name, ...grants: PermissionGrant[]): Permission<Name> {
return this.combine(name, ...grants) ?? (grants.includes(false) ? false : null)
}
public merge<Name extends PermissionName>(
name: Name,
- grant: PermissionGrant<Name>,
+ grant: PermissionGrant,
requested: readonly PermissionValue<Name>[]
): PermissionValue<Name>[] {
- const current = Array.isArray(grant) ? grant : []
+ if (!Array.isArray(grant)) return [...requested]
+ if (programScoped(this.definition(name).valueDomain) && (grant.length === 0 || requested.length === 0)) return []
- return this.resolve(name, [...current, ...requested]) as PermissionValue<Name>[]
+ return this.resolve(name, [...grant, ...requested]) as PermissionValue<Name>[]
}
public changed<Name extends PermissionName>(left: Permission<Name>, right: Permission<Name>) {
@@ -185,14 +212,14 @@ export class PermissionCatalog {
}
}
-type PermissionGrant<Name extends PermissionName = PermissionName> = readonly PermissionValue<Name>[] | false | null
+type PermissionGrant = readonly string[] | false | null
type PermissionReload<Name extends PermissionName = PermissionName> = (
before: Permission<Name>,
permission: Permission<Name>
) => boolean
-type PermissionRule<Name extends PermissionName> = Omit<PermissionDefinition<Name>, "values"> & Readonly<{
+type PermissionRule<Name extends PermissionName> = Omit<PermissionDefinition<Name>, "valueDomain"> & Readonly<{
requiresReload?: PermissionReload<Name>
}>
@@ -201,7 +228,7 @@ type PermissionRules = Readonly<{
}>
type StoredDefinition = Readonly<{
- values: readonly string[]
+ valueDomain: PermissionValueDomain
default: readonly string[]
title: string
description: string
@@ -224,6 +251,16 @@ function unique(values: readonly string[]) {
return [...new Set(values)]
}
+function programScoped(domain: PermissionValueDomain) {
+
+ if (domain === "program") return true
+ if (domain === "none") return false
+
+ domain satisfies never
+
+ throw new Error("The System does not know this permission value domain")
+}
+
/** The presentation and activation rules for every Core-defined permission. */
export const permissionCatalog = new PermissionCatalog({
all: {
@@ -231,5 +268,25 @@ export const permissionCatalog = new PermissionCatalog({
title: "All permissions",
description: "Grant every available Client permission.",
requiresReload: (before, permission) => Array.isArray(before) !== Array.isArray(permission)
+ },
+ services: {
+ default: [],
+ title: "Services",
+ description: "Access Services belonging to every Program or selected Programs."
+ },
+ programs: {
+ default: [],
+ title: "Programs",
+ description: "Access every Program or selected Programs and their Services."
+ },
+ appearance: {
+ default: [],
+ title: "Appearance",
+ description: "Change the System Appearance."
+ },
+ desktopPreferences: {
+ default: [],
+ title: "Desktop preferences",
+ description: "Change this Desktop's preferences."
}
})