Skip to content

Commit 217bb98

Browse files
authoredJul 13, 2023
fix: Support arrays in resource definition value (#1781)
Sometimes, RBAC services/consumers use the array type to illustrate the values in resource definition together with IN operator. However, the utilities might crash because of that. This adds support for arrays, so that the utils work both with strings (split by comma), and arrays.
1 parent 897212a commit 217bb98

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed
 

‎packages/utils/src/RBAC/RBAC.test.js

+48
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,54 @@ describe('RBAC utilities', () => {
525525
];
526526
expect(doesHavePermissions(userPermissions, requiredPermissions, true)).toBe(false);
527527
});
528+
529+
it('recognizes array syntax for IN value, 1', () => {
530+
requiredPermissions = [
531+
{
532+
permission: 'inventory:hosts:read',
533+
resourceDefinitions: [],
534+
},
535+
];
536+
userPermissions = [
537+
{
538+
permission: 'inventory:hosts:read',
539+
resourceDefinitions: [
540+
{
541+
attributeFilter: {
542+
key: 'system-id',
543+
operation: 'in',
544+
value: [123, 456],
545+
},
546+
},
547+
],
548+
},
549+
];
550+
expect(doesHavePermissions(userPermissions, requiredPermissions, true)).toBe(false);
551+
});
552+
553+
it('recognizes array syntax for IN value, 2', () => {
554+
requiredPermissions = [
555+
{
556+
permission: 'inventory:hosts:read',
557+
resourceDefinitions: [
558+
{
559+
attributeFilter: {
560+
key: 'system-id',
561+
operation: 'in',
562+
value: [123, 456],
563+
},
564+
},
565+
],
566+
},
567+
];
568+
userPermissions = [
569+
{
570+
permission: 'inventory:hosts:read',
571+
resourceDefinitions: [],
572+
},
573+
];
574+
expect(doesHavePermissions(userPermissions, requiredPermissions, true)).toBe(true);
575+
});
528576
});
529577
});
530578
describe('has all permissions', () => {

‎packages/utils/src/RBAC/RBAC.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,13 @@ function extractResourceDefinitionValues(rds: ResourceDefinition[]) {
3434
const { key, operation, value } = cur.attributeFilter;
3535

3636
if (operation === ResourceDefinitionFilterOperationEnum.In) {
37-
return [...acc, ...value.split(',').map((value) => `${key}:${value}`)];
37+
return [
38+
...acc,
39+
...value
40+
.toString()
41+
.split(',')
42+
.map((value) => `${key}:${value}`),
43+
];
3844
}
3945

4046
if (operation === ResourceDefinitionFilterOperationEnum.Equal) {

0 commit comments

Comments
 (0)
Please sign in to comment.