Skip to content

Commit

Permalink
fix: workspace permission entire update
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Sep 6, 2023
1 parent c6037d5 commit d972e13
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
37 changes: 36 additions & 1 deletion src/plugins/workspace/server/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { generateRandomId } from './utils';
import { generateRandomId, convertToFullPermissions } from './utils';

describe('workspace utils', () => {
it('should generate id with the specified size', () => {
Expand All @@ -19,3 +19,38 @@ describe('workspace utils', () => {
expect(ids.size).toBe(NUM_OF_ID);
});
});

describe('convertToFullPermissions', () => {
it('should convert partial permissions to full permissions', () => {
// Arrange
const partialPermissions = {
read: { users: ['user1'], groups: ['group1'] },
write: { users: ['user2'] },
management: { groups: ['group1'] },
};

const expectedFullPermissions = {
read: { users: ['user1'], groups: ['group1'] },
write: { users: ['user2'], groups: [] },
management: { users: [], groups: ['group1'] },
library_read: { users: [], groups: [] },
library_write: { users: [], groups: [] },
};

expect(convertToFullPermissions(partialPermissions)).toEqual(expectedFullPermissions);
});

it('should handle empty partial permissions', () => {
const partialPermissions = {};

const expectedFullPermissions = {
read: { users: [], groups: [] },
write: { users: [], groups: [] },
management: { users: [], groups: [] },
library_read: { users: [], groups: [] },
library_write: { users: [], groups: [] },
};

expect(convertToFullPermissions(partialPermissions)).toEqual(expectedFullPermissions);
});
});
23 changes: 23 additions & 0 deletions src/plugins/workspace/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,33 @@
*/

import crypto from 'crypto';
import type { Permissions } from '../../../core/server';
import { WorkspacePermissionMode } from '../../../core/server';

/**
* Generate URL friendly random ID
*/
export const generateRandomId = (size: number) => {
return crypto.randomBytes(size).toString('base64url').slice(0, size);
};

const addMissingPrincipals = (principals: Permissions[string]) => ({
users: principals.users || [],
groups: principals.groups || [],
});

/**
* Converts a partial permissions object to a full permissions object by adding missing principals for each permission mode.
* @param permissions - The partial permissions object.
* @returns The full permissions object with all permission modes and their corresponding principals.
*/
export const convertToFullPermissions = (permissions: Permissions) => ({
...permissions,
...Object.values(WorkspacePermissionMode).reduce(
(previousValue, permissionMode) => ({
...previousValue,
[permissionMode]: addMissingPrincipals(permissions[permissionMode] || {}),
}),
{}
),
});
4 changes: 2 additions & 2 deletions src/plugins/workspace/server/workspace_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
WorkspaceAttributeWithPermission,
} from './types';
import { workspace } from './saved_objects';
import { generateRandomId } from './utils';
import { generateRandomId, convertToFullPermissions } from './utils';

const WORKSPACE_ID_SIZE = 6;

Expand Down Expand Up @@ -167,7 +167,7 @@ export class WorkspaceClientWithSavedObject implements IWorkspaceDBImpl {
}
}
await client.update<Omit<WorkspaceAttribute, 'id'>>(WORKSPACE_TYPE, id, attributes, {
permissions,
permissions: convertToFullPermissions(permissions),
});
return {
success: true,
Expand Down

0 comments on commit d972e13

Please sign in to comment.