| 1 | import { Inject, Injectable } from '@nestjs/common'; |
| 2 | import { type Database, |
| 3 | import { and, eq |
| 4 | |
| 5 | import { DATABASE } |
| 6 | import { |
| 7 | acceptedCollaboration, |
| 8 | type Actor, |
| 9 | type AuthorizedRepository, |
| 10 | basePermissionOf, |
| 11 | canAccess, |
| 12 | decideAccess, |
| 13 | organizationMembership, |
| 14 | ownerNameOf, |
| 15 | type RepositoryOperation, |
| 16 | roleOf, |
| 17 | teamRoleOf, |
| 18 | } from '../../../lib/git/repository-access/repository-access.js'; |
| 19 | |
| 20 | @Injectable() |
| 21 | export class RepositoryAccessService |
| 22 | constructor(@Inject( |
| 23 | |
| 24 | async authorize({ |
| 25 | username, |
| 26 | repo, |
| 27 | actor, |
| 28 | operation, |
| 29 | }: { |
| 30 | username: string |
| 31 | repo: string |
| 32 | actor: Actor |
| 33 | operation: RepositoryOperation |
| 34 | }): Promise |
| 35 | let [row] |
| 36 | actor, |
| 37 | and( |
| 38 | eq(ownerNameOf(schema |
| 39 | eq(schema.repository |
| 40 | ), |
| 41 | ); |
| 42 | // A renamed or transferred repository is still reachable at its old name, as long as nothing took that name since. |
| 43 | if (!row) |
| 44 | const [redirect] |
| 45 | .select({ repositoryId: |
| 46 | .from(schema.repositoryRedirect |
| 47 | .where( |
| 48 | and( |
| 49 | eq( |
| 50 | sql`lower(${schema. |
| 51 | username.toLowerCase(), |
| 52 | ), |
| 53 | eq(schema.repositoryRedirect |
| 54 | ), |
| 55 | ); |
| 56 | if (redirect) { |
| 57 | [row] = await |
| 58 | actor, |
| 59 | eq(schema.repository |
| 60 | ); |
| 61 | } |
| 62 | } |
| 63 | return this.decide |
| 64 | } |
| 65 | |
| 66 | /** For a repository known by row id, such as the other side of a pull request. */ |
| 67 | async authorizeById({ |
| 68 | repositoryId, |
| 69 | actor, |
| 70 | operation, |
| 71 | }: { |
| 72 | repositoryId: string |
| 73 | actor: Actor |
| 74 | operation: RepositoryOperation |
| 75 | }): Promise |
| 76 | const [row] |
| 77 | actor, |
| 78 | eq(schema.repository |
| 79 | ); |
| 80 | return this.decide |
| 81 | } |
| 82 | |
| 83 | /** Whether `userId` may read the repository, for deciding who hears about it rather than for answering their request. */ |
| 84 | async canRead(repositoryId |
| 85 | const actor = |
| 86 | const [row] |
| 87 | actor, |
| 88 | eq(schema.repository |
| 89 | ); |
| 90 | return ( |
| 91 | row !== undefined && |
| 92 | canAccess(row.repository |
| 93 | ); |
| 94 | } |
| 95 | |
| 96 | private lookup(actor |
| 97 | return this.database |
| 98 | .select({ |
| 99 | repository: schema.repository |
| 100 | collaboratorRole: schema.repositoryCollaborator |
| 101 | memberRole: schema.member |
| 102 | teamRole: teamRoleOf(schema |
| 103 | basePermission: basePermissionOf(schema |
| 104 | }) |
| 105 | .from(schema.repository |
| 106 | .innerJoin(schema.user |
| 107 | .leftJoin( |
| 108 | schema.organization, |
| 109 | eq(schema.organization |
| 110 | ) |
| 111 | .leftJoin( |
| 112 | schema.repositoryCollaborator, |
| 113 | acceptedCollaboration(schema.repository |
| 114 | ) |
| 115 | .leftJoin( |
| 116 | schema.member, |
| 117 | organizationMembership(schema.repository |
| 118 | ) |
| 119 | .where(where) |
| 120 | .limit(1); |
| 121 | } |
| 122 | |
| 123 | private decide( |
| 124 | row: |
| 125 | | Awaited |
| 126 | | undefined, |
| 127 | actor: Actor |
| 128 | operation: RepositoryOperation |
| 129 | ) { |
| 130 | return decideAccess( |
| 131 | row?.repository ?? |
| 132 | row ? roleOf( |
| 133 | actor, |
| 134 | operation, |
| 135 | ); |
| 136 | } |
| 137 | } |