From dbe5aabe6d912fb8d4c4fd95dc8170343103b35a Mon Sep 17 00:00:00 2001 From: Gaurav Soni Date: Tue, 28 Jul 2026 11:30:39 +0200 Subject: [PATCH] fix(admin-settings): prevent deleting your own account (#12657) (#12661) * fix(admin-settings): prevent deleting your own account (#12657) In the user management, the account you are currently logged in as could be selected and included in a delete action, even though the backend always rejects self-deletion. Deleting a selection that included your own account produced a "Failed to delete 1 user" error and made your own row temporarily disappear from the list until the page was refreshed. The delete action now excludes the current user from the request, so all other selected users are deleted while your own account is left untouched. When your own account is part of the selection, the delete confirmation dialog shows a hint that it will not be deleted. (cherry picked from commit 898f89c69743bf27768df2d9af2effb913d8c2ad) --- ...ix-admin-settings-prevent-self-deletion.md | 15 +++++ .../src/components/Users/DeleteUserModal.vue | 44 +++++++++++++ .../actions/users/useUserActionsDelete.ts | 30 ++++----- .../components/Users/DeleteUserModal.spec.ts | 35 ++++++++++ .../users/useUserActionsDelete.spec.ts | 64 ++++++++++++++++++- 5 files changed, 170 insertions(+), 18 deletions(-) create mode 100644 changelog/unreleased/bugfix-admin-settings-prevent-self-deletion.md create mode 100644 web/packages/web-app-admin-settings/src/components/Users/DeleteUserModal.vue create mode 100644 web/packages/web-app-admin-settings/tests/unit/components/Users/DeleteUserModal.spec.ts diff --git a/changelog/unreleased/bugfix-admin-settings-prevent-self-deletion.md b/changelog/unreleased/bugfix-admin-settings-prevent-self-deletion.md new file mode 100644 index 00000000000..472406f8820 --- /dev/null +++ b/changelog/unreleased/bugfix-admin-settings-prevent-self-deletion.md @@ -0,0 +1,15 @@ +Bugfix: Prevent deleting your own account in the user management + +In the admin settings user management, the account you are currently logged in +as could be selected and included in a delete action, even though the backend +always rejects self-deletion. Deleting a selection that included your own +account produced a "Failed to delete 1 user" error and made your own row +temporarily disappear from the list until the page was refreshed. + +The delete action now excludes the current user from the request, so all other +selected users are deleted while your own account is left untouched. When your +own account is part of the selection, the delete confirmation dialog shows a +hint that it will not be deleted. + +https://github.com/owncloud/ocis/pull/12661 +https://github.com/owncloud/ocis/issues/12582 diff --git a/web/packages/web-app-admin-settings/src/components/Users/DeleteUserModal.vue b/web/packages/web-app-admin-settings/src/components/Users/DeleteUserModal.vue new file mode 100644 index 00000000000..31e4ccc82b2 --- /dev/null +++ b/web/packages/web-app-admin-settings/src/components/Users/DeleteUserModal.vue @@ -0,0 +1,44 @@ + + + + + diff --git a/web/packages/web-app-admin-settings/src/composables/actions/users/useUserActionsDelete.ts b/web/packages/web-app-admin-settings/src/composables/actions/users/useUserActionsDelete.ts index 1b81fabddfd..10ec9ca21d9 100644 --- a/web/packages/web-app-admin-settings/src/composables/actions/users/useUserActionsDelete.ts +++ b/web/packages/web-app-admin-settings/src/composables/actions/users/useUserActionsDelete.ts @@ -4,13 +4,15 @@ import { useCapabilityStore, useMessages, useModals, - useRouteQuery + useRouteQuery, + useUserStore } from '@ownclouders/web-pkg' import { useClientService } from '@ownclouders/web-pkg' import { UserAction, UserActionOptions } from '@ownclouders/web-pkg' import { useGettext } from 'vue3-gettext' import { User } from '@ownclouders/web-client/graph/generated' import { useUserSettingsStore } from '../../stores/userSettings' +import DeleteUserModal from '../../../components/Users/DeleteUserModal.vue' export const useUserActionsDelete = () => { const { showMessage, showErrorMessage } = useMessages() @@ -18,6 +20,7 @@ export const useUserActionsDelete = () => { const { $gettext, $ngettext } = useGettext() const clientService = useClientService() const { dispatchModal } = useModals() + const userStore = useUserStore() const userSettingsStore = useUserSettingsStore() const currentPageQuery = useRouteQuery('page', '1') @@ -31,15 +34,18 @@ export const useUserActionsDelete = () => { }) const deleteUsers = async (users: User[]) => { + const affectedUsers = users.filter((user) => user.id !== userStore.user.id) const graphClient = clientService.graphAuthenticated - const promises = users.map((user) => graphClient.users.deleteUser(user.id)) + const promises = affectedUsers.map((user) => graphClient.users.deleteUser(user.id)) const results = await Promise.allSettled(promises) const succeeded = results.filter((r) => r.status === 'fulfilled') if (succeeded.length) { const title = - succeeded.length === 1 && users.length === 1 - ? $gettext('User "%{user}" was deleted successfully', { user: users[0].displayName }) + succeeded.length === 1 && affectedUsers.length === 1 + ? $gettext('User "%{user}" was deleted successfully', { + user: affectedUsers[0].displayName + }) : $ngettext( '%{userCount} user was deleted successfully', '%{userCount} users were deleted successfully', @@ -55,8 +61,8 @@ export const useUserActionsDelete = () => { failed.forEach(console.error) const title = - failed.length === 1 && users.length === 1 - ? $gettext('Failed to delete user "%{user}"', { user: users[0].displayName }) + failed.length === 1 && affectedUsers.length === 1 + ? $gettext('Failed to delete user "%{user}"', { user: affectedUsers[0].displayName }) : $ngettext( 'Failed to delete %{userCount} user', 'Failed to delete %{userCount} users', @@ -70,7 +76,7 @@ export const useUserActionsDelete = () => { }) } - userSettingsStore.removeUsers(users) + userSettingsStore.removeUsers(affectedUsers) userSettingsStore.setSelectedUsers([]) const pageCount = Math.ceil(userSettingsStore.users.length / unref(itemsPerPage)) @@ -92,14 +98,8 @@ export const useUserActionsDelete = () => { userCount: resources.length.toString() }), confirmText: $gettext('Delete'), - message: $ngettext( - 'Are you sure you want to delete this user?', - 'Are you sure you want to delete the %{userCount} selected users?', - resources.length, - { - userCount: resources.length.toString() - } - ), + customComponent: DeleteUserModal, + customComponentAttrs: () => ({ users: resources }), hasInput: false, onConfirm: () => deleteUsers(resources) }) diff --git a/web/packages/web-app-admin-settings/tests/unit/components/Users/DeleteUserModal.spec.ts b/web/packages/web-app-admin-settings/tests/unit/components/Users/DeleteUserModal.spec.ts new file mode 100644 index 00000000000..28763d2ca34 --- /dev/null +++ b/web/packages/web-app-admin-settings/tests/unit/components/Users/DeleteUserModal.spec.ts @@ -0,0 +1,35 @@ +import DeleteUserModal from '../../../../src/components/Users/DeleteUserModal.vue' +import { defaultComponentMocks, defaultPlugins, shallowMount } from '@ownclouders/web-test-helpers' +import { mock } from 'vitest-mock-extended' +import { User } from '@ownclouders/web-client/graph/generated' +import { Modal } from '@ownclouders/web-pkg' + +describe('DeleteUserModal', () => { + it('shows a hint when the current user is part of the selection', () => { + // the default mocked user store uses id '1' + const { wrapper } = getWrapper([mock({ id: '1' }), mock({ id: '2' })]) + expect(wrapper.find('.delete-user-own-account-hint').exists()).toBe(true) + }) + it('does not show a hint when the current user is not part of the selection', () => { + const { wrapper } = getWrapper([mock({ id: '2' })]) + expect(wrapper.find('.delete-user-own-account-hint').exists()).toBe(false) + }) +}) + +function getWrapper(users = [mock()]) { + const mocks = defaultComponentMocks() + + return { + mocks, + wrapper: shallowMount(DeleteUserModal, { + props: { + modal: mock(), + users + }, + global: { + provide: mocks, + plugins: [...defaultPlugins()] + } + }) + } +} diff --git a/web/packages/web-app-admin-settings/tests/unit/composables/actions/users/useUserActionsDelete.spec.ts b/web/packages/web-app-admin-settings/tests/unit/composables/actions/users/useUserActionsDelete.spec.ts index 0268781a3d4..01a3dbbba2c 100644 --- a/web/packages/web-app-admin-settings/tests/unit/composables/actions/users/useUserActionsDelete.spec.ts +++ b/web/packages/web-app-admin-settings/tests/unit/composables/actions/users/useUserActionsDelete.spec.ts @@ -1,8 +1,10 @@ import { useUserActionsDelete } from '../../../../../src/composables/actions/users/useUserActionsDelete' +import DeleteUserModal from '../../../../../src/components/Users/DeleteUserModal.vue' import { mock } from 'vitest-mock-extended' +import { Mock } from 'vitest' import { unref } from 'vue' import { User } from '@ownclouders/web-client/graph/generated' -import { useCapabilityStore } from '@ownclouders/web-pkg' +import { useCapabilityStore, useModals } from '@ownclouders/web-pkg' import { defaultComponentMocks, getComposableWrapper, @@ -55,11 +57,58 @@ describe('useUserActionsDelete', () => { } }) }) + it('should not delete the current user when included in the selection', () => { + getWrapper({ + currentUserId: 'self', + setup: async ({ deleteUsers }, { clientService }) => { + const currentUser = mock({ id: 'self' }) + const otherUser = mock({ id: 'other' }) + await deleteUsers([currentUser, otherUser]) + expect(clientService.graphAuthenticated.users.deleteUser).toHaveBeenCalledWith( + otherUser.id + ) + expect(clientService.graphAuthenticated.users.deleteUser).not.toHaveBeenCalledWith( + currentUser.id + ) + } + }) + }) + it('should do nothing when the current user is the only selected user', () => { + getWrapper({ + currentUserId: 'self', + setup: async ({ deleteUsers }, { clientService }) => { + const currentUser = mock({ id: 'self' }) + await deleteUsers([currentUser]) + expect(clientService.graphAuthenticated.users.deleteUser).not.toHaveBeenCalled() + } + }) + }) + }) + describe('method "handler"', () => { + it('dispatches the delete modal with the selected users', () => { + getWrapper({ + setup: ({ actions }) => { + const { dispatchModal } = useModals() + const resources = [mock({ id: 'self' }), mock({ id: 'other' })] + unref(actions)[0].handler({ resources }) + expect(dispatchModal).toHaveBeenCalledWith( + expect.objectContaining({ + variation: 'danger', + customComponent: DeleteUserModal, + customComponentAttrs: expect.any(Function) + }) + ) + const attrs = (dispatchModal as unknown as Mock).mock.calls[0][0].customComponentAttrs() + expect(attrs.users).toEqual(resources) + } + }) + }) }) }) function getWrapper({ - setup + setup, + currentUserId = '0' }: { setup: ( instance: ReturnType, @@ -69,6 +118,7 @@ function getWrapper({ clientService: ReturnType['$clientService'] } ) => void + currentUserId?: string }) { const mocks = defaultComponentMocks() return { @@ -77,7 +127,15 @@ function getWrapper({ const instance = useUserActionsDelete() setup(instance, { clientService: mocks.$clientService }) }, - { mocks, provide: mocks } + { + mocks, + provide: mocks, + pluginOptions: { + piniaOptions: { + userState: { user: { id: currentUserId } as User } + } + } + } ) } }