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