Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions apps/meteor/ee/server/api/abac/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,20 +57,14 @@ const GetAbacAttributesQuery = {
type: 'object',
properties: {
key: { type: 'string', minLength: 1, pattern: ATTRIBUTE_KEY_PATTERN },
values: {
type: 'array',
items: { type: 'string', minLength: 1, pattern: ATTRIBUTE_KEY_PATTERN },
minItems: 1,
maxItems: MAX_ATTRIBUTE_VALUES,
uniqueItems: true,
},
values: { type: 'string', minLength: 1, pattern: ATTRIBUTE_KEY_PATTERN },
offset: { type: 'number' },
count: { type: 'number' },
},
additionalProperties: false,
};

export const GETAbacAttributesQuerySchema = ajv.compile<{ key: string; values: string[]; offset: number; count: number; total: number }>(
export const GETAbacAttributesQuerySchema = ajv.compile<{ key: string; values: string; offset: number; count: number }>(
GetAbacAttributesQuery,
);

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/tests/end-to-end/api/abac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ import { IS_EE } from '../../e2e/config/constants';
await request
.get(`${v1}/abac/attributes`)
.set(credentials)
.query({ 'values[]': 'magenta' })
.query({ values: 'magenta' })
.expect(200)
.expect((res) => {
expect(res.body.success).to.be.true;
Expand Down
1 change: 1 addition & 0 deletions ee/packages/abac/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"@rocket.chat/core-services": "workspace:^",
"@rocket.chat/core-typings": "workspace:^",
"@rocket.chat/models": "workspace:^",
"@rocket.chat/string-helpers": "^0.32.0",
"mongodb": "6.10.0"
},
"devDependencies": {
Expand Down
15 changes: 9 additions & 6 deletions ee/packages/abac/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ describe('AbacService (unit)', () => {
});

const result = await service.listAbacAttributes({ key: 'FilterKey' });
expect(mockAbacFindPaginated).toHaveBeenCalledWith({ key: 'FilterKey' }, { projection: { key: 1, values: 1 }, skip: 0, limit: 25 });
expect(mockAbacFindPaginated).toHaveBeenCalledWith(
{ $or: [{ key: /FilterKey/i }] },
{ projection: { key: 1, values: 1 }, skip: 0, limit: 25 },
);
expect(result).toEqual({
attributes: docs,
offset: 0,
Expand All @@ -130,9 +133,9 @@ describe('AbacService (unit)', () => {
totalCount: Promise.resolve(10),
});

const result = await service.listAbacAttributes({ values: ['n', 'z'], offset: 5, count: 2 });
const result = await service.listAbacAttributes({ values: 'n,z', offset: 5, count: 2 });
expect(mockAbacFindPaginated).toHaveBeenCalledWith(
{ values: { $in: ['n', 'z'] } },
{ $or: [{ values: /n,z/i }] },
{ projection: { key: 1, values: 1 }, skip: 5, limit: 2 },
);
expect(result).toEqual({
Expand All @@ -150,9 +153,9 @@ describe('AbacService (unit)', () => {
totalCount: Promise.resolve(docs.length),
});

const result = await service.listAbacAttributes({ key: 'gamma', values: ['q'] });
const result = await service.listAbacAttributes({ key: 'gamma', values: 'q' });
expect(mockAbacFindPaginated).toHaveBeenCalledWith(
{ key: 'gamma', values: { $in: ['q'] } },
{ $or: [{ key: /gamma/i }, { values: /q/i }] },
{ projection: { key: 1, values: 1 }, skip: 0, limit: 25 },
);
expect(result).toEqual({
Expand All @@ -169,7 +172,7 @@ describe('AbacService (unit)', () => {
totalCount: Promise.resolve(0),
});

const result = await service.listAbacAttributes({ key: 'nope', values: ['none'] });
const result = await service.listAbacAttributes({ key: 'nope', values: 'none' });
expect(result).toEqual({
attributes: [],
offset: 0,
Expand Down
24 changes: 14 additions & 10 deletions ee/packages/abac/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { ServiceClass } from '@rocket.chat/core-services';
import type { IAbacService } from '@rocket.chat/core-services';
import type { IAbacAttribute, IAbacAttributeDefinition } from '@rocket.chat/core-typings';
import { Rooms, AbacAttributes } from '@rocket.chat/models';
import type { Filter, UpdateFilter } from 'mongodb';
import { escapeRegExp } from '@rocket.chat/string-helpers';
import type { Document, UpdateFilter } from 'mongodb';

export class AbacService extends ServiceClass implements IAbacService {
protected name = 'abac';
Expand All @@ -22,28 +23,31 @@ export class AbacService extends ServiceClass implements IAbacService {
}
}

async listAbacAttributes(filters?: { key?: string; values?: string[]; offset?: number; count?: number }): Promise<{
async listAbacAttributes(filters?: { key?: string; values?: string; offset?: number; count?: number }): Promise<{
attributes: IAbacAttribute[];
offset: number;
count: number;
total: number;
}> {
const query: Filter<IAbacAttribute> = {};
const query: Document[] = [];
if (filters?.key) {
query.key = filters.key;
query.push({ key: new RegExp(escapeRegExp(filters.key), 'i') });
}
if (filters?.values?.length) {
query.values = { $in: filters.values };
query.push({ values: new RegExp(escapeRegExp(filters.values), 'i') });
}

const offset = filters?.offset ?? 0;
const limit = filters?.count ?? 25;

const { cursor, totalCount } = AbacAttributes.findPaginated(query, {
projection: { key: 1, values: 1 },
skip: offset,
limit,
});
const { cursor, totalCount } = AbacAttributes.findPaginated(
{ ...(query.length && { $or: query }) },
{
projection: { key: 1, values: 1 },
skip: offset,
limit,
},
);

const attributes = await cursor.toArray();

Expand Down
2 changes: 1 addition & 1 deletion packages/core-services/src/types/IAbacService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface IAbacService {
addAbacAttribute(attribute: IAbacAttributeDefinition): Promise<void>;
listAbacAttributes(filters?: {
key?: string;
values?: string[];
values?: string;
offset?: number;
count?: number;
}): Promise<{ attributes: IAbacAttribute[]; offset: number; count: number; total: number }>;
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7130,6 +7130,7 @@ __metadata:
"@rocket.chat/core-typings": "workspace:^"
"@rocket.chat/eslint-config": "workspace:^"
"@rocket.chat/models": "workspace:^"
"@rocket.chat/string-helpers": "npm:^0.32.0"
"@rocket.chat/tsconfig": "workspace:*"
"@types/jest": "npm:~30.0.0"
"@types/node": "npm:~22.16.1"
Expand Down Expand Up @@ -9294,6 +9295,13 @@ __metadata:
languageName: node
linkType: hard

"@rocket.chat/string-helpers@npm:^0.32.0":
version: 0.32.0
resolution: "@rocket.chat/string-helpers@npm:0.32.0"
checksum: 10/4f503a42e9a93ea9e6e85da320f125da5af5da1693e6782dee828ffc6c0d05e6ec76d75f7efc342d0738a4d37d67a729486a4d96b109642d70610660beb38e21
languageName: node
linkType: hard

"@rocket.chat/styled@npm:^0.32.0, @rocket.chat/styled@npm:~0.32.0":
version: 0.32.0
resolution: "@rocket.chat/styled@npm:0.32.0"
Expand Down
Loading