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 group permissions convert #321

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
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];
};
Loading