feat: api v2 pbac support (#24402)

* feat: Pbac decorator and guard

* feat: v2 roles endpoints

* fix: test

* fix: starting v2

* fix: test error

* fix: test api keys

* fix fixture

* test permission creation

* feat: permissions endpoints

* refactors

* refactor: project structure

* test: role permissions crud

* test: permissions endpoint negative tests

* docs: org, team permissions swagger

* unit tests for validator

* Update roles.guard.ts

* fix type

* test: error messages

* refactor: dont throw error in pbac

* delete redundant test file

* feedback: logging error

* fix: persist role.permissions when updating role.otherProperty

* refactor: use output service to return permissions

* refactor: service functions return current permissions

* refactor: remove OrganizationsRepository from providers

* refactor: try catch possibly duplicate create

* refactor: require min length name if provided

* refactor: org role has orgId and team role teamId

* fix: pbac guard caching

* fix: e2e tests in parallel

* refactor: use IsTeamInOrg guard for orgs teams roles and permissions endpoints

* refactor: use redis service getter and setter

* refactor: invalidate team permissions cache when permissions change

* refactor: delete keys instead of versioning when caching
This commit is contained in:
Lauris Skraucis authored and GitHub committed 2025-11-06 17:14:55 +00:00
1 parent 2e8b89c4ee
commit 45278a2195
59 files changed
+6329 -12

No files matched your search

+1 -1
View File
@@ -39,7 +39,7 @@ export interface CreateRoleData {
export interface UpdateRolePermissionsData {
roleId: string;
permissions: PermissionString[];
permissions?: PermissionString[];
updates?: {
color?: string;
name?: string;
@@ -168,6 +168,25 @@ export const getPermissionsForScope = (scope: Scope, isPrivate?: boolean): Permi
return filteredRegistry as PermissionRegistry;
};
export const getAllPermissionStringsForScope = (scope: Scope): PermissionString[] => {
const registry = getPermissionsForScope(scope);
return Object.entries(registry).flatMap(([resource, config]) =>
Object.keys(config)
.filter((k) => k !== "_resource")
.map((action) => `${resource}.${action}` as PermissionString)
);
};
const getPermissionSetForScope = (scope: Scope): Set<PermissionString> => {
return new Set(getAllPermissionStringsForScope(scope));
};
export const isValidPermissionStringForScope = (val: unknown, scope: Scope): val is PermissionString => {
if (!isValidPermissionString(val)) return false;
const allowed = getPermissionSetForScope(scope);
return allowed.has(val as PermissionString);
};
// Keep in mind these are on a team/organization level, not a user level
export const PERMISSION_REGISTRY: PermissionRegistry = {
[Resource.All]: {
@@ -514,12 +533,14 @@ export const PERMISSION_REGISTRY: PermissionRegistry = {
category: "attributes",
i18nKey: "pbac_action_read",
descriptionI18nKey: "pbac_desc_view_organization_attributes",
scope: [Scope.Organization],
},
[CrudAction.Update]: {
description: "Update organization attributes",
category: "attributes",
i18nKey: "pbac_action_update",
descriptionI18nKey: "pbac_desc_update_organization_attributes",
scope: [Scope.Organization],
dependsOn: ["organization.attributes.read"],
},
[CrudAction.Delete]: {
@@ -527,6 +548,7 @@ export const PERMISSION_REGISTRY: PermissionRegistry = {
category: "attributes",
i18nKey: "pbac_action_delete",
descriptionI18nKey: "pbac_desc_delete_organization_attributes",
scope: [Scope.Organization],
dependsOn: ["organization.attributes.read"],
},
[CrudAction.Create]: {
@@ -534,6 +556,7 @@ export const PERMISSION_REGISTRY: PermissionRegistry = {
category: "attributes",
i18nKey: "pbac_action_create",
descriptionI18nKey: "pbac_desc_create_organization_attributes",
scope: [Scope.Organization],
dependsOn: ["organization.attributes.read"],
},
},
@@ -91,14 +91,8 @@ export class RoleService {
if (role.type === DomainRoleType.SYSTEM) {
throw new Error("Cannot update default roles");
}
// Validate permissions
const validationResult = this.permissionService.validatePermissions(data.permissions);
if (!validationResult.isValid) {
throw new Error(validationResult.error || "Invalid permissions provided");
}
const existingPermissions = await this.repository.getPermissions(data.roleId);
const permissionChanges = this.permissionDiffService.calculateDiff(data.permissions, existingPermissions);
const permissionChanges = await this.getUpdatePermissionChanges(data);
return this.repository.update(data.roleId, permissionChanges, {
color: data.updates?.color,
@@ -106,6 +100,25 @@ export class RoleService {
});
}
private async getUpdatePermissionChanges(data: UpdateRolePermissionsData) {
if (!data.permissions) {
return {
toAdd: [],
toRemove: [],
};
}
const validationResult = this.permissionService.validatePermissions(data.permissions);
if (!validationResult.isValid) {
throw new Error(validationResult.error || "Invalid permissions provided");
}
const existingPermissions = await this.repository.getPermissions(data.roleId);
const permissionChanges = this.permissionDiffService.calculateDiff(data.permissions, existingPermissions);
return permissionChanges;
}
async roleBelongsToTeam(roleId: string, teamId: number) {
return this.repository.roleBelongsToTeam(roleId, teamId);
}