Skip to content

Commit

Permalink
[backend] Avoid user deletion raising exception when authorized_membe…
Browse files Browse the repository at this point in the history
…r is missing (#5580)
  • Loading branch information
aHenryJard authored Jan 30, 2024
1 parent c684db0 commit 38cb81a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
12 changes: 8 additions & 4 deletions opencti-platform/opencti-graphql/src/domain/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -756,11 +756,15 @@ export const meEditField = async (context, user, userId, inputs, password = null
return userEditField(context, user, userId, inputs);
};

const isUserTheLastAdmin = (userId, authorized_members) => {
const currentUserIsAdmin = authorized_members.some(({ id, access_right }) => id === userId && access_right === 'admin');
const anotherUserIsAdmin = authorized_members.some(({ id, access_right }) => id !== userId && access_right === 'admin');
export const isUserTheLastAdmin = (userId, authorized_members) => {
if (authorized_members !== null && authorized_members !== undefined) {
const currentUserIsAdmin = authorized_members.some(({ id, access_right }) => id === userId && access_right === 'admin');
const anotherUserIsAdmin = authorized_members.some(({ id, access_right }) => id !== userId && access_right === 'admin');

return currentUserIsAdmin && !anotherUserIsAdmin;
return currentUserIsAdmin && !anotherUserIsAdmin;
}
// if for some reason there is no authorized_member, then nothing prevent from deleting.
return false;
};

export const deleteAllWorkspaceForUser = async (context, authUser, userId) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ENTITY_TYPE_USER } from '../../../src/schema/internalObject';
import type { AuthContext, AuthUser } from '../../../src/types/user';
import { addNotification, addTrigger, myNotificationsFind, triggerGet } from '../../../src/modules/notification/notification-domain';
import type { MemberAccessInput, TriggerLiveAddInput, WorkspaceAddInput } from '../../../src/generated/graphql';
import { addUser, assignGroupToUser, findById as findUserById, userDelete } from '../../../src/domain/user';
import { addUser, assignGroupToUser, findById as findUserById, isUserTheLastAdmin, userDelete } from '../../../src/domain/user';
import { addWorkspace, editAuthorizedMembers, findById as findWorkspaceById } from '../../../src/modules/workspace/workspace-domain';
import type { NotificationAddInput } from '../../../src/modules/notification/notification-types';
import { TriggerEventType, TriggerType } from '../../../src/generated/graphql';
Expand Down Expand Up @@ -147,4 +147,10 @@ describe('Testing user delete on cascade [issue/3720]', () => {
console.log(JSON.stringify(e));
}
});
it('should data without authorized_member not throw exception during user deletion.', async () => {
// for some reason this can happend, see https://github.com/OpenCTI-Platform/opencti/issues/5580
const isLastAdminResult = isUserTheLastAdmin(ADMIN_USER.id, undefined);
expect(true, 'No exception should be raised here').toBe(true);
expect(isLastAdminResult, 'An entity without authorized_member data should not block deletion.').toBe(false);
});
});

0 comments on commit 38cb81a

Please sign in to comment.