Download tasks/linkedin/solution/site/implementation/clone/lib/tenant.ts from TIGER-Lab/WebsiteBench: direct link, hf CLI and curl.
- Browser
- Download file 1.25 kB
-
https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/solution/site/implementation/clone/lib/tenant.ts
- Command line
-
hf download hf://datasets/TIGER-Lab/WebsiteBench/tasks/linkedin/solution/site/implementation/clone/lib/tenant.ts
-
curl -L -o tenant.ts https://huggingface.co/datasets/TIGER-Lab/WebsiteBench/resolve/main/tasks/linkedin/solution/site/implementation/clone/lib/tenant.ts
1.25 kB
| import { and, eq } from "drizzle-orm"; | |
| import { getDb } from "@/db"; | |
| import { memberships, organizations, users } from "@/db/schema"; | |
| import { ApiError } from "./api"; | |
| import { hasPermission, type Permission, type Role } from "./rbac"; | |
| export async function getTenantForUser(userId: string, requestedTenantId?: string | null) { | |
| const rows = await getDb() | |
| .select({ membership: memberships, organization: organizations }) | |
| .from(memberships) | |
| .innerJoin(organizations, eq(organizations.id, memberships.organizationId)) | |
| .where(requestedTenantId | |
| ? and(eq(memberships.userId, userId), eq(memberships.organizationId, requestedTenantId)) | |
| : eq(memberships.userId, userId)) | |
| .limit(1); | |
| return rows[0] ?? null; | |
| } | |
| export async function requireTenant(user: typeof users.$inferSelect, requestedTenantId?: string | null) { | |
| const context = await getTenantForUser(user.id, requestedTenantId); | |
| if (!context) throw new ApiError(403, "Tenant membership is required.", "tenant_required"); | |
| return context; | |
| } | |
| export function requireTenantPermission(role: Role, permission: Permission) { | |
| if (!hasPermission(role, permission)) throw new ApiError(403, "You do not have permission to perform this action.", "permission_denied"); | |
| } | |