| 1 | import { data } from "react-router"; |
| 2 | |
| 3 | import { |
| 4 | type Capability, |
| 5 | type Repo, |
| 6 | type Result, |
| 7 | type Viewer, |
| 8 | abilities, |
| 9 | can, |
| 10 | granted, |
| 11 | httpStatus, |
| 12 | needs, |
| 13 | permission, |
| 14 | } from "@g1t/contracts"; |
| 15 | |
| 16 | import type { ViewerAccess } from "./access"; |
| 17 | import { projects, repos, work } from "./services.server"; |
| 18 | import { getViewer } from "./session.server"; |
| 19 | |
| 20 | type Context = Parameters<typeof getViewer>[0]; |
| 21 | type RepoParams = { owner?: string; repo?: string }; |
| 22 | |
| 23 | /** |
| 24 | * One count of a project's open issues and pull requests per request: the |
| 25 | * sidebar (root) and the project's tabs (its layout) both show them. |
| 26 | */ |
| 27 | const tallies = new WeakMap<object, Map<string, ReturnType<typeof work.counts>>>(); |
| 28 | |
| 29 | export function countsFor(context: Context, params: RepoParams): ReturnType<typeof work.counts> { |
| 30 | const key = `${params.owner}/${params.repo}`.toLowerCase(); |
| 31 | let seen = tallies.get(context); |
| 32 | if (!seen) tallies.set(context, (seen = new Map())); |
| 33 | let found = seen.get(key); |
| 34 | if (!found) { |
| 35 | found = work.counts({ namespace: params.owner ?? "", name: params.repo ?? "" }, getViewer(context)); |
| 36 | seen.set(key, found); |
| 37 | } |
| 38 | return found; |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * The repositories of these ids the viewer can read: one call for all of |
| 43 | * them, then one each for any it leaves out (forks). |
| 44 | */ |
| 45 | export async function readableRepos(ids: string[], viewer: Viewer): Promise<Repo[]> { |
| 46 | if (ids.length === 0) return []; |
| 47 | const found = await repos.readable(ids, viewer).catch(() => [] as Repo[]); |
| 48 | const seen = new Set(found.map((repo) => repo.id)); |
| 49 | const rest = await Promise.all(ids.filter((id) => !seen.has(id)).map((id) => repos.getById(id, viewer).catch(() => null))); |
| 50 | return [...found, ...rest.flatMap((repo) => (repo?.ok ? [repo.value] : []))]; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * One lookup of a repository per request: the repository's layout and the |
| 55 | * page under it load at the same time and both need it. |
| 56 | */ |
| 57 | const lookups = new WeakMap<object, Map<string, Promise<Result<Repo>>>>(); |
| 58 | |
| 59 | export function repoFor(context: Context, params: RepoParams): Promise<Result<Repo>> { |
| 60 | const key = `${params.owner}/${params.repo}`.toLowerCase(); |
| 61 | let seen = lookups.get(context); |
| 62 | if (!seen) lookups.set(context, (seen = new Map())); |
| 63 | let found = seen.get(key); |
| 64 | if (!found) { |
| 65 | found = repos.get({ namespace: params.owner ?? "", name: params.repo ?? "" }, getViewer(context)); |
| 66 | seen.set(key, found); |
| 67 | } |
| 68 | return found; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * One lookup of a project per request: the project's layout and its |
| 73 | * overview load at the same time and both need it. |
| 74 | */ |
| 75 | const projectLookups = new WeakMap<object, Map<string, ReturnType<typeof projects.get>>>(); |
| 76 | |
| 77 | export function projectFor(context: Context, params: RepoParams): ReturnType<typeof projects.get> { |
| 78 | const key = `${params.owner}/${params.repo}`.toLowerCase(); |
| 79 | let seen = projectLookups.get(context); |
| 80 | if (!seen) projectLookups.set(context, (seen = new Map())); |
| 81 | let found = seen.get(key); |
| 82 | if (!found) { |
| 83 | found = projects.get(params.owner ?? "", params.repo ?? "", getViewer(context)); |
| 84 | seen.set(key, found); |
| 85 | } |
| 86 | return found; |
| 87 | } |
| 88 | |
| 89 | /** The viewer's role on a repository and what it lets them do. */ |
| 90 | export function accessFor(viewer: Viewer, repo: Repo): ViewerAccess { |
| 91 | return { |
| 92 | role: permission(viewer, repo), |
| 93 | // A role of their own, not only because the repository is public. |
| 94 | insider: viewer ? granted(viewer, repo) != null : false, |
| 95 | can: abilities(viewer, repo), |
| 96 | }; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * The repository and the viewer's access to it, refusing with a 404 when |
| 101 | * they cannot read it and a 403 that says which role is needed when they |
| 102 | * can read it but not do `capability`. |
| 103 | */ |
| 104 | export async function requireRepo(context: Context, params: RepoParams, capability: Capability = "read") { |
| 105 | const viewer = getViewer(context); |
| 106 | const found = await repoFor(context, params); |
| 107 | if (!found.ok) { |
| 108 | if (found.error.code === "not_found" || found.error.code === "forbidden") throw data(null, { status: 404 }); |
| 109 | throw data(found.error.message, { status: httpStatus(found.error) }); |
| 110 | } |
| 111 | const access = accessFor(viewer, found.value); |
| 112 | if (!access.can.read) throw data(null, { status: 404 }); |
| 113 | if (!access.can[capability]) throw data(needs(capability), { status: 403 }); |
| 114 | return { viewer, repo: found.value, access }; |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Pages only people with a role of their own see (plans, memory, |
| 119 | * deployments, security, settings): a 404 for anyone else, as before. |
| 120 | */ |
| 121 | export async function requireInsider(context: Context, params: RepoParams, capability: Capability = "read") { |
| 122 | const found = await requireRepo(context, params, "read"); |
| 123 | if (!found.access.insider) throw data(null, { status: 404 }); |
| 124 | if (!found.access.can[capability]) throw data(needs(capability), { status: 403 }); |
| 125 | return found; |
| 126 | } |
| 127 | |
| 128 | /** The viewer's access, or none when the repository cannot be read. */ |
| 129 | export async function accessTo(context: Context, params: RepoParams): Promise<ViewerAccess> { |
| 130 | const found = await repoFor(context, params); |
| 131 | return found.ok ? accessFor(getViewer(context), found.value) : { role: null, insider: false, can: abilities(null, { id: "", namespace: "", isPrivate: true }) }; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * For an action: why the viewer may not do `capability` here, or null when |
| 136 | * they may. Not cached, since the action may change the repository. |
| 137 | */ |
| 138 | export async function refusal(context: Context, params: RepoParams, capability: Capability): Promise<string | null> { |
| 139 | const viewer = getViewer(context); |
| 140 | const found = await repos.get({ namespace: params.owner ?? "", name: params.repo ?? "" }, viewer); |
| 141 | if (!found.ok) return found.error.message; |
| 142 | return can(viewer, found.value, capability) ? null : needs(capability); |
| 143 | } |
| 144 | |
| 145 | /** For an action whose page only people with `capability` see: a 403 that says why, otherwise nothing. */ |
| 146 | export async function requireCapability(context: Context, params: RepoParams, capability: Capability): Promise<void> { |
| 147 | const refused = await refusal(context, params, capability); |
| 148 | if (refused) throw data(refused, { status: 403 }); |
| 149 | } |