import type { Actor, Grant, Person, RepoAccess, Role } from "./contract/actor.ts"; import type { Visibility } from "./contract/views.ts"; import { now } from "./auth.ts"; const ROLES: Role[] = ["owner", "admin", "member"]; export interface RepoFacts { readonly name: string; readonly visibility: Visibility; readonly creatorId: string; } /** The person whose access applies: the human, or the human an agent works for. */ export function principal(actor: Actor): Person | null { if (actor.kind === "human") return actor.person; if (actor.kind === "agent") return actor.onBehalfOf; return null; } /** The branch an agent session holds: the one branch its grant names, or null when the grant names none. */ export function heldBranch(grant: Grant): string | null { return grant.repos === "all" ? null : (grant.repos[0]?.branches?.[0] ?? null); } function grantsFor(actor: Actor, extra: readonly Grant[]): Grant[] { return actor.kind === "agent" ? [actor.grant, ...extra] : [...extra]; } function grantAllowsRepo(grant: Grant, name: string, access: RepoAccess, at: number): boolean { if (grant.expiresAt !== undefined && grant.expiresAt < at) return false; if (grant.repos === "all") return true; const entry = grant.repos.find((repo) => repo.name.toLowerCase() === name.toLowerCase()); return Boolean(entry && (access === "read" || entry.access === "write")); } function grantAllowsOrg(grant: Grant, level: "member" | "admin"): boolean { return grant.org === "admin" || (grant.org === "member" && level === "member"); } export class Policy { readonly actor: Actor; /** The principal's role in the namespace this policy guards; null when they are not a member. */ readonly role: Role | null; /** True when that namespace is a user's own account, which has no members to manage. */ readonly personal: boolean; private readonly grants: readonly Grant[]; /** The time, in seconds, that grant expiry is checked against. */ private readonly at: number; constructor(actor: Actor, role: Role | null, grants: readonly Grant[] = [], personal = false, at = now()) { this.actor = actor; this.role = principal(actor) ? role : null; this.grants = grants; this.personal = personal; this.at = at; } narrowed(grant: Grant): Policy { return new Policy(this.actor, this.role, [...this.grants, grant], this.personal, this.at); } withRole(role: Role | null, personal = false): Policy { return new Policy(this.actor, role, this.grants, personal, this.at); } get person(): Person | null { return principal(this.actor); } get signedIn(): boolean { return this.person !== null; } get member(): boolean { return this.role !== null; } private allGrants(): Grant[] { return grantsFor(this.actor, this.grants); } private repoGranted(name: string, access: RepoAccess): boolean { return this.allGrants().every((grant) => grantAllowsRepo(grant, name, access, this.at)); } private orgGranted(level: "member" | "admin"): boolean { return this.allGrants().every((grant) => grantAllowsOrg(grant, level)); } canSeeRepo(repo: RepoFacts): boolean { const visible = repo.visibility === "public" || this.member; return visible && this.repoGranted(repo.name, "read"); } canWriteRepo(repo: RepoFacts): boolean { return this.member && this.repoGranted(repo.name, "write"); } canAdminRepo(repo: RepoFacts): boolean { const person = this.person; if (!person || !this.member || !this.repoGranted(repo.name, "write") || !this.orgGranted("member")) return false; return this.role !== "member" || person.id === repo.creatorId; } canCreateRepos(): boolean { return this.member && this.orgGranted("member"); } canUseTasks(): boolean { return this.member; } /** Tasks are a plain tracker with no tie to agent work: who can see a task's repository can open the task. */ canOpenTask(repo: string): boolean { return this.member && this.repoGranted(repo, "read"); } /** Agents limited to some branches see only the pull requests opened from those branches. */ canOpenPull(repo: string, pull: { readonly head: string }): boolean { const same = (name: string) => name.toLowerCase() === repo.toLowerCase(); return this.member && this.allGrants().every((grant) => grant.repos === "all" || grant.repos.some((entry) => same(entry.name) && (entry.branches === undefined || entry.branches.includes(pull.head)))); } canManageLabels(): boolean { return this.member && this.orgGranted("member"); } canManageMembers(): boolean { return this.member && !this.personal && this.role !== "member" && this.orgGranted("admin"); } invitableRoles(): Role[] { if (!this.canManageMembers()) return []; return this.role === "owner" ? ROLES : ["admin", "member"]; } /** CI secrets and settings change only by a person's hand: an agent could otherwise widen what its own runs get. */ canManageCiSecrets(): boolean { return this.actor.kind === "human" && this.member && this.role !== "member" && this.orgGranted("admin"); } canModerateComments(): boolean { return this.member && this.role !== "member"; } }