Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

refactor: rbac access checks (#1180)

Marc Klingen28c035da7696
diff --git a/src/components/layouts/layout.tsx b/src/components/layouts/layout.tsx
index 0067347e75..85f7caeb41 100644
--- a/src/components/layouts/layout.tsx
+++ b/src/components/layouts/layout.tsx
@@ -77,7 +77,12 @@ export default function Layout(props: PropsWithChildren) {
// RBAC
if (
route.rbacScope !== undefined &&
- (!projectId || !hasAccess({ projectId, scope: route.rbacScope, session }))
+ (!projectId ||
+ !hasAccess({
+ projectId,
+ scope: route.rbacScope,
+ session: session.data,
+ }))
)
return null;
diff --git a/src/features/rbac/utils/checkAccess.ts b/src/features/rbac/utils/checkAccess.ts
index e171f4beaf..500743abcf 100644
--- a/src/features/rbac/utils/checkAccess.ts
+++ b/src/features/rbac/utils/checkAccess.ts
@@ -5,20 +5,24 @@ import {
import { type MembershipRole } from "@prisma/client";
import { TRPCError } from "@trpc/server";
import { type Session } from "next-auth";
-import { useSession, type SessionContextValue } from "next-auth/react";
+import { useSession } from "next-auth/react";
type HasAccessParams =
| {
role: MembershipRole;
scope: Scope;
+ admin?: boolean; // prop user.admin
}
| {
- session: SessionContextValue | Session;
+ session: null | Session;
projectId: string;
scope: Scope;
};
-// For use in TRPC routes
+/**
+ * Check if user has access to the given scope, for use in TRPC resolvers
+ * @throws TRPCError("UNAUTHORIZED") if user does not have access
+ */
export const throwIfNoAccess = (p: HasAccessParams) => {
if (!hasAccess(p))
throw new TRPCError({
@@ -28,27 +32,26 @@ export const throwIfNoAccess = (p: HasAccessParams) => {
});
};
-// For use in UI components as react hook
+/**
+ * React hook to check if user has access to the given scope
+ * @returns true if user has access, false otherwise or while loading
+ */
export const useHasAccess = (p: { projectId: string; scope: Scope }) => {
const session = useSession();
- return hasAccess({ session, ...p });
+ return hasAccess({ session: session.data, ...p });
};
// For use in UI components as function, if session is already available
export function hasAccess(p: HasAccessParams): boolean {
- const role: MembershipRole | undefined =
+ const isAdmin = "role" in p ? p.admin : p.session?.user?.admin;
+ if (isAdmin && p.scope.endsWith(":read")) return true;
+
+ const projectRole: MembershipRole | undefined =
"role" in p
- ? // MembershipRole
- p.role
- : "data" in p.session
- ? // SessionContextValue
- p.session.data?.user?.projects.find(
- (project) => project.id === p.projectId,
- )?.role
- : // Session
- p.session.user?.projects.find((project) => project.id === p.projectId)
- ?.role;
- if (role === undefined) return false;
+ ? p.role
+ : p.session?.user?.projects.find((project) => project.id === p.projectId)
+ ?.role;
+ if (projectRole === undefined) return false;
- return roleAccessRights[role].includes(p.scope);
+ return roleAccessRights[projectRole].includes(p.scope);
}