| 1 | import { schema } from '@ghost/db'; |
| 2 | import { atLeast, organizationRoleOf |
| 3 | import { |
| 4 | and, |
| 5 | eq, |
| 6 | inArray, |
| 7 | isNotNull, |
| 8 | isNull, |
| 9 | or, |
| 10 | type SQL, |
| 11 | sql, |
| 12 | } from 'drizzle-orm'; |
| 13 | import type { PgColumn |
| 14 | |
| 15 | import { RepositoryNotFoundError } |
| 16 | import { |
| 17 | AuthenticationRequiredError, |
| 18 | RepositoryForbiddenError, |
| 19 | } from './repository-access.errors.js'; |
| 20 | |
| 21 | export type Repository |
| 22 | |
| 23 | /** The repository as the actor who was authorized sees it. */ |
| 24 | export type AuthorizedRepository |
| 25 | |
| 26 | export type Actor |
| 27 | |
| 28 | /** Each operation needs at least the role of the same name. */ |
| 29 | export type RepositoryOperation |
| 30 | |
| 31 | export type CollaboratorRole |
| 32 | (typeof schema |
| 33 | |
| 34 | /** Join condition for the actor's accepted collaboration on the repository whose id is `repositoryId`. Matches nothing for an anonymous actor. */ |
| 35 | export function acceptedCollaboration( |
| 36 | repositoryId: PgColumn |
| 37 | actor: Actor |
| 38 | ): SQL | |
| 39 | return and( |
| 40 | eq(schema.repositoryCollaborator |
| 41 | actor ? eq( |
| 42 | isNotNull(schema.repositoryCollaborator |
| 43 | ); |
| 44 | } |
| 45 | |
| 46 | /** The `owner` in `/owner/repo`: an organization's slug for its repositories, else the owning user's username. Needs the organization left-joined on the repository. */ |
| 47 | export function ownerNameOf( |
| 48 | owner: { |
| 49 | organization: { |
| 50 | ) { |
| 51 | return sql<string |
| 52 | } |
| 53 | |
| 54 | /** `owner/repo`, as URLs spell it. */ |
| 55 | export function repositoryFullNameOf( |
| 56 | owner: { |
| 57 | organization: { |
| 58 | repository: { |
| 59 | ) { |
| 60 | return sql<string |
| 61 | } |
| 62 | |
| 63 | /** Join condition for the actor's membership of the organization whose id is `organizationId`. Matches nothing for an anonymous actor or a user's own repository. */ |
| 64 | export function organizationMembership( |
| 65 | organizationId: PgColumn |
| 66 | actor: Actor |
| 67 | ): SQL | |
| 68 | return and( |
| 69 | eq(schema.member |
| 70 | actor ? eq( |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | /** The highest role granted to any team the actor is in, as a scalar subquery: an actor in several teams must not multiply the outer row. Enum order is ladder order, so `max` ranks. */ |
| 75 | export function teamRoleOf(repositoryId |
| 76 | if (!actor) |
| 77 | return sql<CollaboratorRole |
| 78 | } |
| 79 | |
| 80 | /** The organization's base permission for the repository's organization, as a scalar subquery; null for a user's repository or an organization that grants members nothing by default. */ |
| 81 | export function basePermissionOf(organizationId |
| 82 | return sql<CollaboratorRole |
| 83 | } |
| 84 | |
| 85 | /** Repositories the actor holds any role on: their own, shared with them, through a team, or through an organization that gives its members access. Needs `repository_collaborator` and `member` left-joined by `acceptedCollaboration` and `organizationMembership`. */ |
| 86 | export function grantedTo(actor |
| 87 | return or( |
| 88 | actor |
| 89 | ? and( |
| 90 | eq(schema.repository |
| 91 | isNull(schema.repository |
| 92 | ) |
| 93 | : undefined, |
| 94 | isNotNull(schema.repositoryCollaborator |
| 95 | isNotNull(teamRoleOf(schema |
| 96 | and( |
| 97 | isNotNull(schema.member |
| 98 | or( |
| 99 | inArray(schema.member |
| 100 | isNotNull(basePermissionOf(schema |
| 101 | ), |
| 102 | ), |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | /** Public, or granted to the actor; same joins as `grantedTo`. */ |
| 107 | export function readableBy(actor |
| 108 | return or(eq |
| 109 | } |
| 110 | |
| 111 | export type Grants |
| 112 | collaboratorRole: CollaboratorRole |
| 113 | /** The highest role any of the actor's teams holds on the repository. */ |
| 114 | teamRole: CollaboratorRole |
| 115 | /** `member.role` as Better Auth stores it: one role, or several joined by commas. */ |
| 116 | memberRole: string |
| 117 | /** The organization's floor for its members; see `basePermissionOf`. */ |
| 118 | basePermission: CollaboratorRole |
| 119 | }; |
| 120 | |
| 121 | /** A user's repository is theirs alone. An organization's is owned by the organization's owners and administered by its admins; its other members hold the base permission, raised by their teams and collaborations. The highest wins. */ |
| 122 | export function roleOf( |
| 123 | repository: Pick |
| 124 | { collaboratorRole, memberRole |
| 125 | actor: Actor |
| 126 | ): Role | |
| 127 | if (!actor) |
| 128 | if (!repository.organizationId |
| 129 | return repository.ownerId |
| 130 | } |
| 131 | |
| 132 | const membership = |
| 133 | const fromOrganization: |
| 134 | membership === 'owner' |
| 135 | ? 'owner' |
| 136 | : membership === ' |
| 137 | ? 'admin' |
| 138 | : membership === ' |
| 139 | ? basePermission |
| 140 | : null; |
| 141 | return highestOf([collaboratorRole |
| 142 | } |
| 143 | |
| 144 | function highestOf(roles: |
| 145 | return roles.reduce |
| 146 | (highest, role) |
| 147 | role === null || |
| 148 | null, |
| 149 | ); |
| 150 | } |
| 151 | |
| 152 | export function canAccess( |
| 153 | repository: Pick |
| 154 | role: Role |
| 155 | operation: RepositoryOperation |
| 156 | ) { |
| 157 | if (!repository) |
| 158 | if (operation === |
| 159 | return atLeast(role |
| 160 | } |
| 161 | |
| 162 | export function decideAccess( |
| 163 | repository: Repository |
| 164 | role: Role |
| 165 | actor: Actor |
| 166 | operation: RepositoryOperation |
| 167 | ): AuthorizedRepository { |
| 168 | if (repository && |
| 169 | return { ...repository |
| 170 | } |
| 171 | if (!actor) |
| 172 | // An unreadable repository must look absent; a readable one is safe to admit to. |
| 173 | if (canAccess(repository |
| 174 | throw new RepositoryNotFoundError() |
| 175 | } |