feat: pbac (#21296)

* inital pbac setup

* fix tests

* docs intro

* more docs

* add comments in docs

* revert pr_todo

* update to use action + custom as defaults

* use enums

* move to change user role + multi check

* add beforeEach import

* docs: add PERMISSIONS.md documenting team/organization role usages

Co-Authored-By: [email protected] <[email protected]>

* docs: add permission string alternatives for helper functions

Co-Authored-By: [email protected] <[email protected]>

* feat: enhance permission registry and default role permissions

Co-Authored-By: [email protected] <[email protected]>

* add routers

* add hooks + rsc. Also adjsuted test to use prismock

* make hooks type safe + add some tests

* add a permission context - trpc router to populate context

* update docs

* explore global and default roles member,admin,owner

* fix tests

* use kysley and abstract uril

* tidy up

* update trpc to use pbac route in endpoint + distinct on teamId

* make transform util

* remove role service dependancy from permission check class

* checkTeamId fall back to orgId if team is parent

* todos

* remove manual migrations to launch softly

* address feedback

* move permission checks to a repo

* move role service database calls to repo. improve mocks

* update tests to have describe import

* fix types

* remove router that is not used to pass build

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
authored and GitHub committed 2025-05-20 15:22:52 +01:00
1 parent 1740e34c79
commit b9fadf82a7
21 files changed
+2249

No files matched your search

@@ -0,0 +1,56 @@
-- CreateEnum
CREATE TYPE "RoleType" AS ENUM ('SYSTEM', 'CUSTOM');
-- AlterTable
ALTER TABLE "Membership" ADD COLUMN "customRoleId" TEXT;
-- CreateTable
CREATE TABLE "Role" (
"id" TEXT NOT NULL,
"name" TEXT NOT NULL,
"description" TEXT,
"teamId" INTEGER,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"type" "RoleType" NOT NULL DEFAULT 'CUSTOM',
CONSTRAINT "Role_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "RolePermission" (
"id" TEXT NOT NULL,
"roleId" TEXT NOT NULL,
"resource" TEXT NOT NULL,
"action" TEXT NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "RolePermission_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE INDEX "Role_teamId_idx" ON "Role"("teamId");
-- CreateIndex
CREATE UNIQUE INDEX "Role_name_teamId_key" ON "Role"("name", "teamId");
-- CreateIndex
CREATE INDEX "RolePermission_roleId_idx" ON "RolePermission"("roleId");
-- CreateIndex
CREATE INDEX "RolePermission_action_idx" ON "RolePermission"("action");
-- CreateIndex
CREATE UNIQUE INDEX "RolePermission_roleId_resource_action_key" ON "RolePermission"("roleId", "resource", "action");
-- CreateIndex
CREATE INDEX "Membership_customRoleId_idx" ON "Membership"("customRoleId");
-- AddForeignKey
ALTER TABLE "Membership" ADD CONSTRAINT "Membership_customRoleId_fkey" FOREIGN KEY ("customRoleId") REFERENCES "Role"("id") ON DELETE SET NULL ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "Role" ADD CONSTRAINT "Role_teamId_fkey" FOREIGN KEY ("teamId") REFERENCES "Team"("id") ON DELETE CASCADE ON UPDATE CASCADE;
-- AddForeignKey
ALTER TABLE "RolePermission" ADD CONSTRAINT "RolePermission_roleId_fkey" FOREIGN KEY ("roleId") REFERENCES "Role"("id") ON DELETE CASCADE ON UPDATE CASCADE;
+39
View File
@@ -501,6 +501,7 @@ model Team {
// Available for Organization only
delegationCredentials DelegationCredential[]
domainWideDelegations DomainWideDelegation[]
roles Role[] // Added for Role relation
features TeamFeatures[]
@@ -585,6 +586,8 @@ model Membership {
userId Int
accepted Boolean @default(false)
role MembershipRole
customRoleId String?
customRole Role? @relation(fields: [customRoleId], references: [id])
team Team @relation(fields: [teamId], references: [id], onDelete: Cascade)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
disableImpersonation Boolean @default(false)
@@ -597,6 +600,7 @@ model Membership {
@@index([userId])
@@index([accepted])
@@index([role])
@@index([customRoleId])
}
model VerificationToken {
@@ -2265,3 +2269,38 @@ model WorkflowOptOutContact {
@@unique([type, value])
}
enum RoleType {
SYSTEM
CUSTOM
}
model Role {
id String @id @default(cuid())
name String
description String?
teamId Int? // null for global roles
team Team? @relation(fields: [teamId], references: [id], onDelete: Cascade)
permissions RolePermission[]
memberships Membership[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
type RoleType @default(CUSTOM)
@@unique([name, teamId])
@@index([teamId])
}
model RolePermission {
id String @id @default(cuid())
roleId String
role Role @relation(fields: [roleId], references: [id], onDelete: Cascade)
resource String
action String
createdAt DateTime @default(now())
@@unique([roleId, resource, action])
@@index([roleId])
// TODO: come back to this with indexs.
@@index([action])
}