From 85a0bea927f40b0a073b315207ce14fe262b9dec Mon Sep 17 00:00:00 2001 From: yuye-aws Date: Wed, 18 Oct 2023 17:26:58 +0800 Subject: [PATCH 1/2] feature: cannot delete last admin user/group in workspace settings Signed-off-by: yuye-aws --- .../workspace_creator/workspace_creator.tsx | 2 +- .../workspace_creator/workspace_form.tsx | 6 +-- .../workspace_permission_setting_panel.tsx | 49 ++++++++++++++----- 3 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/plugins/workspace/public/components/workspace_creator/workspace_creator.tsx b/src/plugins/workspace/public/components/workspace_creator/workspace_creator.tsx index d19f68b9ead7..6d475f43a659 100644 --- a/src/plugins/workspace/public/components/workspace_creator/workspace_creator.tsx +++ b/src/plugins/workspace/public/components/workspace_creator/workspace_creator.tsx @@ -78,7 +78,7 @@ export const WorkspaceCreator = () => { onSubmit={handleWorkspaceFormSubmit} opType={WORKSPACE_OP_TYPE_CREATE} permissionEnabled={isPermissionEnabled} - permissionFirstUserDeletable + permissionLastAdminItemDeletable /> )} diff --git a/src/plugins/workspace/public/components/workspace_creator/workspace_form.tsx b/src/plugins/workspace/public/components/workspace_creator/workspace_form.tsx index 6316218f2e8c..d2a540db46ba 100644 --- a/src/plugins/workspace/public/components/workspace_creator/workspace_form.tsx +++ b/src/plugins/workspace/public/components/workspace_creator/workspace_form.tsx @@ -140,7 +140,7 @@ interface WorkspaceFormProps { defaultValues?: WorkspaceFormData; opType?: string; permissionEnabled?: boolean; - permissionFirstUserDeletable?: boolean; + permissionLastAdminItemDeletable?: boolean; } export const WorkspaceForm = ({ @@ -149,7 +149,7 @@ export const WorkspaceForm = ({ defaultValues, opType, permissionEnabled, - permissionFirstUserDeletable, + permissionLastAdminItemDeletable, }: WorkspaceFormProps) => { const applications = useApplications(application); const workspaceNameReadOnly = defaultValues?.reserved; @@ -687,7 +687,7 @@ export const WorkspaceForm = ({ errors={formErrors.permissions} onChange={setPermissionSettings} permissionSettings={permissionSettings} - firstUserDeletable={permissionFirstUserDeletable} + lastAdminItemDeletable={!!permissionLastAdminItemDeletable} data-test-subj={`workspaceForm-permissionSettingPanel`} /> diff --git a/src/plugins/workspace/public/components/workspace_creator/workspace_permission_setting_panel.tsx b/src/plugins/workspace/public/components/workspace_creator/workspace_permission_setting_panel.tsx index 12ff0d21fba1..c67a655baba3 100644 --- a/src/plugins/workspace/public/components/workspace_creator/workspace_permission_setting_panel.tsx +++ b/src/plugins/workspace/public/components/workspace_creator/workspace_permission_setting_panel.tsx @@ -112,11 +112,11 @@ interface WorkspacePermissionSettingInputProps { const WorkspacePermissionSettingInput = ({ index, - deletable, type, userId, group, modes, + deletable, onDelete, onGroupOrUserIdChange, onPermissionModesChange, @@ -201,13 +201,15 @@ const WorkspacePermissionSettingInput = ({ interface WorkspacePermissionSettingPanelProps { errors?: string[]; - firstUserDeletable?: boolean; + lastAdminItemDeletable: boolean; permissionSettings: Array>; onChange?: (value: Array>) => void; } -interface UserOrGroupSectionProps extends WorkspacePermissionSettingPanelProps { +interface UserOrGroupSectionProps + extends Omit { title: string; + nonDeletableIndex: number; type: WorkspacePermissionItemType; } @@ -217,7 +219,7 @@ const UserOrGroupSection = ({ errors, onChange, permissionSettings, - firstUserDeletable, + nonDeletableIndex, }: UserOrGroupSectionProps) => { const transformedValue = useMemo(() => { if (!permissionSettings) { @@ -315,9 +317,7 @@ const UserOrGroupSection = ({ {...item} type={type} index={index} - deletable={ - type === WorkspacePermissionItemType.Group || firstUserDeletable || index !== 0 - } + deletable={index !== nonDeletableIndex} onDelete={handleDelete} onGroupOrUserIdChange={handleGroupOrUserIdChange} onPermissionModesChange={handlePermissionModesChange} @@ -343,7 +343,7 @@ export const WorkspacePermissionSettingPanel = ({ errors, onChange, permissionSettings, - firstUserDeletable, + lastAdminItemDeletable, }: WorkspacePermissionSettingPanelProps) => { const [userPermissionSettings, setUserPermissionSettings] = useState< Array> @@ -359,10 +359,36 @@ export const WorkspacePermissionSettingPanel = ({ (permissionSettingItem) => permissionSettingItem.type === WorkspacePermissionItemType.Group ) ?? [] ); + const [userNonDeletableIndex, setUserNonDeletableIndex] = useState(-1); + const [groupNonDeletableIndex, setGroupNonDeletableIndex] = useState(-1); useEffect(() => { - onChange?.([...userPermissionSettings, ...groupPermissionSettings]); - }, [onChange, userPermissionSettings, groupPermissionSettings]); + const newPermissionSettings = [...userPermissionSettings, ...groupPermissionSettings]; + onChange?.(newPermissionSettings); + if (!lastAdminItemDeletable) { + const adminPermissionSettings = newPermissionSettings.filter( + (permission) => getPermissionModeId(permission.modes ?? []) === PermissionModeId.Admin + ); + if (adminPermissionSettings.length === 1) { + if (adminPermissionSettings[0].type === WorkspacePermissionItemType.User) { + setUserNonDeletableIndex( + userPermissionSettings.findIndex( + (permission) => getPermissionModeId(permission.modes ?? []) === PermissionModeId.Admin + ) + ); + } else { + setGroupNonDeletableIndex( + groupPermissionSettings.findIndex( + (permission) => getPermissionModeId(permission.modes ?? []) === PermissionModeId.Admin + ) + ); + } + return; + } + } + setUserNonDeletableIndex(-1); + setGroupNonDeletableIndex(-1); + }, [onChange, userPermissionSettings, groupPermissionSettings, lastAdminItemDeletable]); return (
@@ -372,8 +398,8 @@ export const WorkspacePermissionSettingPanel = ({ })} errors={errors} onChange={setUserPermissionSettings} + nonDeletableIndex={userNonDeletableIndex} permissionSettings={userPermissionSettings} - firstUserDeletable={firstUserDeletable} type={WorkspacePermissionItemType.User} /> @@ -383,6 +409,7 @@ export const WorkspacePermissionSettingPanel = ({ })} errors={errors} onChange={setGroupPermissionSettings} + nonDeletableIndex={groupNonDeletableIndex} permissionSettings={groupPermissionSettings} type={WorkspacePermissionItemType.Group} /> From 4e8367038a193b7819fe0e23744f612c42e400d6 Mon Sep 17 00:00:00 2001 From: yuye-aws Date: Thu, 19 Oct 2023 12:46:37 +0800 Subject: [PATCH 2/2] refactor code with useMemo Signed-off-by: yuye-aws --- .../workspace_permission_setting_panel.tsx | 29 +++++++++---------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/plugins/workspace/public/components/workspace_creator/workspace_permission_setting_panel.tsx b/src/plugins/workspace/public/components/workspace_creator/workspace_permission_setting_panel.tsx index c67a655baba3..d95b062db3c4 100644 --- a/src/plugins/workspace/public/components/workspace_creator/workspace_permission_setting_panel.tsx +++ b/src/plugins/workspace/public/components/workspace_creator/workspace_permission_setting_panel.tsx @@ -359,36 +359,35 @@ export const WorkspacePermissionSettingPanel = ({ (permissionSettingItem) => permissionSettingItem.type === WorkspacePermissionItemType.Group ) ?? [] ); - const [userNonDeletableIndex, setUserNonDeletableIndex] = useState(-1); - const [groupNonDeletableIndex, setGroupNonDeletableIndex] = useState(-1); useEffect(() => { + onChange?.([...userPermissionSettings, ...groupPermissionSettings]); + }, [onChange, userPermissionSettings, groupPermissionSettings]); + + const nonDeletableIndex = useMemo(() => { + let userNonDeletableIndex = -1; + let groupNonDeletableIndex = -1; const newPermissionSettings = [...userPermissionSettings, ...groupPermissionSettings]; - onChange?.(newPermissionSettings); if (!lastAdminItemDeletable) { const adminPermissionSettings = newPermissionSettings.filter( (permission) => getPermissionModeId(permission.modes ?? []) === PermissionModeId.Admin ); if (adminPermissionSettings.length === 1) { if (adminPermissionSettings[0].type === WorkspacePermissionItemType.User) { - setUserNonDeletableIndex( - userPermissionSettings.findIndex( - (permission) => getPermissionModeId(permission.modes ?? []) === PermissionModeId.Admin - ) + userNonDeletableIndex = userPermissionSettings.findIndex( + (permission) => getPermissionModeId(permission.modes ?? []) === PermissionModeId.Admin ); } else { - setGroupNonDeletableIndex( - groupPermissionSettings.findIndex( - (permission) => getPermissionModeId(permission.modes ?? []) === PermissionModeId.Admin - ) + groupNonDeletableIndex = groupPermissionSettings.findIndex( + (permission) => getPermissionModeId(permission.modes ?? []) === PermissionModeId.Admin ); } - return; } } - setUserNonDeletableIndex(-1); - setGroupNonDeletableIndex(-1); - }, [onChange, userPermissionSettings, groupPermissionSettings, lastAdminItemDeletable]); + return { userNonDeletableIndex, groupNonDeletableIndex }; + }, [userPermissionSettings, groupPermissionSettings, lastAdminItemDeletable]); + + const { userNonDeletableIndex, groupNonDeletableIndex } = nonDeletableIndex; return (