| 1 | import { and, eq, inArray } from 'drizzle-orm' |
| 2 | import type { ReadonlyHeaders |
| 3 | import { headers } |
| 4 | import { redirect } |
| 5 | import { authClient } |
| 6 | import { getSession } |
| 7 | import { roles } |
| 8 | import { db } |
| 9 | import { |
| 10 | projectClientAssignments, |
| 11 | projectMemberAssignments, |
| 12 | projects, |
| 13 | projectTeamAssignments, |
| 14 | teamMembers, |
| 15 | } from '@/server/db/schema' |
| 16 | |
| 17 | export const getCachedUserSession = |
| 18 | const session = |
| 19 | return session?.user |
| 20 | } |
| 21 | |
| 22 | export const getCachedOrganization = |
| 23 | slug: string, |
| 24 | headers: ReadonlyHeaders |
| 25 | ) => { |
| 26 | const session = |
| 27 | if (!session) |
| 28 | return null |
| 29 | } |
| 30 | const organization = |
| 31 | { |
| 32 | query: { organizationSlug: slug |
| 33 | }, |
| 34 | { headers } |
| 35 | ) |
| 36 | if (organization.data |
| 37 | return organization.data |
| 38 | } |
| 39 | return null |
| 40 | } |
| 41 | |
| 42 | export const getCachedActiveOrgMember = |
| 43 | headers: ReadonlyHeaders |
| 44 | _cacheBustKey?: string |
| 45 | ) => { |
| 46 | const member = |
| 47 | fetchOptions: { headers |
| 48 | }) |
| 49 | if (!member.data |
| 50 | const orgs = |
| 51 | fetchOptions: { headers |
| 52 | }) |
| 53 | await authClient.organization |
| 54 | organizationId: orgs.data |
| 55 | fetchOptions: { headers |
| 56 | }) |
| 57 | const member = |
| 58 | fetchOptions: { headers |
| 59 | }) |
| 60 | return member.data |
| 61 | // The above code is duplicated because calling the function recurseively inside cache throws: Chaining cycle detected for promise #<Promise> |
| 62 | } |
| 63 | return member.data |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Resolves and validates the full org context for a page. |
| 68 | * Redirects if session, org, or membership is missing. |
| 69 | * Returns session, organization, orgMember, and resolved role. |
| 70 | */ |
| 71 | export const resolveOrgContext = |
| 72 | const session = |
| 73 | |
| 74 | if (!session) |
| 75 | redirect(`/auth/sign-in?redirectTo=${encodeURIComponent |
| 76 | } |
| 77 | |
| 78 | const h = |
| 79 | |
| 80 | const organization = |
| 81 | |
| 82 | if (!organization) |
| 83 | redirect(`/error/404?message=${encodeURIComponent |
| 84 | } |
| 85 | |
| 86 | const orgMember = |
| 87 | |
| 88 | if (!orgMember) |
| 89 | redirect( |
| 90 | `/error/403?message=${encodeURIComponent('You are not a member of this workspace |
| 91 | ) |
| 92 | } |
| 93 | |
| 94 | const role = |
| 95 | |
| 96 | return { session, |
| 97 | } |
| 98 | /** |
| 99 | * Resolves org context AND verifies the user has access to the given project. |
| 100 | * Access is granted if: |
| 101 | * 1. User is an owner or admin of the organization, OR |
| 102 | * 2. User is individually assigned to the project (member or client), OR |
| 103 | * 3. User belongs to a team that is assigned to the project. |
| 104 | * Redirects if the project is not found or the user has no access. |
| 105 | */ |
| 106 | export const resolveProjectContext = |
| 107 | orgSlug: string, |
| 108 | projectSlug: string |
| 109 | ) => { |
| 110 | const orgContext = |
| 111 | const { organization, |
| 112 | |
| 113 | const [project] |
| 114 | .select() |
| 115 | .from(projects) |
| 116 | .where( |
| 117 | and( |
| 118 | eq(projects.organizationId |
| 119 | eq(projects.slug |
| 120 | ) |
| 121 | ) |
| 122 | |
| 123 | if (!project) |
| 124 | redirect(`/error/404?message=${encodeURIComponent |
| 125 | } |
| 126 | |
| 127 | // Owners and admins have access to all projects in the org |
| 128 | if (orgMember.role |
| 129 | return { ...orgContext |
| 130 | } |
| 131 | |
| 132 | // Check direct member assignment |
| 133 | const [memberAssignment] |
| 134 | .select({ id: |
| 135 | .from(projectMemberAssignments) |
| 136 | .where( |
| 137 | and( |
| 138 | eq(projectMemberAssignments.projectId |
| 139 | eq(projectMemberAssignments.memberId |
| 140 | ) |
| 141 | ) |
| 142 | |
| 143 | if (memberAssignment) { |
| 144 | return { ...orgContext |
| 145 | } |
| 146 | |
| 147 | // Check direct client assignment |
| 148 | const [clientAssignment] |
| 149 | .select({ id: |
| 150 | .from(projectClientAssignments) |
| 151 | .where( |
| 152 | and( |
| 153 | eq(projectClientAssignments.projectId |
| 154 | eq(projectClientAssignments.memberId |
| 155 | ) |
| 156 | ) |
| 157 | |
| 158 | if (clientAssignment) { |
| 159 | return { ...orgContext |
| 160 | } |
| 161 | |
| 162 | // Check team-based assignment: user's teams → project team assignments |
| 163 | const userTeams = |
| 164 | .select({ teamId: |
| 165 | .from(teamMembers) |
| 166 | .where(eq(teamMembers |
| 167 | |
| 168 | if (userTeams.length |
| 169 | const teamIds = |
| 170 | const [teamAssignment] |
| 171 | .select({ id: |
| 172 | .from(projectTeamAssignments) |
| 173 | .where( |
| 174 | and( |
| 175 | eq(projectTeamAssignments.projectId |
| 176 | inArray(projectTeamAssignments.teamId |
| 177 | ) |
| 178 | ) |
| 179 | |
| 180 | if (teamAssignment) { |
| 181 | return { ...orgContext |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | redirect( |
| 186 | `/error/403?message=${encodeURIComponent('You do not have access to this project |
| 187 | ) |
| 188 | } |
| 189 | |
| 190 | type Role |
| 191 | type AuthorizeArg |
| 192 | |
| 193 | /** |
| 194 | * Redirects to the 403 error page if the role does not satisfy the given |
| 195 | * permissions. Use at the top of `/[org]/*` and `/[org]/[project]/*` pages |
| 196 | * after resolving the org/project context. |
| 197 | */ |
| 198 | export const requirePermission = |
| 199 | role: Role |
| 200 | permissions: AuthorizeArg |
| 201 | message = 'You do not have permission to view this page' |
| 202 | ) => { |
| 203 | if (!role.authorize |
| 204 | redirect(`/error/403?message=${encodeURIComponent |
| 205 | } |
| 206 | } |