Skip to content

Commit

Permalink
refactor: rename to convertToFullWorkspacePermissions and remove read…
Browse files Browse the repository at this point in the history
… and write permission

Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Sep 7, 2023
1 parent d972e13 commit a4b7ba7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 17 deletions.
21 changes: 8 additions & 13 deletions 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, convertToFullPermissions } from './utils';
import { generateRandomId, convertToFullWorkspacePermissions } from './utils';

describe('workspace utils', () => {
it('should generate id with the specified size', () => {
Expand All @@ -20,37 +20,32 @@ describe('workspace utils', () => {
});
});

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

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

expect(convertToFullPermissions(partialPermissions)).toEqual(expectedFullPermissions);
expect(convertToFullWorkspacePermissions(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);
expect(convertToFullWorkspacePermissions(partialPermissions)).toEqual(expectedFullPermissions);
});
});
8 changes: 6 additions & 2 deletions src/plugins/workspace/server/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@ const addMissingPrincipals = (principals: Permissions[string]) => ({
* @param permissions - The partial permissions object.
* @returns The full permissions object with all permission modes and their corresponding principals.
*/
export const convertToFullPermissions = (permissions: Permissions) => ({
export const convertToFullWorkspacePermissions = (permissions: Permissions) => ({
...permissions,
...Object.values(WorkspacePermissionMode).reduce(
...[
WorkspacePermissionMode.LibraryRead,
WorkspacePermissionMode.LibraryWrite,
WorkspacePermissionMode.Management,
].reduce(
(previousValue, permissionMode) => ({
...previousValue,
[permissionMode]: addMissingPrincipals(permissions[permissionMode] || {}),
Expand Down
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, convertToFullPermissions } from './utils';
import { generateRandomId, convertToFullWorkspacePermissions } 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: convertToFullPermissions(permissions),
permissions: convertToFullWorkspacePermissions(permissions),
});
return {
success: true,
Expand Down

0 comments on commit a4b7ba7

Please sign in to comment.