Skip to content

Commit 2383384

Browse files
AndrienkoAleksandrdzemanov
authored andcommitted
fix(rbac): remove duplication permission action values (#1939)
Signed-off-by: Oleksandr Andriienko <[email protected]>
1 parent 4fb1e6f commit 2383384

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

plugins/rbac-backend/src/validation/policies-validation.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { AuthorizeResult } from '@backstage/plugin-permission-common';
55
import { Enforcer } from 'casbin';
66

77
import {
8-
PermissionAction,
8+
isValidPermissionAction,
9+
PermissionActionValues,
910
Role,
1011
RoleBasedPolicy,
1112
Source,
@@ -60,11 +61,8 @@ export function validatePolicy(policy: RoleBasedPolicy): Error | undefined {
6061
if (!policy.policy) {
6162
return new Error(`'policy' field must not be empty`);
6263
} else if (!isValidPermissionAction(policy.policy)) {
63-
const validOptions = ['create', 'read', 'update', 'delete', 'use'].join(
64-
', ',
65-
);
6664
return new Error(
67-
`'policy' has invalid value: '${policy.policy}'. It should be one of: ${validOptions}`,
65+
`'policy' has invalid value: '${policy.policy}'. It should be one of: ${PermissionActionValues.join(', ')}`,
6866
);
6967
}
7068

@@ -104,10 +102,6 @@ export function validateRole(role: Role): Error | undefined {
104102
return undefined;
105103
}
106104

107-
function isValidPermissionAction(action: string): action is PermissionAction {
108-
return ['create', 'read', 'update', 'delete', 'use'].includes(action);
109-
}
110-
111105
function isValidEffectValue(effect: string): boolean {
112106
return (
113107
effect === AuthorizeResult.ALLOW.toLocaleLowerCase() ||

plugins/rbac-common/src/types.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,24 @@ export type NonEmptyArray<T> = [T, ...T[]];
7373
// Permission framework attributes action has values: 'create' | 'read' | 'update' | 'delete' | undefined.
7474
// But we are introducing an action named "use" when action does not exist('undefined') to avoid
7575
// a more complicated model with multiple policy and request shapes.
76-
export type PermissionAction = 'create' | 'read' | 'update' | 'delete' | 'use';
76+
export const PermissionActionValues = [
77+
'create',
78+
'read',
79+
'update',
80+
'delete',
81+
'use',
82+
] as const;
83+
export type PermissionAction = (typeof PermissionActionValues)[number];
7784
export const toPermissionAction = (
7885
attr: PermissionAttributes,
7986
): PermissionAction => attr.action ?? 'use';
8087

88+
export function isValidPermissionAction(
89+
action: string,
90+
): action is PermissionAction {
91+
return (PermissionActionValues as readonly string[]).includes(action);
92+
}
93+
8194
export type PermissionInfo = {
8295
name: string;
8396
action: PermissionAction;

0 commit comments

Comments
 (0)