Skip to content

Commit 127fd2e

Browse files
authored
[Workspace] Trim collaboratorId when adding collaborators for workspace (#10908)
* Trim collaboratorId when adding collaborators for workspace Signed-off-by: Binlong Gao <[email protected]> * Add change log Signed-off-by: Binlong Gao <[email protected]> * Revert change in input form Signed-off-by: Binlong Gao <[email protected]> --------- Signed-off-by: Binlong Gao <[email protected]>
1 parent 889338b commit 127fd2e

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

changelogs/fragments/10908.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
fix:
2+
- Trim collaboratorId when adding collaborators for workspace ([#10908](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/10908))

src/plugins/workspace/public/components/workspace_form/add_collaborator_button.test.tsx

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,45 @@ describe('AddCollaboratorButton', () => {
108108
]);
109109
});
110110

111+
it('should trim collaborator IDs when adding collaborators', () => {
112+
const mockOnAdd = jest.fn().mockImplementation(({ onAddCollaborators }) => {
113+
onAddCollaborators([
114+
{
115+
accessLevel: 'readOnly',
116+
collaboratorId: ' user-with-spaces ',
117+
permissionType: 'user',
118+
},
119+
{
120+
accessLevel: 'admin',
121+
collaboratorId: '\tgroup-with-tabs\t',
122+
permissionType: 'group',
123+
},
124+
]);
125+
});
126+
const displayedTypes = [
127+
{
128+
name: 'add user',
129+
buttonLabel: 'add user',
130+
onAdd: mockOnAdd,
131+
id: 'user',
132+
},
133+
];
134+
const { getByTestId, getByText } = render(
135+
<AddCollaboratorButton {...mockProps} displayedTypes={displayedTypes} />
136+
);
137+
const button = getByTestId('add-collaborator-button');
138+
fireEvent.click(button);
139+
const addUserButton = getByText('add user');
140+
fireEvent.click(addUserButton);
141+
142+
expect(mockProps.handleSubmitPermissionSettings).toHaveBeenCalledWith([
143+
{ id: 0, modes: ['library_write', 'write'], type: 'user', userId: 'admin' },
144+
{ group: 'group', id: 1, modes: ['library_read', 'read'], type: 'group' },
145+
{ id: 2, modes: ['library_read', 'read'], type: 'user', userId: 'user-with-spaces' },
146+
{ id: 3, modes: ['library_write', 'write'], type: 'group', group: 'group-with-tabs' },
147+
]);
148+
});
149+
111150
it('should throw DuplicateCollaboratorError with consistent details', async () => {
112151
let errorCached: DuplicateCollaboratorError | undefined;
113152
const mockOnAdd = jest.fn(async ({ onAddCollaborators }) => {

src/plugins/workspace/public/components/workspace_form/add_collaborator_button.tsx

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,23 @@ export const AddCollaboratorButton = ({
5656

5757
const onAddCollaborators = async (collaborators: WorkspaceCollaborator[]) => {
5858
const uniqueCollaboratorIds = new Set();
59-
const addedSettings = collaborators.map(({ permissionType, accessLevel, collaboratorId }) => ({
60-
type: permissionType,
61-
modes: accessLevelNameToWorkspacePermissionModesMap[accessLevel],
62-
id: nextIdGenerator(),
63-
...(permissionType === WorkspacePermissionItemType.User
64-
? {
65-
userId: collaboratorId,
66-
}
67-
: {
68-
group: collaboratorId,
69-
}),
70-
collaboratorId,
71-
})) as Array<WorkspacePermissionSetting & { collaboratorId: string }>;
59+
const addedSettings = collaborators.map(({ permissionType, accessLevel, collaboratorId }) => {
60+
const trimmedId = collaboratorId.trim();
61+
return {
62+
type: permissionType,
63+
modes: accessLevelNameToWorkspacePermissionModesMap[accessLevel],
64+
id: nextIdGenerator(),
65+
...(permissionType === WorkspacePermissionItemType.User
66+
? {
67+
userId: trimmedId,
68+
}
69+
: {
70+
group: trimmedId,
71+
}),
72+
collaboratorId: trimmedId,
73+
};
74+
}) as Array<WorkspacePermissionSetting & { collaboratorId: string }>;
75+
7276
const existingDuplicateSettings = addedSettings.filter((permissionSettingToAdd) =>
7377
hasSameUserIdOrGroup(permissionSettings, permissionSettingToAdd)
7478
);

0 commit comments

Comments
 (0)