Skip to content

Commit

Permalink
feature(vaults): Introduce ACLType (#6515)
Browse files Browse the repository at this point in the history
  • Loading branch information
tdraier authored Jul 25, 2024
1 parent 360ece4 commit 6d2ab76
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
27 changes: 27 additions & 0 deletions front/lib/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { getSession as getAuth0Session } from "@auth0/nextjs-auth0";
import type {
ACLType,
GroupType,
LightWorkspaceType,
RoleType,
SupportedPermissionType,
UserType,
WhitelistableFeature,
WorkspaceType,
Expand All @@ -12,6 +15,7 @@ import type { Result } from "@dust-tt/types";
import type { APIErrorWithStatusCode } from "@dust-tt/types";
import {
Err,
groupHasPermission,
isAdmin,
isBuilder,
isDevelopment,
Expand All @@ -36,6 +40,7 @@ import { FREE_NO_PLAN_DATA } from "@app/lib/plans/free_plans";
import { isUpgraded } from "@app/lib/plans/plan_codes";
import { renderSubscriptionFromModels } from "@app/lib/plans/subscription";
import { getTrialVersionForPlan, isTrial } from "@app/lib/plans/trial";
import type { GroupResource } from "@app/lib/resources/group_resource";
import type { KeyAuthType } from "@app/lib/resources/key_resource";
import { KeyResource } from "@app/lib/resources/key_resource";
import { MembershipResource } from "@app/lib/resources/membership_resource";
Expand All @@ -61,6 +66,7 @@ export class Authenticator {
_role: RoleType;
_subscription: SubscriptionType | null;
_user: UserResource | null;
_groups: GroupResource[];
_workspace: Workspace | null;

// Should only be called from the static methods below.
Expand All @@ -81,6 +87,8 @@ export class Authenticator {
}) {
this._workspace = workspace || null;
this._user = user || null;
// TODO: Load groups memberships
this._groups = [];
this._role = role;
this._subscription = subscription || null;
this._flags = flags;
Expand Down Expand Up @@ -546,6 +554,25 @@ export class Authenticator {

return isDustInternal && isDustSuperUser;
}

groups(): GroupType[] {
return this._groups ? this._groups.map((group) => group.toJSON()) : [];
}

hasPermission(acls: ACLType[], permission: SupportedPermissionType): boolean {
// Does the user belongs to a group which has the required permission on all ACLs ?
return this.groups().some((group) =>
acls.every((acl) => groupHasPermission(acl, permission, group.id))
);
}

canRead(acls: ACLType[]): boolean {
return this.hasPermission(acls, "read");
}

canWrite(acls: ACLType[]): boolean {
return this.hasPermission(acls, "write");
}
}

/**
Expand Down
29 changes: 29 additions & 0 deletions types/src/front/acl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ModelId } from "../shared/model_id";

// Supported permissions
export const SUPPORTED_PERMISSIONS = ["read", "write"] as const;

export type SupportedPermissionType = (typeof SUPPORTED_PERMISSIONS)[number];

// Access Control Entry
export type ACEType = {
groupId: ModelId;
permissions: SupportedPermissionType[];
};

// Access Control List
export type ACLType = {
aclEntries: Array<ACEType>;
};

export function groupHasPermission(
acl: ACLType,
permission: SupportedPermissionType,
groupId: ModelId
): boolean {
const entry = acl.aclEntries.find((ace) => ace.groupId === groupId);
if (entry) {
return entry.permissions.includes(permission);
}
return false;
}
8 changes: 8 additions & 0 deletions types/src/front/groups.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ModelId } from "../shared/model_id";

/**
* system group:
* Accessible by no-one other than our system API keys.
Expand Down Expand Up @@ -25,3 +27,9 @@ export function isSystemGroupType(value: SupportedGroupType): boolean {
export function isGlobalGroupType(value: SupportedGroupType): boolean {
return value === "global";
}

export type GroupType = {
id: ModelId;
type: SupportedGroupType;
name: string;
};
1 change: 1 addition & 0 deletions types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export * from "./connectors/notion";
export * from "./connectors/slack";
export * from "./connectors/webcrawler";
export * from "./core/data_source";
export * from "./front/acl";
export * from "./front/api_handlers/internal/agent_configuration";
export * from "./front/api_handlers/internal/assistant";
export * from "./front/api_handlers/public/assistant";
Expand Down

0 comments on commit 6d2ab76

Please sign in to comment.