Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle empty strings on permissions gracefully in access service #8306

Merged
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
15 changes: 13 additions & 2 deletions src/lib/services/access-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ export interface AccessWithRoles {

const isProjectPermission = (permission) => PROJECT_ADMIN.includes(permission);

export const cleanPermissionEnvironment = (
permissions: PermissionRef[] | undefined,
) => {
return permissions?.map((permission) => {
if (permission.environment === '') {
return { ...permission, environment: null };
}
return permission;
});
};

export class AccessService {
private store: IAccessStore;

Expand Down Expand Up @@ -721,7 +732,7 @@ export class AccessService {
roleType,
};

const rolePermissions = role.permissions;
const rolePermissions = cleanPermissionEnvironment(role.permissions);
const newRole = await this.roleStore.create(baseRole);
if (rolePermissions) {
if (roleType === CUSTOM_ROOT_ROLE_TYPE) {
Expand Down Expand Up @@ -770,7 +781,7 @@ export class AccessService {
description: role.description,
roleType,
};
const rolePermissions = role.permissions;
const rolePermissions = cleanPermissionEnvironment(role.permissions);
const updatedRole = await this.roleStore.update(baseRole);
const existingPermissions = await this.store.getPermissionsForRole(
role.id,
Expand Down
117 changes: 117 additions & 0 deletions src/lib/services/clean-permission-environment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { cleanPermissionEnvironment } from './access-service';

test('should convert all empty strings to null', () => {
const permissions = [
{
name: 'UPDATE_PROJECT',
environment: '',
},
{
name: 'UPDATE_FEATURE_VARIANTS',
environment: '',
},
{
name: 'READ_PROJECT_API_TOKEN',
environment: '',
},
{
name: 'CREATE_PROJECT_API_TOKEN',
environment: '',
},
{
name: 'DELETE_PROJECT_API_TOKEN',
environment: '',
},
{
name: 'UPDATE_PROJECT_SEGMENT',
environment: '',
},
];

const result = cleanPermissionEnvironment(permissions);

expect(result).toEqual([
{
name: 'UPDATE_PROJECT',
environment: null,
},
{
name: 'UPDATE_FEATURE_VARIANTS',
environment: null,
},
{
name: 'READ_PROJECT_API_TOKEN',
environment: null,
},
{
name: 'CREATE_PROJECT_API_TOKEN',
environment: null,
},
{
name: 'DELETE_PROJECT_API_TOKEN',
environment: null,
},
{
name: 'UPDATE_PROJECT_SEGMENT',
environment: null,
},
]);
});

test('should handle some permissions already having null values', () => {
const permissions = [
{
name: 'UPDATE_PROJECT',
environment: null,
},
{
name: 'UPDATE_FEATURE_VARIANTS',
environment: null,
},
{
name: 'READ_PROJECT_API_TOKEN',
environment: '',
},
{
name: 'CREATE_PROJECT_API_TOKEN',
environment: '',
},
{
name: 'DELETE_PROJECT_API_TOKEN',
environment: '',
},
{
name: 'UPDATE_PROJECT_SEGMENT',
environment: '',
},
];

const result = cleanPermissionEnvironment(permissions);

expect(result).toEqual([
{
name: 'UPDATE_PROJECT',
environment: null,
},
{
name: 'UPDATE_FEATURE_VARIANTS',
environment: null,
},
{
name: 'READ_PROJECT_API_TOKEN',
environment: null,
},
{
name: 'CREATE_PROJECT_API_TOKEN',
environment: null,
},
{
name: 'DELETE_PROJECT_API_TOKEN',
environment: null,
},
{
name: 'UPDATE_PROJECT_SEGMENT',
environment: null,
},
]);
});
2 changes: 1 addition & 1 deletion src/lib/types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ export interface IPermission {
name: string;
displayName: string;
type: string;
environment?: string;
environment?: string | null;
}

export interface IEnvironmentPermission {
Expand Down
Loading