Skip to content

Commit

Permalink
Fix group permissions convert (opensearch-project#321)
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <wonglam@amazon.com>
  • Loading branch information
wanglam authored Apr 8, 2024
1 parent 4fc9e70 commit e2a8ae2
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { WorkspacePermissionMode } from '../../../common/constants';
import { WorkspacePermissionItemType } from './constants';
import {
convertPermissionSettingsToPermissions,
convertPermissionsToPermissionSettings,
} from './utils';

describe('convertPermissionSettingsToPermissions', () => {
it('should return undefined if permission items not provided', () => {
expect(convertPermissionSettingsToPermissions(undefined)).toBeUndefined();
expect(convertPermissionSettingsToPermissions([])).toBeUndefined();
});

it('should return consistent permission settings', () => {
expect(
convertPermissionSettingsToPermissions([
{
type: WorkspacePermissionItemType.User,
userId: 'foo',
modes: [WorkspacePermissionMode.LibraryRead, WorkspacePermissionMode.Read],
},
{
type: WorkspacePermissionItemType.Group,
group: 'bar',
modes: [WorkspacePermissionMode.LibraryWrite],
},
])
).toEqual({
library_read: { users: ['foo'] },
library_write: { groups: ['bar'] },
read: { users: ['foo'] },
});
});
});

describe('convertPermissionsToPermissionSettings', () => {
it('should return consistent permission settings', () => {
expect(
convertPermissionsToPermissionSettings({
library_read: { users: ['foo'] },
library_write: { groups: ['bar'] },
read: { users: ['foo'] },
write: { groups: ['bar'] },
})
).toEqual([
{
type: WorkspacePermissionItemType.User,
userId: 'foo',
modes: [WorkspacePermissionMode.LibraryRead, WorkspacePermissionMode.Read],
},
{
type: WorkspacePermissionItemType.Group,
group: 'bar',
modes: [WorkspacePermissionMode.LibraryWrite, WorkspacePermissionMode.Write],
},
]);
});
it('should only convert workspace supported permissions', () => {
expect(
convertPermissionsToPermissionSettings({
another_read: { users: ['foo'] },
})
).toEqual([]);
});
});
61 changes: 29 additions & 32 deletions src/plugins/workspace/public/components/workspace_form/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,38 +136,35 @@ export const convertPermissionsToPermissionSettings = (permissions: SavedObjectP
if (!isWorkspacePermissionMode(mode)) {
return;
}
if (permissions[mode].users) {
permissions[mode].users?.forEach((userId) => {
const settingTypeKey = `userId-${userId}`;
const modes = settingType2Modes[settingTypeKey] ? settingType2Modes[settingTypeKey] : [];

modes.push(mode);
if (modes.length === 1) {
userPermissionSettings.push({
type: WorkspacePermissionItemType.User,
userId,
modes,
});
settingType2Modes[settingTypeKey] = modes;
}
});
permissions[mode].groups?.forEach((group) => {
const settingTypeKey = `group-${group}`;
const modes = settingType2Modes[settingTypeKey] ? settingType2Modes[settingTypeKey] : [];

modes.push(mode);
if (modes.length === 1) {
userPermissionSettings.push({
type: WorkspacePermissionItemType.Group,
group,
modes,
});
}
});
}
permissions[mode].users?.forEach((userId) => {
const settingTypeKey = `userId-${userId}`;
const modes = settingType2Modes[settingTypeKey] ?? [];

modes.push(mode);
if (modes.length === 1) {
userPermissionSettings.push({
type: WorkspacePermissionItemType.User,
userId,
modes,
});
settingType2Modes[settingTypeKey] = modes;
}
});
permissions[mode].groups?.forEach((group) => {
const settingTypeKey = `group-${group}`;
const modes = settingType2Modes[settingTypeKey] ?? [];

modes.push(mode);
if (modes.length === 1) {
groupPermissionSettings.push({
type: WorkspacePermissionItemType.Group,
group,
modes,
});
settingType2Modes[settingTypeKey] = modes;
}
});
});

return [...userPermissionSettings, ...groupPermissionSettings].filter(
isValidWorkspacePermissionSetting
);
return [...userPermissionSettings, ...groupPermissionSettings];
};

0 comments on commit e2a8ae2

Please sign in to comment.