-
Notifications
You must be signed in to change notification settings - Fork 3.2k
feat(enterprise): permission groups, access control #2736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+13,669
−478
Merged
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
a6e8f4d
feat(permission-groups): integration/model access controls for enterp…
icecrasher321 0cb6de4
feat: enterprise gating for BYOK, SSO, credential sets with org admin…
icecrasher321 7406ccb
merge: staging with Access Control as enterprise-only feature
icecrasher321 007c68a
execution time enforcement of mcp and custom tools
icecrasher321 adf088a
add admin routes to cleanup permission group data
icecrasher321 22b8528
fix not being on enterprise checks
icecrasher321 0bf493b
separate out orgs from billing system
icecrasher321 e2366b1
update the docs
icecrasher321 87f0f3f
add custom tool blockers based on perm configs
icecrasher321 79befdf
add migrations
icecrasher321 aba68ba
fix
icecrasher321 2516396
address greptile comments
icecrasher321 101eace
regen migrations
icecrasher321 eba43d2
fix default model picking based on user config
icecrasher321 22e7506
cleaned up UI
icecrasher321 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
apps/sim/app/api/permission-groups/[id]/members/bulk/route.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| import { db } from '@sim/db' | ||
| import { member, permissionGroup, permissionGroupMember } from '@sim/db/schema' | ||
| import { createLogger } from '@sim/logger' | ||
| import { and, eq, inArray, notInArray } from 'drizzle-orm' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { z } from 'zod' | ||
| import { getSession } from '@/lib/auth' | ||
| import { hasAccessControlAccess } from '@/lib/billing' | ||
|
|
||
| const logger = createLogger('PermissionGroupBulkMembers') | ||
|
|
||
| async function getPermissionGroupWithAccess(groupId: string, userId: string) { | ||
| const [group] = await db | ||
| .select({ | ||
| id: permissionGroup.id, | ||
| organizationId: permissionGroup.organizationId, | ||
| }) | ||
| .from(permissionGroup) | ||
| .where(eq(permissionGroup.id, groupId)) | ||
| .limit(1) | ||
|
|
||
| if (!group) return null | ||
|
|
||
| const [membership] = await db | ||
| .select({ role: member.role }) | ||
| .from(member) | ||
| .where(and(eq(member.userId, userId), eq(member.organizationId, group.organizationId))) | ||
| .limit(1) | ||
|
|
||
| if (!membership) return null | ||
|
|
||
| return { group, role: membership.role } | ||
| } | ||
|
|
||
| const bulkAddSchema = z.object({ | ||
| userIds: z.array(z.string()).optional(), | ||
| addAllOrgMembers: z.boolean().optional(), | ||
| }) | ||
|
|
||
| export async function POST(req: NextRequest, { params }: { params: Promise<{ id: string }> }) { | ||
| const session = await getSession() | ||
|
|
||
| if (!session?.user?.id) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const { id } = await params | ||
|
|
||
| try { | ||
| const hasAccess = await hasAccessControlAccess(session.user.id) | ||
| if (!hasAccess) { | ||
| return NextResponse.json( | ||
| { error: 'Access Control is an Enterprise feature' }, | ||
| { status: 403 } | ||
| ) | ||
| } | ||
|
|
||
| const result = await getPermissionGroupWithAccess(id, session.user.id) | ||
|
|
||
| if (!result) { | ||
| return NextResponse.json({ error: 'Permission group not found' }, { status: 404 }) | ||
| } | ||
|
|
||
| if (result.role !== 'admin' && result.role !== 'owner') { | ||
| return NextResponse.json({ error: 'Admin or owner permissions required' }, { status: 403 }) | ||
| } | ||
|
|
||
| const body = await req.json() | ||
| const { userIds, addAllOrgMembers } = bulkAddSchema.parse(body) | ||
|
|
||
| let targetUserIds: string[] = [] | ||
|
|
||
| if (addAllOrgMembers) { | ||
| const orgMembers = await db | ||
| .select({ userId: member.userId }) | ||
| .from(member) | ||
| .where(eq(member.organizationId, result.group.organizationId)) | ||
|
|
||
| targetUserIds = orgMembers.map((m) => m.userId) | ||
| } else if (userIds && userIds.length > 0) { | ||
| const validMembers = await db | ||
| .select({ userId: member.userId }) | ||
| .from(member) | ||
| .where( | ||
| and( | ||
| eq(member.organizationId, result.group.organizationId), | ||
| inArray(member.userId, userIds) | ||
| ) | ||
| ) | ||
|
|
||
| targetUserIds = validMembers.map((m) => m.userId) | ||
| } | ||
|
|
||
| if (targetUserIds.length === 0) { | ||
| return NextResponse.json({ added: 0, moved: 0 }) | ||
| } | ||
|
|
||
| const existingInThisGroup = await db | ||
| .select({ userId: permissionGroupMember.userId }) | ||
| .from(permissionGroupMember) | ||
| .where( | ||
| and( | ||
| eq(permissionGroupMember.permissionGroupId, id), | ||
| inArray(permissionGroupMember.userId, targetUserIds) | ||
| ) | ||
| ) | ||
|
|
||
| const existingUserIds = new Set(existingInThisGroup.map((m) => m.userId)) | ||
| const usersToAdd = targetUserIds.filter((uid) => !existingUserIds.has(uid)) | ||
|
|
||
| if (usersToAdd.length === 0) { | ||
| return NextResponse.json({ added: 0, moved: 0 }) | ||
| } | ||
|
|
||
| const otherGroupMemberships = await db | ||
| .select({ | ||
| id: permissionGroupMember.id, | ||
| userId: permissionGroupMember.userId, | ||
| }) | ||
| .from(permissionGroupMember) | ||
| .innerJoin(permissionGroup, eq(permissionGroupMember.permissionGroupId, permissionGroup.id)) | ||
| .where( | ||
| and( | ||
| eq(permissionGroup.organizationId, result.group.organizationId), | ||
| inArray(permissionGroupMember.userId, usersToAdd), | ||
| notInArray(permissionGroupMember.permissionGroupId, [id]) | ||
| ) | ||
| ) | ||
|
|
||
| const movedCount = otherGroupMemberships.length | ||
|
|
||
| if (otherGroupMemberships.length > 0) { | ||
| const idsToDelete = otherGroupMemberships.map((m) => m.id) | ||
| await db.delete(permissionGroupMember).where(inArray(permissionGroupMember.id, idsToDelete)) | ||
| } | ||
|
|
||
| const newMembers = usersToAdd.map((userId) => ({ | ||
| id: crypto.randomUUID(), | ||
| permissionGroupId: id, | ||
| userId, | ||
| assignedBy: session.user.id, | ||
| assignedAt: new Date(), | ||
| })) | ||
|
|
||
| await db.insert(permissionGroupMember).values(newMembers) | ||
|
|
||
| logger.info('Bulk added members to permission group', { | ||
| permissionGroupId: id, | ||
| addedCount: usersToAdd.length, | ||
| movedCount, | ||
| assignedBy: session.user.id, | ||
| }) | ||
|
|
||
| return NextResponse.json({ added: usersToAdd.length, moved: movedCount }) | ||
| } catch (error) { | ||
| if (error instanceof z.ZodError) { | ||
| return NextResponse.json({ error: error.errors[0].message }, { status: 400 }) | ||
| } | ||
| logger.error('Error bulk adding members to permission group', error) | ||
| return NextResponse.json({ error: 'Failed to add members' }, { status: 500 }) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.