| 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 | /** 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. */ |
| 55 | export function organizationMembership( |
| 56 | organizationId: PgColumn |
| 57 | actor: Actor |
| 58 | ): SQL | |
| 59 | return and( |
| 60 | eq(schema.member |
| 61 | actor ? eq( |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | /** 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. */ |
| 66 | export function teamRoleOf(repositoryId |
| 67 | if (!actor) |
| 68 | return sql<CollaboratorRole |
| 69 | } |
| 70 | |
| 71 | /** 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. */ |
| 72 | export function basePermissionOf(organizationId |
| 73 | return sql<CollaboratorRole |
| 74 | } |
| 75 | |
| 76 | /** 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`. */ |
| 77 | export function grantedTo(actor |
| 78 | return or( |
| 79 | actor |
| 80 | ? and( |
| 81 | eq(schema.repository |
| 82 | isNull(schema.repository |
| 83 | ) |
| 84 | : undefined, |
| 85 | isNotNull(schema.repositoryCollaborator |
| 86 | isNotNull(teamRoleOf(schema |
| 87 | and( |
| 88 | isNotNull(schema.member |
| 89 | or( |
| 90 | inArray(schema.member |
| 91 | isNotNull(basePermissionOf(schema |
| 92 | ), |
| 93 | ), |
| 94 | ); |
| 95 | } |
| 96 | |
| 97 | /** Public, or granted to the actor; same joins as `grantedTo`. */ |
| 98 | export function readableBy(actor |
| 99 | return or(eq |
| 100 | } |
| 101 | |
| 102 | export type Grants |
| 103 | collaboratorRole: CollaboratorRole |
| 104 | /** The highest role any of the actor's teams holds on the repository. */ |
| 105 | teamRole: CollaboratorRole |
| 106 | /** `member.role` as Better Auth stores it: one role, or several joined by commas. */ |
| 107 | memberRole: string |
| 108 | /** The organization's floor for its members; see `basePermissionOf`. */ |
| 109 | basePermission: CollaboratorRole |
| 110 | }; |
| 111 | |
| 112 | /** 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. */ |
| 113 | export function roleOf( |
| 114 | repository: Pick |
| 115 | { collaboratorRole, memberRole |
| 116 | actor: Actor |
| 117 | ): Role | |
| 118 | if (!actor) |
| 119 | if (!repository.organizationId |
| 120 | return repository.ownerId |
| 121 | } |
| 122 | |
| 123 | const membership = |
| 124 | const fromOrganization: |
| 125 | membership === 'owner' |
| 126 | ? 'owner' |
| 127 | : membership === ' |
| 128 | ? 'admin' |
| 129 | : membership === ' |
| 130 | ? basePermission |
| 131 | : null; |
| 132 | return highestOf([collaboratorRole |
| 133 | } |
| 134 | |
| 135 | function highestOf(roles: |
| 136 | return roles.reduce |
| 137 | (highest, role) |
| 138 | role === null || |
| 139 | null, |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | export function canAccess( |
| 144 | repository: Pick |
| 145 | role: Role |
| 146 | operation: RepositoryOperation |
| 147 | ) { |
| 148 | if (!repository) |
| 149 | if (operation === |
| 150 | return atLeast(role |
| 151 | } |
| 152 | |
| 153 | export function decideAccess( |
| 154 | repository: Repository |
| 155 | role: Role |
| 156 | actor: Actor |
| 157 | operation: RepositoryOperation |
| 158 | ): AuthorizedRepository { |
| 159 | if (repository && |
| 160 | return { ...repository |
| 161 | } |
| 162 | if (!actor) |
| 163 | // An unreadable repository must look absent; a readable one is safe to admit to. |
| 164 | if (canAccess(repository |
| 165 | throw new RepositoryNotFoundError() |
| 166 | } |