Skip to content

Commit

Permalink
fix: handle empty strings on permissions gracefully in access service (
Browse files Browse the repository at this point in the history
…#8306)

Splitting #8271 into smaller pieces. This first PR will focus on making
access service handle empty string inputs gracefully and converting them
to null before inserting them into the database.
  • Loading branch information
FredrikOseberg authored Oct 1, 2024
1 parent b037bfb commit dcb0228
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 3 deletions.
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

0 comments on commit dcb0228

Please sign in to comment.