Skip to content
refs
History
145 lines · 5.1 KBRaw
1import type { Actor, Grant, Person, RepoAccess, Role } from "./contract/actor.ts";
2import type { Visibility } from "./contract/views.ts";
3import { now } from "./auth.ts";
4
5const ROLES: Role[] = ["owner", "admin", "member"];
6
7export interface RepoFacts {
8 readonly name: string;
9 readonly visibility: Visibility;
10 readonly creatorId: string;
11}
12
13/** The person whose access applies: the human, or the human an agent works for. */
14export function principal(actor: Actor): Person | null {
15 if (actor.kind === "human") return actor.person;
16 if (actor.kind === "agent") return actor.onBehalfOf;
17 return null;
18}
19
20/** The branch an agent session holds: the one branch its grant names, or null when the grant names none. */
21export function heldBranch(grant: Grant): string | null {
22 return grant.repos === "all" ? null : (grant.repos[0]?.branches?.[0] ?? null);
23}
24
25function grantsFor(actor: Actor, extra: readonly Grant[]): Grant[] {
26 return actor.kind === "agent" ? [actor.grant, ...extra] : [...extra];
27}
28
29function grantAllowsRepo(grant: Grant, name: string, access: RepoAccess, at: number): boolean {
30 if (grant.expiresAt !== undefined && grant.expiresAt < at) return false;
31 if (grant.repos === "all") return true;
32 const entry = grant.repos.find((repo) => repo.name.toLowerCase() === name.toLowerCase());
33 return Boolean(entry && (access === "read" || entry.access === "write"));
34}
35
36function grantAllowsOrg(grant: Grant, level: "member" | "admin"): boolean {
37 return grant.org === "admin" || (grant.org === "member" && level === "member");
38}
39
40export class Policy {
41 readonly actor: Actor;
42 /** The principal's role in the namespace this policy guards; null when they are not a member. */
43 readonly role: Role | null;
44 /** True when that namespace is a user's own account, which has no members to manage. */
45 readonly personal: boolean;
46 private readonly grants: readonly Grant[];
47 /** The time, in seconds, that grant expiry is checked against. */
48 private readonly at: number;
49
50 constructor(actor: Actor, role: Role | null, grants: readonly Grant[] = [], personal = false, at = now()) {
51 this.actor = actor;
52 this.role = principal(actor) ? role : null;
53 this.grants = grants;
54 this.personal = personal;
55 this.at = at;
56 }
57
58 narrowed(grant: Grant): Policy {
59 return new Policy(this.actor, this.role, [...this.grants, grant], this.personal, this.at);
60 }
61
62 withRole(role: Role | null, personal = false): Policy {
63 return new Policy(this.actor, role, this.grants, personal, this.at);
64 }
65
66 get person(): Person | null {
67 return principal(this.actor);
68 }
69
70 get signedIn(): boolean {
71 return this.person !== null;
72 }
73
74 get member(): boolean {
75 return this.role !== null;
76 }
77
78 private allGrants(): Grant[] {
79 return grantsFor(this.actor, this.grants);
80 }
81
82 private repoGranted(name: string, access: RepoAccess): boolean {
83 return this.allGrants().every((grant) => grantAllowsRepo(grant, name, access, this.at));
84 }
85
86 private orgGranted(level: "member" | "admin"): boolean {
87 return this.allGrants().every((grant) => grantAllowsOrg(grant, level));
88 }
89
90 canSeeRepo(repo: RepoFacts): boolean {
91 const visible = repo.visibility === "public" || this.member;
92 return visible && this.repoGranted(repo.name, "read");
93 }
94
95 canWriteRepo(repo: RepoFacts): boolean {
96 return this.member && this.repoGranted(repo.name, "write");
97 }
98
99 canAdminRepo(repo: RepoFacts): boolean {
100 const person = this.person;
101 if (!person || !this.member || !this.repoGranted(repo.name, "write") || !this.orgGranted("member")) return false;
102 return this.role !== "member" || person.id === repo.creatorId;
103 }
104
105 canCreateRepos(): boolean {
106 return this.member && this.orgGranted("member");
107 }
108
109 canUseTasks(): boolean {
110 return this.member;
111 }
112
113 /** Tasks are a plain tracker with no tie to agent work: who can see a task's repository can open the task. */
114 canOpenTask(repo: string): boolean {
115 return this.member && this.repoGranted(repo, "read");
116 }
117
118 /** Agents limited to some branches see only the pull requests opened from those branches. */
119 canOpenPull(repo: string, pull: { readonly head: string }): boolean {
120 const same = (name: string) => name.toLowerCase() === repo.toLowerCase();
121 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))));
122 }
123
124 canManageLabels(): boolean {
125 return this.member && this.orgGranted("member");
126 }
127
128 canManageMembers(): boolean {
129 return this.member && !this.personal && this.role !== "member" && this.orgGranted("admin");
130 }
131
132 invitableRoles(): Role[] {
133 if (!this.canManageMembers()) return [];
134 return this.role === "owner" ? ROLES : ["admin", "member"];
135 }
136
137 /** CI secrets and settings change only by a person's hand: an agent could otherwise widen what its own runs get. */
138 canManageCiSecrets(): boolean {
139 return this.actor.kind === "human" && this.member && this.role !== "member" && this.orgGranted("admin");
140 }
141
142 canModerateComments(): boolean {
143 return this.member && this.role !== "member";
144 }
145}